test/test_list.c

Tue, 28 Dec 2021 14:16:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Dec 2021 14:16:04 +0100
changeset 486
d7ca126eab7f
parent 482
0d998f19d130
child 487
4bd19279778c
permissions
-rw-r--r--

add cx_linked_list_compare() and simplifies some tests

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

mercurial