Skip to content

Commit

Permalink
mulog: Refactor and extend mock functions and tests
Browse files Browse the repository at this point in the history
Refactored test names for clarity and added extensive mocking functions in `mulog_deferred_lock_test.cpp`. This includes mock implementations for `vsnprintf`, `snprintf`, `lwrb_get_full`, and `lwrb_get_linear_block_read_length`. Modified setup to utilize mock expectations and added new tests for deferred processing.
  • Loading branch information
vpetrigo committed Oct 22, 2024
1 parent 063830c commit 73ba7b2
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
109 changes: 106 additions & 3 deletions src/mulog_deferred_lock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* \brief
* \author
*/
#include "internal/utils.h"
#include "mulog.h"

#include <CppUTest/CommandLineTestRunner.h>
Expand Down Expand Up @@ -38,17 +39,68 @@ namespace {
extern "C" void putchar_(int c)
{
}

extern "C" int __real_vsnprintf_(char *s, size_t count, const char *fmt, va_list ap);

extern "C" int __wrap_vsnprintf_(char *s, size_t count, const char *fmt, va_list ap)
{
UNUSED(s);
UNUSED(count);
UNUSED(fmt);
UNUSED(ap);
int value = mock().actualCall("vsnprintf").returnIntValue();

if (value < 0) {
return value;
}

value = __real_vsnprintf_(s, count, fmt, ap);

return value;
}

extern "C" int __wrap_snprintf_(char *s, size_t count, const char *fmt, ...)
{
UNUSED(fmt);
int value = mock().actualCall("snprintf").returnIntValue();

if (value < 0) {
return value;
}

va_list ap;

va_start(ap, fmt);
value = __real_vsnprintf_(s, count, fmt, ap);
va_end(ap);

return value;
}

extern "C" size_t __wrap_lwrb_get_full(const void *buff)
{
UNUSED(buff);

return mock().actualCall("lwrb_get_full").returnUnsignedLongIntValue();
}

extern "C" size_t __wrap_lwrb_get_linear_block_read_length(const void *buff)
{
UNUSED(buff);
return mock().actualCall("lwrb_get_linear_block_read_length").returnUnsignedLongIntValue();
}
} // namespace

TEST_GROUP(MulogDeferredLock)
{
std::array<char, 1024> buffer;
std::array<char, 1024> buffer{};

void setup() override
{
mock().disable();
mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
mulog_set_log_buffer(buffer.data(), buffer.size());
mock().enable();
mock().clear();
}

void teardown() override
Expand Down Expand Up @@ -135,6 +187,57 @@ TEST(MulogDeferredLock, SimpleOperations)
CHECK_EQUAL(0, log_ret);
}

TEST(MulogDeferredLock, MockLog)
{
mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
auto ret = mulog_set_log_level(MULOG_LOG_LVL_TRACE);
mock().checkExpectations();
CHECK_EQUAL(MULOG_RET_CODE_OK, ret);
mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
ret = mulog_add_output(test_output);
mock().checkExpectations();
CHECK_EQUAL(MULOG_RET_CODE_OK, ret);

mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
mock().expectNoCall("vsnprintf");
mock().expectOneCall("snprintf").andReturnValue(-1);
mock().expectNoCall("test_output");
auto log_ret = MULOG_LOG_DBG("Hello %s", "Temp");
mock().checkExpectations();
CHECK_EQUAL(0, log_ret);

mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
mock().expectOneCall("snprintf").andReturnValue(1);
mock().expectOneCall("vsnprintf").andReturnValue(-1);
mock().expectNoCall("test_output");
log_ret = MULOG_LOG_DBG("Hello %s", "Temp");
mock().checkExpectations();
CHECK_EQUAL(-1, log_ret);
}

TEST(MulogDeferredLock, MockLogDeferredProcess)
{
mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
mock().expectOneCall("lwrb_get_full").andReturnValue(100UL);
mock().expectOneCall("lwrb_get_linear_block_read_length").andReturnValue(200UL);
auto ret = mulog_deferred_process();
mock().checkExpectations();
CHECK_EQUAL(100UL, ret);

mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
mock().expectOneCall("lwrb_get_full").andReturnValue(300UL);
mock().expectOneCall("lwrb_get_linear_block_read_length").andReturnValue(300UL);
ret = mulog_deferred_process();
mock().checkExpectations();
CHECK_EQUAL(300UL, ret);
}

int main(int argc, char **argv)
{
return RUN_ALL_TESTS(argc, argv);
Expand Down
2 changes: 1 addition & 1 deletion src/mulog_realtime_lock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ TEST(MulogRealtimeLock, MockLogWithLogMessageError)
CHECK_EQUAL(-1, log_ret);
}

TEST(MulogRealtimeLock, MockLogWithPrependTimestampError)
TEST(MulogRealtimeLock, MockLog)
{
mock().expectOneCall("mulog_config_mulog_lock").andReturnValue(1);
mock().expectOneCall("mulog_config_mulog_unlock");
Expand Down

0 comments on commit 73ba7b2

Please sign in to comment.