From f9e7edeec9e8a42d54fada49fd5b65c4e2a5d1a0 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Wed, 28 Nov 2018 19:04:04 +0100 Subject: [PATCH] tests: complete ptrs_list unit tests Add tests for `list_clear_free_contents`, the last untested func. Also improve type correctness. Change-Id: I4ccc62268757b990e1462a9af9f76f7084848177 Signed-off-by: Michal Bloch --- Makefile.am | 2 +- src/tests/test_ptrs_list.c | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Makefile.am b/Makefile.am index 2fe2327..67936ad 100644 --- a/Makefile.am +++ b/Makefile.am @@ -199,7 +199,7 @@ src_tests_test_logger_log_storage_LDFLAGS = $(AM_LDFLAGS) src_tests_test_ptrs_list_SOURCES = src/tests/test_ptrs_list.c src/shared/ptrs_list.c src_tests_test_ptrs_list_CFLAGS = $(check_CFLAGS) -src_tests_test_ptrs_list_LDFLAGS = $(AM_LDFLAGS) +src_tests_test_ptrs_list_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=free src_tests_test_common_SOURCES = src/tests/test_common.c src/shared/logcommon.c src/shared/logconfig.c src_tests_test_common_CFLAGS = $(check_CFLAGS) diff --git a/src/tests/test_ptrs_list.c b/src/tests/test_ptrs_list.c index 3144ddd..e3913bb 100644 --- a/src/tests/test_ptrs_list.c +++ b/src/tests/test_ptrs_list.c @@ -4,19 +4,30 @@ #define NELEMS(arr) (sizeof(arr)/sizeof(arr[0])) +static void *pointers_to_free[10]; + +void __real_free(void *ptr); +void __wrap_free(void *ptr) { + for (size_t i = 0; i < NELEMS(pointers_to_free); ++i) + if (ptr == pointers_to_free[i]) + pointers_to_free[i] = NULL; + + __real_free(ptr); +} + int main(int argc, char ** argv) { - list_head head = 0; + list_head head = NULL; assert(list_count(head) == 0); - void *elems[] = {"a", "b", "c"}; - for (int i = 0; i < NELEMS(elems); i++) { + void *const elems[] = {"a", "b", "c"}; + for (size_t i = 0; i < NELEMS(elems); i++) { list_add(&head, elems[i]); assert(list_count(head) == i + 1); } list_head iter = head; - for (int i = 0; i < NELEMS(elems); ++i, list_next(&iter)) + for (size_t i = 0; i < NELEMS(elems); ++i, list_next(&iter)) assert(list_at(iter) == elems[NELEMS(elems) - 1 - i]); assert(iter == NULL); @@ -28,19 +39,28 @@ int main(int argc, char ** argv) assert(list_at(head) == elems[1]); list_remove(&head, elems[1]); //remove last assert(list_count(head) == 0); - assert(head == 0); + assert(head == NULL); - for (int i = 0; i < NELEMS(elems); i++) { + for (size_t i = 0; i < NELEMS(elems); i++) { list_add(&head, elems[i]); assert(list_count(head) == i + 1); } list_clear(&head); assert(list_count(head) == 0); - assert(head == 0); + assert(head == NULL); iter = NULL; list_next(&iter); assert(iter == NULL); + for (size_t i = 0; i < NELEMS(pointers_to_free); ++i) { + pointers_to_free[i] = malloc(i + 5); + list_add(&head, pointers_to_free[i]); + } + list_clear_free_contents(&head); + assert(head == NULL); + for (size_t i = 0; i < NELEMS(pointers_to_free); ++i) + assert(pointers_to_free[i] == NULL); + return 0; } -- 2.7.4