1.1 --- a/test/test_list.c Sat Apr 16 17:28:36 2022 +0200 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,976 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 1.8 - * 1.9 - * Redistribution and use in source and binary forms, with or without 1.10 - * modification, are permitted provided that the following conditions are met: 1.11 - * 1.12 - * 1. Redistributions of source code must retain the above copyright 1.13 - * notice, this list of conditions and the following disclaimer. 1.14 - * 1.15 - * 2. Redistributions in binary form must reproduce the above copyright 1.16 - * notice, this list of conditions and the following disclaimer in the 1.17 - * documentation and/or other materials provided with the distribution. 1.18 - * 1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 - * POSSIBILITY OF SUCH DAMAGE. 1.30 - */ 1.31 - 1.32 -#include "cx/linked_list.h" 1.33 -#include "cx/utils.h" 1.34 -#include "test_config.h" 1.35 -#include "util_allocator.h" 1.36 - 1.37 -static int cmp_int_impl( 1.38 - int const *l, 1.39 - int const *r 1.40 -) { 1.41 - int left = *l, right = *r; 1.42 - return left == right ? 0 : (left < right ? -1 : 1); 1.43 -} 1.44 - 1.45 -#define cmp_int ((CxListComparator) cmp_int_impl) 1.46 - 1.47 -struct node { 1.48 - struct node *next; 1.49 - struct node *prev; 1.50 - int data; 1.51 -}; 1.52 - 1.53 -#define nd(name) name = {0} 1.54 - 1.55 -const ptrdiff_t loc_prev = offsetof(struct node, prev); 1.56 -const ptrdiff_t loc_next = offsetof(struct node, next); 1.57 -const ptrdiff_t loc_data = offsetof(struct node, data); 1.58 - 1.59 -static struct node *create_nodes_test_data( 1.60 - size_t n, 1.61 - int const data[] 1.62 -) { 1.63 - CU_ASSERT_NOT_EQUAL_FATAL(n, 0) 1.64 - struct node *begin = calloc(1, sizeof(struct node)); 1.65 - struct node *prev = begin; 1.66 - if (data) begin->data = data[0]; 1.67 - for (size_t i = 1; i < n; i++) { 1.68 - struct node *node = calloc(1, sizeof(struct node)); 1.69 - if (data) node->data = data[i]; 1.70 - cx_linked_list_link(prev, node, loc_prev, loc_next); 1.71 - prev = node; 1.72 - } 1.73 - return begin; 1.74 -} 1.75 - 1.76 -static void destroy_nodes_test_data(struct node *begin) { 1.77 - struct node *node = begin; 1.78 - while (node) { 1.79 - struct node *next = node->next; 1.80 - free(node); 1.81 - node = next; 1.82 - } 1.83 -} 1.84 - 1.85 -static int *create_ints_test_data(size_t len) { 1.86 - int *data = malloc(sizeof(int) * len); 1.87 - cx_for_n (i, len) data[i] = rand(); // NOLINT(cert-msc50-cpp) 1.88 - return data; 1.89 -} 1.90 - 1.91 -void test_linked_list_link_unlink(void) { 1.92 - 1.93 - struct node nd(a), nd(b), nd(c); 1.94 - 1.95 - cx_linked_list_link(&a, &b, loc_prev, loc_next); 1.96 - CU_ASSERT_PTR_NULL(a.prev) 1.97 - CU_ASSERT_PTR_EQUAL(a.next, &b) 1.98 - CU_ASSERT_PTR_EQUAL(b.prev, &a) 1.99 - CU_ASSERT_PTR_NULL(b.next) 1.100 - 1.101 - cx_linked_list_unlink(&a, &b, loc_prev, loc_next); 1.102 - CU_ASSERT_PTR_NULL(a.prev) 1.103 - CU_ASSERT_PTR_NULL(a.next) 1.104 - CU_ASSERT_PTR_NULL(b.prev) 1.105 - CU_ASSERT_PTR_NULL(b.next) 1.106 - 1.107 - cx_linked_list_link(&b, &c, loc_prev, loc_next); 1.108 - cx_linked_list_link(&a, &b, loc_prev, loc_next); 1.109 - cx_linked_list_unlink(&b, &c, loc_prev, loc_next); 1.110 - CU_ASSERT_PTR_NULL(a.prev) 1.111 - CU_ASSERT_PTR_EQUAL(a.next, &b) 1.112 - CU_ASSERT_PTR_EQUAL(b.prev, &a) 1.113 - CU_ASSERT_PTR_NULL(b.next) 1.114 - CU_ASSERT_PTR_NULL(c.prev) 1.115 - CU_ASSERT_PTR_NULL(c.next) 1.116 -} 1.117 - 1.118 -void test_linked_list_at(void) { 1.119 - struct node nd(a), nd(b), nd(c), nd(d); 1.120 - cx_linked_list_link(&a, &b, loc_prev, loc_next); 1.121 - cx_linked_list_link(&b, &c, loc_prev, loc_next); 1.122 - cx_linked_list_link(&c, &d, loc_prev, loc_next); 1.123 - 1.124 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a) 1.125 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b) 1.126 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c) 1.127 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d) 1.128 - CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)) 1.129 - 1.130 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a) 1.131 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b) 1.132 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c) 1.133 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d) 1.134 - CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)) 1.135 - 1.136 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a) 1.137 - CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b) 1.138 -} 1.139 - 1.140 -void test_linked_list_find(void) { 1.141 - int data[] = {2, 4, 6, 8}; 1.142 - void *list = create_nodes_test_data(4, data); 1.143 - int s; 1.144 - 1.145 - s = 2; 1.146 - CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 1.147 - false, cmp_int, &s), 0) 1.148 - s = 4; 1.149 - CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 1.150 - false, cmp_int, &s), 1) 1.151 - s = 6; 1.152 - CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 1.153 - false, cmp_int, &s), 2) 1.154 - s = 8; 1.155 - CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 1.156 - false, cmp_int, &s), 3) 1.157 - s = 10; 1.158 - CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 1.159 - false, cmp_int, &s), 4) 1.160 - s = -2; 1.161 - CU_ASSERT_EQUAL(cx_linked_list_find(list, loc_next, loc_data, 1.162 - false, cmp_int, &s), 4) 1.163 -} 1.164 - 1.165 -void test_linked_list_compare(void) { 1.166 - int a[] = {2, 4, 6, 8}; 1.167 - int b[] = {2, 4, 6}; 1.168 - int c[] = {2, 4, 6, 9}; 1.169 - 1.170 - void *la = create_nodes_test_data(4, a); 1.171 - void *lb = create_nodes_test_data(3, b); 1.172 - void *lc = create_nodes_test_data(4, c); 1.173 - 1.174 - CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data, 1.175 - false, cmp_int) 1.176 - ) 1.177 - CU_ASSERT_TRUE(0 > cx_linked_list_compare(lb, la, loc_next, loc_data, 1.178 - false, cmp_int) 1.179 - ) 1.180 - CU_ASSERT_TRUE(0 < cx_linked_list_compare(lc, la, loc_next, loc_data, 1.181 - false, cmp_int) 1.182 - ) 1.183 - CU_ASSERT_TRUE(0 > cx_linked_list_compare(la, lc, loc_next, loc_data, 1.184 - false, cmp_int) 1.185 - ) 1.186 - CU_ASSERT_TRUE(0 == cx_linked_list_compare(la, la, loc_next, loc_data, 1.187 - false, cmp_int) 1.188 - ) 1.189 - 1.190 - destroy_nodes_test_data(la); 1.191 - destroy_nodes_test_data(lb); 1.192 - destroy_nodes_test_data(lc); 1.193 -} 1.194 - 1.195 -void test_linked_list_add(void) { 1.196 - struct node nodes[4]; 1.197 - void *begin, *end; 1.198 - 1.199 - // test with begin, end / prev, next 1.200 - memset(nodes, 0, 4 * sizeof(struct node)); 1.201 - begin = end = NULL; 1.202 - 1.203 - cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); 1.204 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.205 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.206 - CU_ASSERT_PTR_NULL(nodes[0].prev) 1.207 - CU_ASSERT_PTR_NULL(nodes[0].next) 1.208 - 1.209 - cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); 1.210 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.211 - CU_ASSERT_PTR_EQUAL(end, &nodes[1]) 1.212 - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) 1.213 - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) 1.214 - 1.215 - // test with begin only / prev, next 1.216 - memset(nodes, 0, 4 * sizeof(struct node)); 1.217 - begin = end = NULL; 1.218 - 1.219 - cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); 1.220 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.221 - cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); 1.222 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.223 - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) 1.224 - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) 1.225 - 1.226 - cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); 1.227 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) 1.228 - CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) 1.229 - 1.230 - // test with end only / prev, next 1.231 - memset(nodes, 0, 4 * sizeof(struct node)); 1.232 - begin = end = NULL; 1.233 - 1.234 - cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]); 1.235 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.236 - cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]); 1.237 - CU_ASSERT_PTR_EQUAL(end, &nodes[1]) 1.238 - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) 1.239 - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) 1.240 - 1.241 - cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]); 1.242 - CU_ASSERT_PTR_EQUAL(end, &nodes[2]) 1.243 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) 1.244 - CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) 1.245 - 1.246 - // test with begin, end / next 1.247 - memset(nodes, 0, 4 * sizeof(struct node)); 1.248 - begin = end = NULL; 1.249 - 1.250 - cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); 1.251 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.252 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.253 - cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); 1.254 - CU_ASSERT_PTR_EQUAL(end, &nodes[1]) 1.255 - CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) 1.256 - CU_ASSERT_PTR_NULL(nodes[1].prev) 1.257 -} 1.258 - 1.259 -void test_linked_list_prepend(void) { 1.260 - struct node nodes[4]; 1.261 - void *begin, *end; 1.262 - 1.263 - // test with begin, end / prev, next 1.264 - memset(nodes, 0, 4 * sizeof(struct node)); 1.265 - begin = end = NULL; 1.266 - 1.267 - cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]); 1.268 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.269 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.270 - CU_ASSERT_PTR_NULL(nodes[0].prev) 1.271 - CU_ASSERT_PTR_NULL(nodes[0].next) 1.272 - 1.273 - cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]); 1.274 - CU_ASSERT_PTR_EQUAL(begin, &nodes[1]) 1.275 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.276 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) 1.277 - CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) 1.278 - 1.279 - // test with begin only / prev, next 1.280 - memset(nodes, 0, 4 * sizeof(struct node)); 1.281 - begin = end = NULL; 1.282 - 1.283 - cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]); 1.284 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.285 - cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]); 1.286 - CU_ASSERT_PTR_EQUAL(begin, &nodes[1]) 1.287 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) 1.288 - CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) 1.289 - 1.290 - cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]); 1.291 - CU_ASSERT_PTR_EQUAL(begin, &nodes[2]) 1.292 - CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) 1.293 - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2]) 1.294 - 1.295 - // test with end only / prev, next 1.296 - memset(nodes, 0, 4 * sizeof(struct node)); 1.297 - begin = end = NULL; 1.298 - 1.299 - cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]); 1.300 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.301 - cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]); 1.302 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.303 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) 1.304 - CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) 1.305 - 1.306 - cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]); 1.307 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.308 - CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) 1.309 - CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2]) 1.310 - 1.311 - // test with begin, end / next 1.312 - memset(nodes, 0, 4 * sizeof(struct node)); 1.313 - begin = end = NULL; 1.314 - 1.315 - cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]); 1.316 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.317 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.318 - cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]); 1.319 - cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]); 1.320 - CU_ASSERT_PTR_EQUAL(begin, &nodes[2]) 1.321 - CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 1.322 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) 1.323 - CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) 1.324 - CU_ASSERT_PTR_NULL(nodes[1].prev) 1.325 - CU_ASSERT_PTR_NULL(nodes[0].prev) 1.326 -} 1.327 - 1.328 -void test_linked_list_insert(void) { 1.329 - struct node nodes[4]; 1.330 - void *begin, *end; 1.331 - 1.332 - // insert mid list 1.333 - memset(nodes, 0, 4 * sizeof(struct node)); 1.334 - begin = &nodes[0]; 1.335 - end = &nodes[2]; 1.336 - 1.337 - cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); 1.338 - cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); 1.339 - 1.340 - cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]); 1.341 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.342 - CU_ASSERT_PTR_EQUAL(end, &nodes[2]) 1.343 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3]) 1.344 - CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[3]) 1.345 - CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1]) 1.346 - CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[2]) 1.347 - 1.348 - // insert end 1.349 - memset(nodes, 0, 4 * sizeof(struct node)); 1.350 - begin = &nodes[0]; 1.351 - end = &nodes[2]; 1.352 - 1.353 - cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); 1.354 - cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); 1.355 - 1.356 - cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]); 1.357 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.358 - CU_ASSERT_PTR_EQUAL(end, &nodes[3]) 1.359 - CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3]) 1.360 - CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2]) 1.361 - CU_ASSERT_PTR_NULL(nodes[3].next) 1.362 - 1.363 - // insert begin 1.364 - memset(nodes, 0, 4 * sizeof(struct node)); 1.365 - begin = &nodes[0]; 1.366 - end = &nodes[2]; 1.367 - 1.368 - cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); 1.369 - cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); 1.370 - 1.371 - cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]); 1.372 - CU_ASSERT_PTR_EQUAL(begin, &nodes[3]) 1.373 - CU_ASSERT_PTR_EQUAL(end, &nodes[2]) 1.374 - CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[3]) 1.375 - CU_ASSERT_PTR_NULL(nodes[3].prev) 1.376 - CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[0]) 1.377 -} 1.378 - 1.379 -void test_linked_list_insert_chain(void) { 1.380 - struct node nodes[5]; 1.381 - void *begin, *end; 1.382 - 1.383 - // insert mid list 1.384 - memset(nodes, 0, 5 * sizeof(struct node)); 1.385 - begin = &nodes[0]; 1.386 - end = &nodes[2]; 1.387 - 1.388 - cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); 1.389 - cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); 1.390 - cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); 1.391 - 1.392 - cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL); 1.393 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.394 - CU_ASSERT_PTR_EQUAL(end, &nodes[2]) 1.395 - CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3]) 1.396 - CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[4]) 1.397 - CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1]) 1.398 - CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[2]) 1.399 - 1.400 - // insert end 1.401 - memset(nodes, 0, 5 * sizeof(struct node)); 1.402 - begin = &nodes[0]; 1.403 - end = &nodes[2]; 1.404 - 1.405 - cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); 1.406 - cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); 1.407 - cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); 1.408 - 1.409 - cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL); 1.410 - CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 1.411 - CU_ASSERT_PTR_EQUAL(end, &nodes[4]) 1.412 - CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3]) 1.413 - CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2]) 1.414 - CU_ASSERT_PTR_NULL(nodes[4].next) 1.415 - 1.416 - // insert begin 1.417 - memset(nodes, 0, 5 * sizeof(struct node)); 1.418 - begin = &nodes[0]; 1.419 - end = &nodes[2]; 1.420 - 1.421 - cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); 1.422 - cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); 1.423 - cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); 1.424 - 1.425 - cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL); 1.426 - CU_ASSERT_PTR_EQUAL(begin, &nodes[3]) 1.427 - CU_ASSERT_PTR_EQUAL(end, &nodes[2]) 1.428 - CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[4]) 1.429 - CU_ASSERT_PTR_NULL(nodes[3].prev) 1.430 - CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0]) 1.431 -} 1.432 - 1.433 -void test_linked_list_first(void) { 1.434 - struct node *begin = create_nodes_test_data(3, NULL); 1.435 - CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin) 1.436 - CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin) 1.437 - CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin) 1.438 - destroy_nodes_test_data(begin); 1.439 -} 1.440 - 1.441 -void test_linked_list_last(void) { 1.442 - struct node *begin = create_nodes_test_data(3, NULL); 1.443 - struct node *end = begin->next->next; 1.444 - CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end) 1.445 - CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end) 1.446 - CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end) 1.447 - destroy_nodes_test_data(begin); 1.448 -} 1.449 - 1.450 -void test_linked_list_prev(void) { 1.451 - struct node *begin = create_nodes_test_data(3, NULL); 1.452 - CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin)) 1.453 - CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin) 1.454 - CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next) 1.455 - destroy_nodes_test_data(begin); 1.456 -} 1.457 - 1.458 -void test_linked_list_remove(void) { 1.459 - void *begin, *end; 1.460 - 1.461 - int data[] = {2, 4, 6}; 1.462 - begin = create_nodes_test_data(3, data); 1.463 - struct node *first = begin; 1.464 - struct node *second = first->next; 1.465 - struct node *third = second->next; 1.466 - end = third; 1.467 - 1.468 - cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second); 1.469 - CU_ASSERT_PTR_EQUAL(begin, first) 1.470 - CU_ASSERT_PTR_EQUAL(end, third) 1.471 - CU_ASSERT_PTR_NULL(first->prev) 1.472 - CU_ASSERT_PTR_EQUAL(first->next, third) 1.473 - CU_ASSERT_PTR_EQUAL(third->prev, first) 1.474 - CU_ASSERT_PTR_NULL(third->next) 1.475 - 1.476 - cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third); 1.477 - CU_ASSERT_PTR_EQUAL(begin, first) 1.478 - CU_ASSERT_PTR_EQUAL(end, first) 1.479 - CU_ASSERT_PTR_NULL(first->prev) 1.480 - CU_ASSERT_PTR_NULL(first->next) 1.481 - 1.482 - cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first); 1.483 - CU_ASSERT_PTR_NULL(begin) 1.484 - CU_ASSERT_PTR_NULL(end) 1.485 - 1.486 - free(first); 1.487 - free(second); 1.488 - free(third); 1.489 -} 1.490 - 1.491 -void test_linked_list_size(void) { 1.492 - struct node *list; 1.493 - 1.494 - CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0) 1.495 - 1.496 - list = create_nodes_test_data(5, NULL); 1.497 - CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5) 1.498 - destroy_nodes_test_data(list); 1.499 - 1.500 - list = create_nodes_test_data(13, NULL); 1.501 - CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13) 1.502 - destroy_nodes_test_data(list); 1.503 -} 1.504 - 1.505 -void test_linked_list_sort(void) { 1.506 - int expected[] = { 1.507 - 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, 1.508 - 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, 1.509 - 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, 1.510 - 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, 1.511 - 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, 1.512 - 4785, 4791, 4801, 4859, 4903, 4973 1.513 - }; 1.514 - int scrambled[] = { 1.515 - 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, 1.516 - 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, 1.517 - 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, 1.518 - 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, 1.519 - 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, 1.520 - 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 1.521 - }; 1.522 - 1.523 - void *begin = create_nodes_test_data(100, scrambled); 1.524 - void *end = cx_linked_list_last(begin, loc_next); 1.525 - 1.526 - cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, 1.527 - false, cmp_int); 1.528 - 1.529 - struct node *check = begin; 1.530 - struct node *check_last = NULL; 1.531 - CU_ASSERT_PTR_NULL(check->prev) 1.532 - CU_ASSERT_EQUAL(check->data, expected[0]) 1.533 - cx_for_n (i, 100) { 1.534 - CU_ASSERT_EQUAL(check->data, expected[i]) 1.535 - CU_ASSERT_PTR_EQUAL(check->prev, check_last) 1.536 - if (i < 99) { 1.537 - CU_ASSERT_PTR_NOT_NULL(check->next) 1.538 - } 1.539 - check_last = check; 1.540 - check = check->next; 1.541 - } 1.542 - CU_ASSERT_PTR_NULL(check) 1.543 - CU_ASSERT_PTR_EQUAL(end, check_last) 1.544 - 1.545 - destroy_nodes_test_data(begin); 1.546 -} 1.547 - 1.548 -void test_linked_list_reverse(void) { 1.549 - void *begin, *end; 1.550 - 1.551 - int data[] = {2, 4, 6, 8}; 1.552 - int reversed[] = {8, 6, 4, 2}; 1.553 - 1.554 - void *list = create_nodes_test_data(4, data); 1.555 - void *expected = create_nodes_test_data(4, reversed); 1.556 - 1.557 - begin = list; 1.558 - end = cx_linked_list_last(list, loc_next); 1.559 - 1.560 - cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); 1.561 - CU_ASSERT_PTR_EQUAL(end, list) 1.562 - CU_ASSERT_PTR_EQUAL(begin, cx_linked_list_first(end, loc_prev)) 1.563 - CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data, 1.564 - 0, cmp_int)) 1.565 - 1.566 - destroy_nodes_test_data(begin); 1.567 - destroy_nodes_test_data(expected); 1.568 -} 1.569 - 1.570 -static void verify_linked_list_create(CxList *list) { 1.571 - CU_ASSERT_EQUAL(list->size, 0) 1.572 - CU_ASSERT_EQUAL(list->capacity, (size_t) -1) 1.573 - CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) 1.574 - CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) 1.575 - 1.576 - cxListDestroy(list); 1.577 -} 1.578 - 1.579 -void test_hl_linked_list_create(void) { 1.580 - CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.581 - CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) 1.582 - verify_linked_list_create(list); 1.583 -} 1.584 - 1.585 -void test_hl_ptr_linked_list_create(void) { 1.586 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.587 - CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) 1.588 - verify_linked_list_create(list); 1.589 -} 1.590 - 1.591 -void test_hl_linked_list_from_array(void) { 1.592 - int data[] = {2, 4, 5, 7, 10, 15}; 1.593 - 1.594 - CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.595 - cx_for_n (i, 5) cxListAdd(expected, &data[i]); 1.596 - 1.597 - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data); 1.598 - 1.599 - CU_ASSERT_TRUE(0 == cxListCompare(list, expected)) 1.600 - 1.601 - cxListDestroy(list); 1.602 - cxListDestroy(expected); 1.603 -} 1.604 - 1.605 -static void verify_hl_linked_list_add( 1.606 - CxList *list, 1.607 - int data[], 1.608 - size_t len, 1.609 - bool write_through 1.610 -) { 1.611 - cx_for_n (i, len) CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0) 1.612 - CU_ASSERT_EQUAL(list->size, len) 1.613 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.614 - cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) 1.615 - cx_for_n (i, len) ++data[i]; 1.616 - if (write_through) { 1.617 - cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) 1.618 - } else { 1.619 - cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1) 1.620 - } 1.621 - cxListDestroy(list); 1.622 -} 1.623 - 1.624 -void test_hl_linked_list_add(void) { 1.625 - CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.626 - int data[] = {5, 47, 13, 9, 18, 1, 42}; 1.627 - verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), false); 1.628 -} 1.629 - 1.630 -void test_hl_ptr_linked_list_add(void) { 1.631 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.632 - int data[] = {5, 47, 84, 13, 9, 18, 90, 1, 42}; 1.633 - verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true); 1.634 -} 1.635 - 1.636 -static void verify_hl_linked_list_insert(CxList *list) { 1.637 - int a = 5, b = 47, c = 13, d = 42; 1.638 - 1.639 - CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) 1.640 - CU_ASSERT_EQUAL(list->size, 0) 1.641 - CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) 1.642 - CU_ASSERT_EQUAL(list->size, 1) 1.643 - CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) 1.644 - CU_ASSERT_EQUAL(list->size, 2) 1.645 - CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) 1.646 - CU_ASSERT_EQUAL(list->size, 3) 1.647 - CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) 1.648 - 1.649 - CU_ASSERT_EQUAL(list->size, 4) 1.650 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.651 - 1.652 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) 1.653 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) 1.654 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) 1.655 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) 1.656 - 1.657 - cxListDestroy(list); 1.658 -} 1.659 - 1.660 -void test_hl_linked_list_insert(void) { 1.661 - verify_hl_linked_list_insert(cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int))); 1.662 -} 1.663 - 1.664 -void test_hl_ptr_linked_list_insert(void) { 1.665 - verify_hl_linked_list_insert(cxPointerLinkedListCreate(cxTestingAllocator, cmp_int)); 1.666 -} 1.667 - 1.668 -static void verify_hl_linked_list_remove(CxList *list) { 1.669 - CU_ASSERT_EQUAL(list->size, 4) 1.670 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.671 - 1.672 - CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) 1.673 - 1.674 - CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) 1.675 - CU_ASSERT_EQUAL(list->size, 3) 1.676 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.677 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) 1.678 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) 1.679 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) 1.680 - 1.681 - CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) 1.682 - CU_ASSERT_EQUAL(list->size, 2) 1.683 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.684 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) 1.685 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) 1.686 - 1.687 - CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) 1.688 - CU_ASSERT_EQUAL(list->size, 1) 1.689 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.690 - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) 1.691 - 1.692 - CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) 1.693 - CU_ASSERT_EQUAL(list->size, 0) 1.694 - CU_ASSERT_TRUE(list->capacity >= list->size) 1.695 - 1.696 - CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) 1.697 - 1.698 - cxListDestroy(list); 1.699 -} 1.700 - 1.701 -void test_hl_linked_list_remove(void) { 1.702 - int data[] = {5, 47, 42, 13}; 1.703 - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 1.704 - sizeof(int), 4, data); 1.705 - verify_hl_linked_list_remove(list); 1.706 -} 1.707 - 1.708 -void test_hl_ptr_linked_list_remove(void) { 1.709 - int a = 5, b = 47, c = 42, d = 13; 1.710 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.711 - cxListAdd(list, &a); 1.712 - cxListAdd(list, &b); 1.713 - cxListAdd(list, &c); 1.714 - cxListAdd(list, &d); 1.715 - verify_hl_linked_list_remove(list); 1.716 -} 1.717 - 1.718 -static void verify_hl_linked_list_at( 1.719 - CxList *list, 1.720 - size_t len, 1.721 - int *data 1.722 -) { 1.723 - CU_ASSERT_EQUAL(list->size, len) 1.724 - cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) 1.725 - CU_ASSERT_PTR_NULL(cxListAt(list, len)) 1.726 - free(data); 1.727 - cxListDestroy(list); 1.728 -} 1.729 - 1.730 -void test_hl_linked_list_at(void) { 1.731 - size_t len = 100; 1.732 - int *data = create_ints_test_data(len); 1.733 - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, 1.734 - sizeof(int), len, data); 1.735 - verify_hl_linked_list_at(list, len, data); 1.736 -} 1.737 - 1.738 -void test_hl_ptr_linked_list_at(void) { 1.739 - size_t len = 250; 1.740 - int *data = create_ints_test_data(len); 1.741 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.742 - cx_for_n (i, len) cxListAdd(list, &data[i]); 1.743 - verify_hl_linked_list_at(list, len, data); 1.744 -} 1.745 - 1.746 -static void verify_hl_linked_list_find( 1.747 - CxList *list, 1.748 - size_t len, 1.749 - int *data 1.750 -) { 1.751 - cx_for_n (attempt, 100) { 1.752 - size_t exp = rand() % len; // NOLINT(cert-msc50-cpp) 1.753 - int val = data[exp]; 1.754 - cx_for_n (i, exp) { 1.755 - if (data[i] == val) { 1.756 - exp = i; 1.757 - break; 1.758 - } 1.759 - } 1.760 - CU_ASSERT_EQUAL(cxListFind(list, &val), exp) 1.761 - } 1.762 - free(data); 1.763 - cxListDestroy(list); 1.764 -} 1.765 - 1.766 -void test_hl_linked_list_find(void) { 1.767 - size_t len = 100; 1.768 - int *data = create_ints_test_data(len); 1.769 - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), len, data); 1.770 - verify_hl_linked_list_find(list, len, data); 1.771 -} 1.772 - 1.773 -void test_hl_ptr_linked_list_find(void) { 1.774 - size_t len = 250; 1.775 - int *data = create_ints_test_data(len); 1.776 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.777 - cx_for_n (i, len) cxListAdd(list, &data[i]); 1.778 - verify_hl_linked_list_find(list, len, data); 1.779 -} 1.780 - 1.781 -struct sort_test_data { 1.782 - size_t len; 1.783 - int *data; 1.784 - int *sorted; 1.785 -}; 1.786 - 1.787 -static struct sort_test_data create_sort_test_data(void) { 1.788 - size_t len = 1000; 1.789 - int *data = create_ints_test_data(len); 1.790 - int *sorted = malloc(sizeof(int) * len); 1.791 - memcpy(sorted, data, sizeof(int) * len); 1.792 - qsort(sorted, len, sizeof(int), cmp_int); 1.793 - struct sort_test_data s = {len, data, sorted}; 1.794 - return s; 1.795 -} 1.796 - 1.797 -static void free_sort_test_data(struct sort_test_data s) { 1.798 - free(s.data); 1.799 - free(s.sorted); 1.800 -} 1.801 - 1.802 -static void verify_hl_linked_list_sort( 1.803 - CxList *list, 1.804 - struct sort_test_data td 1.805 -) { 1.806 - cxListSort(list); 1.807 - cx_for_n (i, td.len) CU_ASSERT_EQUAL_FATAL(*(int *) cxListAt(list, i), td.sorted[i]) 1.808 - free_sort_test_data(td); 1.809 - cxListDestroy(list); 1.810 -} 1.811 - 1.812 -void test_hl_linked_list_sort(void) { 1.813 - struct sort_test_data td = create_sort_test_data(); 1.814 - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), td.len, td.data); 1.815 - verify_hl_linked_list_sort(list, td); 1.816 -} 1.817 - 1.818 -void test_hl_ptr_linked_list_sort(void) { 1.819 - struct sort_test_data td = create_sort_test_data(); 1.820 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.821 - cx_for_n (i, td.len) cxListAdd(list, &td.data[i]); 1.822 - verify_hl_linked_list_sort(list, td); 1.823 -} 1.824 - 1.825 -void verify_hl_linked_list_iterator(CxList *list) { 1.826 - int i = 0; 1.827 - CxIterator iter = cxListBegin(list); 1.828 - cx_foreach(int*, x, iter) { 1.829 - CU_ASSERT_EQUAL(iter.index, (size_t) (i + 1) / 2) 1.830 - CU_ASSERT_EQUAL(*x, i) 1.831 - if (*x % 2 == 1) iter.remove = true; 1.832 - i++; 1.833 - } 1.834 - CU_ASSERT_EQUAL(i, 10) 1.835 - CU_ASSERT_EQUAL_FATAL(list->size, 5) 1.836 - cx_for_n(j, 5) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), (int) j * 2) 1.837 - cxListDestroy(list); 1.838 -} 1.839 - 1.840 -void test_hl_linked_list_iterator(void) { 1.841 - CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); 1.842 - cx_for_n (i, 10) cxListAdd(list, &i); 1.843 - verify_hl_linked_list_iterator(list); 1.844 -} 1.845 - 1.846 -void test_hl_ptr_linked_list_iterator(void) { 1.847 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.848 - int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 1.849 - cx_for_n (i, 10) cxListAdd(list, &data[i]); 1.850 - verify_hl_linked_list_iterator(list); 1.851 -} 1.852 - 1.853 -static void verify_hl_linked_list_insert_via_iterator( 1.854 - CxList *list, 1.855 - int *testdata 1.856 -) { 1.857 - CxIterator iter = cxListIterator(list, 2); 1.858 - CU_ASSERT_EQUAL(iter.index, 2) 1.859 - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) 1.860 - size_t i = 4; 1.861 - 1.862 - ++i; 1.863 - cxListInsertAfter(&iter, &testdata[i]); 1.864 - CU_ASSERT_EQUAL(iter.index, 2) 1.865 - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) 1.866 - ++i; 1.867 - cxListInsertBefore(&iter, &testdata[i]); 1.868 - CU_ASSERT_EQUAL(iter.index, 3) 1.869 - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) 1.870 - 1.871 - iter = cxListBegin(list); 1.872 - ++i; 1.873 - cxListInsertBefore(&iter, &testdata[i]); 1.874 - CU_ASSERT_EQUAL(iter.index, 1) 1.875 - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) 1.876 - iter = cxListIterator(list, list->size); 1.877 - ++i; 1.878 - cxListInsertBefore(&iter, &testdata[i]); 1.879 - CU_ASSERT_EQUAL(iter.index, 9) 1.880 - CU_ASSERT_FALSE(cxIteratorValid(&iter)) 1.881 - iter = cxListIterator(list, list->size); 1.882 - ++i; 1.883 - cxListInsertAfter(&iter, &testdata[i]); 1.884 - CU_ASSERT_EQUAL(iter.index, 10) 1.885 - CU_ASSERT_FALSE(cxIteratorValid(&iter)) 1.886 - 1.887 - int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; 1.888 - cx_for_n (j, 10) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), expdata[j]) 1.889 - cxListDestroy(list); 1.890 -} 1.891 - 1.892 -void test_hl_linked_list_insert_via_iterator(void) { 1.893 - int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; 1.894 - // only add the first five elements, the remaining five will be inserted 1.895 - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, testdata); 1.896 - verify_hl_linked_list_insert_via_iterator(list, testdata); 1.897 -} 1.898 - 1.899 -void test_hl_ptr_linked_list_insert_via_iterator(void) { 1.900 - int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; 1.901 - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); 1.902 - // only add the first five elements, the remaining five will be inserted 1.903 - cx_for_n (i, 5) cxListAdd(list, &testdata[i]); 1.904 - verify_hl_linked_list_insert_via_iterator(list, testdata); 1.905 -} 1.906 - 1.907 -static void test_setup_allocator(void) { 1.908 - cxTestingAllocatorReset(); 1.909 -} 1.910 - 1.911 -static void test_verify_allocator(void) { 1.912 - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.913 -} 1.914 - 1.915 -int main() { 1.916 - CU_pSuite suite = NULL; 1.917 - 1.918 - if (CUE_SUCCESS != CU_initialize_registry()) { 1.919 - return CU_get_error(); 1.920 - } 1.921 - 1.922 - suite = CU_add_suite("low level linked list", NULL, NULL); 1.923 - 1.924 - cu_add_test(suite, test_linked_list_link_unlink); 1.925 - cu_add_test(suite, test_linked_list_at); 1.926 - cu_add_test(suite, test_linked_list_find); 1.927 - cu_add_test(suite, test_linked_list_compare); 1.928 - cu_add_test(suite, test_linked_list_prepend); 1.929 - cu_add_test(suite, test_linked_list_add); 1.930 - cu_add_test(suite, test_linked_list_insert); 1.931 - cu_add_test(suite, test_linked_list_insert_chain); 1.932 - cu_add_test(suite, test_linked_list_first); 1.933 - cu_add_test(suite, test_linked_list_last); 1.934 - cu_add_test(suite, test_linked_list_prev); 1.935 - cu_add_test(suite, test_linked_list_remove); 1.936 - cu_add_test(suite, test_linked_list_size); 1.937 - cu_add_test(suite, test_linked_list_sort); 1.938 - cu_add_test(suite, test_linked_list_reverse); 1.939 - 1.940 - suite = CU_add_suite_with_setup_and_teardown( 1.941 - "high level linked list", NULL, NULL, 1.942 - test_setup_allocator, test_verify_allocator); 1.943 - 1.944 - cu_add_test(suite, test_hl_linked_list_create); 1.945 - cu_add_test(suite, test_hl_linked_list_from_array); 1.946 - cu_add_test(suite, test_hl_linked_list_add); 1.947 - cu_add_test(suite, test_hl_linked_list_insert); 1.948 - cu_add_test(suite, test_hl_linked_list_remove); 1.949 - cu_add_test(suite, test_hl_linked_list_at); 1.950 - cu_add_test(suite, test_hl_linked_list_find); 1.951 - cu_add_test(suite, test_hl_linked_list_sort); 1.952 - cu_add_test(suite, test_hl_linked_list_iterator); 1.953 - cu_add_test(suite, test_hl_linked_list_insert_via_iterator); 1.954 - 1.955 - suite = CU_add_suite_with_setup_and_teardown( 1.956 - "high level pointer linked list", NULL, NULL, 1.957 - test_setup_allocator, test_verify_allocator); 1.958 - 1.959 - cu_add_test(suite, test_hl_ptr_linked_list_create); 1.960 - cu_add_test(suite, test_hl_ptr_linked_list_add); 1.961 - cu_add_test(suite, test_hl_ptr_linked_list_insert); 1.962 - cu_add_test(suite, test_hl_ptr_linked_list_remove); 1.963 - cu_add_test(suite, test_hl_ptr_linked_list_at); 1.964 - cu_add_test(suite, test_hl_ptr_linked_list_find); 1.965 - cu_add_test(suite, test_hl_ptr_linked_list_sort); 1.966 - cu_add_test(suite, test_hl_ptr_linked_list_iterator); 1.967 - cu_add_test(suite, test_hl_ptr_linked_list_insert_via_iterator); 1.968 - 1.969 - CU_basic_set_mode(UCX_CU_BRM); 1.970 - 1.971 - int exitcode; 1.972 - if (CU_basic_run_tests()) { 1.973 - exitcode = CU_get_error(); 1.974 - } else { 1.975 - exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; 1.976 - } 1.977 - CU_cleanup_registry(); 1.978 - return exitcode; 1.979 -}