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