Sat, 04 Dec 2021 17:38:23 +0100
add cx_linked_list_first() + cx_linked_list_prepend()
removes concatenating behavior of cx_linked_list_add()
universe@390 | 1 | /* |
universe@390 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@390 | 3 | * |
universe@390 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@390 | 5 | * |
universe@390 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@390 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@390 | 8 | * |
universe@390 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@390 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@390 | 11 | * |
universe@390 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@390 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@390 | 14 | * documentation and/or other materials provided with the distribution. |
universe@390 | 15 | * |
universe@390 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@390 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@390 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@390 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@390 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@390 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@390 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@390 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@390 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@390 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@390 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@390 | 27 | */ |
universe@390 | 28 | |
universe@398 | 29 | #include "cx/linked_list.h" |
universe@411 | 30 | #include "test_config.h" |
universe@422 | 31 | #include "util_allocator.h" |
universe@398 | 32 | |
universe@412 | 33 | int cmp_int(int const *l, int const *r) { |
universe@412 | 34 | int left = *l, right = *r; |
universe@412 | 35 | return left == right ? 0 : (left < right ? -1 : 1); |
universe@412 | 36 | } |
universe@412 | 37 | |
universe@438 | 38 | void test_linked_list_at(void) { |
universe@438 | 39 | struct node { |
universe@438 | 40 | void *next; |
universe@438 | 41 | void *prev; |
universe@438 | 42 | }; |
universe@438 | 43 | const ptrdiff_t loc_prev = offsetof(struct node, prev); |
universe@438 | 44 | const ptrdiff_t loc_next = offsetof(struct node, next); |
universe@438 | 45 | |
universe@438 | 46 | struct node a, b, c, d; |
universe@438 | 47 | a.prev = NULL; |
universe@438 | 48 | a.next = &b; |
universe@438 | 49 | b.prev = &a; |
universe@438 | 50 | b.next = &c; |
universe@438 | 51 | c.prev = &b; |
universe@438 | 52 | c.next = &d; |
universe@438 | 53 | d.prev = &c; |
universe@438 | 54 | d.next = NULL; |
universe@438 | 55 | |
universe@449 | 56 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a) |
universe@449 | 57 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b) |
universe@449 | 58 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c) |
universe@449 | 59 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d) |
universe@449 | 60 | CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)) |
universe@438 | 61 | |
universe@449 | 62 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a) |
universe@449 | 63 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b) |
universe@449 | 64 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c) |
universe@449 | 65 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d) |
universe@449 | 66 | CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)) |
universe@438 | 67 | |
universe@449 | 68 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a) |
universe@449 | 69 | CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b) |
universe@438 | 70 | } |
universe@438 | 71 | |
olaf@444 | 72 | void test_linked_list_add(void) { |
olaf@442 | 73 | struct node { |
olaf@442 | 74 | void *prev; |
olaf@442 | 75 | void *next; |
olaf@442 | 76 | }; |
universe@449 | 77 | |
olaf@442 | 78 | struct node nodes[4]; |
universe@449 | 79 | |
olaf@442 | 80 | // test with begin, end / prev, next |
universe@449 | 81 | memset(nodes, 0, 4 * sizeof(struct node)); |
olaf@442 | 82 | void *begin = NULL; |
olaf@442 | 83 | void *end = NULL; |
universe@449 | 84 | |
olaf@442 | 85 | ptrdiff_t loc_prev = offsetof(struct node, prev); |
olaf@442 | 86 | ptrdiff_t loc_next = offsetof(struct node, next); |
universe@449 | 87 | |
universe@453 | 88 | cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); |
universe@449 | 89 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@449 | 90 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 91 | CU_ASSERT_PTR_NULL(nodes[0].prev) |
universe@475 | 92 | CU_ASSERT_PTR_NULL(nodes[0].next) |
universe@449 | 93 | |
universe@453 | 94 | cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); |
universe@449 | 95 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@449 | 96 | CU_ASSERT_PTR_EQUAL(end, &nodes[1]) |
universe@449 | 97 | CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
universe@449 | 98 | CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) |
universe@449 | 99 | |
olaf@442 | 100 | // test with begin only / prev, next |
universe@449 | 101 | memset(nodes, 0, 4 * sizeof(struct node)); |
olaf@442 | 102 | begin = NULL; |
olaf@442 | 103 | end = NULL; |
universe@449 | 104 | |
universe@453 | 105 | cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); |
universe@449 | 106 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@453 | 107 | cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); |
universe@449 | 108 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@449 | 109 | CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
universe@449 | 110 | CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) |
universe@449 | 111 | |
universe@453 | 112 | cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); |
universe@449 | 113 | CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) |
universe@449 | 114 | CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) |
universe@449 | 115 | |
universe@475 | 116 | // test with end only / prev, next |
universe@475 | 117 | memset(nodes, 0, 4 * sizeof(struct node)); |
universe@475 | 118 | begin = NULL; |
universe@475 | 119 | end = NULL; |
universe@475 | 120 | |
universe@475 | 121 | cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]); |
universe@475 | 122 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 123 | cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]); |
universe@475 | 124 | CU_ASSERT_PTR_EQUAL(end, &nodes[1]) |
universe@475 | 125 | CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
universe@475 | 126 | CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) |
universe@475 | 127 | |
universe@475 | 128 | cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]); |
universe@475 | 129 | CU_ASSERT_PTR_EQUAL(end, &nodes[2]) |
universe@475 | 130 | CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) |
universe@475 | 131 | CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) |
universe@475 | 132 | |
olaf@442 | 133 | // test with begin, end / next |
universe@449 | 134 | memset(nodes, 0, 4 * sizeof(struct node)); |
olaf@442 | 135 | begin = NULL; |
olaf@442 | 136 | end = NULL; |
universe@449 | 137 | |
universe@453 | 138 | cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); |
universe@449 | 139 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@449 | 140 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@453 | 141 | cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); |
universe@449 | 142 | CU_ASSERT_PTR_EQUAL(end, &nodes[1]) |
universe@449 | 143 | CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
universe@449 | 144 | CU_ASSERT_PTR_NULL(nodes[1].prev) |
olaf@442 | 145 | } |
olaf@442 | 146 | |
universe@475 | 147 | void test_linked_list_prepend(void) { |
universe@475 | 148 | struct node { |
universe@475 | 149 | void *prev; |
universe@475 | 150 | void *next; |
universe@475 | 151 | }; |
universe@475 | 152 | |
universe@475 | 153 | struct node nodes[4]; |
universe@475 | 154 | |
universe@475 | 155 | // test with begin, end / prev, next |
universe@475 | 156 | memset(nodes, 0, 4 * sizeof(struct node)); |
universe@475 | 157 | void *begin = NULL; |
universe@475 | 158 | void *end = NULL; |
universe@475 | 159 | |
universe@475 | 160 | ptrdiff_t loc_prev = offsetof(struct node, prev); |
universe@475 | 161 | ptrdiff_t loc_next = offsetof(struct node, next); |
universe@475 | 162 | |
universe@475 | 163 | cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]); |
universe@475 | 164 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@475 | 165 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 166 | CU_ASSERT_PTR_NULL(nodes[0].prev) |
universe@475 | 167 | CU_ASSERT_PTR_NULL(nodes[0].next) |
universe@475 | 168 | |
universe@475 | 169 | cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]); |
universe@475 | 170 | CU_ASSERT_PTR_EQUAL(begin, &nodes[1]) |
universe@475 | 171 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 172 | CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) |
universe@475 | 173 | CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) |
universe@475 | 174 | |
universe@475 | 175 | // test with begin only / prev, next |
universe@475 | 176 | memset(nodes, 0, 4 * sizeof(struct node)); |
universe@475 | 177 | begin = NULL; |
universe@475 | 178 | end = NULL; |
universe@475 | 179 | |
universe@475 | 180 | cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]); |
universe@475 | 181 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@475 | 182 | cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]); |
universe@475 | 183 | CU_ASSERT_PTR_EQUAL(begin, &nodes[1]) |
universe@475 | 184 | CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) |
universe@475 | 185 | CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) |
universe@475 | 186 | |
universe@475 | 187 | cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]); |
universe@475 | 188 | CU_ASSERT_PTR_EQUAL(begin, &nodes[2]) |
universe@475 | 189 | CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) |
universe@475 | 190 | CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2]) |
universe@475 | 191 | |
universe@475 | 192 | // test with end only / prev, next |
universe@475 | 193 | memset(nodes, 0, 4 * sizeof(struct node)); |
universe@475 | 194 | begin = NULL; |
universe@475 | 195 | end = NULL; |
universe@475 | 196 | |
universe@475 | 197 | cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]); |
universe@475 | 198 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 199 | cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]); |
universe@475 | 200 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 201 | CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) |
universe@475 | 202 | CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) |
universe@475 | 203 | |
universe@475 | 204 | cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]); |
universe@475 | 205 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 206 | CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) |
universe@475 | 207 | CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2]) |
universe@475 | 208 | |
universe@475 | 209 | // test with begin, end / next |
universe@475 | 210 | memset(nodes, 0, 4 * sizeof(struct node)); |
universe@475 | 211 | begin = NULL; |
universe@475 | 212 | end = NULL; |
universe@475 | 213 | |
universe@475 | 214 | cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]); |
universe@475 | 215 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@475 | 216 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 217 | cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]); |
universe@475 | 218 | cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]); |
universe@475 | 219 | CU_ASSERT_PTR_EQUAL(begin, &nodes[2]) |
universe@475 | 220 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@475 | 221 | CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) |
universe@475 | 222 | CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) |
universe@475 | 223 | CU_ASSERT_PTR_NULL(nodes[1].prev) |
universe@475 | 224 | CU_ASSERT_PTR_NULL(nodes[0].prev) |
universe@475 | 225 | } |
universe@475 | 226 | |
universe@475 | 227 | void test_linked_list_first(void) { |
universe@475 | 228 | CU_ASSERT_PTR_NULL(cx_linked_list_first(NULL, 0)) |
universe@475 | 229 | |
universe@475 | 230 | struct node { |
universe@475 | 231 | int data; |
universe@475 | 232 | void *prev; |
universe@475 | 233 | }; |
universe@475 | 234 | ptrdiff_t loc = offsetof(struct node, prev); |
universe@475 | 235 | |
universe@475 | 236 | struct node first = {1, NULL}; |
universe@475 | 237 | struct node second = {2, &first}; |
universe@475 | 238 | struct node third = {3, &second}; |
universe@475 | 239 | |
universe@475 | 240 | CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&first, loc), &first) |
universe@475 | 241 | CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&second, loc), &first) |
universe@475 | 242 | CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&third, loc), &first) |
universe@475 | 243 | } |
universe@475 | 244 | |
universe@456 | 245 | void test_linked_list_last(void) { |
universe@456 | 246 | CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0)) |
universe@455 | 247 | |
universe@456 | 248 | struct node { |
universe@456 | 249 | int data; |
universe@456 | 250 | void *next; |
universe@456 | 251 | }; |
universe@456 | 252 | ptrdiff_t loc = offsetof(struct node, next); |
universe@456 | 253 | |
universe@456 | 254 | struct node third = {3, NULL}; |
universe@456 | 255 | struct node second = {2, &third}; |
universe@456 | 256 | struct node first = {1, &second}; |
universe@456 | 257 | |
universe@456 | 258 | CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third) |
universe@456 | 259 | CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third) |
universe@456 | 260 | CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) |
universe@456 | 261 | } |
universe@456 | 262 | |
universe@473 | 263 | void test_linked_list_prev(void) { |
universe@473 | 264 | struct node { |
universe@473 | 265 | void *next; |
universe@473 | 266 | }; |
universe@473 | 267 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 268 | |
universe@473 | 269 | struct node third = {NULL}; |
universe@473 | 270 | struct node second = {&third}; |
universe@473 | 271 | struct node first = {&second}; |
universe@473 | 272 | |
universe@473 | 273 | CU_ASSERT_PTR_NULL(cx_linked_list_prev(&first, loc, &first)) |
universe@473 | 274 | CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &second), &first) |
universe@473 | 275 | CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &third), &second) |
universe@473 | 276 | } |
universe@473 | 277 | |
universe@473 | 278 | void test_linked_list_remove(void) { |
universe@473 | 279 | struct node { |
universe@473 | 280 | void *next; |
universe@473 | 281 | }; |
universe@473 | 282 | struct dnode { |
universe@473 | 283 | void *next; |
universe@473 | 284 | void *prev; |
universe@473 | 285 | }; |
universe@473 | 286 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 287 | ptrdiff_t ploc = offsetof(struct dnode, prev); |
universe@473 | 288 | |
universe@473 | 289 | void *begin; |
universe@473 | 290 | void *end; |
universe@473 | 291 | void *result; |
universe@473 | 292 | |
universe@473 | 293 | // single linked list |
universe@473 | 294 | struct node third = {NULL}; |
universe@473 | 295 | struct node second = {&third}; |
universe@473 | 296 | struct node first = {&second}; |
universe@473 | 297 | begin = &first; |
universe@473 | 298 | |
universe@473 | 299 | result = cx_linked_list_remove(&begin, NULL, -1, loc, &second); |
universe@473 | 300 | CU_ASSERT_PTR_EQUAL(result, &first) |
universe@473 | 301 | CU_ASSERT_PTR_EQUAL(begin, &first) |
universe@473 | 302 | CU_ASSERT_PTR_EQUAL(first.next, &third) |
universe@473 | 303 | CU_ASSERT_PTR_NULL(second.next) |
universe@473 | 304 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 305 | |
universe@473 | 306 | result = cx_linked_list_remove(&begin, NULL, -1, loc, &first); |
universe@473 | 307 | CU_ASSERT_PTR_EQUAL(result, &third) |
universe@473 | 308 | CU_ASSERT_PTR_EQUAL(begin, &third) |
universe@473 | 309 | CU_ASSERT_PTR_NULL(first.next) |
universe@473 | 310 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 311 | |
universe@473 | 312 | result = cx_linked_list_remove(&begin, NULL, -1, loc, &third); |
universe@473 | 313 | CU_ASSERT_PTR_NULL(result) |
universe@473 | 314 | CU_ASSERT_PTR_NULL(begin) |
universe@473 | 315 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 316 | |
universe@473 | 317 | // doubly linked list |
universe@473 | 318 | struct dnode dthird = {NULL , NULL}; |
universe@473 | 319 | struct dnode dsecond = {&dthird, NULL}; |
universe@473 | 320 | struct dnode dfirst = {&dsecond, NULL}; |
universe@473 | 321 | dthird.prev = &dsecond; |
universe@473 | 322 | dsecond.prev = &dfirst; |
universe@473 | 323 | begin = &dfirst; |
universe@473 | 324 | end = &dthird; |
universe@473 | 325 | |
universe@473 | 326 | result = cx_linked_list_remove(&begin, &end, ploc, loc, &dsecond); |
universe@473 | 327 | CU_ASSERT_PTR_EQUAL(result, &dfirst) |
universe@473 | 328 | CU_ASSERT_PTR_EQUAL(begin, &dfirst) |
universe@473 | 329 | CU_ASSERT_PTR_EQUAL(end, &dthird) |
universe@473 | 330 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 331 | CU_ASSERT_PTR_EQUAL(dfirst.next, &dthird) |
universe@473 | 332 | CU_ASSERT_PTR_NULL(dsecond.prev) |
universe@473 | 333 | CU_ASSERT_PTR_NULL(dsecond.next) |
universe@473 | 334 | CU_ASSERT_PTR_EQUAL(dthird.prev, &dfirst) |
universe@473 | 335 | CU_ASSERT_PTR_NULL(dthird.next) |
universe@473 | 336 | |
universe@473 | 337 | result = cx_linked_list_remove(&begin, &end, ploc, loc, &dthird); |
universe@473 | 338 | CU_ASSERT_PTR_EQUAL(result, &dfirst) |
universe@473 | 339 | CU_ASSERT_PTR_EQUAL(begin, &dfirst) |
universe@473 | 340 | CU_ASSERT_PTR_EQUAL(end, &dfirst) |
universe@473 | 341 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 342 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 343 | CU_ASSERT_PTR_NULL(dthird.prev) |
universe@473 | 344 | CU_ASSERT_PTR_NULL(dthird.next) |
universe@473 | 345 | |
universe@473 | 346 | result = cx_linked_list_remove(&begin, &end, ploc, loc, &dfirst); |
universe@473 | 347 | CU_ASSERT_PTR_NULL(result) |
universe@473 | 348 | CU_ASSERT_PTR_NULL(begin) |
universe@473 | 349 | CU_ASSERT_PTR_NULL(end) |
universe@473 | 350 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 351 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 352 | } |
universe@473 | 353 | |
universe@468 | 354 | void test_linked_list_size(void) { |
universe@468 | 355 | struct node { |
universe@468 | 356 | void *next; |
universe@468 | 357 | }; |
universe@468 | 358 | ptrdiff_t loc = offsetof(struct node, next); |
universe@468 | 359 | |
universe@468 | 360 | struct node first = {NULL}; |
universe@468 | 361 | struct node second = {NULL}; |
universe@468 | 362 | struct node third = {NULL}; |
universe@468 | 363 | |
universe@468 | 364 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0) |
universe@468 | 365 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1) |
universe@468 | 366 | first.next = &second; |
universe@468 | 367 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2) |
universe@468 | 368 | second.next = &third; |
universe@468 | 369 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3) |
universe@468 | 370 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2) |
universe@468 | 371 | } |
universe@468 | 372 | |
universe@468 | 373 | void test_linked_list_sort(void) { |
universe@468 | 374 | struct node { |
universe@468 | 375 | void *prev; |
universe@468 | 376 | void *next; |
universe@468 | 377 | int data; |
universe@468 | 378 | }; |
universe@468 | 379 | |
universe@468 | 380 | int expected[] = { |
universe@468 | 381 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@468 | 382 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@468 | 383 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@468 | 384 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@468 | 385 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@468 | 386 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@468 | 387 | }; |
universe@468 | 388 | int scrambled[] = { |
universe@468 | 389 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@468 | 390 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@468 | 391 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@468 | 392 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@468 | 393 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@468 | 394 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@468 | 395 | }; |
universe@468 | 396 | |
universe@468 | 397 | struct node *nodes = calloc(100, sizeof(struct node)); |
universe@468 | 398 | for (int i = 0; i < 100; i++) { |
universe@468 | 399 | nodes[i].prev = i == 0 ? NULL : &nodes[i - 1]; |
universe@468 | 400 | nodes[i].next = i == 99 ? NULL : &nodes[i + 1]; |
universe@468 | 401 | nodes[i].data = scrambled[i]; |
universe@468 | 402 | } |
universe@468 | 403 | |
universe@468 | 404 | struct node *begin = &nodes[0]; |
universe@468 | 405 | struct node *end = &nodes[99]; |
universe@468 | 406 | |
universe@468 | 407 | cx_linked_list_sort((void **) &begin, (void **) &end, |
universe@468 | 408 | offsetof(struct node, prev), |
universe@468 | 409 | offsetof(struct node, next), |
universe@468 | 410 | offsetof(struct node, data), |
universe@468 | 411 | 0, (CxListComparator) cmp_int); |
universe@468 | 412 | |
universe@468 | 413 | CU_ASSERT_PTR_NULL(begin->prev) |
universe@468 | 414 | CU_ASSERT_EQUAL(begin->data, expected[0]) |
universe@468 | 415 | struct node *check = begin; |
universe@468 | 416 | struct node *check_last = NULL; |
universe@473 | 417 | for (int i = 0; i < 100; i++) { |
universe@468 | 418 | CU_ASSERT_EQUAL(check->data, expected[i]) |
universe@468 | 419 | CU_ASSERT_PTR_EQUAL(check->prev, check_last) |
universe@468 | 420 | if (i < 99) { |
universe@468 | 421 | CU_ASSERT_PTR_NOT_NULL(check->next) |
universe@468 | 422 | } |
universe@468 | 423 | check_last = check; |
universe@468 | 424 | check = check->next; |
universe@468 | 425 | } |
universe@468 | 426 | CU_ASSERT_PTR_NULL(check) |
universe@468 | 427 | CU_ASSERT_EQUAL(end->data, expected[99]) |
universe@468 | 428 | } |
universe@468 | 429 | |
universe@473 | 430 | void test_linked_list_reverse(void) { |
universe@473 | 431 | struct node { |
universe@473 | 432 | void *next; |
universe@473 | 433 | }; |
universe@473 | 434 | struct dnode { |
universe@473 | 435 | void *next; |
universe@473 | 436 | void *prev; |
universe@473 | 437 | }; |
universe@473 | 438 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 439 | ptrdiff_t ploc = offsetof(struct dnode, prev); |
universe@473 | 440 | |
universe@473 | 441 | void *begin; |
universe@473 | 442 | void *end; |
universe@473 | 443 | |
universe@473 | 444 | // single linked list |
universe@473 | 445 | struct node third = {NULL}; |
universe@473 | 446 | struct node second = {&third}; |
universe@473 | 447 | struct node first = {&second}; |
universe@473 | 448 | begin = &first; |
universe@473 | 449 | |
universe@473 | 450 | cx_linked_list_reverse(&begin, NULL, -1, loc); |
universe@473 | 451 | CU_ASSERT_PTR_EQUAL(begin, &third) |
universe@473 | 452 | CU_ASSERT_PTR_EQUAL(third.next, &second) |
universe@473 | 453 | CU_ASSERT_PTR_EQUAL(second.next, &first) |
universe@473 | 454 | CU_ASSERT_PTR_NULL(first.next) |
universe@473 | 455 | |
universe@473 | 456 | // doubly linked list |
universe@473 | 457 | struct dnode dthird = {NULL , NULL}; |
universe@473 | 458 | struct dnode dsecond = {&dthird, NULL}; |
universe@473 | 459 | struct dnode dfirst = {&dsecond, NULL}; |
universe@473 | 460 | dthird.prev = &dsecond; |
universe@473 | 461 | dsecond.prev = &dfirst; |
universe@473 | 462 | begin = &dfirst; |
universe@473 | 463 | end = &dthird; |
universe@473 | 464 | |
universe@473 | 465 | cx_linked_list_reverse(&begin, &end, ploc, loc); |
universe@473 | 466 | CU_ASSERT_PTR_EQUAL(begin, &dthird) |
universe@473 | 467 | CU_ASSERT_PTR_EQUAL(end, &dfirst) |
universe@473 | 468 | CU_ASSERT_PTR_EQUAL(dthird.next, &dsecond) |
universe@473 | 469 | CU_ASSERT_PTR_EQUAL(dsecond.next, &dfirst) |
universe@473 | 470 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 471 | CU_ASSERT_PTR_NULL(dthird.prev) |
universe@473 | 472 | CU_ASSERT_PTR_EQUAL(dsecond.prev, &dthird) |
universe@473 | 473 | CU_ASSERT_PTR_EQUAL(dfirst.prev, &dsecond) |
universe@473 | 474 | } |
universe@456 | 475 | |
universe@456 | 476 | void test_hl_linked_list_create(void) { |
universe@455 | 477 | cxTestingAllocatorReset(); |
universe@455 | 478 | |
universe@455 | 479 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@455 | 480 | |
universe@455 | 481 | CU_ASSERT_EQUAL(list->size, 0) |
universe@455 | 482 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@455 | 483 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@455 | 484 | CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) |
universe@455 | 485 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@455 | 486 | |
universe@455 | 487 | cxLinkedListDestroy(list); |
universe@455 | 488 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@455 | 489 | } |
universe@455 | 490 | |
universe@459 | 491 | void test_hl_linked_list_add(void) { |
universe@459 | 492 | cxTestingAllocatorReset(); |
universe@459 | 493 | |
universe@459 | 494 | int data; |
universe@459 | 495 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 496 | |
universe@459 | 497 | data = 5; |
universe@460 | 498 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 499 | data = 47; |
universe@460 | 500 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 501 | data = 13; |
universe@460 | 502 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 503 | |
universe@459 | 504 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 505 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 506 | |
universe@466 | 507 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 508 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 509 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 510 | |
universe@459 | 511 | cxLinkedListDestroy(list); |
universe@459 | 512 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 513 | } |
universe@459 | 514 | |
universe@459 | 515 | void test_hl_linked_list_insert(void) { |
universe@459 | 516 | cxTestingAllocatorReset(); |
universe@459 | 517 | |
universe@459 | 518 | int data; |
universe@459 | 519 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 520 | |
universe@459 | 521 | data = 5; |
universe@460 | 522 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 523 | CU_ASSERT_EQUAL(list->size, 0) |
universe@460 | 524 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 525 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 526 | data = 47; |
universe@460 | 527 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 528 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 529 | data = 13; |
universe@460 | 530 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 531 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 532 | data = 42; |
universe@460 | 533 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0) |
universe@459 | 534 | |
universe@459 | 535 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 536 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 537 | |
universe@466 | 538 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 539 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 540 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 541 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@459 | 542 | |
universe@459 | 543 | cxLinkedListDestroy(list); |
universe@459 | 544 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 545 | } |
universe@459 | 546 | |
universe@459 | 547 | void test_hl_linked_list_remove(void) { |
universe@459 | 548 | cxTestingAllocatorReset(); |
universe@459 | 549 | |
universe@459 | 550 | int data; |
universe@459 | 551 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 552 | |
universe@459 | 553 | data = 5; |
universe@460 | 554 | cxListAdd(list, &data); |
universe@459 | 555 | data = 47; |
universe@460 | 556 | cxListAdd(list, &data); |
universe@459 | 557 | data = 42; |
universe@460 | 558 | cxListAdd(list, &data); |
universe@459 | 559 | data = 13; |
universe@460 | 560 | cxListAdd(list, &data); |
universe@459 | 561 | |
universe@459 | 562 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 563 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 564 | |
universe@459 | 565 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@459 | 566 | |
universe@459 | 567 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@459 | 568 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 569 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 570 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 571 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 572 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 573 | |
universe@459 | 574 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 575 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 576 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 577 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 578 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@459 | 579 | |
universe@459 | 580 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@459 | 581 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 582 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 583 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@459 | 584 | |
universe@459 | 585 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 586 | CU_ASSERT_EQUAL(list->size, 0) |
universe@459 | 587 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 588 | |
universe@459 | 589 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 590 | |
universe@459 | 591 | cxLinkedListDestroy(list); |
universe@459 | 592 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 593 | } |
universe@459 | 594 | |
universe@459 | 595 | void test_hl_linked_list_find(void) { |
universe@459 | 596 | cxTestingAllocatorReset(); |
universe@459 | 597 | |
universe@459 | 598 | int data, criteria; |
universe@459 | 599 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 600 | |
universe@459 | 601 | data = 5; |
universe@460 | 602 | cxListAdd(list, &data); |
universe@459 | 603 | data = 47; |
universe@460 | 604 | cxListAdd(list, &data); |
universe@459 | 605 | data = 13; |
universe@460 | 606 | cxListAdd(list, &data); |
universe@459 | 607 | |
universe@459 | 608 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 609 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 610 | |
universe@459 | 611 | criteria = 5; |
universe@460 | 612 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@459 | 613 | criteria = 47; |
universe@460 | 614 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@459 | 615 | criteria = 13; |
universe@460 | 616 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@459 | 617 | criteria = 9000; |
universe@460 | 618 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 619 | criteria = -5; |
universe@460 | 620 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 621 | |
universe@459 | 622 | cxLinkedListDestroy(list); |
universe@459 | 623 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 624 | } |
universe@459 | 625 | |
universe@469 | 626 | void test_hl_linked_list_sort(void) { |
universe@469 | 627 | int expected[] = { |
universe@469 | 628 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 629 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 630 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 631 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 632 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 633 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 634 | }; |
universe@469 | 635 | int scrambled[] = { |
universe@469 | 636 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 637 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 638 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 639 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 640 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 641 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 642 | }; |
universe@469 | 643 | |
universe@469 | 644 | cxTestingAllocatorReset(); |
universe@469 | 645 | |
universe@469 | 646 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@469 | 647 | |
universe@473 | 648 | for (int i = 0; i < 100; i++) { |
universe@469 | 649 | cxListAdd(list, &scrambled[i]); |
universe@469 | 650 | } |
universe@469 | 651 | |
universe@469 | 652 | cxListSort(list); |
universe@469 | 653 | |
universe@473 | 654 | for (int i = 0; i < 100; i++) { |
universe@473 | 655 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 656 | } |
universe@469 | 657 | |
universe@469 | 658 | cxLinkedListDestroy(list); |
universe@469 | 659 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 660 | } |
universe@469 | 661 | |
universe@466 | 662 | void test_hl_ptr_linked_list_create(void) { |
universe@466 | 663 | cxTestingAllocatorReset(); |
universe@466 | 664 | |
universe@466 | 665 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 666 | |
universe@466 | 667 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 668 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@466 | 669 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@466 | 670 | CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) |
universe@466 | 671 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@466 | 672 | |
universe@466 | 673 | cxLinkedListDestroy(list); |
universe@466 | 674 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 675 | } |
universe@466 | 676 | |
universe@466 | 677 | void test_hl_ptr_linked_list_add(void) { |
universe@466 | 678 | cxTestingAllocatorReset(); |
universe@466 | 679 | |
universe@466 | 680 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 681 | |
universe@466 | 682 | int a = 5, b = 47, c = 13; |
universe@466 | 683 | |
universe@466 | 684 | CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) |
universe@466 | 685 | CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) |
universe@466 | 686 | CU_ASSERT_EQUAL(cxListAdd(list, &c), 0) |
universe@466 | 687 | |
universe@466 | 688 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 689 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 690 | |
universe@466 | 691 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 692 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 693 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 694 | |
universe@466 | 695 | a = 9; |
universe@466 | 696 | b = 10; |
universe@466 | 697 | c = 11; |
universe@466 | 698 | |
universe@466 | 699 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) |
universe@466 | 700 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) |
universe@466 | 701 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) |
universe@466 | 702 | |
universe@466 | 703 | cxLinkedListDestroy(list); |
universe@466 | 704 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 705 | } |
universe@466 | 706 | |
universe@466 | 707 | void test_hl_ptr_linked_list_insert(void) { |
universe@466 | 708 | cxTestingAllocatorReset(); |
universe@466 | 709 | |
universe@466 | 710 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 711 | |
universe@466 | 712 | int a = 5, b = 47, c = 13, d = 42; |
universe@466 | 713 | |
universe@466 | 714 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) |
universe@466 | 715 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 716 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) |
universe@466 | 717 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 718 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) |
universe@466 | 719 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 720 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) |
universe@466 | 721 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 722 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) |
universe@466 | 723 | |
universe@466 | 724 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 725 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 726 | |
universe@466 | 727 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 728 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 729 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 730 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@466 | 731 | |
universe@466 | 732 | cxLinkedListDestroy(list); |
universe@466 | 733 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 734 | } |
universe@466 | 735 | |
universe@466 | 736 | void test_hl_ptr_linked_list_remove(void) { |
universe@466 | 737 | cxTestingAllocatorReset(); |
universe@466 | 738 | |
universe@466 | 739 | int a = 5, b = 47, c = 42, d = 13; |
universe@466 | 740 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 741 | |
universe@466 | 742 | cxListAdd(list, &a); |
universe@466 | 743 | cxListAdd(list, &b); |
universe@466 | 744 | cxListAdd(list, &c); |
universe@466 | 745 | cxListAdd(list, &d); |
universe@466 | 746 | |
universe@466 | 747 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 748 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 749 | |
universe@466 | 750 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@466 | 751 | |
universe@466 | 752 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@466 | 753 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 754 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 755 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 756 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 757 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 758 | |
universe@466 | 759 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 760 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 761 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 762 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 763 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 764 | |
universe@466 | 765 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@466 | 766 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 767 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 768 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 769 | |
universe@466 | 770 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 771 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 772 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 773 | |
universe@466 | 774 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 775 | |
universe@466 | 776 | cxLinkedListDestroy(list); |
universe@466 | 777 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 778 | } |
universe@466 | 779 | |
universe@466 | 780 | void test_hl_ptr_linked_list_find(void) { |
universe@466 | 781 | cxTestingAllocatorReset(); |
universe@466 | 782 | |
universe@466 | 783 | int a = 5, b = 47, c = 13, criteria; |
universe@466 | 784 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 785 | |
universe@466 | 786 | cxListAdd(list, &a); |
universe@466 | 787 | cxListAdd(list, &b); |
universe@466 | 788 | cxListAdd(list, &c); |
universe@466 | 789 | |
universe@466 | 790 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 791 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 792 | |
universe@466 | 793 | criteria = 5; |
universe@466 | 794 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@466 | 795 | criteria = 47; |
universe@466 | 796 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 797 | criteria = 13; |
universe@466 | 798 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@466 | 799 | criteria = 9000; |
universe@466 | 800 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 801 | criteria = -5; |
universe@466 | 802 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 803 | b = -5; |
universe@466 | 804 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 805 | |
universe@466 | 806 | cxLinkedListDestroy(list); |
universe@466 | 807 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 808 | } |
universe@466 | 809 | |
universe@469 | 810 | void test_hl_ptr_linked_list_sort(void) { |
universe@469 | 811 | int expected[] = { |
universe@469 | 812 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 813 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 814 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 815 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 816 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 817 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 818 | }; |
universe@469 | 819 | int scrambled[] = { |
universe@469 | 820 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 821 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 822 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 823 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 824 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 825 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 826 | }; |
universe@469 | 827 | |
universe@469 | 828 | cxTestingAllocatorReset(); |
universe@469 | 829 | |
universe@469 | 830 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@469 | 831 | |
universe@473 | 832 | for (int i = 0; i < 100; i++) { |
universe@469 | 833 | cxListAdd(list, &scrambled[i]); |
universe@469 | 834 | } |
universe@469 | 835 | |
universe@469 | 836 | cxListSort(list); |
universe@469 | 837 | |
universe@473 | 838 | for (int i = 0; i < 100; i++) { |
universe@473 | 839 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 840 | } |
universe@469 | 841 | |
universe@469 | 842 | cxLinkedListDestroy(list); |
universe@469 | 843 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 844 | } |
universe@469 | 845 | |
universe@390 | 846 | int main() { |
universe@411 | 847 | CU_pSuite suite = NULL; |
universe@449 | 848 | |
universe@411 | 849 | if (CUE_SUCCESS != CU_initialize_registry()) { |
universe@411 | 850 | return CU_get_error(); |
universe@411 | 851 | } |
universe@411 | 852 | |
universe@459 | 853 | suite = CU_add_suite("low level linked list", NULL, NULL); |
universe@449 | 854 | |
universe@455 | 855 | cu_add_test(suite, test_linked_list_at); |
universe@475 | 856 | cu_add_test(suite, test_linked_list_prepend); |
universe@455 | 857 | cu_add_test(suite, test_linked_list_add); |
universe@475 | 858 | cu_add_test(suite, test_linked_list_first); |
universe@456 | 859 | cu_add_test(suite, test_linked_list_last); |
universe@473 | 860 | cu_add_test(suite, test_linked_list_prev); |
universe@473 | 861 | cu_add_test(suite, test_linked_list_remove); |
universe@468 | 862 | cu_add_test(suite, test_linked_list_size); |
universe@468 | 863 | cu_add_test(suite, test_linked_list_sort); |
universe@473 | 864 | cu_add_test(suite, test_linked_list_reverse); |
universe@455 | 865 | |
universe@459 | 866 | suite = CU_add_suite("high level linked list", NULL, NULL); |
universe@455 | 867 | |
universe@456 | 868 | cu_add_test(suite, test_hl_linked_list_create); |
universe@456 | 869 | cu_add_test(suite, test_hl_linked_list_add); |
universe@456 | 870 | cu_add_test(suite, test_hl_linked_list_insert); |
universe@456 | 871 | cu_add_test(suite, test_hl_linked_list_remove); |
universe@459 | 872 | cu_add_test(suite, test_hl_linked_list_find); |
universe@469 | 873 | cu_add_test(suite, test_hl_linked_list_sort); |
universe@413 | 874 | |
universe@466 | 875 | suite = CU_add_suite("high level pointer linked list", NULL, NULL); |
universe@466 | 876 | |
universe@466 | 877 | cu_add_test(suite, test_hl_ptr_linked_list_create); |
universe@466 | 878 | cu_add_test(suite, test_hl_ptr_linked_list_add); |
universe@466 | 879 | cu_add_test(suite, test_hl_ptr_linked_list_insert); |
universe@466 | 880 | cu_add_test(suite, test_hl_ptr_linked_list_remove); |
universe@466 | 881 | cu_add_test(suite, test_hl_ptr_linked_list_find); |
universe@469 | 882 | cu_add_test(suite, test_hl_ptr_linked_list_sort); |
universe@466 | 883 | |
universe@411 | 884 | CU_basic_set_mode(UCX_CU_BRM); |
universe@411 | 885 | |
universe@411 | 886 | int exitcode; |
universe@411 | 887 | if (CU_basic_run_tests()) { |
universe@411 | 888 | exitcode = CU_get_error(); |
universe@411 | 889 | } else { |
universe@411 | 890 | exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; |
universe@411 | 891 | } |
universe@411 | 892 | CU_cleanup_registry(); |
universe@411 | 893 | return exitcode; |
universe@390 | 894 | } |