Mon, 20 Dec 2021 11:17:06 +0100
change contract of cx_linked_list_remove()
also use cx_linked_list_remove() in high level API
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 | |
universe@473 | 292 | // single linked list |
universe@473 | 293 | struct node third = {NULL}; |
universe@473 | 294 | struct node second = {&third}; |
universe@473 | 295 | struct node first = {&second}; |
universe@473 | 296 | begin = &first; |
universe@473 | 297 | |
universe@476 | 298 | cx_linked_list_remove(&begin, NULL, -1, loc, &second); |
universe@473 | 299 | CU_ASSERT_PTR_EQUAL(begin, &first) |
universe@473 | 300 | CU_ASSERT_PTR_EQUAL(first.next, &third) |
universe@473 | 301 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 302 | |
universe@476 | 303 | cx_linked_list_remove(&begin, NULL, -1, loc, &first); |
universe@473 | 304 | CU_ASSERT_PTR_EQUAL(begin, &third) |
universe@473 | 305 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 306 | |
universe@476 | 307 | cx_linked_list_remove(&begin, NULL, -1, loc, &third); |
universe@473 | 308 | CU_ASSERT_PTR_NULL(begin) |
universe@473 | 309 | |
universe@473 | 310 | // doubly linked list |
universe@473 | 311 | struct dnode dthird = {NULL , NULL}; |
universe@473 | 312 | struct dnode dsecond = {&dthird, NULL}; |
universe@473 | 313 | struct dnode dfirst = {&dsecond, NULL}; |
universe@473 | 314 | dthird.prev = &dsecond; |
universe@473 | 315 | dsecond.prev = &dfirst; |
universe@473 | 316 | begin = &dfirst; |
universe@473 | 317 | end = &dthird; |
universe@473 | 318 | |
universe@476 | 319 | cx_linked_list_remove(&begin, &end, ploc, loc, &dsecond); |
universe@473 | 320 | CU_ASSERT_PTR_EQUAL(begin, &dfirst) |
universe@473 | 321 | CU_ASSERT_PTR_EQUAL(end, &dthird) |
universe@473 | 322 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 323 | CU_ASSERT_PTR_EQUAL(dfirst.next, &dthird) |
universe@473 | 324 | CU_ASSERT_PTR_EQUAL(dthird.prev, &dfirst) |
universe@473 | 325 | CU_ASSERT_PTR_NULL(dthird.next) |
universe@473 | 326 | |
universe@476 | 327 | cx_linked_list_remove(&begin, &end, ploc, loc, &dthird); |
universe@473 | 328 | CU_ASSERT_PTR_EQUAL(begin, &dfirst) |
universe@473 | 329 | CU_ASSERT_PTR_EQUAL(end, &dfirst) |
universe@473 | 330 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 331 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 332 | |
universe@476 | 333 | cx_linked_list_remove(&begin, &end, ploc, loc, &dfirst); |
universe@473 | 334 | CU_ASSERT_PTR_NULL(begin) |
universe@473 | 335 | CU_ASSERT_PTR_NULL(end) |
universe@473 | 336 | } |
universe@473 | 337 | |
universe@468 | 338 | void test_linked_list_size(void) { |
universe@468 | 339 | struct node { |
universe@468 | 340 | void *next; |
universe@468 | 341 | }; |
universe@468 | 342 | ptrdiff_t loc = offsetof(struct node, next); |
universe@468 | 343 | |
universe@468 | 344 | struct node first = {NULL}; |
universe@468 | 345 | struct node second = {NULL}; |
universe@468 | 346 | struct node third = {NULL}; |
universe@468 | 347 | |
universe@468 | 348 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0) |
universe@468 | 349 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1) |
universe@468 | 350 | first.next = &second; |
universe@468 | 351 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2) |
universe@468 | 352 | second.next = &third; |
universe@468 | 353 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3) |
universe@468 | 354 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2) |
universe@468 | 355 | } |
universe@468 | 356 | |
universe@468 | 357 | void test_linked_list_sort(void) { |
universe@468 | 358 | struct node { |
universe@468 | 359 | void *prev; |
universe@468 | 360 | void *next; |
universe@468 | 361 | int data; |
universe@468 | 362 | }; |
universe@468 | 363 | |
universe@468 | 364 | int expected[] = { |
universe@468 | 365 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@468 | 366 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@468 | 367 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@468 | 368 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@468 | 369 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@468 | 370 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@468 | 371 | }; |
universe@468 | 372 | int scrambled[] = { |
universe@468 | 373 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@468 | 374 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@468 | 375 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@468 | 376 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@468 | 377 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@468 | 378 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@468 | 379 | }; |
universe@468 | 380 | |
universe@468 | 381 | struct node *nodes = calloc(100, sizeof(struct node)); |
universe@468 | 382 | for (int i = 0; i < 100; i++) { |
universe@468 | 383 | nodes[i].prev = i == 0 ? NULL : &nodes[i - 1]; |
universe@468 | 384 | nodes[i].next = i == 99 ? NULL : &nodes[i + 1]; |
universe@468 | 385 | nodes[i].data = scrambled[i]; |
universe@468 | 386 | } |
universe@468 | 387 | |
universe@468 | 388 | struct node *begin = &nodes[0]; |
universe@468 | 389 | struct node *end = &nodes[99]; |
universe@468 | 390 | |
universe@468 | 391 | cx_linked_list_sort((void **) &begin, (void **) &end, |
universe@468 | 392 | offsetof(struct node, prev), |
universe@468 | 393 | offsetof(struct node, next), |
universe@468 | 394 | offsetof(struct node, data), |
universe@468 | 395 | 0, (CxListComparator) cmp_int); |
universe@468 | 396 | |
universe@468 | 397 | CU_ASSERT_PTR_NULL(begin->prev) |
universe@468 | 398 | CU_ASSERT_EQUAL(begin->data, expected[0]) |
universe@468 | 399 | struct node *check = begin; |
universe@468 | 400 | struct node *check_last = NULL; |
universe@473 | 401 | for (int i = 0; i < 100; i++) { |
universe@468 | 402 | CU_ASSERT_EQUAL(check->data, expected[i]) |
universe@468 | 403 | CU_ASSERT_PTR_EQUAL(check->prev, check_last) |
universe@468 | 404 | if (i < 99) { |
universe@468 | 405 | CU_ASSERT_PTR_NOT_NULL(check->next) |
universe@468 | 406 | } |
universe@468 | 407 | check_last = check; |
universe@468 | 408 | check = check->next; |
universe@468 | 409 | } |
universe@468 | 410 | CU_ASSERT_PTR_NULL(check) |
universe@468 | 411 | CU_ASSERT_EQUAL(end->data, expected[99]) |
universe@468 | 412 | } |
universe@468 | 413 | |
universe@473 | 414 | void test_linked_list_reverse(void) { |
universe@473 | 415 | struct node { |
universe@473 | 416 | void *next; |
universe@473 | 417 | }; |
universe@473 | 418 | struct dnode { |
universe@473 | 419 | void *next; |
universe@473 | 420 | void *prev; |
universe@473 | 421 | }; |
universe@473 | 422 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 423 | ptrdiff_t ploc = offsetof(struct dnode, prev); |
universe@473 | 424 | |
universe@473 | 425 | void *begin; |
universe@473 | 426 | void *end; |
universe@473 | 427 | |
universe@473 | 428 | // single linked list |
universe@473 | 429 | struct node third = {NULL}; |
universe@473 | 430 | struct node second = {&third}; |
universe@473 | 431 | struct node first = {&second}; |
universe@473 | 432 | begin = &first; |
universe@473 | 433 | |
universe@473 | 434 | cx_linked_list_reverse(&begin, NULL, -1, loc); |
universe@473 | 435 | CU_ASSERT_PTR_EQUAL(begin, &third) |
universe@473 | 436 | CU_ASSERT_PTR_EQUAL(third.next, &second) |
universe@473 | 437 | CU_ASSERT_PTR_EQUAL(second.next, &first) |
universe@473 | 438 | CU_ASSERT_PTR_NULL(first.next) |
universe@473 | 439 | |
universe@473 | 440 | // doubly linked list |
universe@473 | 441 | struct dnode dthird = {NULL , NULL}; |
universe@473 | 442 | struct dnode dsecond = {&dthird, NULL}; |
universe@473 | 443 | struct dnode dfirst = {&dsecond, NULL}; |
universe@473 | 444 | dthird.prev = &dsecond; |
universe@473 | 445 | dsecond.prev = &dfirst; |
universe@473 | 446 | begin = &dfirst; |
universe@473 | 447 | end = &dthird; |
universe@473 | 448 | |
universe@473 | 449 | cx_linked_list_reverse(&begin, &end, ploc, loc); |
universe@473 | 450 | CU_ASSERT_PTR_EQUAL(begin, &dthird) |
universe@473 | 451 | CU_ASSERT_PTR_EQUAL(end, &dfirst) |
universe@473 | 452 | CU_ASSERT_PTR_EQUAL(dthird.next, &dsecond) |
universe@473 | 453 | CU_ASSERT_PTR_EQUAL(dsecond.next, &dfirst) |
universe@473 | 454 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 455 | CU_ASSERT_PTR_NULL(dthird.prev) |
universe@473 | 456 | CU_ASSERT_PTR_EQUAL(dsecond.prev, &dthird) |
universe@473 | 457 | CU_ASSERT_PTR_EQUAL(dfirst.prev, &dsecond) |
universe@473 | 458 | } |
universe@456 | 459 | |
universe@456 | 460 | void test_hl_linked_list_create(void) { |
universe@455 | 461 | cxTestingAllocatorReset(); |
universe@455 | 462 | |
universe@455 | 463 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@455 | 464 | |
universe@455 | 465 | CU_ASSERT_EQUAL(list->size, 0) |
universe@455 | 466 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@455 | 467 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@455 | 468 | CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) |
universe@455 | 469 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@455 | 470 | |
universe@455 | 471 | cxLinkedListDestroy(list); |
universe@455 | 472 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@455 | 473 | } |
universe@455 | 474 | |
universe@459 | 475 | void test_hl_linked_list_add(void) { |
universe@459 | 476 | cxTestingAllocatorReset(); |
universe@459 | 477 | |
universe@459 | 478 | int data; |
universe@459 | 479 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 480 | |
universe@459 | 481 | data = 5; |
universe@460 | 482 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 483 | data = 47; |
universe@460 | 484 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 485 | data = 13; |
universe@460 | 486 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 487 | |
universe@459 | 488 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 489 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 490 | |
universe@466 | 491 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 492 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 493 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 494 | |
universe@459 | 495 | cxLinkedListDestroy(list); |
universe@459 | 496 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 497 | } |
universe@459 | 498 | |
universe@459 | 499 | void test_hl_linked_list_insert(void) { |
universe@459 | 500 | cxTestingAllocatorReset(); |
universe@459 | 501 | |
universe@459 | 502 | int data; |
universe@459 | 503 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 504 | |
universe@459 | 505 | data = 5; |
universe@460 | 506 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 507 | CU_ASSERT_EQUAL(list->size, 0) |
universe@460 | 508 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 509 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 510 | data = 47; |
universe@460 | 511 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 512 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 513 | data = 13; |
universe@460 | 514 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 515 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 516 | data = 42; |
universe@460 | 517 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0) |
universe@459 | 518 | |
universe@459 | 519 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 520 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 521 | |
universe@466 | 522 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 523 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 524 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 525 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@459 | 526 | |
universe@459 | 527 | cxLinkedListDestroy(list); |
universe@459 | 528 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 529 | } |
universe@459 | 530 | |
universe@459 | 531 | void test_hl_linked_list_remove(void) { |
universe@459 | 532 | cxTestingAllocatorReset(); |
universe@459 | 533 | |
universe@459 | 534 | int data; |
universe@459 | 535 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 536 | |
universe@459 | 537 | data = 5; |
universe@460 | 538 | cxListAdd(list, &data); |
universe@459 | 539 | data = 47; |
universe@460 | 540 | cxListAdd(list, &data); |
universe@459 | 541 | data = 42; |
universe@460 | 542 | cxListAdd(list, &data); |
universe@459 | 543 | data = 13; |
universe@460 | 544 | cxListAdd(list, &data); |
universe@459 | 545 | |
universe@459 | 546 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 547 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 548 | |
universe@459 | 549 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@459 | 550 | |
universe@459 | 551 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@459 | 552 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 553 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 554 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 555 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 556 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 557 | |
universe@459 | 558 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 559 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 560 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 561 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 562 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@459 | 563 | |
universe@459 | 564 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@459 | 565 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 566 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 567 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@459 | 568 | |
universe@459 | 569 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 570 | CU_ASSERT_EQUAL(list->size, 0) |
universe@459 | 571 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 572 | |
universe@459 | 573 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 574 | |
universe@459 | 575 | cxLinkedListDestroy(list); |
universe@459 | 576 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 577 | } |
universe@459 | 578 | |
universe@459 | 579 | void test_hl_linked_list_find(void) { |
universe@459 | 580 | cxTestingAllocatorReset(); |
universe@459 | 581 | |
universe@459 | 582 | int data, criteria; |
universe@459 | 583 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 584 | |
universe@459 | 585 | data = 5; |
universe@460 | 586 | cxListAdd(list, &data); |
universe@459 | 587 | data = 47; |
universe@460 | 588 | cxListAdd(list, &data); |
universe@459 | 589 | data = 13; |
universe@460 | 590 | cxListAdd(list, &data); |
universe@459 | 591 | |
universe@459 | 592 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 593 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 594 | |
universe@459 | 595 | criteria = 5; |
universe@460 | 596 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@459 | 597 | criteria = 47; |
universe@460 | 598 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@459 | 599 | criteria = 13; |
universe@460 | 600 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@459 | 601 | criteria = 9000; |
universe@460 | 602 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 603 | criteria = -5; |
universe@460 | 604 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 605 | |
universe@459 | 606 | cxLinkedListDestroy(list); |
universe@459 | 607 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 608 | } |
universe@459 | 609 | |
universe@469 | 610 | void test_hl_linked_list_sort(void) { |
universe@469 | 611 | int expected[] = { |
universe@469 | 612 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 613 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 614 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 615 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 616 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 617 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 618 | }; |
universe@469 | 619 | int scrambled[] = { |
universe@469 | 620 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 621 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 622 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 623 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 624 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 625 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 626 | }; |
universe@469 | 627 | |
universe@469 | 628 | cxTestingAllocatorReset(); |
universe@469 | 629 | |
universe@469 | 630 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@469 | 631 | |
universe@473 | 632 | for (int i = 0; i < 100; i++) { |
universe@469 | 633 | cxListAdd(list, &scrambled[i]); |
universe@469 | 634 | } |
universe@469 | 635 | |
universe@469 | 636 | cxListSort(list); |
universe@469 | 637 | |
universe@473 | 638 | for (int i = 0; i < 100; i++) { |
universe@473 | 639 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 640 | } |
universe@469 | 641 | |
universe@469 | 642 | cxLinkedListDestroy(list); |
universe@469 | 643 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 644 | } |
universe@469 | 645 | |
universe@466 | 646 | void test_hl_ptr_linked_list_create(void) { |
universe@466 | 647 | cxTestingAllocatorReset(); |
universe@466 | 648 | |
universe@466 | 649 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 650 | |
universe@466 | 651 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 652 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@466 | 653 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@466 | 654 | CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) |
universe@466 | 655 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@466 | 656 | |
universe@466 | 657 | cxLinkedListDestroy(list); |
universe@466 | 658 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 659 | } |
universe@466 | 660 | |
universe@466 | 661 | void test_hl_ptr_linked_list_add(void) { |
universe@466 | 662 | cxTestingAllocatorReset(); |
universe@466 | 663 | |
universe@466 | 664 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 665 | |
universe@466 | 666 | int a = 5, b = 47, c = 13; |
universe@466 | 667 | |
universe@466 | 668 | CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) |
universe@466 | 669 | CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) |
universe@466 | 670 | CU_ASSERT_EQUAL(cxListAdd(list, &c), 0) |
universe@466 | 671 | |
universe@466 | 672 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 673 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 674 | |
universe@466 | 675 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 676 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 677 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 678 | |
universe@466 | 679 | a = 9; |
universe@466 | 680 | b = 10; |
universe@466 | 681 | c = 11; |
universe@466 | 682 | |
universe@466 | 683 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) |
universe@466 | 684 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) |
universe@466 | 685 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) |
universe@466 | 686 | |
universe@466 | 687 | cxLinkedListDestroy(list); |
universe@466 | 688 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 689 | } |
universe@466 | 690 | |
universe@466 | 691 | void test_hl_ptr_linked_list_insert(void) { |
universe@466 | 692 | cxTestingAllocatorReset(); |
universe@466 | 693 | |
universe@466 | 694 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 695 | |
universe@466 | 696 | int a = 5, b = 47, c = 13, d = 42; |
universe@466 | 697 | |
universe@466 | 698 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) |
universe@466 | 699 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 700 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) |
universe@466 | 701 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 702 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) |
universe@466 | 703 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 704 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) |
universe@466 | 705 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 706 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) |
universe@466 | 707 | |
universe@466 | 708 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 709 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 710 | |
universe@466 | 711 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 712 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 713 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 714 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@466 | 715 | |
universe@466 | 716 | cxLinkedListDestroy(list); |
universe@466 | 717 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 718 | } |
universe@466 | 719 | |
universe@466 | 720 | void test_hl_ptr_linked_list_remove(void) { |
universe@466 | 721 | cxTestingAllocatorReset(); |
universe@466 | 722 | |
universe@466 | 723 | int a = 5, b = 47, c = 42, d = 13; |
universe@466 | 724 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 725 | |
universe@466 | 726 | cxListAdd(list, &a); |
universe@466 | 727 | cxListAdd(list, &b); |
universe@466 | 728 | cxListAdd(list, &c); |
universe@466 | 729 | cxListAdd(list, &d); |
universe@466 | 730 | |
universe@466 | 731 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 732 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 733 | |
universe@466 | 734 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@466 | 735 | |
universe@466 | 736 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@466 | 737 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 738 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 739 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 740 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 741 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 742 | |
universe@466 | 743 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 744 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 745 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 746 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 747 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 748 | |
universe@466 | 749 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@466 | 750 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 751 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 752 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 753 | |
universe@466 | 754 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 755 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 756 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 757 | |
universe@466 | 758 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 759 | |
universe@466 | 760 | cxLinkedListDestroy(list); |
universe@466 | 761 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 762 | } |
universe@466 | 763 | |
universe@466 | 764 | void test_hl_ptr_linked_list_find(void) { |
universe@466 | 765 | cxTestingAllocatorReset(); |
universe@466 | 766 | |
universe@466 | 767 | int a = 5, b = 47, c = 13, criteria; |
universe@466 | 768 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 769 | |
universe@466 | 770 | cxListAdd(list, &a); |
universe@466 | 771 | cxListAdd(list, &b); |
universe@466 | 772 | cxListAdd(list, &c); |
universe@466 | 773 | |
universe@466 | 774 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 775 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 776 | |
universe@466 | 777 | criteria = 5; |
universe@466 | 778 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@466 | 779 | criteria = 47; |
universe@466 | 780 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 781 | criteria = 13; |
universe@466 | 782 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@466 | 783 | criteria = 9000; |
universe@466 | 784 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 785 | criteria = -5; |
universe@466 | 786 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 787 | b = -5; |
universe@466 | 788 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 789 | |
universe@466 | 790 | cxLinkedListDestroy(list); |
universe@466 | 791 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 792 | } |
universe@466 | 793 | |
universe@469 | 794 | void test_hl_ptr_linked_list_sort(void) { |
universe@469 | 795 | int expected[] = { |
universe@469 | 796 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 797 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 798 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 799 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 800 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 801 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 802 | }; |
universe@469 | 803 | int scrambled[] = { |
universe@469 | 804 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 805 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 806 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 807 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 808 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 809 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 810 | }; |
universe@469 | 811 | |
universe@469 | 812 | cxTestingAllocatorReset(); |
universe@469 | 813 | |
universe@469 | 814 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@469 | 815 | |
universe@473 | 816 | for (int i = 0; i < 100; i++) { |
universe@469 | 817 | cxListAdd(list, &scrambled[i]); |
universe@469 | 818 | } |
universe@469 | 819 | |
universe@469 | 820 | cxListSort(list); |
universe@469 | 821 | |
universe@473 | 822 | for (int i = 0; i < 100; i++) { |
universe@473 | 823 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 824 | } |
universe@469 | 825 | |
universe@469 | 826 | cxLinkedListDestroy(list); |
universe@469 | 827 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 828 | } |
universe@469 | 829 | |
universe@390 | 830 | int main() { |
universe@411 | 831 | CU_pSuite suite = NULL; |
universe@449 | 832 | |
universe@411 | 833 | if (CUE_SUCCESS != CU_initialize_registry()) { |
universe@411 | 834 | return CU_get_error(); |
universe@411 | 835 | } |
universe@411 | 836 | |
universe@459 | 837 | suite = CU_add_suite("low level linked list", NULL, NULL); |
universe@449 | 838 | |
universe@455 | 839 | cu_add_test(suite, test_linked_list_at); |
universe@475 | 840 | cu_add_test(suite, test_linked_list_prepend); |
universe@455 | 841 | cu_add_test(suite, test_linked_list_add); |
universe@475 | 842 | cu_add_test(suite, test_linked_list_first); |
universe@456 | 843 | cu_add_test(suite, test_linked_list_last); |
universe@473 | 844 | cu_add_test(suite, test_linked_list_prev); |
universe@473 | 845 | cu_add_test(suite, test_linked_list_remove); |
universe@468 | 846 | cu_add_test(suite, test_linked_list_size); |
universe@468 | 847 | cu_add_test(suite, test_linked_list_sort); |
universe@473 | 848 | cu_add_test(suite, test_linked_list_reverse); |
universe@455 | 849 | |
universe@459 | 850 | suite = CU_add_suite("high level linked list", NULL, NULL); |
universe@455 | 851 | |
universe@456 | 852 | cu_add_test(suite, test_hl_linked_list_create); |
universe@456 | 853 | cu_add_test(suite, test_hl_linked_list_add); |
universe@456 | 854 | cu_add_test(suite, test_hl_linked_list_insert); |
universe@456 | 855 | cu_add_test(suite, test_hl_linked_list_remove); |
universe@459 | 856 | cu_add_test(suite, test_hl_linked_list_find); |
universe@469 | 857 | cu_add_test(suite, test_hl_linked_list_sort); |
universe@413 | 858 | |
universe@466 | 859 | suite = CU_add_suite("high level pointer linked list", NULL, NULL); |
universe@466 | 860 | |
universe@466 | 861 | cu_add_test(suite, test_hl_ptr_linked_list_create); |
universe@466 | 862 | cu_add_test(suite, test_hl_ptr_linked_list_add); |
universe@466 | 863 | cu_add_test(suite, test_hl_ptr_linked_list_insert); |
universe@466 | 864 | cu_add_test(suite, test_hl_ptr_linked_list_remove); |
universe@466 | 865 | cu_add_test(suite, test_hl_ptr_linked_list_find); |
universe@469 | 866 | cu_add_test(suite, test_hl_ptr_linked_list_sort); |
universe@466 | 867 | |
universe@411 | 868 | CU_basic_set_mode(UCX_CU_BRM); |
universe@411 | 869 | |
universe@411 | 870 | int exitcode; |
universe@411 | 871 | if (CU_basic_run_tests()) { |
universe@411 | 872 | exitcode = CU_get_error(); |
universe@411 | 873 | } else { |
universe@411 | 874 | exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; |
universe@411 | 875 | } |
universe@411 | 876 | CU_cleanup_registry(); |
universe@411 | 877 | return exitcode; |
universe@390 | 878 | } |