Fri, 25 Feb 2022 14:35:18 +0100
add setup and teardown functions to test_list.c
test/test_list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/test/test_list.c Tue Feb 15 20:01:59 2022 +0100 1.2 +++ b/test/test_list.c Fri Feb 25 14:35:18 2022 +0100 1.3 @@ -557,39 +557,28 @@ 1.4 destroy_test_data(expected); 1.5 } 1.6 1.7 -void test_hl_linked_list_create(void) { 1.8 - cxTestingAllocatorReset(); 1.9 - 1.10 - CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.11 - 1.12 +static void test_linked_list_create(CxList *list) { 1.13 CU_ASSERT_EQUAL(list->size, 0) 1.14 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 1.15 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 1.16 - CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) 1.17 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 1.18 1.19 cxListDestroy(list); 1.20 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.21 +} 1.22 + 1.23 +void test_hl_linked_list_create(void) { 1.24 + CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.25 + CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) 1.26 + test_linked_list_create(list); 1.27 } 1.28 1.29 void test_hl_ptr_linked_list_create(void) { 1.30 - cxTestingAllocatorReset(); 1.31 - 1.32 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.33 - 1.34 - CU_ASSERT_EQUAL(list->size, 0) 1.35 - CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 1.36 - CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 1.37 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) 1.38 - CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 1.39 - 1.40 - cxListDestroy(list); 1.41 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.42 + test_linked_list_create(list); 1.43 } 1.44 1.45 void test_hl_linked_list_from_array(void) { 1.46 - cxTestingAllocatorReset(); 1.47 - 1.48 int data[] = {2, 4, 5, 7, 10, 15}; 1.49 1.50 CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.51 @@ -601,12 +590,9 @@ 1.52 1.53 cxListDestroy(list); 1.54 cxListDestroy(expected); 1.55 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.56 } 1.57 1.58 void test_hl_linked_list_add(void) { 1.59 - cxTestingAllocatorReset(); 1.60 - 1.61 int data; 1.62 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.63 1.64 @@ -626,12 +612,9 @@ 1.65 1.66 cxListDestroy(list); 1.67 cxListDestroy(expected); 1.68 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.69 } 1.70 1.71 void test_hl_ptr_linked_list_add(void) { 1.72 - cxTestingAllocatorReset(); 1.73 - 1.74 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.75 1.76 int a = 5, b = 47, c = 13; 1.77 @@ -656,12 +639,9 @@ 1.78 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) 1.79 1.80 cxListDestroy(list); 1.81 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.82 } 1.83 1.84 void test_hl_linked_list_insert(void) { 1.85 - cxTestingAllocatorReset(); 1.86 - 1.87 int data; 1.88 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.89 1.90 @@ -688,12 +668,9 @@ 1.91 1.92 cxListDestroy(list); 1.93 cxListDestroy(expected); 1.94 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.95 } 1.96 1.97 void test_hl_ptr_linked_list_insert(void) { 1.98 - cxTestingAllocatorReset(); 1.99 - 1.100 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.101 1.102 int a = 5, b = 47, c = 13, d = 42; 1.103 @@ -717,12 +694,9 @@ 1.104 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) 1.105 1.106 cxListDestroy(list); 1.107 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.108 } 1.109 1.110 void test_hl_linked_list_remove(void) { 1.111 - cxTestingAllocatorReset(); 1.112 - 1.113 int data[] = {5, 47, 42, 13}; 1.114 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 1.115 sizeof(int), 4, data); 1.116 @@ -757,12 +731,9 @@ 1.117 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) 1.118 1.119 cxListDestroy(list); 1.120 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.121 } 1.122 1.123 void test_hl_ptr_linked_list_remove(void) { 1.124 - cxTestingAllocatorReset(); 1.125 - 1.126 int a = 5, b = 47, c = 42, d = 13; 1.127 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.128 1.129 @@ -801,12 +772,9 @@ 1.130 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) 1.131 1.132 cxListDestroy(list); 1.133 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.134 } 1.135 1.136 void test_hl_linked_list_at(void) { 1.137 - cxTestingAllocatorReset(); 1.138 - 1.139 int data[] = {5, 47, 13}; 1.140 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 1.141 sizeof(int), 3, data); 1.142 @@ -817,12 +785,9 @@ 1.143 CU_ASSERT_PTR_NULL(cxListAt(list, 3)) 1.144 1.145 cxListDestroy(list); 1.146 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.147 } 1.148 1.149 void test_hl_ptr_linked_list_at(void) { 1.150 - cxTestingAllocatorReset(); 1.151 - 1.152 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.153 1.154 int a = 5, b = 47, c = 13; 1.155 @@ -836,12 +801,9 @@ 1.156 CU_ASSERT_PTR_NULL(cxListAt(list, 3)) 1.157 1.158 cxListDestroy(list); 1.159 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.160 } 1.161 1.162 void test_hl_linked_list_find(void) { 1.163 - cxTestingAllocatorReset(); 1.164 - 1.165 int data[] = {5, 47, 13}; 1.166 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 1.167 sizeof(int), 3, data); 1.168 @@ -862,12 +824,9 @@ 1.169 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) 1.170 1.171 cxListDestroy(list); 1.172 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.173 } 1.174 1.175 void test_hl_ptr_linked_list_find(void) { 1.176 - cxTestingAllocatorReset(); 1.177 - 1.178 int a = 5, b = 47, c = 13, criteria; 1.179 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.180 1.181 @@ -892,7 +851,6 @@ 1.182 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) 1.183 1.184 cxListDestroy(list); 1.185 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.186 } 1.187 1.188 void test_hl_linked_list_sort(void) { 1.189 @@ -912,9 +870,6 @@ 1.190 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, 1.191 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 1.192 }; 1.193 - 1.194 - cxTestingAllocatorReset(); 1.195 - 1.196 CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled); 1.197 CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected); 1.198 1.199 @@ -923,7 +878,6 @@ 1.200 1.201 cxListDestroy(list); 1.202 cxListDestroy(exp); 1.203 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.204 } 1.205 1.206 void test_hl_ptr_linked_list_sort(void) { 1.207 @@ -944,8 +898,6 @@ 1.208 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 1.209 }; 1.210 1.211 - cxTestingAllocatorReset(); 1.212 - 1.213 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.214 1.215 for (int i = 0; i < 100; i++) { 1.216 @@ -959,7 +911,6 @@ 1.217 } 1.218 1.219 cxListDestroy(list); 1.220 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.221 } 1.222 1.223 void test_hl_linked_list_iterator_impl(CxList *list) { 1.224 @@ -979,11 +930,9 @@ 1.225 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6) 1.226 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8) 1.227 cxListDestroy(list); 1.228 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.229 } 1.230 1.231 void test_hl_linked_list_iterator(void) { 1.232 - cxTestingAllocatorReset(); 1.233 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.234 for (int i = 0; i < 10; i++) { 1.235 cxListAdd(list, &i); 1.236 @@ -992,7 +941,6 @@ 1.237 } 1.238 1.239 void test_hl_ptr_linked_list_iterator(void) { 1.240 - cxTestingAllocatorReset(); 1.241 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.242 int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 1.243 for (int i = 0; i < 10; i++) { 1.244 @@ -1002,7 +950,6 @@ 1.245 } 1.246 1.247 void test_hl_linked_list_insert_via_iterator(void) { 1.248 - cxTestingAllocatorReset(); 1.249 CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.250 for (int i = 0; i < 5; i++) { 1.251 cxListAdd(list, &i); 1.252 @@ -1043,12 +990,10 @@ 1.253 CU_ASSERT_EQUAL(0, cxListCompare(list, expected)) 1.254 cxListDestroy(list); 1.255 cxListDestroy(expected); 1.256 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.257 } 1.258 1.259 void test_hl_ptr_linked_list_insert_via_iterator(void) { 1.260 int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; 1.261 - cxTestingAllocatorReset(); 1.262 CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.263 int i; 1.264 for (i = 0; i < 5; i++) { 1.265 @@ -1084,6 +1029,13 @@ 1.266 } 1.267 1.268 cxListDestroy(list); 1.269 +} 1.270 + 1.271 +static void test_setup_allocator(void) { 1.272 + cxTestingAllocatorReset(); 1.273 +} 1.274 + 1.275 +static void test_verify_allocator(void) { 1.276 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.277 } 1.278 1.279 @@ -1112,7 +1064,9 @@ 1.280 cu_add_test(suite, test_linked_list_sort); 1.281 cu_add_test(suite, test_linked_list_reverse); 1.282 1.283 - suite = CU_add_suite("high level linked list", NULL, NULL); 1.284 + suite = CU_add_suite_with_setup_and_teardown( 1.285 + "high level linked list", NULL, NULL, 1.286 + test_setup_allocator, test_verify_allocator); 1.287 1.288 cu_add_test(suite, test_hl_linked_list_create); 1.289 cu_add_test(suite, test_hl_linked_list_from_array); 1.290 @@ -1125,7 +1079,9 @@ 1.291 cu_add_test(suite, test_hl_linked_list_iterator); 1.292 cu_add_test(suite, test_hl_linked_list_insert_via_iterator); 1.293 1.294 - suite = CU_add_suite("high level pointer linked list", NULL, NULL); 1.295 + suite = CU_add_suite_with_setup_and_teardown( 1.296 + "high level pointer linked list", NULL, NULL, 1.297 + test_setup_allocator, test_verify_allocator); 1.298 1.299 cu_add_test(suite, test_hl_ptr_linked_list_create); 1.300 cu_add_test(suite, test_hl_ptr_linked_list_add);