Sun, 03 Oct 2021 16:30:47 +0200
change cx_linked_list_last() and add a test for it
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@390 | 177 | int main() { |
universe@411 | 178 | CU_pSuite suite = NULL; |
universe@449 | 179 | |
universe@411 | 180 | if (CUE_SUCCESS != CU_initialize_registry()) { |
universe@411 | 181 | return CU_get_error(); |
universe@411 | 182 | } |
universe@411 | 183 | |
universe@455 | 184 | suite = CU_add_suite("low level linked list suite", NULL, NULL); |
universe@449 | 185 | |
universe@455 | 186 | cu_add_test(suite, test_linked_list_at); |
universe@455 | 187 | cu_add_test(suite, test_linked_list_add); |
universe@456 | 188 | cu_add_test(suite, test_linked_list_last); |
universe@455 | 189 | |
universe@455 | 190 | suite = CU_add_suite("high level linked list suite", NULL, NULL); |
universe@455 | 191 | |
universe@456 | 192 | cu_add_test(suite, test_hl_linked_list_create); |
universe@456 | 193 | /* |
universe@456 | 194 | cu_add_test(suite, test_hl_linked_list_add); |
universe@456 | 195 | cu_add_test(suite, test_hl_linked_list_last); |
universe@456 | 196 | cu_add_test(suite, test_hl_linked_list_insert); |
universe@456 | 197 | cu_add_test(suite, test_hl_linked_list_remove); |
universe@456 | 198 | cu_add_test(suite, test_hl_linked_list_find);*/ |
universe@413 | 199 | |
universe@411 | 200 | CU_basic_set_mode(UCX_CU_BRM); |
universe@411 | 201 | |
universe@411 | 202 | int exitcode; |
universe@411 | 203 | if (CU_basic_run_tests()) { |
universe@411 | 204 | exitcode = CU_get_error(); |
universe@411 | 205 | } else { |
universe@411 | 206 | exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; |
universe@411 | 207 | } |
universe@411 | 208 | CU_cleanup_registry(); |
universe@411 | 209 | return exitcode; |
universe@390 | 210 | } |