Mon, 27 Dec 2021 14:44:08 +0100
add tests for the new low level functions
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(int const *l, int const *r) {
34 int left = *l, right = *r;
35 return left == right ? 0 : (left < right ? -1 : 1);
36 }
38 void test_linked_list_link_unlink(void) {
39 struct node {
40 void *next;
41 void *prev;
42 };
43 const ptrdiff_t loc_prev = offsetof(struct node, prev);
44 const ptrdiff_t loc_next = offsetof(struct node, next);
46 struct node a = {NULL, NULL}, b = {NULL, NULL};
48 cx_linked_list_link(&a, &b, loc_prev, loc_next);
49 CU_ASSERT_PTR_NULL(a.prev)
50 CU_ASSERT_PTR_EQUAL(a.next, &b)
51 CU_ASSERT_PTR_EQUAL(b.prev, &a)
52 CU_ASSERT_PTR_NULL(b.next)
54 cx_linked_list_unlink(&a, &b, loc_prev, loc_next);
55 CU_ASSERT_PTR_NULL(a.prev)
56 CU_ASSERT_PTR_NULL(a.next)
57 CU_ASSERT_PTR_NULL(b.prev)
58 CU_ASSERT_PTR_NULL(b.next)
60 struct node c = {NULL, NULL};
61 cx_linked_list_link(&b, &c, loc_prev, loc_next);
62 cx_linked_list_link(&a, &b, loc_prev, loc_next);
63 cx_linked_list_unlink(&b, &c, loc_prev, loc_next);
64 CU_ASSERT_PTR_NULL(a.prev)
65 CU_ASSERT_PTR_EQUAL(a.next, &b)
66 CU_ASSERT_PTR_EQUAL(b.prev, &a)
67 CU_ASSERT_PTR_NULL(b.next)
68 CU_ASSERT_PTR_NULL(c.prev)
69 CU_ASSERT_PTR_NULL(c.next)
70 }
72 void test_linked_list_at(void) {
73 struct node {
74 void *next;
75 void *prev;
76 };
77 const ptrdiff_t loc_prev = offsetof(struct node, prev);
78 const ptrdiff_t loc_next = offsetof(struct node, next);
80 struct node a, b, c, d;
81 a.prev = NULL;
82 a.next = &b;
83 b.prev = &a;
84 b.next = &c;
85 c.prev = &b;
86 c.next = &d;
87 d.prev = &c;
88 d.next = NULL;
90 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
91 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
92 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
93 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
94 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
96 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
97 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
98 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
99 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
100 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
102 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
103 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
104 }
106 void test_linked_list_add(void) {
107 struct node {
108 void *prev;
109 void *next;
110 };
112 struct node nodes[4];
114 // test with begin, end / prev, next
115 memset(nodes, 0, 4 * sizeof(struct node));
116 void *begin = NULL;
117 void *end = NULL;
119 ptrdiff_t loc_prev = offsetof(struct node, prev);
120 ptrdiff_t loc_next = offsetof(struct node, next);
122 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
123 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
124 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
125 CU_ASSERT_PTR_NULL(nodes[0].prev)
126 CU_ASSERT_PTR_NULL(nodes[0].next)
128 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
129 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
130 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
131 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
132 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
134 // test with begin only / prev, next
135 memset(nodes, 0, 4 * sizeof(struct node));
136 begin = NULL;
137 end = NULL;
139 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
140 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
141 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
142 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
143 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
144 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
146 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
147 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
148 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
150 // test with end only / prev, next
151 memset(nodes, 0, 4 * sizeof(struct node));
152 begin = NULL;
153 end = NULL;
155 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]);
156 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
157 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]);
158 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
159 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
160 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
162 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]);
163 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
164 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
165 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
167 // test with begin, end / next
168 memset(nodes, 0, 4 * sizeof(struct node));
169 begin = NULL;
170 end = NULL;
172 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
173 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
174 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
175 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
176 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
177 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
178 CU_ASSERT_PTR_NULL(nodes[1].prev)
179 }
181 void test_linked_list_prepend(void) {
182 struct node {
183 void *prev;
184 void *next;
185 };
187 struct node nodes[4];
189 // test with begin, end / prev, next
190 memset(nodes, 0, 4 * sizeof(struct node));
191 void *begin = NULL;
192 void *end = NULL;
194 ptrdiff_t loc_prev = offsetof(struct node, prev);
195 ptrdiff_t loc_next = offsetof(struct node, next);
197 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
198 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
199 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
200 CU_ASSERT_PTR_NULL(nodes[0].prev)
201 CU_ASSERT_PTR_NULL(nodes[0].next)
203 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
204 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
205 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
206 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
207 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
209 // test with begin only / prev, next
210 memset(nodes, 0, 4 * sizeof(struct node));
211 begin = NULL;
212 end = NULL;
214 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]);
215 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
216 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]);
217 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
218 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
219 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
221 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]);
222 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
223 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
224 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
226 // test with end only / prev, next
227 memset(nodes, 0, 4 * sizeof(struct node));
228 begin = NULL;
229 end = NULL;
231 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]);
232 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
233 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]);
234 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
235 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
236 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
238 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]);
239 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
240 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
241 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
243 // test with begin, end / next
244 memset(nodes, 0, 4 * sizeof(struct node));
245 begin = NULL;
246 end = NULL;
248 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
249 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
250 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
251 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
252 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
253 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
254 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
255 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
256 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
257 CU_ASSERT_PTR_NULL(nodes[1].prev)
258 CU_ASSERT_PTR_NULL(nodes[0].prev)
259 }
261 void test_linked_list_insert(void) {
262 struct node {
263 void *prev;
264 void *next;
265 };
266 ptrdiff_t loc_prev = offsetof(struct node, prev);
267 ptrdiff_t loc_next = offsetof(struct node, next);
269 struct node nodes[4];
270 void *begin, *end;
272 // insert mid list
273 memset(nodes, 0, 4 * sizeof(struct node));
274 begin = &nodes[0];
275 end = &nodes[2];
277 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
278 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
280 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]);
281 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
282 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
283 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
284 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[3])
285 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
286 CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[2])
288 // insert end
289 memset(nodes, 0, 4 * sizeof(struct node));
290 begin = &nodes[0];
291 end = &nodes[2];
293 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
294 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
296 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]);
297 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
298 CU_ASSERT_PTR_EQUAL(end, &nodes[3])
299 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
300 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
301 CU_ASSERT_PTR_NULL(nodes[3].next)
303 // insert begin
304 memset(nodes, 0, 4 * sizeof(struct node));
305 begin = &nodes[0];
306 end = &nodes[2];
308 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
309 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
311 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]);
312 CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
313 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
314 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[3])
315 CU_ASSERT_PTR_NULL(nodes[3].prev)
316 CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[0])
317 }
319 void test_linked_list_insert_chain(void) {
320 struct node {
321 void *prev;
322 void *next;
323 };
324 ptrdiff_t loc_prev = offsetof(struct node, prev);
325 ptrdiff_t loc_next = offsetof(struct node, next);
327 struct node nodes[5];
328 void *begin, *end;
330 // insert mid list
331 memset(nodes, 0, 5 * sizeof(struct node));
332 begin = &nodes[0];
333 end = &nodes[2];
335 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
336 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
337 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
339 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL);
340 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
341 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
342 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
343 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[4])
344 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
345 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[2])
347 // insert end
348 memset(nodes, 0, 5 * sizeof(struct node));
349 begin = &nodes[0];
350 end = &nodes[2];
352 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
353 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
354 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
356 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL);
357 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
358 CU_ASSERT_PTR_EQUAL(end, &nodes[4])
359 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
360 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
361 CU_ASSERT_PTR_NULL(nodes[4].next)
363 // insert begin
364 memset(nodes, 0, 5 * sizeof(struct node));
365 begin = &nodes[0];
366 end = &nodes[2];
368 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
369 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
370 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
372 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL);
373 CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
374 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
375 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[4])
376 CU_ASSERT_PTR_NULL(nodes[3].prev)
377 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0])
378 }
380 void test_linked_list_first(void) {
381 struct node {
382 int data;
383 void *prev;
384 };
385 ptrdiff_t loc = offsetof(struct node, prev);
387 struct node first = {1, NULL};
388 struct node second = {2, &first};
389 struct node third = {3, &second};
391 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&first, loc), &first)
392 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&second, loc), &first)
393 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&third, loc), &first)
394 }
396 void test_linked_list_last(void) {
397 struct node {
398 int data;
399 void *next;
400 };
401 ptrdiff_t loc = offsetof(struct node, next);
403 struct node third = {3, NULL};
404 struct node second = {2, &third};
405 struct node first = {1, &second};
407 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third)
408 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third)
409 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third)
410 }
412 void test_linked_list_prev(void) {
413 struct node {
414 void *next;
415 };
416 ptrdiff_t loc = offsetof(struct node, next);
418 struct node third = {NULL};
419 struct node second = {&third};
420 struct node first = {&second};
422 CU_ASSERT_PTR_NULL(cx_linked_list_prev(&first, loc, &first))
423 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &second), &first)
424 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &third), &second)
425 }
427 void test_linked_list_remove(void) {
428 struct node {
429 void *next;
430 };
431 struct dnode {
432 void *next;
433 void *prev;
434 };
435 ptrdiff_t loc = offsetof(struct node, next);
436 ptrdiff_t ploc = offsetof(struct dnode, prev);
438 void *begin;
439 void *end;
441 // single linked list
442 struct node third = {NULL};
443 struct node second = {&third};
444 struct node first = {&second};
445 begin = &first;
447 cx_linked_list_remove(&begin, NULL, -1, loc, &second);
448 CU_ASSERT_PTR_EQUAL(begin, &first)
449 CU_ASSERT_PTR_EQUAL(first.next, &third)
450 CU_ASSERT_PTR_NULL(third.next)
452 cx_linked_list_remove(&begin, NULL, -1, loc, &first);
453 CU_ASSERT_PTR_EQUAL(begin, &third)
454 CU_ASSERT_PTR_NULL(third.next)
456 cx_linked_list_remove(&begin, NULL, -1, loc, &third);
457 CU_ASSERT_PTR_NULL(begin)
459 // doubly linked list
460 struct dnode dthird = {NULL , NULL};
461 struct dnode dsecond = {&dthird, NULL};
462 struct dnode dfirst = {&dsecond, NULL};
463 dthird.prev = &dsecond;
464 dsecond.prev = &dfirst;
465 begin = &dfirst;
466 end = &dthird;
468 cx_linked_list_remove(&begin, &end, ploc, loc, &dsecond);
469 CU_ASSERT_PTR_EQUAL(begin, &dfirst)
470 CU_ASSERT_PTR_EQUAL(end, &dthird)
471 CU_ASSERT_PTR_NULL(dfirst.prev)
472 CU_ASSERT_PTR_EQUAL(dfirst.next, &dthird)
473 CU_ASSERT_PTR_EQUAL(dthird.prev, &dfirst)
474 CU_ASSERT_PTR_NULL(dthird.next)
476 cx_linked_list_remove(&begin, &end, ploc, loc, &dthird);
477 CU_ASSERT_PTR_EQUAL(begin, &dfirst)
478 CU_ASSERT_PTR_EQUAL(end, &dfirst)
479 CU_ASSERT_PTR_NULL(dfirst.prev)
480 CU_ASSERT_PTR_NULL(dfirst.next)
482 cx_linked_list_remove(&begin, &end, ploc, loc, &dfirst);
483 CU_ASSERT_PTR_NULL(begin)
484 CU_ASSERT_PTR_NULL(end)
485 }
487 void test_linked_list_size(void) {
488 struct node {
489 void *next;
490 };
491 ptrdiff_t loc = offsetof(struct node, next);
493 struct node first = {NULL};
494 struct node second = {NULL};
495 struct node third = {NULL};
497 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0)
498 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1)
499 first.next = &second;
500 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2)
501 second.next = &third;
502 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3)
503 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2)
504 }
506 void test_linked_list_sort(void) {
507 struct node {
508 void *prev;
509 void *next;
510 int data;
511 };
513 int expected[] = {
514 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
515 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
516 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
517 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
518 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
519 4785, 4791, 4801, 4859, 4903, 4973
520 };
521 int scrambled[] = {
522 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
523 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
524 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
525 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
526 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
527 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
528 };
530 struct node *nodes = calloc(100, sizeof(struct node));
531 for (int i = 0; i < 100; i++) {
532 nodes[i].prev = i == 0 ? NULL : &nodes[i - 1];
533 nodes[i].next = i == 99 ? NULL : &nodes[i + 1];
534 nodes[i].data = scrambled[i];
535 }
537 struct node *begin = &nodes[0];
538 struct node *end = &nodes[99];
540 cx_linked_list_sort((void **) &begin, (void **) &end,
541 offsetof(struct node, prev),
542 offsetof(struct node, next),
543 offsetof(struct node, data),
544 0, (CxListComparator) cmp_int);
546 CU_ASSERT_PTR_NULL(begin->prev)
547 CU_ASSERT_EQUAL(begin->data, expected[0])
548 struct node *check = begin;
549 struct node *check_last = NULL;
550 for (int i = 0; i < 100; i++) {
551 CU_ASSERT_EQUAL(check->data, expected[i])
552 CU_ASSERT_PTR_EQUAL(check->prev, check_last)
553 if (i < 99) {
554 CU_ASSERT_PTR_NOT_NULL(check->next)
555 }
556 check_last = check;
557 check = check->next;
558 }
559 CU_ASSERT_PTR_NULL(check)
560 CU_ASSERT_EQUAL(end->data, expected[99])
561 }
563 void test_linked_list_reverse(void) {
564 struct node {
565 void *next;
566 };
567 struct dnode {
568 void *next;
569 void *prev;
570 };
571 ptrdiff_t loc = offsetof(struct node, next);
572 ptrdiff_t ploc = offsetof(struct dnode, prev);
574 void *begin;
575 void *end;
577 // single linked list
578 struct node third = {NULL};
579 struct node second = {&third};
580 struct node first = {&second};
581 begin = &first;
583 cx_linked_list_reverse(&begin, NULL, -1, loc);
584 CU_ASSERT_PTR_EQUAL(begin, &third)
585 CU_ASSERT_PTR_EQUAL(third.next, &second)
586 CU_ASSERT_PTR_EQUAL(second.next, &first)
587 CU_ASSERT_PTR_NULL(first.next)
589 // doubly linked list
590 struct dnode dthird = {NULL , NULL};
591 struct dnode dsecond = {&dthird, NULL};
592 struct dnode dfirst = {&dsecond, NULL};
593 dthird.prev = &dsecond;
594 dsecond.prev = &dfirst;
595 begin = &dfirst;
596 end = &dthird;
598 cx_linked_list_reverse(&begin, &end, ploc, loc);
599 CU_ASSERT_PTR_EQUAL(begin, &dthird)
600 CU_ASSERT_PTR_EQUAL(end, &dfirst)
601 CU_ASSERT_PTR_EQUAL(dthird.next, &dsecond)
602 CU_ASSERT_PTR_EQUAL(dsecond.next, &dfirst)
603 CU_ASSERT_PTR_NULL(dfirst.next)
604 CU_ASSERT_PTR_NULL(dthird.prev)
605 CU_ASSERT_PTR_EQUAL(dsecond.prev, &dthird)
606 CU_ASSERT_PTR_EQUAL(dfirst.prev, &dsecond)
607 }
609 void test_hl_linked_list_create(void) {
610 cxTestingAllocatorReset();
612 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
614 CU_ASSERT_EQUAL(list->size, 0)
615 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
616 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
617 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
618 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
620 cxLinkedListDestroy(list);
621 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
622 }
624 void test_hl_linked_list_add(void) {
625 cxTestingAllocatorReset();
627 int data;
628 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
630 data = 5;
631 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
632 data = 47;
633 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
634 data = 13;
635 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
637 CU_ASSERT_EQUAL(list->size, 3)
638 CU_ASSERT_TRUE(list->capacity >= list->size)
640 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
641 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
642 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
644 cxLinkedListDestroy(list);
645 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
646 }
648 void test_hl_linked_list_insert(void) {
649 cxTestingAllocatorReset();
651 int data;
652 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
654 data = 5;
655 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
656 CU_ASSERT_EQUAL(list->size, 0)
657 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
658 CU_ASSERT_EQUAL(list->size, 1)
659 data = 47;
660 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
661 CU_ASSERT_EQUAL(list->size, 2)
662 data = 13;
663 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
664 CU_ASSERT_EQUAL(list->size, 3)
665 data = 42;
666 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
668 CU_ASSERT_EQUAL(list->size, 4)
669 CU_ASSERT_TRUE(list->capacity >= list->size)
671 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
672 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
673 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
674 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
676 cxLinkedListDestroy(list);
677 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
678 }
680 void test_hl_linked_list_remove(void) {
681 cxTestingAllocatorReset();
683 int data;
684 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
686 data = 5;
687 cxListAdd(list, &data);
688 data = 47;
689 cxListAdd(list, &data);
690 data = 42;
691 cxListAdd(list, &data);
692 data = 13;
693 cxListAdd(list, &data);
695 CU_ASSERT_EQUAL(list->size, 4)
696 CU_ASSERT_TRUE(list->capacity >= list->size)
698 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
700 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
701 CU_ASSERT_EQUAL(list->size, 3)
702 CU_ASSERT_TRUE(list->capacity >= list->size)
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)
707 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
708 CU_ASSERT_EQUAL(list->size, 2)
709 CU_ASSERT_TRUE(list->capacity >= list->size)
710 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
711 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
713 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
714 CU_ASSERT_EQUAL(list->size, 1)
715 CU_ASSERT_TRUE(list->capacity >= list->size)
716 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
718 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
719 CU_ASSERT_EQUAL(list->size, 0)
720 CU_ASSERT_TRUE(list->capacity >= list->size)
722 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
724 cxLinkedListDestroy(list);
725 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
726 }
728 void test_hl_linked_list_at(void) {
729 cxTestingAllocatorReset();
731 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
733 int data;
734 data = 5;
735 cxListAdd(list, &data);
736 data = 47;
737 cxListAdd(list, &data);
738 data = 13;
739 cxListAdd(list, &data);
741 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
742 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
743 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
744 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
746 cxLinkedListDestroy(list);
747 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
748 }
750 void test_hl_linked_list_find(void) {
751 cxTestingAllocatorReset();
753 int data, criteria;
754 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
756 data = 5;
757 cxListAdd(list, &data);
758 data = 47;
759 cxListAdd(list, &data);
760 data = 13;
761 cxListAdd(list, &data);
763 CU_ASSERT_EQUAL(list->size, 3)
764 CU_ASSERT_TRUE(list->capacity >= list->size)
766 criteria = 5;
767 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
768 criteria = 47;
769 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
770 criteria = 13;
771 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
772 criteria = 9000;
773 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
774 criteria = -5;
775 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
777 cxLinkedListDestroy(list);
778 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
779 }
781 void test_hl_linked_list_sort(void) {
782 int expected[] = {
783 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
784 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
785 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
786 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
787 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
788 4785, 4791, 4801, 4859, 4903, 4973
789 };
790 int scrambled[] = {
791 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
792 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
793 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
794 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
795 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
796 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
797 };
799 cxTestingAllocatorReset();
801 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
803 for (int i = 0; i < 100; i++) {
804 cxListAdd(list, &scrambled[i]);
805 }
807 cxListSort(list);
809 for (int i = 0; i < 100; i++) {
810 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
811 }
813 cxLinkedListDestroy(list);
814 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
815 }
817 void test_hl_ptr_linked_list_create(void) {
818 cxTestingAllocatorReset();
820 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
822 CU_ASSERT_EQUAL(list->size, 0)
823 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
824 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
825 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
826 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
828 cxLinkedListDestroy(list);
829 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
830 }
832 void test_hl_ptr_linked_list_add(void) {
833 cxTestingAllocatorReset();
835 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
837 int a = 5, b = 47, c = 13;
839 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
840 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
841 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
843 CU_ASSERT_EQUAL(list->size, 3)
844 CU_ASSERT_TRUE(list->capacity >= list->size)
846 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
847 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
848 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
850 a = 9;
851 b = 10;
852 c = 11;
854 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
855 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
856 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
858 cxLinkedListDestroy(list);
859 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
860 }
862 void test_hl_ptr_linked_list_insert(void) {
863 cxTestingAllocatorReset();
865 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
867 int a = 5, b = 47, c = 13, d = 42;
869 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
870 CU_ASSERT_EQUAL(list->size, 0)
871 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
872 CU_ASSERT_EQUAL(list->size, 1)
873 CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
874 CU_ASSERT_EQUAL(list->size, 2)
875 CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
876 CU_ASSERT_EQUAL(list->size, 3)
877 CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
879 CU_ASSERT_EQUAL(list->size, 4)
880 CU_ASSERT_TRUE(list->capacity >= list->size)
882 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
883 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
884 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
885 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
887 cxLinkedListDestroy(list);
888 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
889 }
891 void test_hl_ptr_linked_list_remove(void) {
892 cxTestingAllocatorReset();
894 int a = 5, b = 47, c = 42, d = 13;
895 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
897 cxListAdd(list, &a);
898 cxListAdd(list, &b);
899 cxListAdd(list, &c);
900 cxListAdd(list, &d);
902 CU_ASSERT_EQUAL(list->size, 4)
903 CU_ASSERT_TRUE(list->capacity >= list->size)
905 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
907 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
908 CU_ASSERT_EQUAL(list->size, 3)
909 CU_ASSERT_TRUE(list->capacity >= list->size)
910 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
911 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
912 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
914 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
915 CU_ASSERT_EQUAL(list->size, 2)
916 CU_ASSERT_TRUE(list->capacity >= list->size)
917 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
918 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
920 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
921 CU_ASSERT_EQUAL(list->size, 1)
922 CU_ASSERT_TRUE(list->capacity >= list->size)
923 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
925 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
926 CU_ASSERT_EQUAL(list->size, 0)
927 CU_ASSERT_TRUE(list->capacity >= list->size)
929 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
931 cxLinkedListDestroy(list);
932 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
933 }
935 void test_hl_ptr_linked_list_at(void) {
936 cxTestingAllocatorReset();
938 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
940 int a = 5, b = 47, c = 13;
941 cxListAdd(list, &a);
942 cxListAdd(list, &b);
943 cxListAdd(list, &c);
945 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
946 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
947 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
948 CU_ASSERT_PTR_NULL(cxListAt(list, 3))
950 cxLinkedListDestroy(list);
951 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
952 }
954 void test_hl_ptr_linked_list_find(void) {
955 cxTestingAllocatorReset();
957 int a = 5, b = 47, c = 13, criteria;
958 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
960 cxListAdd(list, &a);
961 cxListAdd(list, &b);
962 cxListAdd(list, &c);
964 CU_ASSERT_EQUAL(list->size, 3)
965 CU_ASSERT_TRUE(list->capacity >= list->size)
967 criteria = 5;
968 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
969 criteria = 47;
970 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
971 criteria = 13;
972 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
973 criteria = 9000;
974 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
975 criteria = -5;
976 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
977 b = -5;
978 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
980 cxLinkedListDestroy(list);
981 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
982 }
984 void test_hl_ptr_linked_list_sort(void) {
985 int expected[] = {
986 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
987 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
988 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
989 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
990 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
991 4785, 4791, 4801, 4859, 4903, 4973
992 };
993 int scrambled[] = {
994 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
995 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
996 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
997 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
998 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
999 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
1000 };
1002 cxTestingAllocatorReset();
1004 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
1006 for (int i = 0; i < 100; i++) {
1007 cxListAdd(list, &scrambled[i]);
1008 }
1010 cxListSort(list);
1012 for (int i = 0; i < 100; i++) {
1013 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
1014 }
1016 cxLinkedListDestroy(list);
1017 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
1018 }
1020 int main() {
1021 CU_pSuite suite = NULL;
1023 if (CUE_SUCCESS != CU_initialize_registry()) {
1024 return CU_get_error();
1025 }
1027 suite = CU_add_suite("low level linked list", NULL, NULL);
1029 cu_add_test(suite, test_linked_list_link_unlink);
1030 cu_add_test(suite, test_linked_list_at);
1031 cu_add_test(suite, test_linked_list_prepend);
1032 cu_add_test(suite, test_linked_list_add);
1033 cu_add_test(suite, test_linked_list_insert);
1034 cu_add_test(suite, test_linked_list_insert_chain);
1035 cu_add_test(suite, test_linked_list_first);
1036 cu_add_test(suite, test_linked_list_last);
1037 cu_add_test(suite, test_linked_list_prev);
1038 cu_add_test(suite, test_linked_list_remove);
1039 cu_add_test(suite, test_linked_list_size);
1040 cu_add_test(suite, test_linked_list_sort);
1041 cu_add_test(suite, test_linked_list_reverse);
1043 suite = CU_add_suite("high level linked list", NULL, NULL);
1045 cu_add_test(suite, test_hl_linked_list_create);
1046 cu_add_test(suite, test_hl_linked_list_add);
1047 cu_add_test(suite, test_hl_linked_list_insert);
1048 cu_add_test(suite, test_hl_linked_list_remove);
1049 cu_add_test(suite, test_hl_linked_list_at);
1050 cu_add_test(suite, test_hl_linked_list_find);
1051 cu_add_test(suite, test_hl_linked_list_sort);
1053 suite = CU_add_suite("high level pointer linked list", NULL, NULL);
1055 cu_add_test(suite, test_hl_ptr_linked_list_create);
1056 cu_add_test(suite, test_hl_ptr_linked_list_add);
1057 cu_add_test(suite, test_hl_ptr_linked_list_insert);
1058 cu_add_test(suite, test_hl_ptr_linked_list_remove);
1059 cu_add_test(suite, test_hl_ptr_linked_list_at);
1060 cu_add_test(suite, test_hl_ptr_linked_list_find);
1061 cu_add_test(suite, test_hl_ptr_linked_list_sort);
1063 CU_basic_set_mode(UCX_CU_BRM);
1065 int exitcode;
1066 if (CU_basic_run_tests()) {
1067 exitcode = CU_get_error();
1068 } else {
1069 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
1070 }
1071 CU_cleanup_registry();
1072 return exitcode;
1073 }