test/test_list.c

Tue, 05 Oct 2021 16:33:11 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Oct 2021 16:33:11 +0200
changeset 468
75ae1dccd101
parent 466
28bc3e10ac28
child 469
0458bff0b1cd
permissions
-rw-r--r--

add cx_linked_list_sort()

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@449 91 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL)
universe@449 92 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL)
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
olaf@442 116 // test with begin, end / next
universe@449 117 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 118 begin = NULL;
olaf@442 119 end = NULL;
universe@449 120
universe@453 121 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
universe@449 122 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 123 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@453 124 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
universe@449 125 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
universe@449 126 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 127 CU_ASSERT_PTR_NULL(nodes[1].prev)
olaf@442 128 }
olaf@442 129
universe@456 130 void test_linked_list_last(void) {
universe@456 131 CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, -1))
universe@456 132 CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0))
universe@455 133
universe@456 134 struct node {
universe@456 135 int data;
universe@456 136 void *next;
universe@456 137 };
universe@456 138 ptrdiff_t loc = offsetof(struct node, next);
universe@456 139
universe@456 140 struct node third = {3, NULL};
universe@456 141 struct node second = {2, &third};
universe@456 142 struct node first = {1, &second};
universe@456 143
universe@456 144 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third)
universe@456 145 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third)
universe@456 146 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third)
universe@456 147 }
universe@456 148
universe@468 149 void test_linked_list_size(void) {
universe@468 150 struct node {
universe@468 151 void *next;
universe@468 152 };
universe@468 153 ptrdiff_t loc = offsetof(struct node, next);
universe@468 154
universe@468 155 struct node first = {NULL};
universe@468 156 struct node second = {NULL};
universe@468 157 struct node third = {NULL};
universe@468 158
universe@468 159 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0)
universe@468 160 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1)
universe@468 161 first.next = &second;
universe@468 162 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2)
universe@468 163 second.next = &third;
universe@468 164 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3)
universe@468 165 CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2)
universe@468 166 }
universe@468 167
universe@468 168 void test_linked_list_sort(void) {
universe@468 169 struct node {
universe@468 170 void *prev;
universe@468 171 void *next;
universe@468 172 int data;
universe@468 173 };
universe@468 174
universe@468 175 int expected[] = {
universe@468 176 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
universe@468 177 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
universe@468 178 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
universe@468 179 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
universe@468 180 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
universe@468 181 4785, 4791, 4801, 4859, 4903, 4973
universe@468 182 };
universe@468 183 int scrambled[] = {
universe@468 184 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
universe@468 185 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
universe@468 186 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
universe@468 187 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
universe@468 188 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
universe@468 189 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
universe@468 190 };
universe@468 191
universe@468 192 struct node *nodes = calloc(100, sizeof(struct node));
universe@468 193 for (int i = 0; i < 100; i++) {
universe@468 194 nodes[i].prev = i == 0 ? NULL : &nodes[i - 1];
universe@468 195 nodes[i].next = i == 99 ? NULL : &nodes[i + 1];
universe@468 196 nodes[i].data = scrambled[i];
universe@468 197 }
universe@468 198
universe@468 199 struct node *begin = &nodes[0];
universe@468 200 struct node *end = &nodes[99];
universe@468 201
universe@468 202 cx_linked_list_sort((void **) &begin, (void **) &end,
universe@468 203 offsetof(struct node, prev),
universe@468 204 offsetof(struct node, next),
universe@468 205 offsetof(struct node, data),
universe@468 206 0, (CxListComparator) cmp_int);
universe@468 207
universe@468 208 CU_ASSERT_PTR_NULL(begin->prev)
universe@468 209 CU_ASSERT_EQUAL(begin->data, expected[0])
universe@468 210 struct node *check = begin;
universe@468 211 struct node *check_last = NULL;
universe@468 212 for (int i = 0 ; i < 100 ; i++) {
universe@468 213 CU_ASSERT_EQUAL(check->data, expected[i])
universe@468 214 CU_ASSERT_PTR_EQUAL(check->prev, check_last)
universe@468 215 if (i < 99) {
universe@468 216 CU_ASSERT_PTR_NOT_NULL(check->next)
universe@468 217 }
universe@468 218 check_last = check;
universe@468 219 check = check->next;
universe@468 220 }
universe@468 221 CU_ASSERT_PTR_NULL(check)
universe@468 222 CU_ASSERT_EQUAL(end->data, expected[99])
universe@468 223 }
universe@468 224
universe@456 225
universe@456 226 void test_hl_linked_list_create(void) {
universe@455 227 cxTestingAllocatorReset();
universe@455 228
universe@455 229 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@455 230
universe@455 231 CU_ASSERT_EQUAL(list->size, 0)
universe@455 232 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
universe@455 233 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
universe@455 234 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
universe@455 235 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
universe@455 236
universe@455 237 cxLinkedListDestroy(list);
universe@455 238 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@455 239 }
universe@455 240
universe@459 241 void test_hl_linked_list_add(void) {
universe@459 242 cxTestingAllocatorReset();
universe@459 243
universe@459 244 int data;
universe@459 245 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 246
universe@459 247 data = 5;
universe@460 248 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 249 data = 47;
universe@460 250 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 251 data = 13;
universe@460 252 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 253
universe@459 254 CU_ASSERT_EQUAL(list->size, 3)
universe@459 255 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 256
universe@466 257 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 258 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 259 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@459 260
universe@459 261 cxLinkedListDestroy(list);
universe@459 262 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 263 }
universe@459 264
universe@459 265 void test_hl_linked_list_last(void) {
universe@459 266 cxTestingAllocatorReset();
universe@459 267
universe@459 268 int data;
universe@459 269 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 270
universe@460 271 CU_ASSERT_PTR_NULL(cxListLast(list))
universe@459 272
universe@459 273 data = 5;
universe@460 274 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@466 275 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5)
universe@459 276
universe@459 277 data = 47;
universe@460 278 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@466 279 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47)
universe@459 280
universe@459 281 cxLinkedListDestroy(list);
universe@459 282 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 283 }
universe@459 284
universe@459 285 void test_hl_linked_list_insert(void) {
universe@459 286 cxTestingAllocatorReset();
universe@459 287
universe@459 288 int data;
universe@459 289 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 290
universe@459 291 data = 5;
universe@460 292 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
universe@459 293 CU_ASSERT_EQUAL(list->size, 0)
universe@460 294 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
universe@459 295 CU_ASSERT_EQUAL(list->size, 1)
universe@459 296 data = 47;
universe@460 297 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
universe@459 298 CU_ASSERT_EQUAL(list->size, 2)
universe@459 299 data = 13;
universe@460 300 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
universe@459 301 CU_ASSERT_EQUAL(list->size, 3)
universe@459 302 data = 42;
universe@460 303 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
universe@459 304
universe@459 305 CU_ASSERT_EQUAL(list->size, 4)
universe@459 306 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 307
universe@466 308 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 309 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@466 310 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
universe@466 311 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
universe@459 312
universe@459 313 cxLinkedListDestroy(list);
universe@459 314 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 315 }
universe@459 316
universe@459 317 void test_hl_linked_list_remove(void) {
universe@459 318 cxTestingAllocatorReset();
universe@459 319
universe@459 320 int data;
universe@459 321 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 322
universe@459 323 data = 5;
universe@460 324 cxListAdd(list, &data);
universe@459 325 data = 47;
universe@460 326 cxListAdd(list, &data);
universe@459 327 data = 42;
universe@460 328 cxListAdd(list, &data);
universe@459 329 data = 13;
universe@460 330 cxListAdd(list, &data);
universe@459 331
universe@459 332 CU_ASSERT_EQUAL(list->size, 4)
universe@459 333 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 334
universe@459 335 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
universe@459 336
universe@459 337 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
universe@459 338 CU_ASSERT_EQUAL(list->size, 3)
universe@459 339 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 340 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 341 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 342 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@459 343
universe@459 344 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@459 345 CU_ASSERT_EQUAL(list->size, 2)
universe@459 346 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 347 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 348 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@459 349
universe@459 350 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
universe@459 351 CU_ASSERT_EQUAL(list->size, 1)
universe@459 352 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 353 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@459 354
universe@459 355 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@459 356 CU_ASSERT_EQUAL(list->size, 0)
universe@459 357 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 358
universe@459 359 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
universe@459 360
universe@459 361 cxLinkedListDestroy(list);
universe@459 362 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 363 }
universe@459 364
universe@459 365 void test_hl_linked_list_find(void) {
universe@459 366 cxTestingAllocatorReset();
universe@459 367
universe@459 368 int data, criteria;
universe@459 369 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 370
universe@459 371 data = 5;
universe@460 372 cxListAdd(list, &data);
universe@459 373 data = 47;
universe@460 374 cxListAdd(list, &data);
universe@459 375 data = 13;
universe@460 376 cxListAdd(list, &data);
universe@459 377
universe@459 378 CU_ASSERT_EQUAL(list->size, 3)
universe@459 379 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 380
universe@459 381 criteria = 5;
universe@460 382 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
universe@459 383 criteria = 47;
universe@460 384 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@459 385 criteria = 13;
universe@460 386 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
universe@459 387 criteria = 9000;
universe@460 388 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@459 389 criteria = -5;
universe@460 390 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@459 391
universe@459 392 cxLinkedListDestroy(list);
universe@459 393 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 394 }
universe@459 395
universe@466 396 void test_hl_ptr_linked_list_create(void) {
universe@466 397 cxTestingAllocatorReset();
universe@466 398
universe@466 399 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 400
universe@466 401 CU_ASSERT_EQUAL(list->size, 0)
universe@466 402 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
universe@466 403 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
universe@466 404 CU_ASSERT_EQUAL(list->itemsize, sizeof(void *))
universe@466 405 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
universe@466 406
universe@466 407 cxLinkedListDestroy(list);
universe@466 408 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 409 }
universe@466 410
universe@466 411 void test_hl_ptr_linked_list_add(void) {
universe@466 412 cxTestingAllocatorReset();
universe@466 413
universe@466 414 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 415
universe@466 416 int a = 5, b = 47, c = 13;
universe@466 417
universe@466 418 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
universe@466 419 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
universe@466 420 CU_ASSERT_EQUAL(cxListAdd(list, &c), 0)
universe@466 421
universe@466 422 CU_ASSERT_EQUAL(list->size, 3)
universe@466 423 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 424
universe@466 425 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 426 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 427 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@466 428
universe@466 429 a = 9;
universe@466 430 b = 10;
universe@466 431 c = 11;
universe@466 432
universe@466 433 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9)
universe@466 434 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10)
universe@466 435 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11)
universe@466 436
universe@466 437 cxLinkedListDestroy(list);
universe@466 438 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 439 }
universe@466 440
universe@466 441 void test_hl_ptr_linked_list_last(void) {
universe@466 442 cxTestingAllocatorReset();
universe@466 443
universe@466 444 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 445 CU_ASSERT_PTR_NULL(cxListLast(list))
universe@466 446
universe@466 447 int a = 5, b = 47;
universe@466 448
universe@466 449 CU_ASSERT_EQUAL(cxListAdd(list, &a), 0)
universe@466 450 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5)
universe@466 451 CU_ASSERT_EQUAL(cxListAdd(list, &b), 0)
universe@466 452 CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47)
universe@466 453
universe@466 454 cxLinkedListDestroy(list);
universe@466 455 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 456 }
universe@466 457
universe@466 458 void test_hl_ptr_linked_list_insert(void) {
universe@466 459 cxTestingAllocatorReset();
universe@466 460
universe@466 461 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 462
universe@466 463 int a = 5, b = 47, c = 13, d = 42;
universe@466 464
universe@466 465 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0)
universe@466 466 CU_ASSERT_EQUAL(list->size, 0)
universe@466 467 CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0)
universe@466 468 CU_ASSERT_EQUAL(list->size, 1)
universe@466 469 CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0)
universe@466 470 CU_ASSERT_EQUAL(list->size, 2)
universe@466 471 CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0)
universe@466 472 CU_ASSERT_EQUAL(list->size, 3)
universe@466 473 CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0)
universe@466 474
universe@466 475 CU_ASSERT_EQUAL(list->size, 4)
universe@466 476 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 477
universe@466 478 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 479 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@466 480 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5)
universe@466 481 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42)
universe@466 482
universe@466 483 cxLinkedListDestroy(list);
universe@466 484 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 485 }
universe@466 486
universe@466 487 void test_hl_ptr_linked_list_remove(void) {
universe@466 488 cxTestingAllocatorReset();
universe@466 489
universe@466 490 int a = 5, b = 47, c = 42, d = 13;
universe@466 491 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 492
universe@466 493 cxListAdd(list, &a);
universe@466 494 cxListAdd(list, &b);
universe@466 495 cxListAdd(list, &c);
universe@466 496 cxListAdd(list, &d);
universe@466 497
universe@466 498 CU_ASSERT_EQUAL(list->size, 4)
universe@466 499 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 500
universe@466 501 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
universe@466 502
universe@466 503 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
universe@466 504 CU_ASSERT_EQUAL(list->size, 3)
universe@466 505 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 506 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5)
universe@466 507 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47)
universe@466 508 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13)
universe@466 509
universe@466 510 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@466 511 CU_ASSERT_EQUAL(list->size, 2)
universe@466 512 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 513 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 514 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13)
universe@466 515
universe@466 516 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
universe@466 517 CU_ASSERT_EQUAL(list->size, 1)
universe@466 518 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 519 CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47)
universe@466 520
universe@466 521 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@466 522 CU_ASSERT_EQUAL(list->size, 0)
universe@466 523 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 524
universe@466 525 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
universe@466 526
universe@466 527 cxLinkedListDestroy(list);
universe@466 528 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 529 }
universe@466 530
universe@466 531 void test_hl_ptr_linked_list_find(void) {
universe@466 532 cxTestingAllocatorReset();
universe@466 533
universe@466 534 int a = 5, b = 47, c = 13, criteria;
universe@466 535 CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int);
universe@466 536
universe@466 537 cxListAdd(list, &a);
universe@466 538 cxListAdd(list, &b);
universe@466 539 cxListAdd(list, &c);
universe@466 540
universe@466 541 CU_ASSERT_EQUAL(list->size, 3)
universe@466 542 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@466 543
universe@466 544 criteria = 5;
universe@466 545 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
universe@466 546 criteria = 47;
universe@466 547 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@466 548 criteria = 13;
universe@466 549 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
universe@466 550 criteria = 9000;
universe@466 551 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@466 552 criteria = -5;
universe@466 553 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@466 554 b = -5;
universe@466 555 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@466 556
universe@466 557 cxLinkedListDestroy(list);
universe@466 558 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@466 559 }
universe@466 560
universe@390 561 int main() {
universe@411 562 CU_pSuite suite = NULL;
universe@449 563
universe@411 564 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 565 return CU_get_error();
universe@411 566 }
universe@411 567
universe@459 568 suite = CU_add_suite("low level linked list", NULL, NULL);
universe@449 569
universe@455 570 cu_add_test(suite, test_linked_list_at);
universe@455 571 cu_add_test(suite, test_linked_list_add);
universe@456 572 cu_add_test(suite, test_linked_list_last);
universe@468 573 cu_add_test(suite, test_linked_list_size);
universe@468 574 cu_add_test(suite, test_linked_list_sort);
universe@455 575
universe@459 576 suite = CU_add_suite("high level linked list", NULL, NULL);
universe@455 577
universe@456 578 cu_add_test(suite, test_hl_linked_list_create);
universe@456 579 cu_add_test(suite, test_hl_linked_list_add);
universe@456 580 cu_add_test(suite, test_hl_linked_list_last);
universe@456 581 cu_add_test(suite, test_hl_linked_list_insert);
universe@456 582 cu_add_test(suite, test_hl_linked_list_remove);
universe@459 583 cu_add_test(suite, test_hl_linked_list_find);
universe@413 584
universe@466 585 suite = CU_add_suite("high level pointer linked list", NULL, NULL);
universe@466 586
universe@466 587 cu_add_test(suite, test_hl_ptr_linked_list_create);
universe@466 588 cu_add_test(suite, test_hl_ptr_linked_list_add);
universe@466 589 cu_add_test(suite, test_hl_ptr_linked_list_last);
universe@466 590 cu_add_test(suite, test_hl_ptr_linked_list_insert);
universe@466 591 cu_add_test(suite, test_hl_ptr_linked_list_remove);
universe@466 592 cu_add_test(suite, test_hl_ptr_linked_list_find);
universe@466 593
universe@411 594 CU_basic_set_mode(UCX_CU_BRM);
universe@411 595
universe@411 596 int exitcode;
universe@411 597 if (CU_basic_run_tests()) {
universe@411 598 exitcode = CU_get_error();
universe@411 599 } else {
universe@411 600 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 601 }
universe@411 602 CU_cleanup_registry();
universe@411 603 return exitcode;
universe@390 604 }

mercurial