test/test_list.c

Sat, 09 Apr 2022 18:02:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 Apr 2022 18:02:53 +0200
changeset 509
0d3c6075f82c
parent 507
2e8878770de0
permissions
-rw-r--r--

#129 - remove test code duplication

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

mercurial