diff options
author | sanine <sanine.not@pm.me> | 2021-09-28 22:38:02 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-09-28 22:38:02 -0500 |
commit | bedf73313c8f19ff1b19217d989b1886c8f809ae (patch) | |
tree | dcc4568d03837d1a3f35d62edf22c47451d62dfd | |
parent | 1895d16f8ad5487db25650c628f4d9eaf8542e80 (diff) |
add mock queue
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/test/mock_queue.c | 69 | ||||
-rw-r--r-- | src/test/mock_queue.h | 23 | ||||
-rw-r--r-- | src/test/mock_queue_tests.c | 81 | ||||
-rw-r--r-- | src/test/suites.h | 4 |
5 files changed, 179 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 10f0b84..25064f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,9 @@ target_link_libraries(honey ${LIBRARIES}) set(TEST_ROOT ${CMAKE_SOURCE_DIR}/src/test) set(TEST_SOURCES ${TEST_ROOT}/test_main.c + ${TEST_ROOT}/mock_queue.c + + ${TEST_ROOT}/mock_queue_tests.c ${TEST_ROOT}/logging/logging_tests.c ${SRC_ROOT}/logging/logging.c diff --git a/src/test/mock_queue.c b/src/test/mock_queue.c new file mode 100644 index 0000000..10ce834 --- /dev/null +++ b/src/test/mock_queue.c @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "mock_queue.h" + +struct mock_queue_node_t; + +struct mock_queue_node_t { + struct mock_queue_node_t *next; + void *data; +}; + +static struct mock_queue_node_t mock_queue_head; +static struct mock_queue_node_t *mock_queue_tail; +size_t mock_queue_length; + +void mock_queue_init() +{ + mock_queue_length = 0; + mock_queue_head.next = NULL; + mock_queue_tail = &mock_queue_head; +} + + +size_t mock_queue_len() { + return mock_queue_length; +} + + +void mock_queue_data(size_t size, void *data) +{ + struct mock_queue_node_t *node = malloc(sizeof(struct mock_queue_node_t)); + if (node == NULL) { + fprintf(stderr, "WARNING: memory allocation for mock queue node failed!\n"); + return; + } + + node->data = data; + node->next = NULL; + mock_queue_tail->next = node; + mock_queue_tail = node; + + mock_queue_length += 1; +} + + +void * mock_front_data() +{ + if (mock_queue_head.next == NULL) + return NULL; + return mock_queue_head.next->data; +} + + +void mock_pop() +{ + if (mock_queue_head.next == NULL) + return; + + if (mock_queue_head.next->data != NULL) + free(mock_queue_head.next->data); + struct mock_queue_node_t *headnext = mock_queue_head.next->next; + free(mock_queue_head.next); + mock_queue_head.next = headnext; + if (headnext == NULL) + // no more nodes, bring tail back to head + mock_queue_tail = &mock_queue_head; + mock_queue_length -= 1; +} diff --git a/src/test/mock_queue.h b/src/test/mock_queue.h new file mode 100644 index 0000000..9cfd415 --- /dev/null +++ b/src/test/mock_queue.h @@ -0,0 +1,23 @@ +#ifndef MOCK_QUEUE_H +#define MOCK_QUEUE_H + +#include <stdlib.h> + +void mock_queue_init(); + +size_t mock_queue_len(); + +void mock_queue_data(size_t size, void *data); +void * mock_front_data(); + +#define mock_queue(type, raw) do { \ + type *data = malloc(sizeof(type)); \ + *data = raw; \ + mock_queue_data(sizeof(type), data); \ + } while(0) + +#define mock_front(type) * (type *) mock_front_data() + +void mock_pop(); + +#endif diff --git a/src/test/mock_queue_tests.c b/src/test/mock_queue_tests.c new file mode 100644 index 0000000..0e208e4 --- /dev/null +++ b/src/test/mock_queue_tests.c @@ -0,0 +1,81 @@ +#include "test/minunit.h" +#include "test/mock_queue.h" + +mu_test mock_queue_int(); +mu_test mock_queue_int_seq(); +mu_test mock_queue_pop_multi(); + + +void mock_queue_tests() +{ + mock_queue_init(); + mu_run_test("push and pop integer to mock queue", mock_queue_int); + mu_run_test("push and pop integer sequence", mock_queue_int_seq); + mu_run_test("pop multiple times with empty queue", mock_queue_pop_multi); +} + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +mu_test mock_queue_int() +{ + mock_queue(int, 5); + mu_assert_unequal(mock_queue_len(), 0); + mu_assert_equal(mock_front(int), 5); + + mock_pop(); + + mu_assert_equal(mock_queue_len(), 0); + + return 0; +} + + +mu_test mock_queue_int_seq() +{ + mock_queue(int, 2); + mock_queue(int, 1); + mock_queue(int, 3); + mock_queue(int, 4); + + mu_assert_unequal(mock_queue_len(), 0); + mu_assert_equal(mock_queue_len(), 4); + mu_assert_equal(mock_front(int), 2); + + mock_pop(); + + mu_assert_unequal(mock_queue_len(), 0); + mu_assert_equal(mock_queue_len(), 3); + mu_assert_equal(mock_front(int), 1); + + mock_pop(); + + mu_assert_unequal(mock_queue_len(), 0); + mu_assert_equal(mock_queue_len(), 2); + mu_assert_equal(mock_front(int), 3); + + mock_pop(); + + mu_assert_unequal(mock_queue_len(), 0); + mu_assert_equal(mock_queue_len(), 1); + mu_assert_equal(mock_front(int), 4); + + mock_pop(); + + mu_assert_equal(mock_queue_len(), 0); + + return 0; +} + + +mu_test mock_queue_pop_multi() +{ + mu_assert_equal(mock_queue_len(), 0); + + mock_pop(); + mock_pop(); + mock_pop(); + + mu_assert_equal(mock_queue_len(), 0); + return 0; +} diff --git a/src/test/suites.h b/src/test/suites.h index 1dec589..893f2b1 100644 --- a/src/test/suites.h +++ b/src/test/suites.h @@ -3,9 +3,11 @@ #include "minunit.h" +void mock_queue_tests(); void honey_logging_tests(); #define RUN_TESTS \ - mu_run_suite(honey_logging_tests); + mu_run_suite(honey_logging_tests); \ + mu_run_suite(mock_queue_tests); #endif |