test/test_list.c

Tue, 05 Oct 2021 11:19:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Oct 2021 11:19:32 +0200
changeset 460
e075009b33b7
parent 459
c0e2e9f83399
child 466
28bc3e10ac28
permissions
-rw-r--r--

remove convenience macros

Users should write their own wrappers s.t. the type
information does not have to be repeated on every
call site.

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, -1))
universe@456 132 CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0))
universe@455 133
universe@456 134 struct node {
universe@456 135 int data;
universe@456 136 void *next;
universe@456 137 };
universe@456 138 ptrdiff_t loc = offsetof(struct node, next);
universe@456 139
universe@456 140 struct node third = {3, NULL};
universe@456 141 struct node second = {2, &third};
universe@456 142 struct node first = {1, &second};
universe@456 143
universe@456 144 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&first, loc), &third)
universe@456 145 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&second, loc), &third)
universe@456 146 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third)
universe@456 147 }
universe@456 148
universe@456 149
universe@456 150 void test_hl_linked_list_create(void) {
universe@455 151 cxTestingAllocatorReset();
universe@455 152
universe@455 153 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@455 154
universe@455 155 CU_ASSERT_EQUAL(list->size, 0)
universe@455 156 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
universe@455 157 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
universe@455 158 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
universe@455 159 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
universe@455 160
universe@455 161 // assume this structure for a linked list
universe@455 162 struct ll_check {
universe@455 163 cx_list_s base;
universe@455 164 void *begin;
universe@455 165 void *end;
universe@455 166 };
universe@455 167
universe@455 168 struct ll_check *actual = (struct ll_check *) list;
universe@455 169 CU_ASSERT_PTR_NULL(actual->begin)
universe@455 170 CU_ASSERT_PTR_NULL(actual->end)
universe@455 171
universe@455 172 cxLinkedListDestroy(list);
universe@455 173
universe@455 174 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@455 175 }
universe@455 176
universe@459 177 void test_hl_linked_list_add(void) {
universe@459 178 cxTestingAllocatorReset();
universe@459 179
universe@459 180 int data;
universe@459 181 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 182
universe@459 183 data = 5;
universe@460 184 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 185 data = 47;
universe@460 186 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 187 data = 13;
universe@460 188 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@459 189
universe@459 190 CU_ASSERT_EQUAL(list->size, 3)
universe@459 191 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 192
universe@460 193 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
universe@460 194 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
universe@460 195 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
universe@459 196
universe@459 197 cxLinkedListDestroy(list);
universe@459 198 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 199 }
universe@459 200
universe@459 201 void test_hl_linked_list_last(void) {
universe@459 202 cxTestingAllocatorReset();
universe@459 203
universe@459 204 int data;
universe@459 205 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 206
universe@460 207 CU_ASSERT_PTR_NULL(cxListLast(list))
universe@459 208
universe@459 209 data = 5;
universe@460 210 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@460 211 CU_ASSERT_EQUAL(*(int*)cxListLast(list), 5)
universe@459 212
universe@459 213 data = 47;
universe@460 214 CU_ASSERT_EQUAL(cxListAdd(list, &data), 0)
universe@460 215 CU_ASSERT_EQUAL(*(int*)cxListLast(list), 47)
universe@459 216
universe@459 217 cxLinkedListDestroy(list);
universe@459 218 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 219 }
universe@459 220
universe@459 221 void test_hl_linked_list_insert(void) {
universe@459 222 cxTestingAllocatorReset();
universe@459 223
universe@459 224 int data;
universe@459 225 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 226
universe@459 227 data = 5;
universe@460 228 CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0)
universe@459 229 CU_ASSERT_EQUAL(list->size, 0)
universe@460 230 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
universe@459 231 CU_ASSERT_EQUAL(list->size, 1)
universe@459 232 data = 47;
universe@460 233 CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0)
universe@459 234 CU_ASSERT_EQUAL(list->size, 2)
universe@459 235 data = 13;
universe@460 236 CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0)
universe@459 237 CU_ASSERT_EQUAL(list->size, 3)
universe@459 238 data = 42;
universe@460 239 CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0)
universe@459 240
universe@459 241 CU_ASSERT_EQUAL(list->size, 4)
universe@459 242 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 243
universe@460 244 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 47)
universe@460 245 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 13)
universe@460 246 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 5)
universe@460 247 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 3), 42)
universe@459 248
universe@459 249 cxLinkedListDestroy(list);
universe@459 250 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 251 }
universe@459 252
universe@459 253 void test_hl_linked_list_remove(void) {
universe@459 254 cxTestingAllocatorReset();
universe@459 255
universe@459 256 int data;
universe@459 257 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 258
universe@459 259 data = 5;
universe@460 260 cxListAdd(list, &data);
universe@459 261 data = 47;
universe@460 262 cxListAdd(list, &data);
universe@459 263 data = 42;
universe@460 264 cxListAdd(list, &data);
universe@459 265 data = 13;
universe@460 266 cxListAdd(list, &data);
universe@459 267
universe@459 268 CU_ASSERT_EQUAL(list->size, 4)
universe@459 269 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 270
universe@459 271 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0)
universe@459 272
universe@459 273 CU_ASSERT_EQUAL(cxListRemove(list, 2), 0)
universe@459 274 CU_ASSERT_EQUAL(list->size, 3)
universe@459 275 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@460 276 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 5)
universe@460 277 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 47)
universe@460 278 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 2), 13)
universe@459 279
universe@459 280 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@459 281 CU_ASSERT_EQUAL(list->size, 2)
universe@459 282 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@460 283 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 47)
universe@460 284 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 1), 13)
universe@459 285
universe@459 286 CU_ASSERT_EQUAL(cxListRemove(list, 1), 0)
universe@459 287 CU_ASSERT_EQUAL(list->size, 1)
universe@459 288 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@460 289 CU_ASSERT_EQUAL(*(int*)cxListAt(list, 0), 47)
universe@459 290
universe@459 291 CU_ASSERT_EQUAL(cxListRemove(list, 0), 0)
universe@459 292 CU_ASSERT_EQUAL(list->size, 0)
universe@459 293 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 294
universe@459 295 CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0)
universe@459 296
universe@459 297 cxLinkedListDestroy(list);
universe@459 298 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 299 }
universe@459 300
universe@459 301 void test_hl_linked_list_find(void) {
universe@459 302 cxTestingAllocatorReset();
universe@459 303
universe@459 304 int data, criteria;
universe@459 305 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@459 306
universe@459 307 data = 5;
universe@460 308 cxListAdd(list, &data);
universe@459 309 data = 47;
universe@460 310 cxListAdd(list, &data);
universe@459 311 data = 13;
universe@460 312 cxListAdd(list, &data);
universe@459 313
universe@459 314 CU_ASSERT_EQUAL(list->size, 3)
universe@459 315 CU_ASSERT_TRUE(list->capacity >= list->size)
universe@459 316
universe@459 317 criteria = 5;
universe@460 318 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0)
universe@459 319 criteria = 47;
universe@460 320 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1)
universe@459 321 criteria = 13;
universe@460 322 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2)
universe@459 323 criteria = 9000;
universe@460 324 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@459 325 criteria = -5;
universe@460 326 CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3)
universe@459 327
universe@459 328 cxLinkedListDestroy(list);
universe@459 329 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@459 330 }
universe@459 331
universe@390 332 int main() {
universe@411 333 CU_pSuite suite = NULL;
universe@449 334
universe@411 335 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 336 return CU_get_error();
universe@411 337 }
universe@411 338
universe@459 339 suite = CU_add_suite("low level linked list", NULL, NULL);
universe@449 340
universe@455 341 cu_add_test(suite, test_linked_list_at);
universe@455 342 cu_add_test(suite, test_linked_list_add);
universe@456 343 cu_add_test(suite, test_linked_list_last);
universe@455 344
universe@459 345 suite = CU_add_suite("high level linked list", NULL, NULL);
universe@455 346
universe@456 347 cu_add_test(suite, test_hl_linked_list_create);
universe@456 348 cu_add_test(suite, test_hl_linked_list_add);
universe@456 349 cu_add_test(suite, test_hl_linked_list_last);
universe@456 350 cu_add_test(suite, test_hl_linked_list_insert);
universe@456 351 cu_add_test(suite, test_hl_linked_list_remove);
universe@459 352 cu_add_test(suite, test_hl_linked_list_find);
universe@413 353
universe@411 354 CU_basic_set_mode(UCX_CU_BRM);
universe@411 355
universe@411 356 int exitcode;
universe@411 357 if (CU_basic_run_tests()) {
universe@411 358 exitcode = CU_get_error();
universe@411 359 } else {
universe@411 360 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 361 }
universe@411 362 CU_cleanup_registry();
universe@411 363 return exitcode;
universe@390 364 }

mercurial