test/test_list.c

changeset 506
18782bbe3607
parent 503
a89857072ace
child 507
2e8878770de0
--- a/test/test_list.c	Tue Feb 15 20:01:59 2022 +0100
+++ b/test/test_list.c	Fri Feb 25 14:35:18 2022 +0100
@@ -557,39 +557,28 @@
     destroy_test_data(expected);
 }
 
-void test_hl_linked_list_create(void) {
-    cxTestingAllocatorReset();
-
-    CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
-
+static void test_linked_list_create(CxList *list) {
     CU_ASSERT_EQUAL(list->size, 0)
     CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
     CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
-    CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
     CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
+}
+
+void test_hl_linked_list_create(void) {
+    CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
+    CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
+    test_linked_list_create(list);
 }
 
 void test_hl_ptr_linked_list_create(void) {
-    cxTestingAllocatorReset();
-
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
-
-    CU_ASSERT_EQUAL(list->size, 0)
-    CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
-    CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
     CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
-    CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
-
-    cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
+    test_linked_list_create(list);
 }
 
 void test_hl_linked_list_from_array(void) {
-    cxTestingAllocatorReset();
-
     int data[] = {2, 4, 5, 7, 10, 15};
 
     CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
@@ -601,12 +590,9 @@
 
     cxListDestroy(list);
     cxListDestroy(expected);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_add(void) {
-    cxTestingAllocatorReset();
-
     int data;
     CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
 
@@ -626,12 +612,9 @@
 
     cxListDestroy(list);
     cxListDestroy(expected);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_add(void) {
-    cxTestingAllocatorReset();
-
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     int a = 5, b = 47, c = 13;
@@ -656,12 +639,9 @@
     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_insert(void) {
-    cxTestingAllocatorReset();
-
     int data;
     CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
 
@@ -688,12 +668,9 @@
 
     cxListDestroy(list);
     cxListDestroy(expected);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_insert(void) {
-    cxTestingAllocatorReset();
-
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     int a = 5, b = 47, c = 13, d = 42;
@@ -717,12 +694,9 @@
     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_remove(void) {
-    cxTestingAllocatorReset();
-
     int data[] = {5, 47, 42, 13};
     CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
                                          sizeof(int), 4, data);
@@ -757,12 +731,9 @@
     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_remove(void) {
-    cxTestingAllocatorReset();
-
     int a = 5, b = 47, c = 42, d = 13;
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
@@ -801,12 +772,9 @@
     CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_at(void) {
-    cxTestingAllocatorReset();
-
     int data[] = {5, 47, 13};
     CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
                                          sizeof(int), 3, data);
@@ -817,12 +785,9 @@
     CU_ASSERT_PTR_NULL(cxListAt(list, 3))
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_at(void) {
-    cxTestingAllocatorReset();
-
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     int a = 5, b = 47, c = 13;
@@ -836,12 +801,9 @@
     CU_ASSERT_PTR_NULL(cxListAt(list, 3))
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_find(void) {
-    cxTestingAllocatorReset();
-
     int data[] = {5, 47, 13};
     CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int,
                                          sizeof(int), 3, data);
@@ -862,12 +824,9 @@
     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_find(void) {
-    cxTestingAllocatorReset();
-
     int a = 5, b = 47, c = 13, criteria;
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
@@ -892,7 +851,6 @@
     CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_sort(void) {
@@ -912,9 +870,6 @@
             3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
             4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
     };
-
-    cxTestingAllocatorReset();
-
     CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled);
     CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected);
 
@@ -923,7 +878,6 @@
 
     cxListDestroy(list);
     cxListDestroy(exp);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_sort(void) {
@@ -944,8 +898,6 @@
             4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
     };
 
-    cxTestingAllocatorReset();
-
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
 
     for (int i = 0; i < 100; i++) {
@@ -959,7 +911,6 @@
     }
 
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_iterator_impl(CxList *list) {
@@ -979,11 +930,9 @@
     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6)
     CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8)
     cxListDestroy(list);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_linked_list_iterator(void) {
-    cxTestingAllocatorReset();
     CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
     for (int i = 0; i < 10; i++) {
         cxListAdd(list, &i);
@@ -992,7 +941,6 @@
 }
 
 void test_hl_ptr_linked_list_iterator(void) {
-    cxTestingAllocatorReset();
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
     int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
     for (int i = 0; i < 10; i++) {
@@ -1002,7 +950,6 @@
 }
 
 void test_hl_linked_list_insert_via_iterator(void) {
-    cxTestingAllocatorReset();
     CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int));
     for (int i = 0; i < 5; i++) {
         cxListAdd(list, &i);
@@ -1043,12 +990,10 @@
     CU_ASSERT_EQUAL(0, cxListCompare(list, expected))
     cxListDestroy(list);
     cxListDestroy(expected);
-    CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
 void test_hl_ptr_linked_list_insert_via_iterator(void) {
     int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50};
-    cxTestingAllocatorReset();
     CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int);
     int i;
     for (i = 0; i < 5; i++) {
@@ -1084,6 +1029,13 @@
     }
 
     cxListDestroy(list);
+}
+
+static void test_setup_allocator(void) {
+    cxTestingAllocatorReset();
+}
+
+static void test_verify_allocator(void) {
     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
@@ -1112,7 +1064,9 @@
     cu_add_test(suite, test_linked_list_sort);
     cu_add_test(suite, test_linked_list_reverse);
 
-    suite = CU_add_suite("high level linked list", NULL, NULL);
+    suite = CU_add_suite_with_setup_and_teardown(
+            "high level linked list", NULL, NULL,
+            test_setup_allocator, test_verify_allocator);
 
     cu_add_test(suite, test_hl_linked_list_create);
     cu_add_test(suite, test_hl_linked_list_from_array);
@@ -1125,7 +1079,9 @@
     cu_add_test(suite, test_hl_linked_list_iterator);
     cu_add_test(suite, test_hl_linked_list_insert_via_iterator);
 
-    suite = CU_add_suite("high level pointer linked list", NULL, NULL);
+    suite = CU_add_suite_with_setup_and_teardown(
+            "high level pointer linked list", NULL, NULL,
+            test_setup_allocator, test_verify_allocator);
 
     cu_add_test(suite, test_hl_ptr_linked_list_create);
     cu_add_test(suite, test_hl_ptr_linked_list_add);

mercurial