Tue, 28 Dec 2021 17:49:52 +0100
fix missing cleanup in test_hl_linked_list_from_array
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "cx/linked_list.h"
30 #include "test_config.h"
31 #include "util_allocator.h"
33 int cmp_int(
34 int const *l,
35 int const *r
36 ) {
37 int left = *l, right = *r;
38 return left == right ? 0 : (left < right ? -1 : 1);
39 }
41 struct node {
42 struct node *next;
43 struct node *prev;
44 int data;
45 };
47 #define nd(name) name = {0}
49 const ptrdiff_t loc_prev = offsetof(struct node, prev);
50 const ptrdiff_t loc_next = offsetof(struct node, next);
51 const ptrdiff_t loc_data = offsetof(struct node, data);
53 struct node *create_test_data(
54 size_t n,
55 int const data[]
56 ) {
57 if (n == 0) return NULL;
58 struct node *begin = calloc(1, sizeof(struct node));
59 struct node *prev = begin;
60 if (data) begin->data = data[0];
61 for (size_t i = 1; i < n; i++) {
62 struct node *node = calloc(1, sizeof(struct node));
63 if (data) node->data = data[i];
64 cx_linked_list_link(prev, node, loc_prev, loc_next);
65 prev = node;
66 }
67 return begin;
68 }
70 void destroy_test_data(struct node *begin) {
71 struct node *node = begin;
72 while (node) {
73 struct node *next = node->next;
74 free(node);
75 node = next;
76 }
77 }
79 void test_linked_list_link_unlink(void) {
81 struct node nd(a), nd(b), nd(c);
83 cx_linked_list_link(&a, &b, loc_prev, loc_next);
84 CU_ASSERT_PTR_NULL(a.prev)
85 CU_ASSERT_PTR_EQUAL(a.next, &b)
86 CU_ASSERT_PTR_EQUAL(b.prev, &a)
87 CU_ASSERT_PTR_NULL(b.next)
89 cx_linked_list_unlink(&a, &b, loc_prev, loc_next);
90 CU_ASSERT_PTR_NULL(a.prev)
91 CU_ASSERT_PTR_NULL(a.next)
92 CU_ASSERT_PTR_NULL(b.prev)
93 CU_ASSERT_PTR_NULL(b.next)
95 cx_linked_list_link(&b, &c, loc_prev, loc_next);
96 cx_linked_list_link(&a, &b, loc_prev, loc_next);
97 cx_linked_list_unlink(&b, &c, loc_prev, loc_next);
98 CU_ASSERT_PTR_NULL(a.prev)
99 CU_ASSERT_PTR_EQUAL(a.next, &b)
100 CU_ASSERT_PTR_EQUAL(b.prev, &a)
101 CU_ASSERT_PTR_NULL(b.next)
102 CU_ASSERT_PTR_NULL(c.prev)
103 CU_ASSERT_PTR_NULL(c.next)
104 }
106 void test_linked_list_at(void) {
107 struct node nd(a), nd(b), nd(c), nd(d);
108 cx_linked_list_link(&a, &b, loc_prev, loc_next);
109 cx_linked_list_link(&b, &c, loc_prev, loc_next);
110 cx_linked_list_link(&c, &d, loc_prev, loc_next);
112 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
113 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
114 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
115 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
116 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
118 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
119 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
120 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
121 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
122 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
124 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
125 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
126 }
128 void test_linked_list_find(void) {
129 int data[] = {2, 4, 6, 8};
130 void *list = create_test_data(4, data);
131 int s;
133 s = 2;
134 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
135 false, (CxListComparator) cmp_int, &s), 0)
136 s = 4;
137 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
138 false, (CxListComparator) cmp_int, &s), 1)
139 s = 6;
140 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
141 false, (CxListComparator) cmp_int, &s), 2)
142 s = 8;
143 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
144 false, (CxListComparator) cmp_int, &s), 3)
145 s = 10;
146 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
147 false, (CxListComparator) cmp_int, &s), 4)
148 s = -2;
149 CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data,
150 false, (CxListComparator) cmp_int, &s), 4)
151 }
153 void test_linked_list_compare(void) {
154 int a[] = {2, 4, 6, 8};
155 int b[] = {2, 4, 6};
156 int c[] = {2, 4, 6, 9};
158 void *la = create_test_data(4, a);
159 void *lb = create_test_data(3, b);
160 void *lc = create_test_data(4, c);
162 CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data,
163 false, (CxListComparator) cmp_int)
164 )
165 CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data,
166 false, (CxListComparator) cmp_int)
167 )
168 CU_ASSERT_TRUE(0 < cx_linked_list_compare(lc, la, loc_next, loc_data,
169 false, (CxListComparator) cmp_int)
170 )
171 CU_ASSERT_TRUE(0 > cx_linked_list_compare(la, lc, loc_next, loc_data,
172 false, (CxListComparator) cmp_int)
173 )
174 CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data,
175 false, (CxListComparator) cmp_int)
176 )
178 destroy_test_data(la);
179 destroy_test_data(lb);
180 destroy_test_data(lc);
181 }
183 void test_linked_list_add(void) {
184 struct node nodes[4];
185 void *begin, *end;
187 // test with begin, end / prev, next
188 memset(nodes, 0, 4 * sizeof(struct node));
189 begin = end = NULL;
191 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
192 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
193 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
194 CU_ASSERT_PTR_NULL(nodes[0].prev)
195 CU_ASSERT_PTR_NULL(nodes[0].next)
197 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
198 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
199 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
200 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
201 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
203 // test with begin only / prev, next
204 memset(nodes, 0, 4 * sizeof(struct node));
205 begin = end = NULL;
207 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
208 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
209 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
210 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
211 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
212 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
214 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
215 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
216 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
218 // test with end only / prev, next
219 memset(nodes, 0, 4 * sizeof(struct node));
220 begin = end = NULL;
222 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]);
223 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
224 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]);
225 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
226 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
227 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
229 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]);
230 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
231 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
232 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
234 // test with begin, end / next
235 memset(nodes, 0, 4 * sizeof(struct node));
236 begin = end = NULL;
238 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
239 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
240 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
241 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
242 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
243 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
244 CU_ASSERT_PTR_NULL(nodes[1].prev)
245 }
247 void test_linked_list_prepend(void) {
248 struct node nodes[4];
249 void *begin, *end;
251 // test with begin, end / prev, next
252 memset(nodes, 0, 4 * sizeof(struct node));
253 begin = end = NULL;
255 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
256 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
257 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
258 CU_ASSERT_PTR_NULL(nodes[0].prev)
259 CU_ASSERT_PTR_NULL(nodes[0].next)
261 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
262 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
263 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
264 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
265 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
267 // test with begin only / prev, next
268 memset(nodes, 0, 4 * sizeof(struct node));
269 begin = end = NULL;
271 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]);
272 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
273 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]);
274 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
275 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
276 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
278 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]);
279 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
280 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
281 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
283 // test with end only / prev, next
284 memset(nodes, 0, 4 * sizeof(struct node));
285 begin = end = NULL;
287 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]);
288 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
289 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]);
290 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
291 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
292 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
294 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]);
295 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
296 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
297 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
299 // test with begin, end / next
300 memset(nodes, 0, 4 * sizeof(struct node));
301 begin = end = NULL;
303 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
304 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
305 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
306 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
307 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
308 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
309 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
310 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
311 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
312 CU_ASSERT_PTR_NULL(nodes[1].prev)
313 CU_ASSERT_PTR_NULL(nodes[0].prev)
314 }
316 void test_linked_list_insert(void) {
317 struct node nodes[4];
318 void *begin, *end;
320 // insert mid list
321 memset(nodes, 0, 4 * sizeof(struct node));
322 begin = &nodes[0];
323 end = &nodes[2];
325 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
326 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
328 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]);
329 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
330 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
331 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
332 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[3])
333 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
334 CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[2])
336 // insert end
337 memset(nodes, 0, 4 * sizeof(struct node));
338 begin = &nodes[0];
339 end = &nodes[2];
341 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
342 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
344 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]);
345 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
346 CU_ASSERT_PTR_EQUAL(end, &nodes[3])
347 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
348 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
349 CU_ASSERT_PTR_NULL(nodes[3].next)
351 // insert begin
352 memset(nodes, 0, 4 * sizeof(struct node));
353 begin = &nodes[0];
354 end = &nodes[2];
356 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
357 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
359 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]);
360 CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
361 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
362 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[3])
363 CU_ASSERT_PTR_NULL(nodes[3].prev)
364 CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[0])
365 }
367 void test_linked_list_insert_chain(void) {
368 struct node nodes[5];
369 void *begin, *end;
371 // insert mid list
372 memset(nodes, 0, 5 * sizeof(struct node));
373 begin = &nodes[0];
374 end = &nodes[2];
376 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
377 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
378 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
380 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL);
381 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
382 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
383 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
384 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[4])
385 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
386 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[2])
388 // insert end
389 memset(nodes, 0, 5 * sizeof(struct node));
390 begin = &nodes[0];
391 end = &nodes[2];
393 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
394 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
395 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
397 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL);
398 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
399 CU_ASSERT_PTR_EQUAL(end, &nodes[4])
400 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
401 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
402 CU_ASSERT_PTR_NULL(nodes[4].next)
404 // insert begin
405 memset(nodes, 0, 5 * sizeof(struct node));
406 begin = &nodes[0];
407 end = &nodes[2];
409 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
410 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
411 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
413 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL);
414 CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
415 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
416 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[4])
417 CU_ASSERT_PTR_NULL(nodes[3].prev)
418 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0])
419 }
421 void test_linked_list_first(void) {
422 struct node *begin = create_test_data(3, NULL);
423 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin)
424 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin)
425 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin)
426 destroy_test_data(begin);
427 }
429 void test_linked_list_last(void) {
430 struct node *begin = create_test_data(3, NULL);
431 struct node *end = begin->next->next;
432 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end)
433 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end)
434 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end)
435 destroy_test_data(begin);
436 }
438 void test_linked_list_prev(void) {
439 struct node *begin = create_test_data(3, NULL);
440 CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin))
441 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin)
442 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next)
443 destroy_test_data(begin);
444 }
446 void test_linked_list_remove(void) {
447 void *begin, *end;
449 int data[] = {2, 4, 6};
450 begin = create_test_data(3, data);
451 struct node *first = begin;
452 struct node *second = first->next;
453 struct node *third = second->next;
454 end = third;
456 cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second);
457 CU_ASSERT_PTR_EQUAL(begin, first)
458 CU_ASSERT_PTR_EQUAL(end, third)
459 CU_ASSERT_PTR_NULL(first->prev)
460 CU_ASSERT_PTR_EQUAL(first->next, third)
461 CU_ASSERT_PTR_EQUAL(third->prev, first)
462 CU_ASSERT_PTR_NULL(third->next)
464 cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third);
465 CU_ASSERT_PTR_EQUAL(begin, first)
466 CU_ASSERT_PTR_EQUAL(end, first)
467 CU_ASSERT_PTR_NULL(first->prev)
468 CU_ASSERT_PTR_NULL(first->next)
470 cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first);
471 CU_ASSERT_PTR_NULL(begin)
472 CU_ASSERT_PTR_NULL(end)
474 free(first);
475 free(second);
476 free(third);
477 }
479 void test_linked_list_size(void) {
480 struct node *list;
482 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0)
484 list = create_test_data(5, NULL);
485 CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5)
486 destroy_test_data(list);
488 list = create_test_data(13, NULL);
489 CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13)
490 destroy_test_data(list);
491 }
493 void test_linked_list_sort(void) {
494 int expected[] = {
495 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
496 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
497 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
498 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
499 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
500 4785, 4791, 4801, 4859, 4903, 4973
501 };
502 int scrambled[] = {
503 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
504 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
505 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
506 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
507 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
508 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
509 };
511 void *begin = create_test_data(100, scrambled);
512 void *end = cx_linked_list_last(begin, loc_next);
514 cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
515 false, (CxListComparator) cmp_int);
517 struct node *check = begin;
518 struct node *check_last = NULL;
519 CU_ASSERT_PTR_NULL(check->prev)
520 CU_ASSERT_EQUAL(check->data, expected[0])
521 for (int i = 0; i < 100; i++) {
522 CU_ASSERT_EQUAL(check->data, expected[i])
523 CU_ASSERT_PTR_EQUAL(check->prev, check_last)
524 if (i < 99) {
525 CU_ASSERT_PTR_NOT_NULL(check->next)
526 }
527 check_last = check;
528 check = check->next;
529 }
530 CU_ASSERT_PTR_NULL(check)
531 CU_ASSERT_PTR_EQUAL(end, check_last)
533 destroy_test_data(begin);
534 }
536 void test_linked_list_reverse(void) {
537 void *begin, *end;
539 int data[] = {2, 4, 6, 8};
540 int reversed[] = {8, 6, 4, 2};
542 void *list = create_test_data(4, data);
543 void *expected = create_test_data(4, reversed);
545 begin = list;
546 end = cx_linked_list_last(list, loc_next);
548 cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
549 CU_ASSERT_PTR_EQUAL(end, list)
550 CU_ASSERT_PTR_EQUAL(begin, cx_linked_list_first(end, loc_prev))
551 CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data,
552 0, (CxListComparator) cmp_int))
554 destroy_test_data(begin);
555 destroy_test_data(expected);
556 }
558 void test_hl_linked_list_create(void) {
559 cxTestingAllocatorReset();
561 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
563 CU_ASSERT_EQUAL(list->size, 0)
564 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
565 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
566 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
567 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
569 cxLinkedListDestroy(list);
570 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
571 }
573 void test_hl_linked_list_from_array(void) {
574 cxTestingAllocatorReset();
576 int data[] = {2, 4, 5, 7, 10, 15};
578 CxList expected = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
579 for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]);
581 CxList list = cxLinkedListFromArray(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int), 5, data);
583 CU_ASSERT_TRUE(0 == cxListCompare(list, expected))
585 cxLinkedListDestroy(list);
586 cxLinkedListDestroy(expected);
587 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
588 }
590 void test_hl_linked_list_add(void) {
591 cxTestingAllocatorReset();
593 int data;
594 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
596 data = 5;
597 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
598 data = 47;
599 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
600 data = 13;
601 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
603 CU_ASSERT_EQUAL(list->size, 3)
604 CU_ASSERT_TRUE(list->capacity >= list->size)
606 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
607 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
608 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
610 cxLinkedListDestroy(list);
611 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
612 }
614 void test_hl_linked_list_insert(void) {
615 cxTestingAllocatorReset();
617 int data;
618 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
620 data = 5;
621 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
622 CU_ASSERT_EQUAL(list->size, 0)
623 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
624 CU_ASSERT_EQUAL(list->size, 1)
625 data = 47;
626 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
627 CU_ASSERT_EQUAL(list->size, 2)
628 data = 13;
629 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
630 CU_ASSERT_EQUAL(list->size, 3)
631 data = 42;
632 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
634 CU_ASSERT_EQUAL(list->size, 4)
635 CU_ASSERT_TRUE(list->capacity >= list->size)
637 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
638 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
639 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
640 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
642 cxLinkedListDestroy(list);
643 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
644 }
646 void test_hl_linked_list_remove(void) {
647 cxTestingAllocatorReset();
649 int data;
650 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
652 data = 5;
653 cxListAdd(list, &data);
654 data = 47;
655 cxListAdd(list, &data);
656 data = 42;
657 cxListAdd(list, &data);
658 data = 13;
659 cxListAdd(list, &data);
661 CU_ASSERT_EQUAL(list->size, 4)
662 CU_ASSERT_TRUE(list->capacity >= list->size)
664 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
666 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
667 CU_ASSERT_EQUAL(list->size, 3)
668 CU_ASSERT_TRUE(list->capacity >= list->size)
669 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
670 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
671 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
673 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
674 CU_ASSERT_EQUAL(list->size, 2)
675 CU_ASSERT_TRUE(list->capacity >= list->size)
676 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
677 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
679 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
680 CU_ASSERT_EQUAL(list->size, 1)
681 CU_ASSERT_TRUE(list->capacity >= list->size)
682 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
684 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
685 CU_ASSERT_EQUAL(list->size, 0)
686 CU_ASSERT_TRUE(list->capacity >= list->size)
688 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
690 cxLinkedListDestroy(list);
691 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
692 }
694 void test_hl_linked_list_at(void) {
695 cxTestingAllocatorReset();
697 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
699 int data;
700 data = 5;
701 cxListAdd(list, &data);
702 data = 47;
703 cxListAdd(list, &data);
704 data = 13;
705 cxListAdd(list, &data);
707 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
708 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
709 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
710 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
712 cxLinkedListDestroy(list);
713 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
714 }
716 void test_hl_linked_list_find(void) {
717 cxTestingAllocatorReset();
719 int data, criteria;
720 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
722 data = 5;
723 cxListAdd(list, &data);
724 data = 47;
725 cxListAdd(list, &data);
726 data = 13;
727 cxListAdd(list, &data);
729 CU_ASSERT_EQUAL(list->size, 3)
730 CU_ASSERT_TRUE(list->capacity >= list->size)
732 criteria = 5;
733 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
734 criteria = 47;
735 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
736 criteria = 13;
737 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
738 criteria = 9000;
739 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
740 criteria = -5;
741 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
743 cxLinkedListDestroy(list);
744 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
745 }
747 void test_hl_linked_list_sort(void) {
748 int expected[] = {
749 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
750 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
751 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
752 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
753 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
754 4785, 4791, 4801, 4859, 4903, 4973
755 };
756 int scrambled[] = {
757 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
758 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
759 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
760 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
761 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
762 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
763 };
765 cxTestingAllocatorReset();
767 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
769 for (int i = 0; i < 100; i++) {
770 cxListAdd(list, &scrambled[i]);
771 }
773 cxListSort(list);
775 for (int i = 0; i < 100; i++) {
776 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
777 }
779 cxLinkedListDestroy(list);
780 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
781 }
783 void test_hl_ptr_linked_list_create(void) {
784 cxTestingAllocatorReset();
786 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
788 CU_ASSERT_EQUAL(list->size, 0)
789 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
790 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
791 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
792 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
794 cxLinkedListDestroy(list);
795 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
796 }
798 void test_hl_ptr_linked_list_add(void) {
799 cxTestingAllocatorReset();
801 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
803 int a = 5, b = 47, c = 13;
805 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
806 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
807 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
809 CU_ASSERT_EQUAL(list->size, 3)
810 CU_ASSERT_TRUE(list->capacity >= list->size)
812 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
813 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
814 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
816 a = 9;
817 b = 10;
818 c = 11;
820 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
821 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
822 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
824 cxLinkedListDestroy(list);
825 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
826 }
828 void test_hl_ptr_linked_list_insert(void) {
829 cxTestingAllocatorReset();
831 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
833 int a = 5, b = 47, c = 13, d = 42;
835 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
836 CU_ASSERT_EQUAL(list->size, 0)
837 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
838 CU_ASSERT_EQUAL(list->size, 1)
839 CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
840 CU_ASSERT_EQUAL(list->size, 2)
841 CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
842 CU_ASSERT_EQUAL(list->size, 3)
843 CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
845 CU_ASSERT_EQUAL(list->size, 4)
846 CU_ASSERT_TRUE(list->capacity >= list->size)
848 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
849 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
850 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
851 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
853 cxLinkedListDestroy(list);
854 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
855 }
857 void test_hl_ptr_linked_list_remove(void) {
858 cxTestingAllocatorReset();
860 int a = 5, b = 47, c = 42, d = 13;
861 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
863 cxListAdd(list, &a);
864 cxListAdd(list, &b);
865 cxListAdd(list, &c);
866 cxListAdd(list, &d);
868 CU_ASSERT_EQUAL(list->size, 4)
869 CU_ASSERT_TRUE(list->capacity >= list->size)
871 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
873 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
874 CU_ASSERT_EQUAL(list->size, 3)
875 CU_ASSERT_TRUE(list->capacity >= list->size)
876 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
877 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
878 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
880 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
881 CU_ASSERT_EQUAL(list->size, 2)
882 CU_ASSERT_TRUE(list->capacity >= list->size)
883 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
884 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
886 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
887 CU_ASSERT_EQUAL(list->size, 1)
888 CU_ASSERT_TRUE(list->capacity >= list->size)
889 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
891 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
892 CU_ASSERT_EQUAL(list->size, 0)
893 CU_ASSERT_TRUE(list->capacity >= list->size)
895 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
897 cxLinkedListDestroy(list);
898 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
899 }
901 void test_hl_ptr_linked_list_at(void) {
902 cxTestingAllocatorReset();
904 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
906 int a = 5, b = 47, c = 13;
907 cxListAdd(list, &a);
908 cxListAdd(list, &b);
909 cxListAdd(list, &c);
911 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
912 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
913 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
914 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
916 cxLinkedListDestroy(list);
917 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
918 }
920 void test_hl_ptr_linked_list_find(void) {
921 cxTestingAllocatorReset();
923 int a = 5, b = 47, c = 13, criteria;
924 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
926 cxListAdd(list, &a);
927 cxListAdd(list, &b);
928 cxListAdd(list, &c);
930 CU_ASSERT_EQUAL(list->size, 3)
931 CU_ASSERT_TRUE(list->capacity >= list->size)
933 criteria = 5;
934 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
935 criteria = 47;
936 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
937 criteria = 13;
938 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
939 criteria = 9000;
940 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
941 criteria = -5;
942 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
943 b = -5;
944 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
946 cxLinkedListDestroy(list);
947 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
948 }
950 void test_hl_ptr_linked_list_sort(void) {
951 int expected[] = {
952 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
953 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
954 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
955 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
956 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
957 4785, 4791, 4801, 4859, 4903, 4973
958 };
959 int scrambled[] = {
960 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
961 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
962 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
963 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
964 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
965 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
966 };
968 cxTestingAllocatorReset();
970 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
972 for (int i = 0; i < 100; i++) {
973 cxListAdd(list, &scrambled[i]);
974 }
976 cxListSort(list);
978 for (int i = 0; i < 100; i++) {
979 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
980 }
982 cxLinkedListDestroy(list);
983 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
984 }
986 int main() {
987 CU_pSuite suite = NULL;
989 if (CUE_SUCCESS != CU_initialize_registry()) {
990 return CU_get_error();
991 }
993 suite = CU_add_suite("low level linked list", NULL, NULL);
995 cu_add_test(suite, test_linked_list_link_unlink);
996 cu_add_test(suite, test_linked_list_at);
997 cu_add_test(suite, test_linked_list_find);
998 cu_add_test(suite, test_linked_list_compare);
999 cu_add_test(suite, test_linked_list_prepend);
1000 cu_add_test(suite, test_linked_list_add);
1001 cu_add_test(suite, test_linked_list_insert);
1002 cu_add_test(suite, test_linked_list_insert_chain);
1003 cu_add_test(suite, test_linked_list_first);
1004 cu_add_test(suite, test_linked_list_last);
1005 cu_add_test(suite, test_linked_list_prev);
1006 cu_add_test(suite, test_linked_list_remove);
1007 cu_add_test(suite, test_linked_list_size);
1008 cu_add_test(suite, test_linked_list_sort);
1009 cu_add_test(suite, test_linked_list_reverse);
1011 suite = CU_add_suite("high level linked list", NULL, NULL);
1013 cu_add_test(suite, test_hl_linked_list_create);
1014 cu_add_test(suite, test_hl_linked_list_from_array);
1015 cu_add_test(suite, test_hl_linked_list_add);
1016 cu_add_test(suite, test_hl_linked_list_insert);
1017 cu_add_test(suite, test_hl_linked_list_remove);
1018 cu_add_test(suite, test_hl_linked_list_at);
1019 cu_add_test(suite, test_hl_linked_list_find);
1020 cu_add_test(suite, test_hl_linked_list_sort);
1022 suite = CU_add_suite("high level pointer linked list", NULL, NULL);
1024 cu_add_test(suite, test_hl_ptr_linked_list_create);
1025 cu_add_test(suite, test_hl_ptr_linked_list_add);
1026 cu_add_test(suite, test_hl_ptr_linked_list_insert);
1027 cu_add_test(suite, test_hl_ptr_linked_list_remove);
1028 cu_add_test(suite, test_hl_ptr_linked_list_at);
1029 cu_add_test(suite, test_hl_ptr_linked_list_find);
1030 cu_add_test(suite, test_hl_ptr_linked_list_sort);
1032 CU_basic_set_mode(UCX_CU_BRM);
1034 int exitcode;
1035 if (CU_basic_run_tests()) {
1036 exitcode = CU_get_error();
1037 } else {
1038 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
1039 }
1040 CU_cleanup_registry();
1041 return exitcode;
1042 }