Sat, 09 Oct 2021 11:12:48 +0200
remove cxListLast (can be realized via cxListAt and index=size-1)
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_insert(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@459 | 406 | data = 5; |
universe@460 | 407 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 408 | CU_ASSERT_EQUAL(list->size, 0) |
universe@460 | 409 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 410 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 411 | data = 47; |
universe@460 | 412 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) |
universe@459 | 413 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 414 | data = 13; |
universe@460 | 415 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0) |
universe@459 | 416 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 417 | data = 42; |
universe@460 | 418 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0) |
universe@459 | 419 | |
universe@459 | 420 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 421 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 422 | |
universe@466 | 423 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 424 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 425 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 426 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@459 | 427 | |
universe@459 | 428 | cxLinkedListDestroy(list); |
universe@459 | 429 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 430 | } |
universe@459 | 431 | |
universe@459 | 432 | void test_hl_linked_list_remove(void) { |
universe@459 | 433 | cxTestingAllocatorReset(); |
universe@459 | 434 | |
universe@459 | 435 | int data; |
universe@459 | 436 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 437 | |
universe@459 | 438 | data = 5; |
universe@460 | 439 | cxListAdd(list, &data); |
universe@459 | 440 | data = 47; |
universe@460 | 441 | cxListAdd(list, &data); |
universe@459 | 442 | data = 42; |
universe@460 | 443 | cxListAdd(list, &data); |
universe@459 | 444 | data = 13; |
universe@460 | 445 | cxListAdd(list, &data); |
universe@459 | 446 | |
universe@459 | 447 | CU_ASSERT_EQUAL(list->size, 4) |
universe@459 | 448 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 449 | |
universe@459 | 450 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@459 | 451 | |
universe@459 | 452 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@459 | 453 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 454 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 455 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 456 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 457 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@459 | 458 | |
universe@459 | 459 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 460 | CU_ASSERT_EQUAL(list->size, 2) |
universe@459 | 461 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 462 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 463 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@459 | 464 | |
universe@459 | 465 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@459 | 466 | CU_ASSERT_EQUAL(list->size, 1) |
universe@459 | 467 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 468 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@459 | 469 | |
universe@459 | 470 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 471 | CU_ASSERT_EQUAL(list->size, 0) |
universe@459 | 472 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 473 | |
universe@459 | 474 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@459 | 475 | |
universe@459 | 476 | cxLinkedListDestroy(list); |
universe@459 | 477 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 478 | } |
universe@459 | 479 | |
universe@459 | 480 | void test_hl_linked_list_find(void) { |
universe@459 | 481 | cxTestingAllocatorReset(); |
universe@459 | 482 | |
universe@459 | 483 | int data, criteria; |
universe@459 | 484 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@459 | 485 | |
universe@459 | 486 | data = 5; |
universe@460 | 487 | cxListAdd(list, &data); |
universe@459 | 488 | data = 47; |
universe@460 | 489 | cxListAdd(list, &data); |
universe@459 | 490 | data = 13; |
universe@460 | 491 | cxListAdd(list, &data); |
universe@459 | 492 | |
universe@459 | 493 | CU_ASSERT_EQUAL(list->size, 3) |
universe@459 | 494 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@459 | 495 | |
universe@459 | 496 | criteria = 5; |
universe@460 | 497 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@459 | 498 | criteria = 47; |
universe@460 | 499 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@459 | 500 | criteria = 13; |
universe@460 | 501 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@459 | 502 | criteria = 9000; |
universe@460 | 503 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 504 | criteria = -5; |
universe@460 | 505 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@459 | 506 | |
universe@459 | 507 | cxLinkedListDestroy(list); |
universe@459 | 508 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@459 | 509 | } |
universe@459 | 510 | |
universe@469 | 511 | void test_hl_linked_list_sort(void) { |
universe@469 | 512 | int expected[] = { |
universe@469 | 513 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 514 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 515 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 516 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 517 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 518 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 519 | }; |
universe@469 | 520 | int scrambled[] = { |
universe@469 | 521 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 522 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 523 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 524 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 525 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 526 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 527 | }; |
universe@469 | 528 | |
universe@469 | 529 | cxTestingAllocatorReset(); |
universe@469 | 530 | |
universe@469 | 531 | CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
universe@469 | 532 | |
universe@473 | 533 | for (int i = 0; i < 100; i++) { |
universe@469 | 534 | cxListAdd(list, &scrambled[i]); |
universe@469 | 535 | } |
universe@469 | 536 | |
universe@469 | 537 | cxListSort(list); |
universe@469 | 538 | |
universe@473 | 539 | for (int i = 0; i < 100; i++) { |
universe@473 | 540 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 541 | } |
universe@469 | 542 | |
universe@469 | 543 | cxLinkedListDestroy(list); |
universe@469 | 544 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 545 | } |
universe@469 | 546 | |
universe@466 | 547 | void test_hl_ptr_linked_list_create(void) { |
universe@466 | 548 | cxTestingAllocatorReset(); |
universe@466 | 549 | |
universe@466 | 550 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 551 | |
universe@466 | 552 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 553 | CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
universe@466 | 554 | CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
universe@466 | 555 | CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) |
universe@466 | 556 | CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
universe@466 | 557 | |
universe@466 | 558 | cxLinkedListDestroy(list); |
universe@466 | 559 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 560 | } |
universe@466 | 561 | |
universe@466 | 562 | void test_hl_ptr_linked_list_add(void) { |
universe@466 | 563 | cxTestingAllocatorReset(); |
universe@466 | 564 | |
universe@466 | 565 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 566 | |
universe@466 | 567 | int a = 5, b = 47, c = 13; |
universe@466 | 568 | |
universe@466 | 569 | CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) |
universe@466 | 570 | CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) |
universe@466 | 571 | CU_ASSERT_EQUAL(cxListAdd(list, &c), 0) |
universe@466 | 572 | |
universe@466 | 573 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 574 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 575 | |
universe@466 | 576 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 577 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 578 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 579 | |
universe@466 | 580 | a = 9; |
universe@466 | 581 | b = 10; |
universe@466 | 582 | c = 11; |
universe@466 | 583 | |
universe@466 | 584 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) |
universe@466 | 585 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) |
universe@466 | 586 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) |
universe@466 | 587 | |
universe@466 | 588 | cxLinkedListDestroy(list); |
universe@466 | 589 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 590 | } |
universe@466 | 591 | |
universe@466 | 592 | void test_hl_ptr_linked_list_insert(void) { |
universe@466 | 593 | cxTestingAllocatorReset(); |
universe@466 | 594 | |
universe@466 | 595 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 596 | |
universe@466 | 597 | int a = 5, b = 47, c = 13, d = 42; |
universe@466 | 598 | |
universe@466 | 599 | CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) |
universe@466 | 600 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 601 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) |
universe@466 | 602 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 603 | CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) |
universe@466 | 604 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 605 | CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) |
universe@466 | 606 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 607 | CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) |
universe@466 | 608 | |
universe@466 | 609 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 610 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 611 | |
universe@466 | 612 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 613 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 614 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 5) |
universe@466 | 615 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) |
universe@466 | 616 | |
universe@466 | 617 | cxLinkedListDestroy(list); |
universe@466 | 618 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 619 | } |
universe@466 | 620 | |
universe@466 | 621 | void test_hl_ptr_linked_list_remove(void) { |
universe@466 | 622 | cxTestingAllocatorReset(); |
universe@466 | 623 | |
universe@466 | 624 | int a = 5, b = 47, c = 42, d = 13; |
universe@466 | 625 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 626 | |
universe@466 | 627 | cxListAdd(list, &a); |
universe@466 | 628 | cxListAdd(list, &b); |
universe@466 | 629 | cxListAdd(list, &c); |
universe@466 | 630 | cxListAdd(list, &d); |
universe@466 | 631 | |
universe@466 | 632 | CU_ASSERT_EQUAL(list->size, 4) |
universe@466 | 633 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 634 | |
universe@466 | 635 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) |
universe@466 | 636 | |
universe@466 | 637 | CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) |
universe@466 | 638 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 639 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 640 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) |
universe@466 | 641 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) |
universe@466 | 642 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) |
universe@466 | 643 | |
universe@466 | 644 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 645 | CU_ASSERT_EQUAL(list->size, 2) |
universe@466 | 646 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 647 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 648 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) |
universe@466 | 649 | |
universe@466 | 650 | CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) |
universe@466 | 651 | CU_ASSERT_EQUAL(list->size, 1) |
universe@466 | 652 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 653 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) |
universe@466 | 654 | |
universe@466 | 655 | CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 656 | CU_ASSERT_EQUAL(list->size, 0) |
universe@466 | 657 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 658 | |
universe@466 | 659 | CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) |
universe@466 | 660 | |
universe@466 | 661 | cxLinkedListDestroy(list); |
universe@466 | 662 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 663 | } |
universe@466 | 664 | |
universe@466 | 665 | void test_hl_ptr_linked_list_find(void) { |
universe@466 | 666 | cxTestingAllocatorReset(); |
universe@466 | 667 | |
universe@466 | 668 | int a = 5, b = 47, c = 13, criteria; |
universe@466 | 669 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@466 | 670 | |
universe@466 | 671 | cxListAdd(list, &a); |
universe@466 | 672 | cxListAdd(list, &b); |
universe@466 | 673 | cxListAdd(list, &c); |
universe@466 | 674 | |
universe@466 | 675 | CU_ASSERT_EQUAL(list->size, 3) |
universe@466 | 676 | CU_ASSERT_TRUE(list->capacity >= list->size) |
universe@466 | 677 | |
universe@466 | 678 | criteria = 5; |
universe@466 | 679 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) |
universe@466 | 680 | criteria = 47; |
universe@466 | 681 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 682 | criteria = 13; |
universe@466 | 683 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) |
universe@466 | 684 | criteria = 9000; |
universe@466 | 685 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 686 | criteria = -5; |
universe@466 | 687 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) |
universe@466 | 688 | b = -5; |
universe@466 | 689 | CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) |
universe@466 | 690 | |
universe@466 | 691 | cxLinkedListDestroy(list); |
universe@466 | 692 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@466 | 693 | } |
universe@466 | 694 | |
universe@469 | 695 | void test_hl_ptr_linked_list_sort(void) { |
universe@469 | 696 | int expected[] = { |
universe@469 | 697 | 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, |
universe@469 | 698 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, |
universe@469 | 699 | 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, |
universe@469 | 700 | 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, |
universe@469 | 701 | 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, |
universe@469 | 702 | 4785, 4791, 4801, 4859, 4903, 4973 |
universe@469 | 703 | }; |
universe@469 | 704 | int scrambled[] = { |
universe@469 | 705 | 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, |
universe@469 | 706 | 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, |
universe@469 | 707 | 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, |
universe@469 | 708 | 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, |
universe@469 | 709 | 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, |
universe@469 | 710 | 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 |
universe@469 | 711 | }; |
universe@469 | 712 | |
universe@469 | 713 | cxTestingAllocatorReset(); |
universe@469 | 714 | |
universe@469 | 715 | CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); |
universe@469 | 716 | |
universe@473 | 717 | for (int i = 0; i < 100; i++) { |
universe@469 | 718 | cxListAdd(list, &scrambled[i]); |
universe@469 | 719 | } |
universe@469 | 720 | |
universe@469 | 721 | cxListSort(list); |
universe@469 | 722 | |
universe@473 | 723 | for (int i = 0; i < 100; i++) { |
universe@473 | 724 | CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) |
universe@469 | 725 | } |
universe@469 | 726 | |
universe@469 | 727 | cxLinkedListDestroy(list); |
universe@469 | 728 | CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
universe@469 | 729 | } |
universe@469 | 730 | |
universe@390 | 731 | int main() { |
universe@411 | 732 | CU_pSuite suite = NULL; |
universe@449 | 733 | |
universe@411 | 734 | if (CUE_SUCCESS != CU_initialize_registry()) { |
universe@411 | 735 | return CU_get_error(); |
universe@411 | 736 | } |
universe@411 | 737 | |
universe@459 | 738 | suite = CU_add_suite("low level linked list", NULL, NULL); |
universe@449 | 739 | |
universe@455 | 740 | cu_add_test(suite, test_linked_list_at); |
universe@455 | 741 | cu_add_test(suite, test_linked_list_add); |
universe@456 | 742 | cu_add_test(suite, test_linked_list_last); |
universe@473 | 743 | cu_add_test(suite, test_linked_list_prev); |
universe@473 | 744 | cu_add_test(suite, test_linked_list_remove); |
universe@468 | 745 | cu_add_test(suite, test_linked_list_size); |
universe@468 | 746 | cu_add_test(suite, test_linked_list_sort); |
universe@473 | 747 | cu_add_test(suite, test_linked_list_reverse); |
universe@455 | 748 | |
universe@459 | 749 | suite = CU_add_suite("high level linked list", NULL, NULL); |
universe@455 | 750 | |
universe@456 | 751 | cu_add_test(suite, test_hl_linked_list_create); |
universe@456 | 752 | cu_add_test(suite, test_hl_linked_list_add); |
universe@456 | 753 | cu_add_test(suite, test_hl_linked_list_insert); |
universe@456 | 754 | cu_add_test(suite, test_hl_linked_list_remove); |
universe@459 | 755 | cu_add_test(suite, test_hl_linked_list_find); |
universe@469 | 756 | cu_add_test(suite, test_hl_linked_list_sort); |
universe@413 | 757 | |
universe@466 | 758 | suite = CU_add_suite("high level pointer linked list", NULL, NULL); |
universe@466 | 759 | |
universe@466 | 760 | cu_add_test(suite, test_hl_ptr_linked_list_create); |
universe@466 | 761 | cu_add_test(suite, test_hl_ptr_linked_list_add); |
universe@466 | 762 | cu_add_test(suite, test_hl_ptr_linked_list_insert); |
universe@466 | 763 | cu_add_test(suite, test_hl_ptr_linked_list_remove); |
universe@466 | 764 | cu_add_test(suite, test_hl_ptr_linked_list_find); |
universe@469 | 765 | cu_add_test(suite, test_hl_ptr_linked_list_sort); |
universe@466 | 766 | |
universe@411 | 767 | CU_basic_set_mode(UCX_CU_BRM); |
universe@411 | 768 | |
universe@411 | 769 | int exitcode; |
universe@411 | 770 | if (CU_basic_run_tests()) { |
universe@411 | 771 | exitcode = CU_get_error(); |
universe@411 | 772 | } else { |
universe@411 | 773 | exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; |
universe@411 | 774 | } |
universe@411 | 775 | CU_cleanup_registry(); |
universe@411 | 776 | return exitcode; |
universe@390 | 777 | } |