From f133f0a424db19dd3263794e8fa71ea920ef3ea9 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 4 Apr 2022 16:42:05 -0500 Subject: add lily_mock_t basic functionality --- lily-test.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lily-test.h | 14 ++++++++++++++ tests/main.c | 1 + tests/mock_queue.c | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/tests.h | 1 + 5 files changed, 100 insertions(+) diff --git a/lily-test.c b/lily-test.c index 0581263..1e7e5f4 100644 --- a/lily-test.c +++ b/lily-test.c @@ -242,3 +242,45 @@ void _lily_dequeue(lily_queue_t *q, size_t size, uint8_t *ptr) memcpy(ptr, q->front, size); q->front += size; } + + +lily_mock_t * lily_mock_create() +{ + lily_mock_t *m = malloc(sizeof(lily_mock_t)); + m->n_calls = 0; + m->arguments = lily_queue_create(); + m->values = lily_queue_create(); + return m; +} + +void lily_mock_destroy(lily_mock_t *m) +{ + lily_queue_destroy(m->arguments); + lily_queue_destroy(m->values); + free(m); +} + +void _lily_mock_call(lily_mock_t *m, struct lily_mock_arg_t *args, size_t n_args) +{ + m->n_calls += 1; + + for (int i=0; iarguments, args[i].size, args[i].var); + } +} + +void _lily_get_call(lily_mock_t *m, + struct lily_mock_arg_t *args, + size_t n_args, + unsigned int call_num) +{ + size_t stride = 0; + for (int i=0; iarguments->front = m->arguments->buf + (call_num * stride); + for (int i=0; iarguments, args[i].size, args[i].var); + } +} diff --git a/lily-test.h b/lily-test.h index 51b440f..bf0bbae 100644 --- a/lily-test.h +++ b/lily-test.h @@ -151,4 +151,18 @@ typedef struct lily_mock_t { lily_queue_t *values; } lily_mock_t; +lily_mock_t * lily_mock_create(); +void lily_mock_destroy(lily_mock_t *m); + +#define lily_mock_call(m, args) \ + _lily_mock_call(m, args, LILY_NARGS(args)) +void _lily_mock_call(lily_mock_t *m, struct lily_mock_arg_t *args, size_t n_args); + +#define lily_get_call(m, args, call_num) \ + _lily_get_call(m, args, LILY_NARGS(args), call_num) +void _lily_get_call(lily_mock_t *m, + struct lily_mock_arg_t *args, + size_t n_args, + unsigned int call_num); + #endif diff --git a/tests/main.c b/tests/main.c index f4e634c..84c8c09 100644 --- a/tests/main.c +++ b/tests/main.c @@ -24,6 +24,7 @@ int main() run_test(test_mock_enqueue_dequeue_int); run_test(test_mock_enqueue_dequeue_heterogenous); run_test(test_LILY_NARGS); + run_test(test_lily_mock_call); return 0; } diff --git a/tests/mock_queue.c b/tests/mock_queue.c index 4015c68..b7f852c 100644 --- a/tests/mock_queue.c +++ b/tests/mock_queue.c @@ -76,3 +76,45 @@ const char* test_LILY_NARGS() return 0; } + + +const char* test_lily_mock_call() +{ + lily_mock_t *m = lily_mock_create(); + + int n = 5; + const char *str = "hello, world!"; + lily_queue_t *q; + + struct lily_mock_arg_t args[] = + { { sizeof(int), &n }, + { sizeof(const char *), &str }, + { sizeof(lily_queue_t *), &q }, + }; + + lily_mock_call(m, args); + n = 16; + str = "hi there"; + lily_mock_call(m, args); + + if (m->n_calls != 2) return "incorrect number of calls registered"; + + int k; const char *s; lily_queue_t *p; + struct lily_mock_arg_t get_args[] = + { { sizeof(int), &k }, + { sizeof(const char *), &s }, + { sizeof(lily_queue_t *), &p }, + }; + lily_get_call(m, get_args, 0); + if (k != 5) return "incorrect int argument 0 registered"; + if (strcmp(s, "hello, world!") != 0) return "incorrect string argument 0 registered"; + if (p != q) return "incorrect pointer argument 0 registered"; + + lily_get_call(m, get_args, 1); + if (k != 16) return "incorrect int argument 1 registered"; + if (strcmp(s, "hi there") != 0) return "incorrect string argument 1 registered"; + if (p != q) return "incorrect pointer argument 1 registered"; + + lily_mock_destroy(m); + return 0; +} diff --git a/tests/tests.h b/tests/tests.h index 39ad153..9584cac 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -21,5 +21,6 @@ const char* test_assert_int_not_equal(); const char* test_mock_enqueue_dequeue_int(); const char* test_mock_enqueue_dequeue_heterogenous(); const char* test_LILY_NARGS(); +const char* test_lily_mock_call(); #endif -- cgit v1.2.1