test/test_list.c

Mon, 20 Dec 2021 11:58:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Dec 2021 11:58:36 +0100
changeset 478
599770bb6314
parent 476
60ff4561dc04
child 479
a29bdd703e02
permissions
-rw-r--r--

add more nonnull attributes

This also changes the contract for last/first in the sense that these
functions now also require a valid pointer.

universe@390 1 /*
universe@390 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@390 3 *
universe@390 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@390 5 *
universe@390 6 * Redistribution and use in source and binary forms, with or without
universe@390 7 * modification, are permitted provided that the following conditions are met:
universe@390 8 *
universe@390 9 * 1. Redistributions of source code must retain the above copyright
universe@390 10 * notice, this list of conditions and the following disclaimer.
universe@390 11 *
universe@390 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@390 13 * notice, this list of conditions and the following disclaimer in the
universe@390 14 * documentation and/or other materials provided with the distribution.
universe@390 15 *
universe@390 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@390 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@390 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@390 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@390 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@390 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@390 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@390 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@390 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@390 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@390 26 * POSSIBILITY OF SUCH DAMAGE.
universe@390 27 */
universe@390 28
universe@398 29 #include "cx/linked_list.h"
universe@411 30 #include "test_config.h"
universe@422 31 #include "util_allocator.h"
universe@398 32
universe@412 33 int cmp_int(int const *l, int const *r) {
universe@412 34 int left = *l, right = *r;
universe@412 35 return left == right ? 0 : (left < right ? -1 : 1);
universe@412 36 }
universe@412 37
universe@438 38 void test_linked_list_at(void) {
universe@438 39 struct node {
universe@438 40 void *next;
universe@438 41 void *prev;
universe@438 42 };
universe@438 43 const ptrdiff_t loc_prev = offsetof(struct node, prev);
universe@438 44 const ptrdiff_t loc_next = offsetof(struct node, next);
universe@438 45
universe@438 46 struct node a, b, c, d;
universe@438 47 a.prev = NULL;
universe@438 48 a.next = &b;
universe@438 49 b.prev = &a;
universe@438 50 b.next = &c;
universe@438 51 c.prev = &b;
universe@438 52 c.next = &d;
universe@438 53 d.prev = &c;
universe@438 54 d.next = NULL;
universe@438 55
universe@449 56 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
universe@449 57 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
universe@449 58 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
universe@449 59 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
universe@449 60 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
universe@438 61
universe@449 62 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
universe@449 63 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
universe@449 64 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
universe@449 65 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
universe@449 66 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
universe@438 67
universe@449 68 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
universe@449 69 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
universe@438 70 }
universe@438 71
olaf@444 72 void test_linked_list_add(void) {
olaf@442 73 struct node {
olaf@442 74 void *prev;
olaf@442 75 void *next;
olaf@442 76 };
universe@449 77
olaf@442 78 struct node nodes[4];
universe@449 79
olaf@442 80 // test with begin, end / prev, next
universe@449 81 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 82 void *begin = NULL;
olaf@442 83 void *end = NULL;
universe@449 84
olaf@442 85 ptrdiff_t loc_prev = offsetof(struct node, prev);
olaf@442 86 ptrdiff_t loc_next = offsetof(struct node, next);
universe@449 87
universe@453 88 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
universe@449 89 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 90 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 91 CU_ASSERT_PTR_NULL(nodes[0].prev)
universe@475 92 CU_ASSERT_PTR_NULL(nodes[0].next)
universe@449 93
universe@453 94 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
universe@449 95 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 96 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
universe@449 97 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 98 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
universe@449 99
olaf@442 100 // test with begin only / prev, next
universe@449 101 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 102 begin = NULL;
olaf@442 103 end = NULL;
universe@449 104
universe@453 105 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
universe@449 106 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@453 107 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
universe@449 108 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 109 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 110 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
universe@449 111
universe@453 112 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
universe@449 113 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
universe@449 114 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
universe@449 115
universe@475 116 // test with end only / prev, next
universe@475 117 memset(nodes, 0, 4 * sizeof(struct node));
universe@475 118 begin = NULL;
universe@475 119 end = NULL;
universe@475 120
universe@475 121 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]);
universe@475 122 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 123 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]);
universe@475 124 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
universe@475 125 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@475 126 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
universe@475 127
universe@475 128 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]);
universe@475 129 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
universe@475 130 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
universe@475 131 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
universe@475 132
olaf@442 133 // test with begin, end / next
universe@449 134 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 135 begin = NULL;
olaf@442 136 end = NULL;
universe@449 137
universe@453 138 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
universe@449 139 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 140 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@453 141 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
universe@449 142 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
universe@449 143 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 144 CU_ASSERT_PTR_NULL(nodes[1].prev)
olaf@442 145 }
olaf@442 146
universe@475 147 void test_linked_list_prepend(void) {
universe@475 148 struct node {
universe@475 149 void *prev;
universe@475 150 void *next;
universe@475 151 };
universe@475 152
universe@475 153 struct node nodes[4];
universe@475 154
universe@475 155 // test with begin, end / prev, next
universe@475 156 memset(nodes, 0, 4 * sizeof(struct node));
universe@475 157 void *begin = NULL;
universe@475 158 void *end = NULL;
universe@475 159
universe@475 160 ptrdiff_t loc_prev = offsetof(struct node, prev);
universe@475 161 ptrdiff_t loc_next = offsetof(struct node, next);
universe@475 162
universe@475 163 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
universe@475 164 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@475 165 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 166 CU_ASSERT_PTR_NULL(nodes[0].prev)
universe@475 167 CU_ASSERT_PTR_NULL(nodes[0].next)
universe@475 168
universe@475 169 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
universe@475 170 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
universe@475 171 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 172 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
universe@475 173 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
universe@475 174
universe@475 175 // test with begin only / prev, next
universe@475 176 memset(nodes, 0, 4 * sizeof(struct node));
universe@475 177 begin = NULL;
universe@475 178 end = NULL;
universe@475 179
universe@475 180 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]);
universe@475 181 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@475 182 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]);
universe@475 183 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
universe@475 184 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
universe@475 185 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
universe@475 186
universe@475 187 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]);
universe@475 188 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
universe@475 189 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
universe@475 190 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
universe@475 191
universe@475 192 // test with end only / prev, next
universe@475 193 memset(nodes, 0, 4 * sizeof(struct node));
universe@475 194 begin = NULL;
universe@475 195 end = NULL;
universe@475 196
universe@475 197 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]);
universe@475 198 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 199 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]);
universe@475 200 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 201 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
universe@475 202 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
universe@475 203
universe@475 204 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]);
universe@475 205 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 206 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
universe@475 207 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
universe@475 208
universe@475 209 // test with begin, end / next
universe@475 210 memset(nodes, 0, 4 * sizeof(struct node));
universe@475 211 begin = NULL;
universe@475 212 end = NULL;
universe@475 213
universe@475 214 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
universe@475 215 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@475 216 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 217 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
universe@475 218 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
universe@475 219 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
universe@475 220 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@475 221 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
universe@475 222 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
universe@475 223 CU_ASSERT_PTR_NULL(nodes[1].prev)
universe@475 224 CU_ASSERT_PTR_NULL(nodes[0].prev)
universe@475 225 }
universe@475 226
universe@475 227 void test_linked_list_first(void) {
universe@475 228 struct node {
universe@475 229 int data;
universe@475 230 void *prev;
universe@475 231 };
universe@475 232 ptrdiff_t loc = offsetof(struct node, prev);
universe@475 233
universe@475 234 struct node first = {1, NULL};
universe@475 235 struct node second = {2, &first};
universe@475 236 struct node third = {3, &second};
universe@475 237
universe@475 238 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&first, loc), &first)
universe@475 239 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&second, loc), &first)
universe@475 240 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&third, loc), &first)
universe@475 241 }
universe@475 242
universe@456 243 void test_linked_list_last(void) {
universe@456 244 struct node {
universe@456 245 int data;
universe@456 246 void *next;
universe@456 247 };
universe@456 248 ptrdiff_t loc = offsetof(struct node, next);
universe@456 249
universe@456 250 struct node third = {3, NULL};
universe@456 251 struct node second = {2, &third};
universe@456 252 struct node first = {1, &second};
universe@456 253
universe@456 254 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third)
universe@456 255 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third)
universe@456 256 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third)
universe@456 257 }
universe@456 258
universe@473 259 void test_linked_list_prev(void) {
universe@473 260 struct node {
universe@473 261 void *next;
universe@473 262 };
universe@473 263 ptrdiff_t loc = offsetof(struct node, next);
universe@473 264
universe@473 265 struct node third = {NULL};
universe@473 266 struct node second = {&third};
universe@473 267 struct node first = {&second};
universe@473 268
universe@473 269 CU_ASSERT_PTR_NULL(cx_linked_list_prev(&first, loc, &first))
universe@473 270 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &second), &first)
universe@473 271 CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &third), &second)
universe@473 272 }
universe@473 273
universe@473 274 void test_linked_list_remove(void) {
universe@473 275 struct node {
universe@473 276 void *next;
universe@473 277 };
universe@473 278 struct dnode {
universe@473 279 void *next;
universe@473 280 void *prev;
universe@473 281 };
universe@473 282 ptrdiff_t loc = offsetof(struct node, next);
universe@473 283 ptrdiff_t ploc = offsetof(struct dnode, prev);
universe@473 284
universe@473 285 void *begin;
universe@473 286 void *end;
universe@473 287
universe@473 288 // single linked list
universe@473 289 struct node third = {NULL};
universe@473 290 struct node second = {&third};
universe@473 291 struct node first = {&second};
universe@473 292 begin = &first;
universe@473 293
universe@476 294 cx_linked_list_remove(&begin, NULL, -1, loc, &second);
universe@473 295 CU_ASSERT_PTR_EQUAL(begin, &first)
universe@473 296 CU_ASSERT_PTR_EQUAL(first.next, &third)
universe@473 297 CU_ASSERT_PTR_NULL(third.next)
universe@473 298
universe@476 299 cx_linked_list_remove(&begin, NULL, -1, loc, &first);
universe@473 300 CU_ASSERT_PTR_EQUAL(begin, &third)
universe@473 301 CU_ASSERT_PTR_NULL(third.next)
universe@473 302
universe@476 303 cx_linked_list_remove(&begin, NULL, -1, loc, &third);
universe@473 304 CU_ASSERT_PTR_NULL(begin)
universe@473 305
universe@473 306 // doubly linked list
universe@473 307 struct dnode dthird = {NULL , NULL};
universe@473 308 struct dnode dsecond = {&dthird, NULL};
universe@473 309 struct dnode dfirst = {&dsecond, NULL};
universe@473 310 dthird.prev = &dsecond;
universe@473 311 dsecond.prev = &dfirst;
universe@473 312 begin = &dfirst;
universe@473 313 end = &dthird;
universe@473 314
universe@476 315 cx_linked_list_remove(&begin, &end, ploc, loc, &dsecond);
universe@473 316 CU_ASSERT_PTR_EQUAL(begin, &dfirst)
universe@473 317 CU_ASSERT_PTR_EQUAL(end, &dthird)
universe@473 318 CU_ASSERT_PTR_NULL(dfirst.prev)
universe@473 319 CU_ASSERT_PTR_EQUAL(dfirst.next, &dthird)
universe@473 320 CU_ASSERT_PTR_EQUAL(dthird.prev, &dfirst)
universe@473 321 CU_ASSERT_PTR_NULL(dthird.next)
universe@473 322
universe@476 323 cx_linked_list_remove(&begin, &end, ploc, loc, &dthird);
universe@473 324 CU_ASSERT_PTR_EQUAL(begin, &dfirst)
universe@473 325 CU_ASSERT_PTR_EQUAL(end, &dfirst)
universe@473 326 CU_ASSERT_PTR_NULL(dfirst.prev)
universe@473 327 CU_ASSERT_PTR_NULL(dfirst.next)
universe@473 328
universe@476 329 cx_linked_list_remove(&begin, &end, ploc, loc, &dfirst);
universe@473 330 CU_ASSERT_PTR_NULL(begin)
universe@473 331 CU_ASSERT_PTR_NULL(end)
universe@473 332 }
universe@473 333
universe@468 334 void test_linked_list_size(void) {
universe@468 335 struct node {
universe@468 336 void *next;
universe@468 337 };
universe@468 338 ptrdiff_t loc = offsetof(struct node, next);
universe@468 339
universe@468 340 struct node first = {NULL};
universe@468 341 struct node second = {NULL};
universe@468 342 struct node third = {NULL};
universe@468 343
universe@468 344 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0)
universe@468 345 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1)
universe@468 346 first.next = &second;
universe@468 347 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2)
universe@468 348 second.next = &third;
universe@468 349 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3)
universe@468 350 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2)
universe@468 351 }
universe@468 352
universe@468 353 void test_linked_list_sort(void) {
universe@468 354 struct node {
universe@468 355 void *prev;
universe@468 356 void *next;
universe@468 357 int data;
universe@468 358 };
universe@468 359
universe@468 360 int expected[] = {
universe@468 361 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
universe@468 362 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
universe@468 363 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
universe@468 364 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
universe@468 365 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
universe@468 366 4785, 4791, 4801, 4859, 4903, 4973
universe@468 367 };
universe@468 368 int scrambled[] = {
universe@468 369 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
universe@468 370 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
universe@468 371 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
universe@468 372 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
universe@468 373 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
universe@468 374 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
universe@468 375 };
universe@468 376
universe@468 377 struct node *nodes = calloc(100, sizeof(struct node));
universe@468 378 for (int i = 0; i < 100; i++) {
universe@468 379 nodes[i].prev = i == 0 ? NULL : &nodes[i - 1];
universe@468 380 nodes[i].next = i == 99 ? NULL : &nodes[i + 1];
universe@468 381 nodes[i].data = scrambled[i];
universe@468 382 }
universe@468 383
universe@468 384 struct node *begin = &nodes[0];
universe@468 385 struct node *end = &nodes[99];
universe@468 386
universe@468 387 cx_linked_list_sort((void **) &begin, (void **) &end,
universe@468 388 offsetof(struct node, prev),
universe@468 389 offsetof(struct node, next),
universe@468 390 offsetof(struct node, data),
universe@468 391 0, (CxListComparator) cmp_int);
universe@468 392
universe@468 393 CU_ASSERT_PTR_NULL(begin->prev)
universe@468 394 CU_ASSERT_EQUAL(begin->data, expected[0])
universe@468 395 struct node *check = begin;
universe@468 396 struct node *check_last = NULL;
universe@473 397 for (int i = 0; i < 100; i++) {
universe@468 398 CU_ASSERT_EQUAL(check->data, expected[i])
universe@468 399 CU_ASSERT_PTR_EQUAL(check->prev, check_last)
universe@468 400 if (i < 99) {
universe@468 401 CU_ASSERT_PTR_NOT_NULL(check->next)
universe@468 402 }
universe@468 403 check_last = check;
universe@468 404 check = check->next;
universe@468 405 }
universe@468 406 CU_ASSERT_PTR_NULL(check)
universe@468 407 CU_ASSERT_EQUAL(end->data, expected[99])
universe@468 408 }
universe@468 409
universe@473 410 void test_linked_list_reverse(void) {
universe@473 411 struct node {
universe@473 412 void *next;
universe@473 413 };
universe@473 414 struct dnode {
universe@473 415 void *next;
universe@473 416 void *prev;
universe@473 417 };
universe@473 418 ptrdiff_t loc = offsetof(struct node, next);
universe@473 419 ptrdiff_t ploc = offsetof(struct dnode, prev);
universe@473 420
universe@473 421 void *begin;
universe@473 422 void *end;
universe@473 423
universe@473 424 // single linked list
universe@473 425 struct node third = {NULL};
universe@473 426 struct node second = {&third};
universe@473 427 struct node first = {&second};
universe@473 428 begin = &first;
universe@473 429
universe@473 430 cx_linked_list_reverse(&begin, NULL, -1, loc);
universe@473 431 CU_ASSERT_PTR_EQUAL(begin, &third)
universe@473 432 CU_ASSERT_PTR_EQUAL(third.next, &second)
universe@473 433 CU_ASSERT_PTR_EQUAL(second.next, &first)
universe@473 434 CU_ASSERT_PTR_NULL(first.next)
universe@473 435
universe@473 436 // doubly linked list
universe@473 437 struct dnode dthird = {NULL , NULL};
universe@473 438 struct dnode dsecond = {&dthird, NULL};
universe@473 439 struct dnode dfirst = {&dsecond, NULL};
universe@473 440 dthird.prev = &dsecond;
universe@473 441 dsecond.prev = &dfirst;
universe@473 442 begin = &dfirst;
universe@473 443 end = &dthird;
universe@473 444
universe@473 445 cx_linked_list_reverse(&begin, &end, ploc, loc);
universe@473 446 CU_ASSERT_PTR_EQUAL(begin, &dthird)
universe@473 447 CU_ASSERT_PTR_EQUAL(end, &dfirst)
universe@473 448 CU_ASSERT_PTR_EQUAL(dthird.next, &dsecond)
universe@473 449 CU_ASSERT_PTR_EQUAL(dsecond.next, &dfirst)
universe@473 450 CU_ASSERT_PTR_NULL(dfirst.next)
universe@473 451 CU_ASSERT_PTR_NULL(dthird.prev)
universe@473 452 CU_ASSERT_PTR_EQUAL(dsecond.prev, &dthird)
universe@473 453 CU_ASSERT_PTR_EQUAL(dfirst.prev, &dsecond)
universe@473 454 }
universe@456 455
universe@456 456 void test_hl_linked_list_create(void) {
universe@455 457 cxTestingAllocatorReset();
universe@455 458
universe@455 459 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@455 460
universe@455 461 CU_ASSERT_EQUAL(list->size, 0)
universe@455 462 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
universe@455 463 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
universe@455 464 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
universe@455 465 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
universe@455 466
universe@455 467 cxLinkedListDestroy(list);
universe@455 468 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@455 469 }
universe@455 470
universe@459 471 void test_hl_linked_list_add(void) {
universe@459 472 cxTestingAllocatorReset();
universe@459 473
universe@459 474 int data;
universe@459 475 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 476
universe@459 477 data = 5;
universe@460 478 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 479 data = 47;
universe@460 480 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 481 data = 13;
universe@460 482 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 483
universe@459 484 CU_ASSERT_EQUAL(list->size, 3)
universe@459 485 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 486
universe@466 487 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 488 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 489 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@459 490
universe@459 491 cxLinkedListDestroy(list);
universe@459 492 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 493 }
universe@459 494
universe@459 495 void test_hl_linked_list_insert(void) {
universe@459 496 cxTestingAllocatorReset();
universe@459 497
universe@459 498 int data;
universe@459 499 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 500
universe@459 501 data = 5;
universe@460 502 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
universe@459 503 CU_ASSERT_EQUAL(list->size, 0)
universe@460 504 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
universe@459 505 CU_ASSERT_EQUAL(list->size, 1)
universe@459 506 data = 47;
universe@460 507 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
universe@459 508 CU_ASSERT_EQUAL(list->size, 2)
universe@459 509 data = 13;
universe@460 510 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
universe@459 511 CU_ASSERT_EQUAL(list->size, 3)
universe@459 512 data = 42;
universe@460 513 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
universe@459 514
universe@459 515 CU_ASSERT_EQUAL(list->size, 4)
universe@459 516 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 517
universe@466 518 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 519 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@466 520 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
universe@466 521 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
universe@459 522
universe@459 523 cxLinkedListDestroy(list);
universe@459 524 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 525 }
universe@459 526
universe@459 527 void test_hl_linked_list_remove(void) {
universe@459 528 cxTestingAllocatorReset();
universe@459 529
universe@459 530 int data;
universe@459 531 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 532
universe@459 533 data = 5;
universe@460 534 cxListAdd(list, &data);
universe@459 535 data = 47;
universe@460 536 cxListAdd(list, &data);
universe@459 537 data = 42;
universe@460 538 cxListAdd(list, &data);
universe@459 539 data = 13;
universe@460 540 cxListAdd(list, &data);
universe@459 541
universe@459 542 CU_ASSERT_EQUAL(list->size, 4)
universe@459 543 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 544
universe@459 545 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
universe@459 546
universe@459 547 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
universe@459 548 CU_ASSERT_EQUAL(list->size, 3)
universe@459 549 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 550 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 551 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 552 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@459 553
universe@459 554 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@459 555 CU_ASSERT_EQUAL(list->size, 2)
universe@459 556 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 557 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 558 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@459 559
universe@459 560 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
universe@459 561 CU_ASSERT_EQUAL(list->size, 1)
universe@459 562 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 563 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@459 564
universe@459 565 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@459 566 CU_ASSERT_EQUAL(list->size, 0)
universe@459 567 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 568
universe@459 569 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
universe@459 570
universe@459 571 cxLinkedListDestroy(list);
universe@459 572 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 573 }
universe@459 574
universe@459 575 void test_hl_linked_list_find(void) {
universe@459 576 cxTestingAllocatorReset();
universe@459 577
universe@459 578 int data, criteria;
universe@459 579 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 580
universe@459 581 data = 5;
universe@460 582 cxListAdd(list, &data);
universe@459 583 data = 47;
universe@460 584 cxListAdd(list, &data);
universe@459 585 data = 13;
universe@460 586 cxListAdd(list, &data);
universe@459 587
universe@459 588 CU_ASSERT_EQUAL(list->size, 3)
universe@459 589 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 590
universe@459 591 criteria = 5;
universe@460 592 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
universe@459 593 criteria = 47;
universe@460 594 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@459 595 criteria = 13;
universe@460 596 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
universe@459 597 criteria = 9000;
universe@460 598 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@459 599 criteria = -5;
universe@460 600 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@459 601
universe@459 602 cxLinkedListDestroy(list);
universe@459 603 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 604 }
universe@459 605
universe@469 606 void test_hl_linked_list_sort(void) {
universe@469 607 int expected[] = {
universe@469 608 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
universe@469 609 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
universe@469 610 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
universe@469 611 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
universe@469 612 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
universe@469 613 4785, 4791, 4801, 4859, 4903, 4973
universe@469 614 };
universe@469 615 int scrambled[] = {
universe@469 616 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
universe@469 617 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
universe@469 618 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
universe@469 619 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
universe@469 620 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
universe@469 621 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
universe@469 622 };
universe@469 623
universe@469 624 cxTestingAllocatorReset();
universe@469 625
universe@469 626 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@469 627
universe@473 628 for (int i = 0; i < 100; i++) {
universe@469 629 cxListAdd(list, &scrambled[i]);
universe@469 630 }
universe@469 631
universe@469 632 cxListSort(list);
universe@469 633
universe@473 634 for (int i = 0; i < 100; i++) {
universe@473 635 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
universe@469 636 }
universe@469 637
universe@469 638 cxLinkedListDestroy(list);
universe@469 639 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@469 640 }
universe@469 641
universe@466 642 void test_hl_ptr_linked_list_create(void) {
universe@466 643 cxTestingAllocatorReset();
universe@466 644
universe@466 645 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 646
universe@466 647 CU_ASSERT_EQUAL(list->size, 0)
universe@466 648 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
universe@466 649 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
universe@466 650 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
universe@466 651 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
universe@466 652
universe@466 653 cxLinkedListDestroy(list);
universe@466 654 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 655 }
universe@466 656
universe@466 657 void test_hl_ptr_linked_list_add(void) {
universe@466 658 cxTestingAllocatorReset();
universe@466 659
universe@466 660 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 661
universe@466 662 int a = 5, b = 47, c = 13;
universe@466 663
universe@466 664 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
universe@466 665 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
universe@466 666 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
universe@466 667
universe@466 668 CU_ASSERT_EQUAL(list->size, 3)
universe@466 669 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 670
universe@466 671 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 672 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 673 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@466 674
universe@466 675 a = 9;
universe@466 676 b = 10;
universe@466 677 c = 11;
universe@466 678
universe@466 679 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
universe@466 680 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
universe@466 681 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
universe@466 682
universe@466 683 cxLinkedListDestroy(list);
universe@466 684 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 685 }
universe@466 686
universe@466 687 void test_hl_ptr_linked_list_insert(void) {
universe@466 688 cxTestingAllocatorReset();
universe@466 689
universe@466 690 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 691
universe@466 692 int a = 5, b = 47, c = 13, d = 42;
universe@466 693
universe@466 694 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
universe@466 695 CU_ASSERT_EQUAL(list->size, 0)
universe@466 696 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
universe@466 697 CU_ASSERT_EQUAL(list->size, 1)
universe@466 698 CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
universe@466 699 CU_ASSERT_EQUAL(list->size, 2)
universe@466 700 CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
universe@466 701 CU_ASSERT_EQUAL(list->size, 3)
universe@466 702 CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
universe@466 703
universe@466 704 CU_ASSERT_EQUAL(list->size, 4)
universe@466 705 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 706
universe@466 707 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 708 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@466 709 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
universe@466 710 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
universe@466 711
universe@466 712 cxLinkedListDestroy(list);
universe@466 713 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 714 }
universe@466 715
universe@466 716 void test_hl_ptr_linked_list_remove(void) {
universe@466 717 cxTestingAllocatorReset();
universe@466 718
universe@466 719 int a = 5, b = 47, c = 42, d = 13;
universe@466 720 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 721
universe@466 722 cxListAdd(list, &a);
universe@466 723 cxListAdd(list, &b);
universe@466 724 cxListAdd(list, &c);
universe@466 725 cxListAdd(list, &d);
universe@466 726
universe@466 727 CU_ASSERT_EQUAL(list->size, 4)
universe@466 728 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 729
universe@466 730 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
universe@466 731
universe@466 732 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
universe@466 733 CU_ASSERT_EQUAL(list->size, 3)
universe@466 734 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 735 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 736 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 737 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@466 738
universe@466 739 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@466 740 CU_ASSERT_EQUAL(list->size, 2)
universe@466 741 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 742 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 743 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@466 744
universe@466 745 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
universe@466 746 CU_ASSERT_EQUAL(list->size, 1)
universe@466 747 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 748 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 749
universe@466 750 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@466 751 CU_ASSERT_EQUAL(list->size, 0)
universe@466 752 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 753
universe@466 754 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
universe@466 755
universe@466 756 cxLinkedListDestroy(list);
universe@466 757 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 758 }
universe@466 759
universe@466 760 void test_hl_ptr_linked_list_find(void) {
universe@466 761 cxTestingAllocatorReset();
universe@466 762
universe@466 763 int a = 5, b = 47, c = 13, criteria;
universe@466 764 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 765
universe@466 766 cxListAdd(list, &a);
universe@466 767 cxListAdd(list, &b);
universe@466 768 cxListAdd(list, &c);
universe@466 769
universe@466 770 CU_ASSERT_EQUAL(list->size, 3)
universe@466 771 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 772
universe@466 773 criteria = 5;
universe@466 774 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
universe@466 775 criteria = 47;
universe@466 776 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@466 777 criteria = 13;
universe@466 778 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
universe@466 779 criteria = 9000;
universe@466 780 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@466 781 criteria = -5;
universe@466 782 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@466 783 b = -5;
universe@466 784 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@466 785
universe@466 786 cxLinkedListDestroy(list);
universe@466 787 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 788 }
universe@466 789
universe@469 790 void test_hl_ptr_linked_list_sort(void) {
universe@469 791 int expected[] = {
universe@469 792 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
universe@469 793 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
universe@469 794 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
universe@469 795 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
universe@469 796 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
universe@469 797 4785, 4791, 4801, 4859, 4903, 4973
universe@469 798 };
universe@469 799 int scrambled[] = {
universe@469 800 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
universe@469 801 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
universe@469 802 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
universe@469 803 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
universe@469 804 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
universe@469 805 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
universe@469 806 };
universe@469 807
universe@469 808 cxTestingAllocatorReset();
universe@469 809
universe@469 810 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@469 811
universe@473 812 for (int i = 0; i < 100; i++) {
universe@469 813 cxListAdd(list, &scrambled[i]);
universe@469 814 }
universe@469 815
universe@469 816 cxListSort(list);
universe@469 817
universe@473 818 for (int i = 0; i < 100; i++) {
universe@473 819 CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i])
universe@469 820 }
universe@469 821
universe@469 822 cxLinkedListDestroy(list);
universe@469 823 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@469 824 }
universe@469 825
universe@390 826 int main() {
universe@411 827 CU_pSuite suite = NULL;
universe@449 828
universe@411 829 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 830 return CU_get_error();
universe@411 831 }
universe@411 832
universe@459 833 suite = CU_add_suite("low level linked list", NULL, NULL);
universe@449 834
universe@455 835 cu_add_test(suite, test_linked_list_at);
universe@475 836 cu_add_test(suite, test_linked_list_prepend);
universe@455 837 cu_add_test(suite, test_linked_list_add);
universe@475 838 cu_add_test(suite, test_linked_list_first);
universe@456 839 cu_add_test(suite, test_linked_list_last);
universe@473 840 cu_add_test(suite, test_linked_list_prev);
universe@473 841 cu_add_test(suite, test_linked_list_remove);
universe@468 842 cu_add_test(suite, test_linked_list_size);
universe@468 843 cu_add_test(suite, test_linked_list_sort);
universe@473 844 cu_add_test(suite, test_linked_list_reverse);
universe@455 845
universe@459 846 suite = CU_add_suite("high level linked list", NULL, NULL);
universe@455 847
universe@456 848 cu_add_test(suite, test_hl_linked_list_create);
universe@456 849 cu_add_test(suite, test_hl_linked_list_add);
universe@456 850 cu_add_test(suite, test_hl_linked_list_insert);
universe@456 851 cu_add_test(suite, test_hl_linked_list_remove);
universe@459 852 cu_add_test(suite, test_hl_linked_list_find);
universe@469 853 cu_add_test(suite, test_hl_linked_list_sort);
universe@413 854
universe@466 855 suite = CU_add_suite("high level pointer linked list", NULL, NULL);
universe@466 856
universe@466 857 cu_add_test(suite, test_hl_ptr_linked_list_create);
universe@466 858 cu_add_test(suite, test_hl_ptr_linked_list_add);
universe@466 859 cu_add_test(suite, test_hl_ptr_linked_list_insert);
universe@466 860 cu_add_test(suite, test_hl_ptr_linked_list_remove);
universe@466 861 cu_add_test(suite, test_hl_ptr_linked_list_find);
universe@469 862 cu_add_test(suite, test_hl_ptr_linked_list_sort);
universe@466 863
universe@411 864 CU_basic_set_mode(UCX_CU_BRM);
universe@411 865
universe@411 866 int exitcode;
universe@411 867 if (CU_basic_run_tests()) {
universe@411 868 exitcode = CU_get_error();
universe@411 869 } else {
universe@411 870 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 871 }
universe@411 872 CU_cleanup_registry();
universe@411 873 return exitcode;
universe@390 874 }

mercurial