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_create(void) { universe@422: cxTestingAllocatorReset(); universe@422: universe@422: CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); universe@412: universe@435: CU_ASSERT_EQUAL(list->size, 0) universe@435: CU_ASSERT_EQUAL(list->capacity, (size_t) -1) universe@435: CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) universe@435: CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) universe@435: CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) universe@412: universe@435: // assume this structure for a linked list universe@435: struct ll_check { universe@435: cx_list_s base; universe@422: void *begin; universe@422: void *end; universe@422: ptrdiff_t ploc; universe@422: ptrdiff_t nloc; universe@412: }; universe@412: universe@435: struct ll_check *actual = (struct ll_check *) list; universe@412: CU_ASSERT_PTR_NULL(actual->begin) universe@412: CU_ASSERT_PTR_NULL(actual->end) universe@412: CU_ASSERT_EQUAL(0, actual->ploc) universe@422: CU_ASSERT_EQUAL(sizeof(void *), actual->nloc) universe@412: universe@412: cxLinkedListDestroy(list); universe@413: universe@422: CU_ASSERT_TRUE(cxTestingAllocatorVerify()) universe@411: } universe@398: 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@438: CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0)); universe@438: CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1)); universe@438: CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2)); universe@438: CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3)); universe@438: CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)); universe@438: universe@438: CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0)); universe@438: CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1)); universe@438: CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2)); universe@438: CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3)); universe@438: CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)); universe@438: universe@438: CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0)); universe@438: CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1)); universe@438: } universe@438: olaf@442: void test_cx_linked_list_add(void) { olaf@442: struct node { olaf@442: void *prev; olaf@442: void *next; olaf@442: int value; olaf@442: }; olaf@442: olaf@442: struct node nodes[4]; olaf@442: olaf@442: // test with begin, end / prev, next olaf@442: memset(nodes, 0, 4*sizeof(struct node)); olaf@442: void *begin = NULL; olaf@442: void *end = NULL; olaf@442: olaf@442: ptrdiff_t loc_prev = offsetof(struct node, prev); olaf@442: ptrdiff_t loc_next = offsetof(struct node, next); olaf@442: olaf@442: int ret; olaf@442: ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); olaf@442: CU_ASSERT_EQUAL(ret, 0); olaf@442: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); olaf@442: CU_ASSERT_PTR_EQUAL(end, &nodes[0]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL); olaf@442: olaf@442: ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); olaf@442: CU_ASSERT_EQUAL(ret, 0); olaf@442: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); olaf@442: CU_ASSERT_PTR_EQUAL(end, &nodes[1]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); olaf@442: olaf@442: // test with begin only / prev, next olaf@442: memset(nodes, 0, 4*sizeof(struct node)); olaf@442: begin = NULL; olaf@442: end = NULL; olaf@442: olaf@442: ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); olaf@442: CU_ASSERT_EQUAL(ret, 0); olaf@442: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); olaf@442: ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); olaf@442: CU_ASSERT_EQUAL(ret, 0); olaf@442: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); olaf@442: olaf@442: ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]); olaf@442: olaf@442: // test with begin, end / next olaf@442: memset(nodes, 0, 4*sizeof(struct node)); olaf@442: begin = NULL; olaf@442: end = NULL; olaf@442: olaf@442: ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); olaf@442: CU_ASSERT_EQUAL(ret, 0); olaf@442: CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); olaf@442: CU_ASSERT_PTR_EQUAL(end, &nodes[0]); olaf@442: ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); olaf@442: CU_ASSERT_PTR_EQUAL(end, &nodes[1]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); olaf@442: CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL); olaf@442: } olaf@442: universe@390: int main() { universe@411: CU_pSuite suite = NULL; olaf@442: universe@411: if (CUE_SUCCESS != CU_initialize_registry()) { universe@411: return CU_get_error(); universe@411: } universe@411: universe@413: suite = CU_add_suite("linked list suite", NULL, NULL); universe@411: if (NULL == suite) { universe@411: CU_cleanup_registry(); universe@411: return CU_get_error(); universe@411: } universe@411: universe@411: if ( universe@438: !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) || olaf@442: !CU_add_test(suite, "linked list: get node at index", test_linked_list_at) || olaf@442: !CU_add_test(suite, "linked list: add", test_cx_linked_list_add) universe@411: ) { universe@411: CU_cleanup_registry(); universe@411: return CU_get_error(); universe@411: } universe@411: universe@413: suite = CU_add_suite("array suite", NULL, NULL); universe@413: if (NULL == suite) { universe@413: CU_cleanup_registry(); universe@413: return CU_get_error(); universe@413: } universe@413: universe@413: /* universe@413: if ( universe@413: !CU_add_test(suite, "array...", test_array...) universe@413: ) { universe@413: CU_cleanup_registry(); universe@413: return CU_get_error(); universe@413: } universe@413: */ 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: }