Fri, 08 Oct 2021 19:47:31 +0200
add cx_linked_list_{prev, remove, reverse}
changes assertions for some low level methods (loc_next is now always mandatory)
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@449 | 91 | CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL) |
universe@449 | 92 | CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL) |
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 | |
olaf@442 | 116 | // test with begin, end / next |
universe@449 | 117 | memset(nodes, 0, 4 * sizeof(struct node)); |
olaf@442 | 118 | begin = NULL; |
olaf@442 | 119 | end = NULL; |
universe@449 | 120 | |
universe@453 | 121 | cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); |
universe@449 | 122 | CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) |
universe@449 | 123 | CU_ASSERT_PTR_EQUAL(end, &nodes[0]) |
universe@453 | 124 | cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); |
universe@449 | 125 | CU_ASSERT_PTR_EQUAL(end, &nodes[1]) |
universe@449 | 126 | CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
universe@449 | 127 | CU_ASSERT_PTR_NULL(nodes[1].prev) |
olaf@442 | 128 | } |
olaf@442 | 129 | |
universe@456 | 130 | void test_linked_list_last(void) { |
universe@456 | 131 | CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0)) |
universe@455 | 132 | |
universe@456 | 133 | struct node { |
universe@456 | 134 | int data; |
universe@456 | 135 | void *next; |
universe@456 | 136 | }; |
universe@456 | 137 | ptrdiff_t loc = offsetof(struct node, next); |
universe@456 | 138 | |
universe@456 | 139 | struct node third = {3, NULL}; |
universe@456 | 140 | struct node second = {2, &third}; |
universe@456 | 141 | struct node first = {1, &second}; |
universe@456 | 142 | |
universe@456 | 143 | CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third) |
universe@456 | 144 | CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third) |
universe@456 | 145 | CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) |
universe@456 | 146 | } |
universe@456 | 147 | |
universe@473 | 148 | void test_linked_list_prev(void) { |
universe@473 | 149 | struct node { |
universe@473 | 150 | void *next; |
universe@473 | 151 | }; |
universe@473 | 152 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 153 | |
universe@473 | 154 | struct node third = {NULL}; |
universe@473 | 155 | struct node second = {&third}; |
universe@473 | 156 | struct node first = {&second}; |
universe@473 | 157 | |
universe@473 | 158 | CU_ASSERT_PTR_NULL(cx_linked_list_prev(&first, loc, &first)) |
universe@473 | 159 | CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &second), &first) |
universe@473 | 160 | CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &third), &second) |
universe@473 | 161 | } |
universe@473 | 162 | |
universe@473 | 163 | void test_linked_list_remove(void) { |
universe@473 | 164 | struct node { |
universe@473 | 165 | void *next; |
universe@473 | 166 | }; |
universe@473 | 167 | struct dnode { |
universe@473 | 168 | void *next; |
universe@473 | 169 | void *prev; |
universe@473 | 170 | }; |
universe@473 | 171 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 172 | ptrdiff_t ploc = offsetof(struct dnode, prev); |
universe@473 | 173 | |
universe@473 | 174 | void *begin; |
universe@473 | 175 | void *end; |
universe@473 | 176 | void *result; |
universe@473 | 177 | |
universe@473 | 178 | // single linked list |
universe@473 | 179 | struct node third = {NULL}; |
universe@473 | 180 | struct node second = {&third}; |
universe@473 | 181 | struct node first = {&second}; |
universe@473 | 182 | begin = &first; |
universe@473 | 183 | |
universe@473 | 184 | result = cx_linked_list_remove(&begin, NULL, -1, loc, &second); |
universe@473 | 185 | CU_ASSERT_PTR_EQUAL(result, &first) |
universe@473 | 186 | CU_ASSERT_PTR_EQUAL(begin, &first) |
universe@473 | 187 | CU_ASSERT_PTR_EQUAL(first.next, &third) |
universe@473 | 188 | CU_ASSERT_PTR_NULL(second.next) |
universe@473 | 189 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 190 | |
universe@473 | 191 | result = cx_linked_list_remove(&begin, NULL, -1, loc, &first); |
universe@473 | 192 | CU_ASSERT_PTR_EQUAL(result, &third) |
universe@473 | 193 | CU_ASSERT_PTR_EQUAL(begin, &third) |
universe@473 | 194 | CU_ASSERT_PTR_NULL(first.next) |
universe@473 | 195 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 196 | |
universe@473 | 197 | result = cx_linked_list_remove(&begin, NULL, -1, loc, &third); |
universe@473 | 198 | CU_ASSERT_PTR_NULL(result) |
universe@473 | 199 | CU_ASSERT_PTR_NULL(begin) |
universe@473 | 200 | CU_ASSERT_PTR_NULL(third.next) |
universe@473 | 201 | |
universe@473 | 202 | // doubly linked list |
universe@473 | 203 | struct dnode dthird = {NULL , NULL}; |
universe@473 | 204 | struct dnode dsecond = {&dthird, NULL}; |
universe@473 | 205 | struct dnode dfirst = {&dsecond, NULL}; |
universe@473 | 206 | dthird.prev = &dsecond; |
universe@473 | 207 | dsecond.prev = &dfirst; |
universe@473 | 208 | begin = &dfirst; |
universe@473 | 209 | end = &dthird; |
universe@473 | 210 | |
universe@473 | 211 | result = cx_linked_list_remove(&begin, &end, ploc, loc, &dsecond); |
universe@473 | 212 | CU_ASSERT_PTR_EQUAL(result, &dfirst) |
universe@473 | 213 | CU_ASSERT_PTR_EQUAL(begin, &dfirst) |
universe@473 | 214 | CU_ASSERT_PTR_EQUAL(end, &dthird) |
universe@473 | 215 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 216 | CU_ASSERT_PTR_EQUAL(dfirst.next, &dthird) |
universe@473 | 217 | CU_ASSERT_PTR_NULL(dsecond.prev) |
universe@473 | 218 | CU_ASSERT_PTR_NULL(dsecond.next) |
universe@473 | 219 | CU_ASSERT_PTR_EQUAL(dthird.prev, &dfirst) |
universe@473 | 220 | CU_ASSERT_PTR_NULL(dthird.next) |
universe@473 | 221 | |
universe@473 | 222 | result = cx_linked_list_remove(&begin, &end, ploc, loc, &dthird); |
universe@473 | 223 | CU_ASSERT_PTR_EQUAL(result, &dfirst) |
universe@473 | 224 | CU_ASSERT_PTR_EQUAL(begin, &dfirst) |
universe@473 | 225 | CU_ASSERT_PTR_EQUAL(end, &dfirst) |
universe@473 | 226 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 227 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 228 | CU_ASSERT_PTR_NULL(dthird.prev) |
universe@473 | 229 | CU_ASSERT_PTR_NULL(dthird.next) |
universe@473 | 230 | |
universe@473 | 231 | result = cx_linked_list_remove(&begin, &end, ploc, loc, &dfirst); |
universe@473 | 232 | CU_ASSERT_PTR_NULL(result) |
universe@473 | 233 | CU_ASSERT_PTR_NULL(begin) |
universe@473 | 234 | CU_ASSERT_PTR_NULL(end) |
universe@473 | 235 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 236 | CU_ASSERT_PTR_NULL(dfirst.prev) |
universe@473 | 237 | } |
universe@473 | 238 | |
universe@468 | 239 | void test_linked_list_size(void) { |
universe@468 | 240 | struct node { |
universe@468 | 241 | void *next; |
universe@468 | 242 | }; |
universe@468 | 243 | ptrdiff_t loc = offsetof(struct node, next); |
universe@468 | 244 | |
universe@468 | 245 | struct node first = {NULL}; |
universe@468 | 246 | struct node second = {NULL}; |
universe@468 | 247 | struct node third = {NULL}; |
universe@468 | 248 | |
universe@468 | 249 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0) |
universe@468 | 250 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1) |
universe@468 | 251 | first.next = &second; |
universe@468 | 252 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2) |
universe@468 | 253 | second.next = &third; |
universe@468 | 254 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3) |
universe@468 | 255 | CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2) |
universe@468 | 256 | } |
universe@468 | 257 | |
universe@468 | 258 | void test_linked_list_sort(void) { |
universe@468 | 259 | struct node { |
universe@468 | 260 | void *prev; |
universe@468 | 261 | void *next; |
universe@468 | 262 | int data; |
universe@468 | 263 | }; |
universe@468 | 264 | |
universe@468 | 265 | int expected[] = { |
universe@468 | 266 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@468 | 267 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@468 | 268 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@468 | 269 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@468 | 270 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@468 | 271 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@468 | 272 | }; |
universe@468 | 273 | int scrambled[] = { |
universe@468 | 274 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@468 | 275 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@468 | 276 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@468 | 277 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@468 | 278 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@468 | 279 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@468 | 280 | }; |
universe@468 | 281 | |
universe@468 | 282 | struct node *nodes = calloc(100, sizeof(struct node)); |
universe@468 | 283 | for (int i = 0; i < 100; i++) { |
universe@468 | 284 | nodes[i].prev = i == 0 ? NULL : &nodes[i - 1]; |
universe@468 | 285 | nodes[i].next = i == 99 ? NULL : &nodes[i + 1]; |
universe@468 | 286 | nodes[i].data = scrambled[i]; |
universe@468 | 287 | } |
universe@468 | 288 | |
universe@468 | 289 | struct node *begin = &nodes[0]; |
universe@468 | 290 | struct node *end = &nodes[99]; |
universe@468 | 291 | |
universe@468 | 292 | cx_linked_list_sort((void **) &begin, (void **) &end, |
universe@468 | 293 | offsetof(struct node, prev), |
universe@468 | 294 | offsetof(struct node, next), |
universe@468 | 295 | offsetof(struct node, data), |
universe@468 | 296 | 0, (CxListComparator) cmp_int); |
universe@468 | 297 | |
universe@468 | 298 | CU_ASSERT_PTR_NULL(begin->prev) |
universe@468 | 299 | CU_ASSERT_EQUAL(begin->data, expected[0]) |
universe@468 | 300 | struct node *check = begin; |
universe@468 | 301 | struct node *check_last = NULL; |
universe@473 | 302 | for (int i = 0; i < 100; i++) { |
universe@468 | 303 | CU_ASSERT_EQUAL(check->data, expected[i]) |
universe@468 | 304 | CU_ASSERT_PTR_EQUAL(check->prev, check_last) |
universe@468 | 305 | if (i < 99) { |
universe@468 | 306 | CU_ASSERT_PTR_NOT_NULL(check->next) |
universe@468 | 307 | } |
universe@468 | 308 | check_last = check; |
universe@468 | 309 | check = check->next; |
universe@468 | 310 | } |
universe@468 | 311 | CU_ASSERT_PTR_NULL(check) |
universe@468 | 312 | CU_ASSERT_EQUAL(end->data, expected[99]) |
universe@468 | 313 | } |
universe@468 | 314 | |
universe@473 | 315 | void test_linked_list_reverse(void) { |
universe@473 | 316 | struct node { |
universe@473 | 317 | void *next; |
universe@473 | 318 | }; |
universe@473 | 319 | struct dnode { |
universe@473 | 320 | void *next; |
universe@473 | 321 | void *prev; |
universe@473 | 322 | }; |
universe@473 | 323 | ptrdiff_t loc = offsetof(struct node, next); |
universe@473 | 324 | ptrdiff_t ploc = offsetof(struct dnode, prev); |
universe@473 | 325 | |
universe@473 | 326 | void *begin; |
universe@473 | 327 | void *end; |
universe@473 | 328 | |
universe@473 | 329 | // single linked list |
universe@473 | 330 | struct node third = {NULL}; |
universe@473 | 331 | struct node second = {&third}; |
universe@473 | 332 | struct node first = {&second}; |
universe@473 | 333 | begin = &first; |
universe@473 | 334 | |
universe@473 | 335 | cx_linked_list_reverse(&begin, NULL, -1, loc); |
universe@473 | 336 | CU_ASSERT_PTR_EQUAL(begin, &third) |
universe@473 | 337 | CU_ASSERT_PTR_EQUAL(third.next, &second) |
universe@473 | 338 | CU_ASSERT_PTR_EQUAL(second.next, &first) |
universe@473 | 339 | CU_ASSERT_PTR_NULL(first.next) |
universe@473 | 340 | |
universe@473 | 341 | // doubly linked list |
universe@473 | 342 | struct dnode dthird = {NULL , NULL}; |
universe@473 | 343 | struct dnode dsecond = {&dthird, NULL}; |
universe@473 | 344 | struct dnode dfirst = {&dsecond, NULL}; |
universe@473 | 345 | dthird.prev = &dsecond; |
universe@473 | 346 | dsecond.prev = &dfirst; |
universe@473 | 347 | begin = &dfirst; |
universe@473 | 348 | end = &dthird; |
universe@473 | 349 | |
universe@473 | 350 | cx_linked_list_reverse(&begin, &end, ploc, loc); |
universe@473 | 351 | CU_ASSERT_PTR_EQUAL(begin, &dthird) |
universe@473 | 352 | CU_ASSERT_PTR_EQUAL(end, &dfirst) |
universe@473 | 353 | CU_ASSERT_PTR_EQUAL(dthird.next, &dsecond) |
universe@473 | 354 | CU_ASSERT_PTR_EQUAL(dsecond.next, &dfirst) |
universe@473 | 355 | CU_ASSERT_PTR_NULL(dfirst.next) |
universe@473 | 356 | CU_ASSERT_PTR_NULL(dthird.prev) |
universe@473 | 357 | CU_ASSERT_PTR_EQUAL(dsecond.prev, &dthird) |
universe@473 | 358 | CU_ASSERT_PTR_EQUAL(dfirst.prev, &dsecond) |
universe@473 | 359 | } |
universe@456 | 360 | |
universe@456 | 361 | void test_hl_linked_list_create(void) { |
universe@455 | 362 | cxTestingAllocatorReset(); |
universe@455 | 363 | |
universe@455 | 364 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@455 | 365 | |
universe@455 | 366 | CU_ASSERT_EQUAL(list->size, 0) |
universe@455 | 367 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@455 | 368 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@455 | 369 | CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) |
universe@455 | 370 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@455 | 371 | |
universe@455 | 372 | cxLinkedListDestroy(list); |
universe@455 | 373 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@455 | 374 | } |
universe@455 | 375 | |
universe@459 | 376 | void test_hl_linked_list_add(void) { |
universe@459 | 377 | cxTestingAllocatorReset(); |
universe@459 | 378 | |
universe@459 | 379 | int data; |
universe@459 | 380 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 381 | |
universe@459 | 382 | data = 5; |
universe@460 | 383 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 384 | data = 47; |
universe@460 | 385 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 386 | data = 13; |
universe@460 | 387 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@459 | 388 | |
universe@459 | 389 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 390 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 391 | |
universe@466 | 392 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 393 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 394 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 395 | |
universe@459 | 396 | cxLinkedListDestroy(list); |
universe@459 | 397 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 398 | } |
universe@459 | 399 | |
universe@459 | 400 | void test_hl_linked_list_last(void) { |
universe@459 | 401 | cxTestingAllocatorReset(); |
universe@459 | 402 | |
universe@459 | 403 | int data; |
universe@459 | 404 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 405 | |
universe@460 | 406 | CU_ASSERT_PTR_NULL(cxListLast(list)) |
universe@459 | 407 | |
universe@459 | 408 | data = 5; |
universe@460 | 409 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@466 | 410 | CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5) |
universe@459 | 411 | |
universe@459 | 412 | data = 47; |
universe@460 | 413 | CU_ASSERT_EQUAL(cxListAdd(list, &data), 0) |
universe@466 | 414 | CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47) |
universe@459 | 415 | |
universe@459 | 416 | cxLinkedListDestroy(list); |
universe@459 | 417 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 418 | } |
universe@459 | 419 | |
universe@459 | 420 | void test_hl_linked_list_insert(void) { |
universe@459 | 421 | cxTestingAllocatorReset(); |
universe@459 | 422 | |
universe@459 | 423 | int data; |
universe@459 | 424 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 425 | |
universe@459 | 426 | data = 5; |
universe@460 | 427 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 428 | CU_ASSERT_EQUAL(list->size, 0) |
universe@460 | 429 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 430 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 431 | data = 47; |
universe@460 | 432 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 433 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 434 | data = 13; |
universe@460 | 435 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 436 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 437 | data = 42; |
universe@460 | 438 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0) |
universe@459 | 439 | |
universe@459 | 440 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 441 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 442 | |
universe@466 | 443 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 444 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 445 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 446 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@459 | 447 | |
universe@459 | 448 | cxLinkedListDestroy(list); |
universe@459 | 449 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 450 | } |
universe@459 | 451 | |
universe@459 | 452 | void test_hl_linked_list_remove(void) { |
universe@459 | 453 | cxTestingAllocatorReset(); |
universe@459 | 454 | |
universe@459 | 455 | int data; |
universe@459 | 456 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 457 | |
universe@459 | 458 | data = 5; |
universe@460 | 459 | cxListAdd(list, &data); |
universe@459 | 460 | data = 47; |
universe@460 | 461 | cxListAdd(list, &data); |
universe@459 | 462 | data = 42; |
universe@460 | 463 | cxListAdd(list, &data); |
universe@459 | 464 | data = 13; |
universe@460 | 465 | cxListAdd(list, &data); |
universe@459 | 466 | |
universe@459 | 467 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 468 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 469 | |
universe@459 | 470 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@459 | 471 | |
universe@459 | 472 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@459 | 473 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 474 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 475 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 476 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 477 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 478 | |
universe@459 | 479 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 480 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 481 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 482 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 483 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@459 | 484 | |
universe@459 | 485 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@459 | 486 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 487 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 488 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@459 | 489 | |
universe@459 | 490 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 491 | CU_ASSERT_EQUAL(list->size, 0) |
universe@459 | 492 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 493 | |
universe@459 | 494 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 495 | |
universe@459 | 496 | cxLinkedListDestroy(list); |
universe@459 | 497 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 498 | } |
universe@459 | 499 | |
universe@459 | 500 | void test_hl_linked_list_find(void) { |
universe@459 | 501 | cxTestingAllocatorReset(); |
universe@459 | 502 | |
universe@459 | 503 | int data, criteria; |
universe@459 | 504 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 505 | |
universe@459 | 506 | data = 5; |
universe@460 | 507 | cxListAdd(list, &data); |
universe@459 | 508 | data = 47; |
universe@460 | 509 | cxListAdd(list, &data); |
universe@459 | 510 | data = 13; |
universe@460 | 511 | cxListAdd(list, &data); |
universe@459 | 512 | |
universe@459 | 513 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 514 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 515 | |
universe@459 | 516 | criteria = 5; |
universe@460 | 517 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@459 | 518 | criteria = 47; |
universe@460 | 519 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@459 | 520 | criteria = 13; |
universe@460 | 521 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@459 | 522 | criteria = 9000; |
universe@460 | 523 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 524 | criteria = -5; |
universe@460 | 525 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 526 | |
universe@459 | 527 | cxLinkedListDestroy(list); |
universe@459 | 528 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 529 | } |
universe@459 | 530 | |
universe@469 | 531 | void test_hl_linked_list_sort(void) { |
universe@469 | 532 | int expected[] = { |
universe@469 | 533 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 534 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 535 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 536 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 537 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 538 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 539 | }; |
universe@469 | 540 | int scrambled[] = { |
universe@469 | 541 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 542 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 543 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 544 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 545 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 546 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 547 | }; |
universe@469 | 548 | |
universe@469 | 549 | cxTestingAllocatorReset(); |
universe@469 | 550 | |
universe@469 | 551 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@469 | 552 | |
universe@473 | 553 | for (int i = 0; i < 100; i++) { |
universe@469 | 554 | cxListAdd(list, &scrambled[i]); |
universe@469 | 555 | } |
universe@469 | 556 | |
universe@469 | 557 | cxListSort(list); |
universe@469 | 558 | |
universe@473 | 559 | for (int i = 0; i < 100; i++) { |
universe@473 | 560 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 561 | } |
universe@469 | 562 | |
universe@469 | 563 | cxLinkedListDestroy(list); |
universe@469 | 564 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 565 | } |
universe@469 | 566 | |
universe@466 | 567 | void test_hl_ptr_linked_list_create(void) { |
universe@466 | 568 | cxTestingAllocatorReset(); |
universe@466 | 569 | |
universe@466 | 570 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 571 | |
universe@466 | 572 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 573 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@466 | 574 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@466 | 575 | CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) |
universe@466 | 576 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@466 | 577 | |
universe@466 | 578 | cxLinkedListDestroy(list); |
universe@466 | 579 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 580 | } |
universe@466 | 581 | |
universe@466 | 582 | void test_hl_ptr_linked_list_add(void) { |
universe@466 | 583 | cxTestingAllocatorReset(); |
universe@466 | 584 | |
universe@466 | 585 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 586 | |
universe@466 | 587 | int a = 5, b = 47, c = 13; |
universe@466 | 588 | |
universe@466 | 589 | CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) |
universe@466 | 590 | CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) |
universe@466 | 591 | CU_ASSERT_EQUAL(cxListAdd(list, &c), 0) |
universe@466 | 592 | |
universe@466 | 593 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 594 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 595 | |
universe@466 | 596 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 597 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 598 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 599 | |
universe@466 | 600 | a = 9; |
universe@466 | 601 | b = 10; |
universe@466 | 602 | c = 11; |
universe@466 | 603 | |
universe@466 | 604 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) |
universe@466 | 605 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) |
universe@466 | 606 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) |
universe@466 | 607 | |
universe@466 | 608 | cxLinkedListDestroy(list); |
universe@466 | 609 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 610 | } |
universe@466 | 611 | |
universe@466 | 612 | void test_hl_ptr_linked_list_last(void) { |
universe@466 | 613 | cxTestingAllocatorReset(); |
universe@466 | 614 | |
universe@466 | 615 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 616 | CU_ASSERT_PTR_NULL(cxListLast(list)) |
universe@466 | 617 | |
universe@466 | 618 | int a = 5, b = 47; |
universe@466 | 619 | |
universe@466 | 620 | CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) |
universe@466 | 621 | CU_ASSERT_EQUAL(*(int *) cxListLast(list), 5) |
universe@466 | 622 | CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) |
universe@466 | 623 | CU_ASSERT_EQUAL(*(int *) cxListLast(list), 47) |
universe@466 | 624 | |
universe@466 | 625 | cxLinkedListDestroy(list); |
universe@466 | 626 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 627 | } |
universe@466 | 628 | |
universe@466 | 629 | void test_hl_ptr_linked_list_insert(void) { |
universe@466 | 630 | cxTestingAllocatorReset(); |
universe@466 | 631 | |
universe@466 | 632 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 633 | |
universe@466 | 634 | int a = 5, b = 47, c = 13, d = 42; |
universe@466 | 635 | |
universe@466 | 636 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) |
universe@466 | 637 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 638 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) |
universe@466 | 639 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 640 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) |
universe@466 | 641 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 642 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) |
universe@466 | 643 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 644 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) |
universe@466 | 645 | |
universe@466 | 646 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 647 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 648 | |
universe@466 | 649 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 650 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 651 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 652 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@466 | 653 | |
universe@466 | 654 | cxLinkedListDestroy(list); |
universe@466 | 655 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 656 | } |
universe@466 | 657 | |
universe@466 | 658 | void test_hl_ptr_linked_list_remove(void) { |
universe@466 | 659 | cxTestingAllocatorReset(); |
universe@466 | 660 | |
universe@466 | 661 | int a = 5, b = 47, c = 42, d = 13; |
universe@466 | 662 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 663 | |
universe@466 | 664 | cxListAdd(list, &a); |
universe@466 | 665 | cxListAdd(list, &b); |
universe@466 | 666 | cxListAdd(list, &c); |
universe@466 | 667 | cxListAdd(list, &d); |
universe@466 | 668 | |
universe@466 | 669 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 670 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 671 | |
universe@466 | 672 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@466 | 673 | |
universe@466 | 674 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@466 | 675 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 676 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 677 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 678 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 679 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 680 | |
universe@466 | 681 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 682 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 683 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 684 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 685 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 686 | |
universe@466 | 687 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@466 | 688 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 689 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 690 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 691 | |
universe@466 | 692 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 693 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 694 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 695 | |
universe@466 | 696 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 697 | |
universe@466 | 698 | cxLinkedListDestroy(list); |
universe@466 | 699 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 700 | } |
universe@466 | 701 | |
universe@466 | 702 | void test_hl_ptr_linked_list_find(void) { |
universe@466 | 703 | cxTestingAllocatorReset(); |
universe@466 | 704 | |
universe@466 | 705 | int a = 5, b = 47, c = 13, criteria; |
universe@466 | 706 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 707 | |
universe@466 | 708 | cxListAdd(list, &a); |
universe@466 | 709 | cxListAdd(list, &b); |
universe@466 | 710 | cxListAdd(list, &c); |
universe@466 | 711 | |
universe@466 | 712 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 713 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 714 | |
universe@466 | 715 | criteria = 5; |
universe@466 | 716 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@466 | 717 | criteria = 47; |
universe@466 | 718 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 719 | criteria = 13; |
universe@466 | 720 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@466 | 721 | criteria = 9000; |
universe@466 | 722 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 723 | criteria = -5; |
universe@466 | 724 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 725 | b = -5; |
universe@466 | 726 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 727 | |
universe@466 | 728 | cxLinkedListDestroy(list); |
universe@466 | 729 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 730 | } |
universe@466 | 731 | |
universe@469 | 732 | void test_hl_ptr_linked_list_sort(void) { |
universe@469 | 733 | int expected[] = { |
universe@469 | 734 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 735 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 736 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 737 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 738 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 739 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 740 | }; |
universe@469 | 741 | int scrambled[] = { |
universe@469 | 742 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 743 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 744 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 745 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 746 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 747 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 748 | }; |
universe@469 | 749 | |
universe@469 | 750 | cxTestingAllocatorReset(); |
universe@469 | 751 | |
universe@469 | 752 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@469 | 753 | |
universe@473 | 754 | for (int i = 0; i < 100; i++) { |
universe@469 | 755 | cxListAdd(list, &scrambled[i]); |
universe@469 | 756 | } |
universe@469 | 757 | |
universe@469 | 758 | cxListSort(list); |
universe@469 | 759 | |
universe@473 | 760 | for (int i = 0; i < 100; i++) { |
universe@473 | 761 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 762 | } |
universe@469 | 763 | |
universe@469 | 764 | cxLinkedListDestroy(list); |
universe@469 | 765 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 766 | } |
universe@469 | 767 | |
universe@390 | 768 | int main() { |
universe@411 | 769 | CU_pSuite suite = NULL; |
universe@449 | 770 | |
universe@411 | 771 | if (CUE_SUCCESS != CU_initialize_registry()) { |
universe@411 | 772 | return CU_get_error(); |
universe@411 | 773 | } |
universe@411 | 774 | |
universe@459 | 775 | suite = CU_add_suite("low level linked list", NULL, NULL); |
universe@449 | 776 | |
universe@455 | 777 | cu_add_test(suite, test_linked_list_at); |
universe@455 | 778 | cu_add_test(suite, test_linked_list_add); |
universe@456 | 779 | cu_add_test(suite, test_linked_list_last); |
universe@473 | 780 | cu_add_test(suite, test_linked_list_prev); |
universe@473 | 781 | cu_add_test(suite, test_linked_list_remove); |
universe@468 | 782 | cu_add_test(suite, test_linked_list_size); |
universe@468 | 783 | cu_add_test(suite, test_linked_list_sort); |
universe@473 | 784 | cu_add_test(suite, test_linked_list_reverse); |
universe@455 | 785 | |
universe@459 | 786 | suite = CU_add_suite("high level linked list", NULL, NULL); |
universe@455 | 787 | |
universe@456 | 788 | cu_add_test(suite, test_hl_linked_list_create); |
universe@456 | 789 | cu_add_test(suite, test_hl_linked_list_add); |
universe@456 | 790 | cu_add_test(suite, test_hl_linked_list_last); |
universe@456 | 791 | cu_add_test(suite, test_hl_linked_list_insert); |
universe@456 | 792 | cu_add_test(suite, test_hl_linked_list_remove); |
universe@459 | 793 | cu_add_test(suite, test_hl_linked_list_find); |
universe@469 | 794 | cu_add_test(suite, test_hl_linked_list_sort); |
universe@413 | 795 | |
universe@466 | 796 | suite = CU_add_suite("high level pointer linked list", NULL, NULL); |
universe@466 | 797 | |
universe@466 | 798 | cu_add_test(suite, test_hl_ptr_linked_list_create); |
universe@466 | 799 | cu_add_test(suite, test_hl_ptr_linked_list_add); |
universe@466 | 800 | cu_add_test(suite, test_hl_ptr_linked_list_last); |
universe@466 | 801 | cu_add_test(suite, test_hl_ptr_linked_list_insert); |
universe@466 | 802 | cu_add_test(suite, test_hl_ptr_linked_list_remove); |
universe@466 | 803 | cu_add_test(suite, test_hl_ptr_linked_list_find); |
universe@469 | 804 | cu_add_test(suite, test_hl_ptr_linked_list_sort); |
universe@466 | 805 | |
universe@411 | 806 | CU_basic_set_mode(UCX_CU_BRM); |
universe@411 | 807 | |
universe@411 | 808 | int exitcode; |
universe@411 | 809 | if (CU_basic_run_tests()) { |
universe@411 | 810 | exitcode = CU_get_error(); |
universe@411 | 811 | } else { |
universe@411 | 812 | exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; |
universe@411 | 813 | } |
universe@411 | 814 | CU_cleanup_registry(); |
universe@411 | 815 | return exitcode; |
universe@390 | 816 | } |