Tue, 28 Dec 2021 17:24:18 +0100
add cxLinkedListFromArray() and cxListCompare()
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 const int 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))
584 }
586 void test_hl_linked_list_add(void) {
587 cxTestingAllocatorReset();
589 int data;
590 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
592 data = 5;
593 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
594 data = 47;
595 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
596 data = 13;
597 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
599 CU_ASSERT_EQUAL(list->size, 3)
600 CU_ASSERT_TRUE(list->capacity >= list->size)
602 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
603 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
604 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
606 cxLinkedListDestroy(list);
607 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
608 }
610 void test_hl_linked_list_insert(void) {
611 cxTestingAllocatorReset();
613 int data;
614 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
616 data = 5;
617 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
618 CU_ASSERT_EQUAL(list->size, 0)
619 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
620 CU_ASSERT_EQUAL(list->size, 1)
621 data = 47;
622 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
623 CU_ASSERT_EQUAL(list->size, 2)
624 data = 13;
625 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
626 CU_ASSERT_EQUAL(list->size, 3)
627 data = 42;
628 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
630 CU_ASSERT_EQUAL(list->size, 4)
631 CU_ASSERT_TRUE(list->capacity >= list->size)
633 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
634 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
635 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
636 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
638 cxLinkedListDestroy(list);
639 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
640 }
642 void test_hl_linked_list_remove(void) {
643 cxTestingAllocatorReset();
645 int data;
646 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
648 data = 5;
649 cxListAdd(list, &data);
650 data = 47;
651 cxListAdd(list, &data);
652 data = 42;
653 cxListAdd(list, &data);
654 data = 13;
655 cxListAdd(list, &data);
657 CU_ASSERT_EQUAL(list->size, 4)
658 CU_ASSERT_TRUE(list->capacity >= list->size)
660 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
662 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
663 CU_ASSERT_EQUAL(list->size, 3)
664 CU_ASSERT_TRUE(list->capacity >= list->size)
665 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
666 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
667 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
669 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
670 CU_ASSERT_EQUAL(list->size, 2)
671 CU_ASSERT_TRUE(list->capacity >= list->size)
672 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
673 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
675 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
676 CU_ASSERT_EQUAL(list->size, 1)
677 CU_ASSERT_TRUE(list->capacity >= list->size)
678 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
680 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
681 CU_ASSERT_EQUAL(list->size, 0)
682 CU_ASSERT_TRUE(list->capacity >= list->size)
684 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
686 cxLinkedListDestroy(list);
687 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
688 }
690 void test_hl_linked_list_at(void) {
691 cxTestingAllocatorReset();
693 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
695 int data;
696 data = 5;
697 cxListAdd(list, &data);
698 data = 47;
699 cxListAdd(list, &data);
700 data = 13;
701 cxListAdd(list, &data);
703 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
704 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
705 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
706 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
708 cxLinkedListDestroy(list);
709 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
710 }
712 void test_hl_linked_list_find(void) {
713 cxTestingAllocatorReset();
715 int data, criteria;
716 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
718 data = 5;
719 cxListAdd(list, &data);
720 data = 47;
721 cxListAdd(list, &data);
722 data = 13;
723 cxListAdd(list, &data);
725 CU_ASSERT_EQUAL(list->size, 3)
726 CU_ASSERT_TRUE(list->capacity >= list->size)
728 criteria = 5;
729 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
730 criteria = 47;
731 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
732 criteria = 13;
733 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
734 criteria = 9000;
735 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
736 criteria = -5;
737 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
739 cxLinkedListDestroy(list);
740 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
741 }
743 void test_hl_linked_list_sort(void) {
744 int expected[] = {
745 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
746 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
747 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
748 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
749 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
750 4785, 4791, 4801, 4859, 4903, 4973
751 };
752 int scrambled[] = {
753 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
754 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
755 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
756 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
757 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
758 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
759 };
761 cxTestingAllocatorReset();
763 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
765 for (int i = 0; i < 100; i++) {
766 cxListAdd(list, &scrambled[i]);
767 }
769 cxListSort(list);
771 for (int i = 0; i < 100; i++) {
772 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
773 }
775 cxLinkedListDestroy(list);
776 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
777 }
779 void test_hl_ptr_linked_list_create(void) {
780 cxTestingAllocatorReset();
782 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
784 CU_ASSERT_EQUAL(list->size, 0)
785 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
786 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
787 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
788 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
790 cxLinkedListDestroy(list);
791 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
792 }
794 void test_hl_ptr_linked_list_add(void) {
795 cxTestingAllocatorReset();
797 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
799 int a = 5, b = 47, c = 13;
801 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
802 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
803 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
805 CU_ASSERT_EQUAL(list->size, 3)
806 CU_ASSERT_TRUE(list->capacity >= list->size)
808 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
809 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
810 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
812 a = 9;
813 b = 10;
814 c = 11;
816 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
817 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
818 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
820 cxLinkedListDestroy(list);
821 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
822 }
824 void test_hl_ptr_linked_list_insert(void) {
825 cxTestingAllocatorReset();
827 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
829 int a = 5, b = 47, c = 13, d = 42;
831 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
832 CU_ASSERT_EQUAL(list->size, 0)
833 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
834 CU_ASSERT_EQUAL(list->size, 1)
835 CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
836 CU_ASSERT_EQUAL(list->size, 2)
837 CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
838 CU_ASSERT_EQUAL(list->size, 3)
839 CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
841 CU_ASSERT_EQUAL(list->size, 4)
842 CU_ASSERT_TRUE(list->capacity >= list->size)
844 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
845 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
846 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
847 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
849 cxLinkedListDestroy(list);
850 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
851 }
853 void test_hl_ptr_linked_list_remove(void) {
854 cxTestingAllocatorReset();
856 int a = 5, b = 47, c = 42, d = 13;
857 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
859 cxListAdd(list, &a);
860 cxListAdd(list, &b);
861 cxListAdd(list, &c);
862 cxListAdd(list, &d);
864 CU_ASSERT_EQUAL(list->size, 4)
865 CU_ASSERT_TRUE(list->capacity >= list->size)
867 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
869 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
870 CU_ASSERT_EQUAL(list->size, 3)
871 CU_ASSERT_TRUE(list->capacity >= list->size)
872 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
873 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
874 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
876 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
877 CU_ASSERT_EQUAL(list->size, 2)
878 CU_ASSERT_TRUE(list->capacity >= list->size)
879 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
880 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
882 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
883 CU_ASSERT_EQUAL(list->size, 1)
884 CU_ASSERT_TRUE(list->capacity >= list->size)
885 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
887 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
888 CU_ASSERT_EQUAL(list->size, 0)
889 CU_ASSERT_TRUE(list->capacity >= list->size)
891 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
893 cxLinkedListDestroy(list);
894 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
895 }
897 void test_hl_ptr_linked_list_at(void) {
898 cxTestingAllocatorReset();
900 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
902 int a = 5, b = 47, c = 13;
903 cxListAdd(list, &a);
904 cxListAdd(list, &b);
905 cxListAdd(list, &c);
907 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
908 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
909 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
910 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
912 cxLinkedListDestroy(list);
913 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
914 }
916 void test_hl_ptr_linked_list_find(void) {
917 cxTestingAllocatorReset();
919 int a = 5, b = 47, c = 13, criteria;
920 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
922 cxListAdd(list, &a);
923 cxListAdd(list, &b);
924 cxListAdd(list, &c);
926 CU_ASSERT_EQUAL(list->size, 3)
927 CU_ASSERT_TRUE(list->capacity >= list->size)
929 criteria = 5;
930 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
931 criteria = 47;
932 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
933 criteria = 13;
934 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
935 criteria = 9000;
936 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
937 criteria = -5;
938 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
939 b = -5;
940 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
942 cxLinkedListDestroy(list);
943 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
944 }
946 void test_hl_ptr_linked_list_sort(void) {
947 int expected[] = {
948 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
949 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
950 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
951 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
952 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
953 4785, 4791, 4801, 4859, 4903, 4973
954 };
955 int scrambled[] = {
956 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
957 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
958 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
959 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
960 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
961 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
962 };
964 cxTestingAllocatorReset();
966 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
968 for (int i = 0; i < 100; i++) {
969 cxListAdd(list, &scrambled[i]);
970 }
972 cxListSort(list);
974 for (int i = 0; i < 100; i++) {
975 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
976 }
978 cxLinkedListDestroy(list);
979 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
980 }
982 int main() {
983 CU_pSuite suite = NULL;
985 if (CUE_SUCCESS != CU_initialize_registry()) {
986 return CU_get_error();
987 }
989 suite = CU_add_suite("low level linked list", NULL, NULL);
991 cu_add_test(suite, test_linked_list_link_unlink);
992 cu_add_test(suite, test_linked_list_at);
993 cu_add_test(suite, test_linked_list_find);
994 cu_add_test(suite, test_linked_list_compare);
995 cu_add_test(suite, test_linked_list_prepend);
996 cu_add_test(suite, test_linked_list_add);
997 cu_add_test(suite, test_linked_list_insert);
998 cu_add_test(suite, test_linked_list_insert_chain);
999 cu_add_test(suite, test_linked_list_first);
1000 cu_add_test(suite, test_linked_list_last);
1001 cu_add_test(suite, test_linked_list_prev);
1002 cu_add_test(suite, test_linked_list_remove);
1003 cu_add_test(suite, test_linked_list_size);
1004 cu_add_test(suite, test_linked_list_sort);
1005 cu_add_test(suite, test_linked_list_reverse);
1007 suite = CU_add_suite("high level linked list", NULL, NULL);
1009 cu_add_test(suite, test_hl_linked_list_create);
1010 cu_add_test(suite, test_hl_linked_list_from_array);
1011 cu_add_test(suite, test_hl_linked_list_add);
1012 cu_add_test(suite, test_hl_linked_list_insert);
1013 cu_add_test(suite, test_hl_linked_list_remove);
1014 cu_add_test(suite, test_hl_linked_list_at);
1015 cu_add_test(suite, test_hl_linked_list_find);
1016 cu_add_test(suite, test_hl_linked_list_sort);
1018 suite = CU_add_suite("high level pointer linked list", NULL, NULL);
1020 cu_add_test(suite, test_hl_ptr_linked_list_create);
1021 cu_add_test(suite, test_hl_ptr_linked_list_add);
1022 cu_add_test(suite, test_hl_ptr_linked_list_insert);
1023 cu_add_test(suite, test_hl_ptr_linked_list_remove);
1024 cu_add_test(suite, test_hl_ptr_linked_list_at);
1025 cu_add_test(suite, test_hl_ptr_linked_list_find);
1026 cu_add_test(suite, test_hl_ptr_linked_list_sort);
1028 CU_basic_set_mode(UCX_CU_BRM);
1030 int exitcode;
1031 if (CU_basic_run_tests()) {
1032 exitcode = CU_get_error();
1033 } else {
1034 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
1035 }
1036 CU_cleanup_registry();
1037 return exitcode;
1038 }