test/test_list.c

Mon, 27 Dec 2021 14:44:08 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Dec 2021 14:44:08 +0100
changeset 482
0d998f19d130
parent 479
a29bdd703e02
child 486
d7ca126eab7f
permissions
-rw-r--r--

add tests for the new low level functions

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

mercurial