144 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third) |
144 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third) |
145 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third) |
145 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third) |
146 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) |
146 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) |
147 } |
147 } |
148 |
148 |
|
149 void test_linked_list_size(void) { |
|
150 struct node { |
|
151 void *next; |
|
152 }; |
|
153 ptrdiff_t loc = offsetof(struct node, next); |
|
154 |
|
155 struct node first = {NULL}; |
|
156 struct node second = {NULL}; |
|
157 struct node third = {NULL}; |
|
158 |
|
159 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0) |
|
160 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1) |
|
161 first.next = &second; |
|
162 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2) |
|
163 second.next = &third; |
|
164 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3) |
|
165 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2) |
|
166 } |
|
167 |
|
168 void test_linked_list_sort(void) { |
|
169 struct node { |
|
170 void *prev; |
|
171 void *next; |
|
172 int data; |
|
173 }; |
|
174 |
|
175 int expected[] = { |
|
176 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
|
177 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
|
178 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
|
179 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
|
180 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
|
181 4785, 4791, 4801, 4859, 4903, 4973 |
|
182 }; |
|
183 int scrambled[] = { |
|
184 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
|
185 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
|
186 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
|
187 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
|
188 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
|
189 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
|
190 }; |
|
191 |
|
192 struct node *nodes = calloc(100, sizeof(struct node)); |
|
193 for (int i = 0; i < 100; i++) { |
|
194 nodes[i].prev = i == 0 ? NULL : &nodes[i - 1]; |
|
195 nodes[i].next = i == 99 ? NULL : &nodes[i + 1]; |
|
196 nodes[i].data = scrambled[i]; |
|
197 } |
|
198 |
|
199 struct node *begin = &nodes[0]; |
|
200 struct node *end = &nodes[99]; |
|
201 |
|
202 cx_linked_list_sort((void **) &begin, (void **) &end, |
|
203 offsetof(struct node, prev), |
|
204 offsetof(struct node, next), |
|
205 offsetof(struct node, data), |
|
206 0, (CxListComparator) cmp_int); |
|
207 |
|
208 CU_ASSERT_PTR_NULL(begin->prev) |
|
209 CU_ASSERT_EQUAL(begin->data, expected[0]) |
|
210 struct node *check = begin; |
|
211 struct node *check_last = NULL; |
|
212 for (int i = 0 ; i < 100 ; i++) { |
|
213 CU_ASSERT_EQUAL(check->data, expected[i]) |
|
214 CU_ASSERT_PTR_EQUAL(check->prev, check_last) |
|
215 if (i < 99) { |
|
216 CU_ASSERT_PTR_NOT_NULL(check->next) |
|
217 } |
|
218 check_last = check; |
|
219 check = check->next; |
|
220 } |
|
221 CU_ASSERT_PTR_NULL(check) |
|
222 CU_ASSERT_EQUAL(end->data, expected[99]) |
|
223 } |
|
224 |
149 |
225 |
150 void test_hl_linked_list_create(void) { |
226 void test_hl_linked_list_create(void) { |
151 cxTestingAllocatorReset(); |
227 cxTestingAllocatorReset(); |
152 |
228 |
153 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
229 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |