universe@390: /* universe@390: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@390: * universe@390: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@390: * universe@390: * Redistribution and use in source and binary forms, with or without universe@390: * modification, are permitted provided that the following conditions are met: universe@390: * universe@390: * 1. Redistributions of source code must retain the above copyright universe@390: * notice, this list of conditions and the following disclaimer. universe@390: * universe@390: * 2. Redistributions in binary form must reproduce the above copyright universe@390: * notice, this list of conditions and the following disclaimer in the universe@390: * documentation and/or other materials provided with the distribution. universe@390: * universe@390: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@390: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@390: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@390: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@390: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@390: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@390: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@390: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@390: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@390: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@390: * POSSIBILITY OF SUCH DAMAGE. universe@390: */ universe@390: universe@398: #include "cx/linked_list.h" universe@411: #include "test_config.h" universe@422: #include "util_allocator.h" universe@398: universe@412: int cmp_int(int const *l, int const *r) { universe@412: int left = *l, right = *r; universe@412: return left == right ? 0 : (left < right ? -1 : 1); universe@412: } universe@412: universe@438: void test_linked_list_at(void) { universe@438: struct node { universe@438: void *next; universe@438: void *prev; universe@438: }; universe@438: const ptrdiff_t loc_prev = offsetof(struct node, prev); universe@438: const ptrdiff_t loc_next = offsetof(struct node, next); universe@438: universe@438: struct node a, b, c, d; universe@438: a.prev = NULL; universe@438: a.next = &b; universe@438: b.prev = &a; universe@438: b.next = &c; universe@438: c.prev = &b; universe@438: c.next = &d; universe@438: d.prev = &c; universe@438: d.next = NULL; universe@438: universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d) universe@449: CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)) universe@438: universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d) universe@449: CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)) universe@438: universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a) universe@449: CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b) universe@438: } universe@438: olaf@444: void test_linked_list_add(void) { olaf@442: struct node { olaf@442: void *prev; olaf@442: void *next; olaf@442: }; universe@449: olaf@442: struct node nodes[4]; universe@449: olaf@442: // test with begin, end / prev, next universe@449: memset(nodes, 0, 4 * sizeof(struct node)); olaf@442: void *begin = NULL; olaf@442: void *end = NULL; universe@449: olaf@442: ptrdiff_t loc_prev = offsetof(struct node, prev); olaf@442: ptrdiff_t loc_next = offsetof(struct node, next); universe@449: universe@453: cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); universe@449: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) universe@449: CU_ASSERT_PTR_EQUAL(end, &nodes[0]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL) universe@449: CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL) universe@449: universe@453: cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); universe@449: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) universe@449: CU_ASSERT_PTR_EQUAL(end, &nodes[1]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) universe@449: olaf@442: // test with begin only / prev, next universe@449: memset(nodes, 0, 4 * sizeof(struct node)); olaf@442: begin = NULL; olaf@442: end = NULL; universe@449: universe@453: cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); universe@449: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) universe@453: cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); universe@449: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) universe@449: universe@453: cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); universe@449: CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) universe@449: olaf@442: // test with begin, end / next universe@449: memset(nodes, 0, 4 * sizeof(struct node)); olaf@442: begin = NULL; olaf@442: end = NULL; universe@449: universe@453: cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); universe@449: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) universe@449: CU_ASSERT_PTR_EQUAL(end, &nodes[0]) universe@453: cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); universe@449: CU_ASSERT_PTR_EQUAL(end, &nodes[1]) universe@449: CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) universe@449: CU_ASSERT_PTR_NULL(nodes[1].prev) olaf@442: } olaf@442: universe@455: universe@455: void test_linked_list_create(void) { universe@455: cxTestingAllocatorReset(); universe@455: universe@455: CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); universe@455: universe@455: CU_ASSERT_EQUAL(list->size, 0) universe@455: CU_ASSERT_EQUAL(list->capacity, (size_t) -1) universe@455: CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) universe@455: CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) universe@455: CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) universe@455: universe@455: // assume this structure for a linked list universe@455: struct ll_check { universe@455: cx_list_s base; universe@455: void *begin; universe@455: void *end; universe@455: }; universe@455: universe@455: struct ll_check *actual = (struct ll_check *) list; universe@455: CU_ASSERT_PTR_NULL(actual->begin) universe@455: CU_ASSERT_PTR_NULL(actual->end) universe@455: universe@455: cxLinkedListDestroy(list); universe@455: universe@455: CU_ASSERT_TRUE(cxTestingAllocatorVerify()) universe@455: } universe@455: universe@390: int main() { universe@411: CU_pSuite suite = NULL; universe@449: universe@411: if (CUE_SUCCESS != CU_initialize_registry()) { universe@411: return CU_get_error(); universe@411: } universe@411: universe@455: suite = CU_add_suite("low level linked list suite", NULL, NULL); universe@449: universe@455: cu_add_test(suite, test_linked_list_at); universe@455: cu_add_test(suite, test_linked_list_add); universe@455: universe@455: suite = CU_add_suite("high level linked list suite", NULL, NULL); universe@455: universe@455: cu_add_test(suite, test_linked_list_create); universe@413: universe@411: CU_basic_set_mode(UCX_CU_BRM); universe@411: universe@411: int exitcode; universe@411: if (CU_basic_run_tests()) { universe@411: exitcode = CU_get_error(); universe@411: } else { universe@411: exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; universe@411: } universe@411: CU_cleanup_registry(); universe@411: return exitcode; universe@390: }