From 0e8f26743214b903f66eb732e4a8c151213d61e1 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 28 Sep 2021 22:44:30 -0500 Subject: add mock_queue_cleanup() function and move queue malloc call inside mock_queue_data --- src/test/mock_queue.c | 16 +++++++++++++++- src/test/mock_queue.h | 6 +++--- src/test/mock_queue_tests.c | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/test/mock_queue.c b/src/test/mock_queue.c index 10ce834..7a36b65 100644 --- a/src/test/mock_queue.c +++ b/src/test/mock_queue.c @@ -1,5 +1,6 @@ #include #include +#include #include "mock_queue.h" @@ -35,7 +36,13 @@ void mock_queue_data(size_t size, void *data) return; } - node->data = data; + node->data = malloc(size); + if (node->data == NULL) { + fprintf(stderr, "WARNING: memory allocation of %zu bytes for mock queue data failed!\n", size); + return; + } + + memcpy(node->data, data, size); node->next = NULL; mock_queue_tail->next = node; mock_queue_tail = node; @@ -67,3 +74,10 @@ void mock_pop() mock_queue_tail = &mock_queue_head; mock_queue_length -= 1; } + + +void mock_queue_cleanup() +{ + while(mock_queue_length != 0) + mock_pop(); +} diff --git a/src/test/mock_queue.h b/src/test/mock_queue.h index 9cfd415..1c2bbf7 100644 --- a/src/test/mock_queue.h +++ b/src/test/mock_queue.h @@ -4,6 +4,7 @@ #include void mock_queue_init(); +void mock_queue_cleanup(); size_t mock_queue_len(); @@ -11,9 +12,8 @@ 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); \ + type data = raw; \ + mock_queue_data(sizeof(type), &data); \ } while(0) #define mock_front(type) * (type *) mock_front_data() diff --git a/src/test/mock_queue_tests.c b/src/test/mock_queue_tests.c index 0e208e4..004444c 100644 --- a/src/test/mock_queue_tests.c +++ b/src/test/mock_queue_tests.c @@ -4,6 +4,7 @@ mu_test mock_queue_int(); mu_test mock_queue_int_seq(); mu_test mock_queue_pop_multi(); +mu_test mock_queue_clean_finish(); void mock_queue_tests() @@ -12,6 +13,7 @@ void mock_queue_tests() 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_run_test("clean up mock queue", mock_queue_clean_finish); } @@ -79,3 +81,21 @@ mu_test mock_queue_pop_multi() mu_assert_equal(mock_queue_len(), 0); return 0; } + + +mu_test mock_queue_clean_finish() +{ + mu_assert_equal(mock_queue_len(), 0); + + mock_queue(int, 5); + mock_queue(int, 5); + mock_queue(int, 5); + + mu_assert_equal(mock_queue_len(), 3); + + mock_queue_cleanup(); + + mu_assert_equal(mock_queue_len(), 0); + + return 0; +} -- cgit v1.2.1