universe@798: /* universe@798: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@798: * universe@798: * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. universe@798: * universe@798: * Redistribution and use in source and binary forms, with or without universe@798: * modification, are permitted provided that the following conditions are met: universe@798: * universe@798: * 1. Redistributions of source code must retain the above copyright universe@798: * notice, this list of conditions and the following disclaimer. universe@798: * universe@798: * 2. Redistributions in binary form must reproduce the above copyright universe@798: * notice, this list of conditions and the following disclaimer in the universe@798: * documentation and/or other materials provided with the distribution. universe@798: * universe@798: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@798: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@798: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@798: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@798: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@798: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@798: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@798: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@798: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@798: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@798: * POSSIBILITY OF SUCH DAMAGE. universe@798: */ universe@798: universe@798: #include "cx/test.h" universe@798: #include "util_allocator.h" universe@798: #include "cx/compare.h" universe@803: #include "cx/utils.h" universe@798: universe@798: #include "cx/array_list.h" universe@798: #include "cx/linked_list.h" universe@798: universe@798: #include universe@798: universe@818: CX_TEST(test_array_add) { universe@818: int stackspace[5] = {1,1,2,3,5}; universe@818: int *stackarray = stackspace; universe@818: size_t stackarray_size = 3; universe@818: size_t stackarray_capacity = 5; universe@831: cx_array_declare(int, heaparray); universe@831: heaparray = calloc(5, sizeof(int)); universe@818: heaparray[0] = 2; universe@818: heaparray[1] = 3; universe@818: heaparray[2] = 5; universe@818: heaparray[3] = 7; universe@818: heaparray[4] = 11; universe@831: heaparray_size = 3; universe@831: heaparray_capacity = 5; universe@818: int elem = 8, elem2 = 47; universe@819: enum cx_array_result result; universe@818: CX_TEST_DO { universe@818: result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem, NULL); universe@819: CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); universe@818: CX_TEST_ASSERT(stackarray[0] == 1); universe@818: CX_TEST_ASSERT(stackarray[1] == 1); universe@818: CX_TEST_ASSERT(stackarray[2] == 2); universe@818: CX_TEST_ASSERT(stackarray[3] == 8); universe@818: CX_TEST_ASSERT(stackarray[4] == 5); universe@818: CX_TEST_ASSERT(stackarray_size == 4); universe@818: CX_TEST_ASSERT(stackarray_capacity == 5); universe@818: universe@818: stackarray_size = 5; universe@818: result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem2, NULL); universe@819: CX_TEST_ASSERT(result == CX_ARRAY_REALLOC_NOT_SUPPORTED); universe@818: CX_TEST_ASSERT(stackarray[0] == 1); universe@818: CX_TEST_ASSERT(stackarray[1] == 1); universe@818: CX_TEST_ASSERT(stackarray[2] == 2); universe@818: CX_TEST_ASSERT(stackarray[3] == 8); universe@818: CX_TEST_ASSERT(stackarray[4] == 5); universe@818: CX_TEST_ASSERT(stackarray_size == 5); universe@818: CX_TEST_ASSERT(stackarray_capacity == 5); universe@818: universe@831: result = cx_array_simple_add(heaparray, &elem); universe@819: CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); universe@818: CX_TEST_ASSERT(heaparray[0] == 2); universe@818: CX_TEST_ASSERT(heaparray[1] == 3); universe@818: CX_TEST_ASSERT(heaparray[2] == 5); universe@818: CX_TEST_ASSERT(heaparray[3] == 8); universe@818: CX_TEST_ASSERT(heaparray[4] == 11); universe@818: CX_TEST_ASSERT(heaparray_size == 4); universe@818: CX_TEST_ASSERT(heaparray_capacity == 5); universe@818: universe@818: heaparray_size = 5; universe@831: result = cx_array_simple_add(heaparray, &elem2); universe@819: CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); universe@818: CX_TEST_ASSERT(heaparray[0] == 2); universe@818: CX_TEST_ASSERT(heaparray[1] == 3); universe@818: CX_TEST_ASSERT(heaparray[2] == 5); universe@818: CX_TEST_ASSERT(heaparray[3] == 8); universe@818: CX_TEST_ASSERT(heaparray[4] == 11); universe@818: CX_TEST_ASSERT(heaparray[5] == 47); universe@818: CX_TEST_ASSERT(heaparray_size == 6); universe@818: CX_TEST_ASSERT(heaparray_capacity >= 6); universe@818: } universe@818: free(heaparray); universe@818: } universe@818: universe@798: typedef struct node { universe@798: struct node *next; universe@798: struct node *prev; universe@798: int data; universe@798: } node; universe@798: universe@798: const ptrdiff_t loc_prev = offsetof(struct node, prev); universe@798: const ptrdiff_t loc_next = offsetof(struct node, next); universe@798: const ptrdiff_t loc_data = offsetof(struct node, data); universe@798: universe@798: static node *create_nodes_test_data(size_t len) { universe@798: node *begin = calloc(1, sizeof(node)); universe@798: void *prev = begin; universe@798: for (size_t i = 1; i < len; i++) { universe@798: node *n = calloc(1, sizeof(node)); universe@798: cx_linked_list_link(prev, n, loc_prev, loc_next); universe@798: prev = n; universe@798: } universe@798: return begin; universe@798: } universe@798: universe@798: void assign_nodes_test_data(node *n, ...) { universe@798: va_list ap; universe@798: va_start(ap, n); universe@798: while (n != NULL) { universe@798: n->data = va_arg(ap, int); universe@798: n = n->next; universe@798: } universe@798: va_end(ap); universe@798: } universe@798: universe@798: static void destroy_nodes_test_data(node *n) { universe@798: while (n != NULL) { universe@798: void *next = n->next; universe@798: free(n); universe@798: n = next; universe@798: } universe@798: } universe@798: universe@798: static int *int_test_data(size_t len) { universe@798: int *data = malloc(len*sizeof(int)); universe@798: for (size_t i = 0 ; i < len ; i++) { universe@798: data[i] = rand(); // NOLINT(*-msc50-cpp) universe@798: } universe@798: return data; universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_link_unlink) { universe@798: node a = {0}, b = {0}, c = {0}; universe@798: universe@798: CX_TEST_DO { universe@798: cx_linked_list_link(&a, &b, loc_prev, loc_next); universe@798: CX_TEST_ASSERT(a.prev == NULL); universe@798: CX_TEST_ASSERT(a.next == &b); universe@798: CX_TEST_ASSERT(b.prev == &a); universe@798: CX_TEST_ASSERT(b.next == NULL); universe@798: universe@798: cx_linked_list_unlink(&a, &b, loc_prev, loc_next); universe@798: CX_TEST_ASSERT(a.prev == NULL); universe@798: CX_TEST_ASSERT(a.next == NULL); universe@798: CX_TEST_ASSERT(b.prev == NULL); universe@798: CX_TEST_ASSERT(b.next == NULL); universe@798: universe@798: cx_linked_list_link(&b, &c, loc_prev, loc_next); universe@798: cx_linked_list_link(&a, &b, loc_prev, loc_next); universe@798: cx_linked_list_unlink(&b, &c, loc_prev, loc_next); universe@798: CX_TEST_ASSERT(a.prev == NULL); universe@798: CX_TEST_ASSERT(a.next == &b); universe@798: CX_TEST_ASSERT(b.prev == &a); universe@798: CX_TEST_ASSERT(b.next == NULL); universe@798: CX_TEST_ASSERT(c.prev == NULL); universe@798: CX_TEST_ASSERT(c.next == NULL); universe@798: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_at) { universe@798: node a = {0}, b = {0}, c = {0}, d = {0}; universe@798: universe@798: cx_linked_list_link(&a, &b, loc_prev, loc_next); universe@798: cx_linked_list_link(&b, &c, loc_prev, loc_next); universe@798: cx_linked_list_link(&c, &d, loc_prev, loc_next); universe@798: universe@798: CX_TEST_DO { universe@798: CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 0) == &a); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 1) == &b); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 2) == &c); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 3) == &d); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 4) == NULL); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_prev, 0) == &a); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 1) == &b); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 2) == &c); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 3) == &d); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 4) == NULL); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&d, 3, loc_prev, 0) == &a); universe@798: CX_TEST_ASSERT(cx_linked_list_at(&d, 3, loc_prev, 1) == &b); universe@798: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_find) { universe@798: void *list = create_nodes_test_data(4); universe@798: assign_nodes_test_data(list, 2, 4, 6, 8); universe@798: CX_TEST_DO { universe@798: int s; universe@798: s = 2; universe@798: CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 0); universe@798: s = 4; universe@798: CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 1); universe@798: s = 6; universe@798: CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 2); universe@798: s = 8; universe@798: CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 3); universe@798: s = 10; universe@798: CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0); universe@798: s = -2; universe@798: CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0); universe@798: } universe@798: destroy_nodes_test_data(list); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_compare) { universe@798: void *la = create_nodes_test_data(4); universe@798: void *lb = create_nodes_test_data(3); universe@798: void *lc = create_nodes_test_data(4); universe@798: assign_nodes_test_data(la, 2, 4, 6, 8); universe@798: assign_nodes_test_data(lb, 2, 4, 6); universe@798: assign_nodes_test_data(lc, 2, 4, 6, 9); universe@798: CX_TEST_DO { universe@798: CX_TEST_ASSERT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int) > 0); universe@798: CX_TEST_ASSERT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int) < 0); universe@798: CX_TEST_ASSERT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int) > 0); universe@798: CX_TEST_ASSERT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int) < 0); universe@798: CX_TEST_ASSERT(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int) == 0); universe@798: } universe@798: destroy_nodes_test_data(la); universe@798: destroy_nodes_test_data(lb); universe@798: destroy_nodes_test_data(lc); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_add) { universe@798: node nodes[4]; universe@798: void *begin, *end; universe@798: CX_TEST_DO { universe@798: // test with begin, end / prev, next universe@798: memset(nodes, 0, sizeof(node)*4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[0].prev == NULL); universe@798: CX_TEST_ASSERT(nodes[0].next == NULL); universe@798: universe@798: cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[0].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == &nodes[0]); universe@798: universe@798: // test with begin only / prev, next universe@798: memset(nodes, 0, sizeof(node)*4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[0].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == &nodes[0]); universe@798: universe@798: cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[2].prev == &nodes[1]); universe@798: universe@798: // test with end only / prev, next universe@798: memset(nodes, 0, sizeof(node)*4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(end == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[0].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == &nodes[0]); universe@798: universe@798: cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]); universe@798: CX_TEST_ASSERT(end == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[2].prev == &nodes[1]); universe@798: universe@798: // test with begin, end / next universe@798: memset(nodes, 0, sizeof(node)*4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(end == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[0].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == NULL); universe@798: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_prepend) { universe@798: node nodes[4]; universe@798: void *begin, *end; universe@798: CX_TEST_DO { universe@798: // test with begin, end / prev, next universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[0].prev == NULL); universe@798: CX_TEST_ASSERT(nodes[0].next == NULL); universe@798: universe@798: cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(begin == &nodes[1]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[0].prev == &nodes[1]); universe@798: universe@798: // test with begin only / prev, next universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(begin == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[0].prev == &nodes[1]); universe@798: universe@798: cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]); universe@798: CX_TEST_ASSERT(begin == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[2].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == &nodes[2]); universe@798: universe@798: // test with end only / prev, next universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[0].prev == &nodes[1]); universe@798: universe@798: cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[2].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == &nodes[2]); universe@798: universe@798: // test with begin, end / next universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: end = begin = NULL; universe@798: universe@798: cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]); universe@798: cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]); universe@798: CX_TEST_ASSERT(begin == &nodes[2]); universe@798: CX_TEST_ASSERT(end == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[0]); universe@798: CX_TEST_ASSERT(nodes[2].next == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[1].prev == NULL); universe@798: CX_TEST_ASSERT(nodes[0].prev == NULL); universe@798: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_insert) { universe@798: node nodes[4]; universe@798: void *begin, *end; universe@798: CX_TEST_DO { universe@798: // insert mid list universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: begin = &nodes[0]; universe@798: end = &nodes[2]; universe@798: universe@798: cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); universe@798: universe@798: cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[2].prev == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[3].prev == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[3].next == &nodes[2]); universe@798: universe@798: // insert end universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: begin = &nodes[0]; universe@798: end = &nodes[2]; universe@798: universe@798: cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); universe@798: universe@798: cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[2].next == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[3].prev == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[3].next == NULL); universe@798: universe@798: // insert begin universe@798: memset(nodes, 0, sizeof(node) * 4); universe@798: begin = &nodes[0]; universe@798: end = &nodes[2]; universe@798: universe@798: cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); universe@798: universe@798: cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]); universe@798: CX_TEST_ASSERT(begin == &nodes[3]); universe@798: CX_TEST_ASSERT(end == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[0].prev == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[3].prev == NULL); universe@798: CX_TEST_ASSERT(nodes[3].next == &nodes[0]); universe@798: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_insert_chain) { universe@798: node nodes[5]; universe@798: void *begin, *end; universe@798: CX_TEST_DO { universe@798: // insert mid list universe@798: memset(nodes, 0, sizeof(node) * 5); universe@798: begin = &nodes[0]; end = &nodes[2]; universe@798: universe@798: cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); universe@798: universe@798: cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[1].next == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[2].prev == &nodes[4]); universe@798: CX_TEST_ASSERT(nodes[3].prev == &nodes[1]); universe@798: CX_TEST_ASSERT(nodes[4].next == &nodes[2]); universe@798: universe@798: // insert end universe@798: memset(nodes, 0, sizeof(node) * 5); universe@798: begin = &nodes[0]; end = &nodes[2]; universe@798: universe@798: cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); universe@798: universe@798: cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL); universe@798: CX_TEST_ASSERT(begin == &nodes[0]); universe@798: CX_TEST_ASSERT(end == &nodes[4]); universe@798: CX_TEST_ASSERT(nodes[2].next == &nodes[3]); universe@798: CX_TEST_ASSERT(nodes[3].prev == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[4].next == NULL); universe@798: universe@798: // insert begin universe@798: memset(nodes, 0, sizeof(node) * 5); universe@798: begin = &nodes[0]; end = &nodes[2]; universe@798: universe@798: cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); universe@798: cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); universe@798: universe@798: cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL); universe@798: CX_TEST_ASSERT(begin == &nodes[3]); universe@798: CX_TEST_ASSERT(end == &nodes[2]); universe@798: CX_TEST_ASSERT(nodes[0].prev == &nodes[4]); universe@798: CX_TEST_ASSERT(nodes[3].prev == NULL); universe@798: CX_TEST_ASSERT(nodes[4].next == &nodes[0]); universe@798: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_first) { universe@798: node *testdata = create_nodes_test_data(3); universe@798: void *begin = testdata; universe@798: CX_TEST_DO { universe@798: CX_TEST_ASSERT(begin == cx_linked_list_first(testdata, loc_prev)); universe@798: CX_TEST_ASSERT(begin == cx_linked_list_first(testdata->next, loc_prev)); universe@798: CX_TEST_ASSERT(begin == cx_linked_list_first(testdata->next->next, loc_prev)); universe@798: } universe@798: destroy_nodes_test_data(testdata); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_last) { universe@798: node *testdata = create_nodes_test_data(3); universe@798: void *end = testdata->next->next; universe@798: CX_TEST_DO { universe@798: CX_TEST_ASSERT(end == cx_linked_list_last(testdata, loc_next)); universe@798: CX_TEST_ASSERT(end == cx_linked_list_last(testdata->next, loc_next)); universe@798: CX_TEST_ASSERT(end == cx_linked_list_last(testdata->next->next, loc_next)); universe@798: } universe@798: destroy_nodes_test_data(testdata); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_prev) { universe@798: node *testdata = create_nodes_test_data(3); universe@798: CX_TEST_DO { universe@798: CX_TEST_ASSERT(cx_linked_list_prev(testdata, loc_next, testdata) == NULL); universe@798: CX_TEST_ASSERT(cx_linked_list_prev(testdata, loc_next, testdata->next) == testdata); universe@798: CX_TEST_ASSERT(cx_linked_list_prev(testdata, loc_next, testdata->next->next) == testdata->next); universe@798: } universe@798: destroy_nodes_test_data(testdata); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_remove) { universe@798: node *testdata = create_nodes_test_data(3); universe@798: assign_nodes_test_data(testdata, 2, 4, 6); universe@798: node *first = testdata; universe@798: node *second = first->next; universe@798: node *third = second->next; universe@798: void *begin = testdata; universe@798: void *end = third; universe@798: universe@798: CX_TEST_DO { universe@798: cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second); universe@798: CX_TEST_ASSERT(begin == first); universe@798: CX_TEST_ASSERT(end == third); universe@798: CX_TEST_ASSERT(first->prev == NULL); universe@798: CX_TEST_ASSERT(first->next == third); universe@798: CX_TEST_ASSERT(third->prev == first); universe@798: CX_TEST_ASSERT(third->next == NULL); universe@798: universe@798: cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third); universe@798: CX_TEST_ASSERT(begin == first); universe@798: CX_TEST_ASSERT(end == first); universe@798: CX_TEST_ASSERT(first->prev == NULL); universe@798: CX_TEST_ASSERT(first->next == NULL); universe@798: universe@798: cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first); universe@798: CX_TEST_ASSERT(begin == NULL); universe@798: CX_TEST_ASSERT(end == NULL); universe@798: } universe@798: // list is not intact anymore, we have to free nodes individually universe@798: free(first); universe@798: free(second); universe@798: free(third); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_size) { universe@798: node *td5 = create_nodes_test_data(5); universe@798: node *td13 = create_nodes_test_data(13); universe@798: CX_TEST_DO { universe@798: CX_TEST_ASSERT(cx_linked_list_size(NULL, loc_next) == 0); universe@798: CX_TEST_ASSERT(cx_linked_list_size(td5, loc_next) == 5); universe@798: CX_TEST_ASSERT(cx_linked_list_size(td13, loc_next) == 13); universe@798: } universe@798: destroy_nodes_test_data(td5); universe@798: destroy_nodes_test_data(td13); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_sort_empty) { universe@798: void *begin = NULL; universe@799: CX_TEST_DO { universe@799: // cannot assert something, we can just test that it does not crash universe@799: cx_linked_list_sort(&begin, NULL, loc_prev, loc_next, loc_data, cx_cmp_int); universe@799: CX_TEST_ASSERT(true); universe@799: } universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_sort) { universe@798: const size_t len = 1500; universe@798: int *testdata = int_test_data(len); universe@798: void *scrambled = create_nodes_test_data(len); universe@798: node *n = scrambled; universe@798: for (size_t i = 0; i < len; i++) { universe@798: n->data = testdata[i]; universe@798: n = n->next; universe@798: } universe@798: int *sorted = malloc(len*sizeof(int)); universe@798: memcpy(sorted, testdata, len*sizeof(int)); universe@798: qsort(sorted, len, sizeof(int), cx_cmp_int); universe@798: universe@798: void *begin = scrambled; universe@798: void *end = cx_linked_list_last(begin, loc_next); universe@798: universe@798: CX_TEST_DO { universe@798: cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, cx_cmp_int); universe@798: node *check = begin; universe@798: node *check_last = NULL; universe@798: for (size_t i = 0; i < len; i++) { universe@798: CX_TEST_ASSERT(check->data == sorted[i]); universe@798: CX_TEST_ASSERT(check->prev == check_last); universe@798: if (i < len - 1) { universe@798: CX_TEST_ASSERT(check->next != NULL); universe@798: } universe@798: check_last = check; universe@798: check = check->next; universe@798: } universe@798: CX_TEST_ASSERT(check == NULL); universe@798: CX_TEST_ASSERT(end == check_last); universe@798: } universe@799: destroy_nodes_test_data(begin); universe@799: free(sorted); universe@798: free(testdata); universe@798: } universe@798: universe@798: CX_TEST(test_linked_list_reverse) { universe@798: void *testdata = create_nodes_test_data(4); universe@798: void *expected = create_nodes_test_data(4); universe@798: assign_nodes_test_data(testdata, 2, 4, 6, 8); universe@798: assign_nodes_test_data(expected, 8, 6, 4, 2); universe@799: void *begin = testdata; universe@798: CX_TEST_DO { universe@798: void *end = cx_linked_list_last(begin, loc_next); universe@798: void *orig_begin = begin, *orig_end = end; universe@798: universe@798: cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); universe@798: CX_TEST_ASSERT(end == orig_begin); universe@798: CX_TEST_ASSERT(begin == orig_end); universe@798: CX_TEST_ASSERT(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data, cx_cmp_int)); universe@798: } universe@799: destroy_nodes_test_data(begin); universe@798: destroy_nodes_test_data(expected); universe@798: } universe@798: universe@800: universe@800: CX_TEST(test_empty_list_size) { universe@800: CX_TEST_DO { universe@800: CX_TEST_ASSERT(cxEmptyList->size == 0); universe@800: CX_TEST_ASSERT(cxListSize(cxEmptyList) == 0); universe@800: } universe@800: } universe@800: universe@800: CX_TEST(test_empty_list_iterator) { universe@800: CxList *list = cxEmptyList; universe@800: universe@800: CxIterator it1 = cxListIterator(list); universe@800: CxIterator it2 = cxListBackwardsIterator(list); universe@800: CxMutIterator it3 = cxListMutIterator(list); universe@800: CxMutIterator it4 = cxListMutBackwardsIterator(list); universe@800: universe@800: CX_TEST_DO { universe@800: CX_TEST_ASSERT(!cxIteratorValid(it1)); universe@800: CX_TEST_ASSERT(!cxIteratorValid(it2)); universe@800: CX_TEST_ASSERT(!cxIteratorValid(it3)); universe@800: CX_TEST_ASSERT(!cxIteratorValid(it4)); universe@800: universe@800: int c = 0; universe@800: cx_foreach(void*, data, it1) c++; universe@800: cx_foreach(void*, data, it2) c++; universe@800: cx_foreach(void*, data, it3) c++; universe@800: cx_foreach(void*, data, it4) c++; universe@800: CX_TEST_ASSERT(c == 0); universe@800: } universe@800: } universe@800: universe@800: CX_TEST(test_empty_list_noops) { universe@800: CX_TEST_DO { universe@800: CxList copy = *cxEmptyList; universe@800: cxListSort(cxEmptyList); universe@800: cxListClear(cxEmptyList); universe@800: cxListDestroy(cxEmptyList); universe@800: CX_TEST_ASSERT(0 == memcmp(©, cxEmptyList, sizeof(CxList))); // NOLINT(*-suspicious-memory-comparison) universe@800: } universe@800: } universe@800: universe@800: CX_TEST(test_empty_list_at) { universe@800: CX_TEST_DO { universe@800: CX_TEST_ASSERT(cxListAt(cxEmptyList, 0) == NULL); universe@800: CX_TEST_ASSERT(cxListAt(cxEmptyList, 1) == NULL); universe@800: } universe@800: } universe@800: universe@800: CX_TEST(test_empty_list_find) { universe@800: int x = 42, y = 1337; universe@800: CX_TEST_DO { universe@800: CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) < 0); universe@800: CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) < 0); universe@800: } universe@800: } universe@800: universe@800: CX_TEST(test_empty_list_compare) { universe@800: CxList *empty = cxEmptyList; universe@800: CxList *ll = cxLinkedListCreateSimple(sizeof(int)); universe@800: CxList *al = cxArrayListCreateSimple(sizeof(int), 8); universe@800: int x = 5; universe@800: CX_TEST_DO { universe@800: CX_TEST_ASSERT(0 == cxListCompare(empty, cxEmptyList)); universe@800: CX_TEST_ASSERT(0 == cxListCompare(ll, cxEmptyList)); universe@800: CX_TEST_ASSERT(0 == cxListCompare(al, cxEmptyList)); universe@800: CX_TEST_ASSERT(0 == cxListCompare(cxEmptyList, ll)); universe@800: CX_TEST_ASSERT(0 == cxListCompare(cxEmptyList, al)); universe@800: universe@800: cxListAdd(ll, &x); universe@800: cxListAdd(al, &x); universe@800: universe@800: CX_TEST_ASSERT(0 < cxListCompare(ll, cxEmptyList)); universe@800: CX_TEST_ASSERT(0 < cxListCompare(al, cxEmptyList)); universe@800: CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll)); universe@800: CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al)); universe@800: } universe@800: cxListDestroy(ll); universe@800: cxListDestroy(al); universe@800: } universe@800: universe@801: CX_TEST(test_list_ll_create) { universe@801: CxTestingAllocator talloc; universe@801: cx_testing_allocator_init(&talloc); universe@801: CxAllocator *alloc = &talloc.base; universe@801: CX_TEST_DO { universe@801: CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); universe@801: CX_TEST_ASSERT(list != NULL); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(int)); universe@801: CX_TEST_ASSERT(list->simple_destructor == NULL); universe@801: CX_TEST_ASSERT(list->advanced_destructor == NULL); universe@801: CX_TEST_ASSERT(list->destructor_data == NULL); universe@801: CX_TEST_ASSERT(cxListSize(list) == 0); universe@801: CX_TEST_ASSERT(list->allocator == alloc); universe@801: CX_TEST_ASSERT(list->cmpfunc == cx_cmp_int); universe@801: CX_TEST_ASSERT(!cxListIsStoringPointers(list)); universe@801: cxListDestroy(list); universe@801: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@801: } universe@801: cx_testing_allocator_destroy(&talloc); universe@801: } universe@801: universe@801: CX_TEST(test_list_ll_create_simple) { universe@801: CxList *list = cxLinkedListCreateSimple(sizeof(int)); universe@801: CX_TEST_DO { universe@801: CX_TEST_ASSERT(list != NULL); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(int)); universe@801: CX_TEST_ASSERT(list->simple_destructor == NULL); universe@801: CX_TEST_ASSERT(list->advanced_destructor == NULL); universe@801: CX_TEST_ASSERT(list->destructor_data == NULL); universe@801: CX_TEST_ASSERT(cxListSize(list) == 0); universe@801: CX_TEST_ASSERT(list->allocator == cxDefaultAllocator); universe@801: CX_TEST_ASSERT(list->cmpfunc == NULL); universe@801: CX_TEST_ASSERT(!cxListIsStoringPointers(list)); universe@801: } universe@801: cxListDestroy(list); universe@801: } universe@801: universe@801: CX_TEST(test_list_ll_store_pointers) { universe@801: CxList *list = cxLinkedListCreateSimple(47); universe@801: CX_TEST_DO { universe@801: CX_TEST_ASSERT(!cxListIsStoringPointers(list)); universe@801: cxListStorePointers(list); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(void *)); universe@801: CX_TEST_ASSERT(list->cl != NULL); universe@801: CX_TEST_ASSERT(list->climpl != NULL); universe@801: CX_TEST_ASSERT(cxListIsStoringPointers(list)); universe@801: cxListStoreObjects(list); universe@801: CX_TEST_ASSERT(list->cl != NULL); universe@801: CX_TEST_ASSERT(list->climpl == NULL); universe@801: CX_TEST_ASSERT(!cxListIsStoringPointers(list)); universe@801: } universe@801: cxListDestroy(list); universe@801: } universe@801: universe@801: CX_TEST(test_list_ll_create_simple_for_pointers) { universe@801: CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS); universe@801: CX_TEST_DO { universe@801: CX_TEST_ASSERT(list != NULL); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(void*)); universe@801: CX_TEST_ASSERT(list->simple_destructor == NULL); universe@801: CX_TEST_ASSERT(list->advanced_destructor == NULL); universe@801: CX_TEST_ASSERT(list->destructor_data == NULL); universe@801: CX_TEST_ASSERT(cxListSize(list) == 0); universe@801: CX_TEST_ASSERT(list->allocator == cxDefaultAllocator); universe@801: CX_TEST_ASSERT(list->cmpfunc == cx_cmp_ptr); universe@801: CX_TEST_ASSERT(cxListIsStoringPointers(list)); universe@801: } universe@801: cxListDestroy(list); universe@801: } universe@801: universe@801: CX_TEST(test_list_arl_create) { universe@801: CxTestingAllocator talloc; universe@801: cx_testing_allocator_init(&talloc); universe@801: CxAllocator *alloc = &talloc.base; universe@801: CX_TEST_DO { universe@801: CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); universe@801: CX_TEST_ASSERT(list != NULL); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(int)); universe@801: CX_TEST_ASSERT(list->simple_destructor == NULL); universe@801: CX_TEST_ASSERT(list->advanced_destructor == NULL); universe@801: CX_TEST_ASSERT(list->destructor_data == NULL); universe@801: CX_TEST_ASSERT(cxListSize(list) == 0); universe@801: CX_TEST_ASSERT(list->allocator == alloc); universe@801: CX_TEST_ASSERT(list->cmpfunc == cx_cmp_int); universe@801: CX_TEST_ASSERT(!cxListIsStoringPointers(list)); universe@801: cxListDestroy(list); universe@801: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@801: } universe@801: cx_testing_allocator_destroy(&talloc); universe@801: } universe@801: universe@801: CX_TEST(test_list_arl_create_simple) { universe@801: CxList *list = cxArrayListCreateSimple(sizeof(int), 8); universe@801: CX_TEST_DO { universe@801: CX_TEST_ASSERT(list != NULL); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(int)); universe@801: CX_TEST_ASSERT(list->simple_destructor == NULL); universe@801: CX_TEST_ASSERT(list->advanced_destructor == NULL); universe@801: CX_TEST_ASSERT(list->destructor_data == NULL); universe@801: CX_TEST_ASSERT(cxListSize(list) == 0); universe@801: CX_TEST_ASSERT(list->allocator == cxDefaultAllocator); universe@801: CX_TEST_ASSERT(list->cmpfunc == NULL); universe@801: CX_TEST_ASSERT(!cxListIsStoringPointers(list)); universe@801: } universe@801: cxListDestroy(list); universe@801: } universe@801: universe@801: CX_TEST(test_list_arl_create_simple_for_pointers) { universe@801: CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8); universe@801: CX_TEST_DO { universe@801: CX_TEST_ASSERT(list != NULL); universe@801: CX_TEST_ASSERT(list->item_size == sizeof(void*)); universe@801: CX_TEST_ASSERT(list->simple_destructor == NULL); universe@801: CX_TEST_ASSERT(list->advanced_destructor == NULL); universe@801: CX_TEST_ASSERT(list->destructor_data == NULL); universe@801: CX_TEST_ASSERT(cxListSize(list) == 0); universe@801: CX_TEST_ASSERT(list->allocator == cxDefaultAllocator); universe@801: CX_TEST_ASSERT(list->cmpfunc == cx_cmp_ptr); universe@801: CX_TEST_ASSERT(cxListIsStoringPointers(list)); universe@801: } universe@801: cxListDestroy(list); universe@801: } universe@801: universe@801: static void test_fake_simple_int_destr(void *elem) { universe@801: *(int *) elem = 42; universe@801: } universe@801: universe@801: CX_TEST(test_list_pll_destroy_no_destr) { universe@801: CxTestingAllocator talloc; universe@801: cx_testing_allocator_init(&talloc); universe@801: CxAllocator *alloc = &talloc.base; universe@801: CX_TEST_DO { universe@801: void *item = cxMalloc(alloc, sizeof(int)); universe@801: CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); universe@801: cxListAdd(list, item); universe@801: CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); universe@801: cxListDestroy(list); universe@801: // item is not yet freed universe@801: CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); universe@801: cxFree(alloc, item); universe@801: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@801: } universe@801: cx_testing_allocator_destroy(&talloc); universe@801: } universe@801: universe@801: CX_TEST(test_list_pll_destroy_simple_destr) { universe@801: CX_TEST_DO { universe@801: int item = 0; universe@801: CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); universe@801: list->simple_destructor = test_fake_simple_int_destr; universe@801: cxListAdd(list, &item); universe@801: cxListDestroy(list); universe@801: CX_TEST_ASSERT(item == 42); universe@801: } universe@801: } universe@801: universe@801: CX_TEST(test_list_pll_destroy_adv_destr) { universe@801: CxTestingAllocator talloc; universe@801: cx_testing_allocator_init(&talloc); universe@801: CxAllocator *alloc = &talloc.base; universe@801: CX_TEST_DO { universe@801: void *item = cxMalloc(alloc, sizeof(int)); universe@801: CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); universe@801: list->destructor_data = alloc; universe@801: list->advanced_destructor = (cx_destructor_func2) cxFree; universe@801: cxListAdd(list, item); universe@801: CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); universe@801: cxListDestroy(list); universe@801: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@801: } universe@801: cx_testing_allocator_destroy(&talloc); universe@801: } universe@801: universe@801: CX_TEST(test_list_parl_destroy_no_destr) { universe@801: CxTestingAllocator talloc; universe@801: cx_testing_allocator_init(&talloc); universe@801: CxAllocator *alloc = &talloc.base; universe@801: CX_TEST_DO { universe@801: void *item = cxMalloc(alloc, sizeof(int)); universe@801: CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); universe@801: cxListAdd(list, item); universe@801: CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); universe@801: cxListDestroy(list); universe@801: // item is not yet freed universe@801: CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); universe@801: cxFree(alloc, item); universe@801: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@801: } universe@801: cx_testing_allocator_destroy(&talloc); universe@801: } universe@801: universe@801: CX_TEST(test_list_parl_destroy_simple_destr) { universe@801: CX_TEST_DO { universe@801: int item = 0; universe@801: CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); universe@801: list->simple_destructor = test_fake_simple_int_destr; universe@801: cxListAdd(list, &item); universe@801: cxListDestroy(list); universe@801: CX_TEST_ASSERT(item == 42); universe@801: } universe@801: } universe@801: universe@801: CX_TEST(test_list_parl_destroy_adv_destr) { universe@801: CxTestingAllocator talloc; universe@801: cx_testing_allocator_init(&talloc); universe@801: CxAllocator *alloc = &talloc.base; universe@801: CX_TEST_DO { universe@801: void *item = cxMalloc(alloc, sizeof(int)); universe@801: CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); universe@801: list->destructor_data = alloc; universe@801: list->advanced_destructor = (cx_destructor_func2) cxFree; universe@801: cxListAdd(list, item); universe@801: CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); universe@801: cxListDestroy(list); universe@801: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); universe@801: } universe@801: cx_testing_allocator_destroy(&talloc); universe@801: } universe@801: universe@803: #define set_up_combo \ universe@803: CxTestingAllocator talloc; \ universe@803: cx_testing_allocator_init(&talloc); \ universe@803: CxAllocator *alloc = &talloc.base; \ universe@803: CX_TEST_DO { universe@803: #define tear_down_combo \ universe@803: cxListDestroy(list); \ universe@803: CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\ universe@803: } \ universe@803: cx_testing_allocator_destroy(&talloc); universe@803: #define roll_out_test_combos(name, body) \ universe@803: static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ universe@807: __attribute__((__unused__)) bool isptrlist) body \ universe@803: CX_TEST(test_list_ll_##name) { \ universe@803: set_up_combo \ universe@803: CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ universe@807: CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ universe@803: tear_down_combo \ universe@803: } \ universe@803: CX_TEST(test_list_arl_##name) { \ universe@803: set_up_combo \ universe@803: CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \ universe@807: CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ universe@803: tear_down_combo \ universe@803: } \ universe@803: CX_TEST(test_list_pll_##name) { \ universe@803: set_up_combo \ universe@803: CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ universe@807: CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ universe@803: tear_down_combo \ universe@803: } \ universe@803: CX_TEST(test_list_parl_##name) { \ universe@803: set_up_combo \ universe@803: CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ universe@807: CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ universe@803: tear_down_combo \ universe@803: } universe@803: #define array_init(...) {__VA_ARGS__} universe@803: universe@803: static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) { universe@803: int *testdata = int_test_data(len); universe@803: if (isptrlist) { universe@803: cx_for_n(i, len) { universe@803: cxListAdd(list, &testdata[i]); universe@803: } universe@803: } else { universe@803: cxListAddArray(list, testdata, len); universe@803: } universe@803: return testdata; universe@803: } universe@803: universe@803: roll_out_test_combos(add, { universe@803: const size_t len = 250; universe@803: int *testdata = int_test_data(len); universe@803: cx_for_n (i, len) CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == len); universe@803: cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); universe@803: cx_for_n (i, len) ++testdata[i]; universe@803: if (isptrlist) { universe@803: cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); universe@803: } else { universe@803: cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1); universe@803: } universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(insert, { universe@803: int a = 5; universe@803: int b = 47; universe@803: int c = 13; universe@803: int d = 42; universe@803: CX_TEST_ASSERT(cxListInsert(list, 1, &a) != 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == 0); universe@803: CX_TEST_ASSERT(cxListInsert(list, 0, &a) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == 1); universe@803: CX_TEST_ASSERT(cxListInsert(list, 0, &b) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == 2); universe@803: CX_TEST_ASSERT(cxListInsert(list, 1, &c) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == 3); universe@803: CX_TEST_ASSERT(cxListInsert(list, 3, &d) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == 4); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 47); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 13); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 5); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 42); universe@803: }) universe@803: universe@803: roll_out_test_combos(insert_array, { universe@803: int a[5] = array_init(5, 47, 11, 13, 42); universe@803: int b[5] = array_init(9, 18, 72, 50, 7); universe@803: int *aptr[5]; universe@803: int *bptr[5]; universe@803: cx_for_n(i, 5) { universe@803: aptr[i] = &a[i]; universe@803: bptr[i] = &b[i]; universe@803: } universe@803: universe@803: size_t inserted; universe@803: universe@803: if (isptrlist) { universe@803: inserted = cxListInsertArray(list, 0, aptr, 5); universe@803: } else { universe@803: inserted = cxListInsertArray(list, 0, a, 5); universe@803: } universe@803: CX_TEST_ASSERT(inserted == 5); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 5); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 47); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 11); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 13); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == 42); universe@803: if (isptrlist) { universe@803: inserted = cxListInsertArray(list, 3, bptr, 5); universe@803: } else { universe@803: inserted = cxListInsertArray(list, 3, b, 5); universe@803: } universe@803: CX_TEST_ASSERT(inserted == 5); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 5); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 47); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 11); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 9); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == 18); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 5) == 72); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 6) == 50); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 7) == 7); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 8) == 13); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 9) == 42); universe@803: }) universe@803: universe@803: roll_out_test_combos(remove, { universe@803: const size_t testdata_len = 32; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); universe@803: universe@803: CX_TEST_ASSERT(cxListRemove(list, 2) == 0); universe@803: CX_TEST_ASSERT(cxListRemove(list, 4) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == testdata_len - 2); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == testdata[0]); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == testdata[1]); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == testdata[3]); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == testdata[4]); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == testdata[6]); universe@803: CX_TEST_ASSERT(cxListRemove(list, 0) == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == testdata_len - 3); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == testdata[1]); universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == testdata[3]); universe@803: CX_TEST_ASSERT(cxListRemove(list, testdata_len) != 0); universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(find_remove, { universe@803: const size_t testdata_len = 250; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); universe@803: universe@803: int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) universe@803: int val = testdata[exp]; universe@803: // randomly picked number could occur earlier in list - find first position universe@803: for (int i = 0 ; i < exp ; i++) { universe@803: if (testdata[i] == val) { universe@803: exp = i; universe@803: break; universe@803: } universe@803: } universe@803: CX_TEST_ASSERT(cxListSize(list) == testdata_len); universe@803: CX_TEST_ASSERT(cxListFind(list, &val) == exp); universe@803: CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp); universe@803: CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); universe@803: CX_TEST_ASSERT(cxListFind(list, &val) != exp); universe@803: universe@803: int notinlist = -1; universe@803: CX_TEST_ASSERT(cxListFindRemove(list, ¬inlist) < 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); universe@803: universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(clear, { universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, 8); universe@803: CX_TEST_ASSERT(cxListSize(list) > 0); universe@803: cxListClear(list); universe@803: CX_TEST_ASSERT(cxListSize(list) == 0); universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(at, { universe@803: size_t len = 128; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, 128); universe@803: CX_TEST_ASSERT(cxListSize(list) == len); universe@803: cx_for_n (i, len) { universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); universe@803: } universe@803: CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL); universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(swap, { universe@803: int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); universe@803: int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13); universe@803: universe@803: cx_for_n(i, 16) { universe@803: cxListAdd(list, &original[i]); universe@803: } universe@803: universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 1, 4)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 2, 14)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 9, 6)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 3, 3)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 10, 11)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 8, 0)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 7, 12)); universe@803: CX_TEST_ASSERT(0 == cxListSwap(list, 13, 15)); universe@803: universe@803: CX_TEST_ASSERT(0 != cxListSwap(list, 5, 16)); universe@803: CX_TEST_ASSERT(0 != cxListSwap(list, 16, 6)); universe@803: CX_TEST_ASSERT(0 != cxListSwap(list, 16, 17)); universe@803: universe@803: CxIterator iter = cxListIterator(list); universe@803: cx_foreach(int*, e, iter) { universe@803: CX_TEST_ASSERT(*e == swapped[iter.index]); universe@803: } universe@803: iter = cxListBackwardsIterator(list); universe@803: cx_foreach(int*, e, iter) { universe@803: CX_TEST_ASSERT(*e == swapped[iter.index]); universe@803: } universe@803: }) universe@803: universe@807: CX_TEST(test_list_ll_swap_no_sbo) { universe@807: set_up_combo universe@807: CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, 2*cx_linked_list_swap_sbo_size); universe@807: CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, false); universe@807: tear_down_combo universe@807: } universe@807: CX_TEST(test_list_arl_swap_no_sbo) { universe@807: set_up_combo universe@807: CxList *list = cxArrayListCreate(alloc, cx_cmp_int, 2*cx_array_swap_sbo_size, 8); universe@807: CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, false); universe@807: tear_down_combo universe@807: } universe@803: universe@803: roll_out_test_combos(find, { universe@803: const size_t testdata_len = 500; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); universe@803: universe@803: cx_for_n (attempt, 25) { universe@803: int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) universe@803: int val = testdata[exp]; universe@803: // randomly picked number could occur earlier in list - find first position universe@803: for (int i = 0 ; i < exp ; i++) { universe@803: if (testdata[i] == val) { universe@803: exp = i; universe@803: break; universe@803: } universe@803: } universe@803: CX_TEST_ASSERT(cxListFind(list, &val) == exp); universe@803: } universe@803: universe@803: int notinlist = -1; universe@803: CX_TEST_ASSERT(cxListFind(list, ¬inlist) < 0); universe@803: universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(sort, { universe@803: const size_t testdata_len = 250; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); universe@803: int *expected = malloc(testdata_len*sizeof(int)); universe@803: memcpy(expected, testdata, testdata_len*sizeof(int)); universe@803: qsort(expected, testdata_len, sizeof(int), cx_cmp_int); universe@803: universe@803: cxListSort(list); universe@803: cx_for_n (i, testdata_len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]); universe@803: universe@803: free(expected); universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(reverse, { universe@803: const size_t testdata_len = 50; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); universe@803: cxListReverse(list); universe@803: cx_for_n(i, testdata_len) { universe@803: CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[testdata_len - 1 - i]); universe@803: } universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(iterator, { universe@803: const size_t len = 50; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, len); universe@803: universe@803: CxIterator iter = cxListIterator(list); universe@803: size_t i = 0; universe@803: cx_foreach(int*, x, iter) { universe@803: CX_TEST_ASSERT(i == iter.index); universe@803: CX_TEST_ASSERT(*x == testdata[iter.index]); universe@803: i++; universe@803: } universe@803: CX_TEST_ASSERT(i == cxListSize(list)); universe@803: iter = cxListBackwardsIterator(list); universe@803: cx_foreach(int*, x, iter) { universe@803: CX_TEST_ASSERT(i - 1 == iter.index); universe@803: CX_TEST_ASSERT(*x == testdata[iter.index]); universe@803: i--; universe@803: } universe@803: CX_TEST_ASSERT(i == 0); universe@803: i = len / 2; universe@803: CxMutIterator mut_iter = cxListMutIteratorAt(list, i); universe@803: size_t j = 0; universe@803: cx_foreach(int*, x, mut_iter) { universe@803: CX_TEST_ASSERT(mut_iter.index == len / 2 + j / 2); universe@803: CX_TEST_ASSERT(*x == testdata[i]); universe@803: if (i % 2 == 1) cxIteratorFlagRemoval(mut_iter); universe@803: i++; universe@803: j++; universe@803: } universe@803: CX_TEST_ASSERT(i == len); universe@803: i = len / 2; universe@803: j = 0; universe@803: mut_iter = cxListMutBackwardsIteratorAt(list, i - 1); universe@803: cx_foreach(int*, x, mut_iter) { universe@803: CX_TEST_ASSERT(mut_iter.index == len / 2 - 1 - j); universe@803: CX_TEST_ASSERT(*x == testdata[i - 1]); universe@803: if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter); universe@803: i--; universe@803: j++; universe@803: } universe@803: CX_TEST_ASSERT(i == 0); universe@803: CX_TEST_ASSERT(cxListSize(list) == len / 2); universe@803: cx_for_n(k, len / 2) CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]); universe@803: universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(insert_with_iterator, { universe@803: int fivenums[] = array_init(0, 1, 2, 3, 4); universe@803: cx_for_n(i, 5) cxListAdd(list, &fivenums[i]); universe@803: int newdata[] = array_init(10, 20, 30, 40, 50); universe@803: universe@803: CxMutIterator iter = cxListMutIteratorAt(list, 2); universe@803: CX_TEST_ASSERT(cxIteratorValid(iter)); universe@803: CX_TEST_ASSERT(iter.index == 2); universe@803: CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); universe@803: cxListInsertAfter(&iter, &newdata[0]); universe@803: CX_TEST_ASSERT(cxIteratorValid(iter)); universe@803: CX_TEST_ASSERT(iter.index == 2); universe@803: CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); universe@803: cxListInsertBefore(&iter, &newdata[1]); universe@803: CX_TEST_ASSERT(cxIteratorValid(iter)); universe@803: CX_TEST_ASSERT(iter.index == 3); universe@803: CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); universe@803: universe@803: iter = cxListMutIterator(list); universe@803: cxListInsertBefore(&iter, &newdata[2]); universe@803: CX_TEST_ASSERT(cxIteratorValid(iter)); universe@803: CX_TEST_ASSERT(iter.index == 1); universe@803: CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 0); universe@803: iter = cxListMutIteratorAt(list, cxListSize(list)); universe@803: cxListInsertBefore(&iter, &newdata[3]); universe@803: CX_TEST_ASSERT(!cxIteratorValid(iter)); universe@803: CX_TEST_ASSERT(iter.index == 9); universe@803: iter = cxListMutIteratorAt(list, cxListSize(list)); universe@803: cxListInsertAfter(&iter, &newdata[4]); universe@803: CX_TEST_ASSERT(!cxIteratorValid(iter)); universe@803: CX_TEST_ASSERT(iter.index == 10); universe@803: universe@803: int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50); universe@803: cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]); universe@803: }) universe@803: universe@803: static CX_TEST_SUBROUTINE(test_list_verify_compare, CxList *left, CxList *right) { universe@803: CX_TEST_ASSERTM(cxListCompare(left, right) == 0, "lists don't start identical"); universe@803: int x = 42; universe@803: cxListAdd(left, &x); universe@803: CX_TEST_ASSERT(cxListSize(left) > cxListSize(right)); universe@803: CX_TEST_ASSERT(cxListCompare(left, right) > 0); universe@803: CX_TEST_ASSERT(cxListCompare(right, left) < 0); universe@803: cxListAdd(right, &x); universe@803: CX_TEST_ASSERT(cxListSize(left) == cxListSize(right)); universe@803: CX_TEST_ASSERT(cxListCompare(left, right) == 0); universe@803: int a = 5, b = 10; universe@803: cxListInsert(left, 15, &a); universe@803: cxListInsert(right, 15, &b); universe@803: CX_TEST_ASSERT(cxListSize(left) == cxListSize(right)); universe@803: CX_TEST_ASSERT(cxListCompare(left, right) < 0); universe@803: CX_TEST_ASSERT(cxListCompare(right, left) > 0); universe@803: *(int *) cxListAt(left, 15) = 10; universe@803: CX_TEST_ASSERT(cxListCompare(left, right) == 0); universe@803: } universe@803: universe@803: #define roll_out_compare_tests(suffix, otherctr) \ universe@803: roll_out_test_combos(compare_##suffix, { \ universe@803: const size_t len = 47; \ universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ universe@803: CxList *other = otherctr; \ universe@803: cx_for_n(i, len) cxListAdd(other, &testdata[i]); \ universe@803: CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ universe@803: cxListDestroy(other); \ universe@803: free(testdata); \ universe@803: }) universe@803: universe@803: roll_out_compare_tests( universe@803: ll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)) universe@803: ) universe@803: universe@803: roll_out_compare_tests( universe@803: pll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS) universe@803: ) universe@803: universe@803: roll_out_compare_tests( universe@803: arl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50) universe@803: ) universe@803: universe@803: roll_out_compare_tests( universe@803: parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50) universe@803: ) universe@803: universe@803: static unsigned destr_test_ctr; universe@803: static int destr_last_value; universe@803: universe@803: static void simple_destr_test_fun(void *data) { universe@803: int *ptr = data; universe@803: destr_last_value = *ptr; universe@803: *ptr = destr_last_value + 1; universe@803: destr_test_ctr++; universe@803: } universe@803: universe@803: static void advanced_destr_test_fun(__attribute__((__unused__)) void *u, void *data) { universe@803: simple_destr_test_fun(data); universe@803: } universe@803: universe@803: static CX_TEST_SUBROUTINE(test_list_verify_destructor, CxList *list, universe@803: int const *testdata, size_t testdata_len) { universe@803: destr_test_ctr = 0; universe@803: universe@803: int off = cxListIsStoringPointers(list) ? 1 : 0; universe@803: universe@803: cxListRemove(list, 15); universe@803: CX_TEST_ASSERT(1 == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[15] == destr_last_value + off); universe@803: CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); universe@803: cxListRemove(list, 47); universe@803: CX_TEST_ASSERT(2 == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[48] == destr_last_value + off); universe@803: CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); universe@803: universe@803: CxMutIterator iter = cxListMutIteratorAt(list, 7); universe@803: cxIteratorNext(iter); universe@803: CX_TEST_ASSERT(2 == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[48] == destr_last_value + off); universe@803: CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); universe@803: cxIteratorFlagRemoval(iter); universe@803: cxIteratorNext(iter); universe@803: CX_TEST_ASSERT(3 == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[8] == destr_last_value + off); universe@803: CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); universe@803: universe@803: iter = cxListMutBackwardsIteratorAt(list, 5); universe@803: cxIteratorNext(iter); universe@803: CX_TEST_ASSERT(3 == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[8] == destr_last_value + off); universe@803: CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); universe@803: cxIteratorFlagRemoval(iter); universe@803: cxIteratorNext(iter); universe@803: CX_TEST_ASSERT(4 == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[4] == destr_last_value + off); universe@803: CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); universe@803: universe@803: cxListClear(list); universe@803: CX_TEST_ASSERT(testdata_len == destr_test_ctr); universe@803: CX_TEST_ASSERT(testdata[testdata_len - 1] == destr_last_value + off); universe@803: } universe@803: universe@803: roll_out_test_combos(simple_destr, { universe@803: const size_t len = 60; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, len); universe@803: list->simple_destructor = simple_destr_test_fun; universe@803: CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); universe@803: free(testdata); universe@803: }) universe@803: universe@803: roll_out_test_combos(advanced_destr, { universe@803: const size_t len = 75; universe@803: int *testdata = int_test_data_added_to_list(list, isptrlist, len); universe@803: list->advanced_destructor = advanced_destr_test_fun; universe@803: CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); universe@803: free(testdata); universe@803: }) universe@803: universe@798: CxTestSuite *cx_test_suite_array_list(void) { universe@798: CxTestSuite *suite = cx_test_suite_new("array_list"); universe@798: universe@818: cx_test_register(suite, test_array_add); universe@818: universe@801: cx_test_register(suite, test_list_arl_create); universe@801: cx_test_register(suite, test_list_arl_create_simple); universe@801: cx_test_register(suite, test_list_arl_create_simple_for_pointers); universe@801: cx_test_register(suite, test_list_parl_destroy_no_destr); universe@801: cx_test_register(suite, test_list_parl_destroy_simple_destr); universe@801: cx_test_register(suite, test_list_parl_destroy_adv_destr); universe@801: universe@803: cx_test_register(suite, test_list_arl_add); universe@803: cx_test_register(suite, test_list_parl_add); universe@803: cx_test_register(suite, test_list_arl_insert); universe@803: cx_test_register(suite, test_list_parl_insert); universe@803: cx_test_register(suite, test_list_arl_insert_array); universe@803: cx_test_register(suite, test_list_parl_insert_array); universe@803: cx_test_register(suite, test_list_arl_remove); universe@803: cx_test_register(suite, test_list_parl_remove); universe@803: cx_test_register(suite, test_list_arl_find_remove); universe@803: cx_test_register(suite, test_list_parl_find_remove); universe@803: cx_test_register(suite, test_list_arl_clear); universe@803: cx_test_register(suite, test_list_parl_clear); universe@803: cx_test_register(suite, test_list_arl_at); universe@803: cx_test_register(suite, test_list_parl_at); universe@803: cx_test_register(suite, test_list_arl_swap); universe@803: cx_test_register(suite, test_list_parl_swap); universe@803: cx_test_register(suite, test_list_arl_swap_no_sbo); universe@803: cx_test_register(suite, test_list_arl_find); universe@803: cx_test_register(suite, test_list_parl_find); universe@803: cx_test_register(suite, test_list_arl_sort); universe@803: cx_test_register(suite, test_list_parl_sort); universe@803: cx_test_register(suite, test_list_arl_reverse); universe@803: cx_test_register(suite, test_list_parl_reverse); universe@803: cx_test_register(suite, test_list_arl_iterator); universe@803: cx_test_register(suite, test_list_parl_iterator); universe@803: cx_test_register(suite, test_list_arl_insert_with_iterator); universe@803: cx_test_register(suite, test_list_parl_insert_with_iterator); universe@803: cx_test_register(suite, test_list_arl_compare_ll); universe@803: cx_test_register(suite, test_list_arl_compare_arl); universe@803: cx_test_register(suite, test_list_arl_compare_pll); universe@803: cx_test_register(suite, test_list_arl_compare_parl); universe@803: cx_test_register(suite, test_list_parl_compare_ll); universe@803: cx_test_register(suite, test_list_parl_compare_arl); universe@803: cx_test_register(suite, test_list_parl_compare_pll); universe@803: cx_test_register(suite, test_list_parl_compare_parl); universe@803: cx_test_register(suite, test_list_arl_simple_destr); universe@803: cx_test_register(suite, test_list_parl_simple_destr); universe@803: cx_test_register(suite, test_list_arl_advanced_destr); universe@803: cx_test_register(suite, test_list_parl_advanced_destr); universe@803: universe@798: return suite; universe@798: } universe@798: universe@798: CxTestSuite *cx_test_suite_linked_list(void) { universe@798: CxTestSuite *suite = cx_test_suite_new("linked_list"); universe@798: universe@798: cx_test_register(suite, test_linked_list_link_unlink); universe@798: cx_test_register(suite, test_linked_list_at); universe@798: cx_test_register(suite, test_linked_list_find); universe@798: cx_test_register(suite, test_linked_list_compare); universe@798: cx_test_register(suite, test_linked_list_add); universe@798: cx_test_register(suite, test_linked_list_prepend); universe@798: cx_test_register(suite, test_linked_list_insert); universe@798: cx_test_register(suite, test_linked_list_insert_chain); universe@798: cx_test_register(suite, test_linked_list_first); universe@798: cx_test_register(suite, test_linked_list_last); universe@798: cx_test_register(suite, test_linked_list_prev); universe@798: cx_test_register(suite, test_linked_list_remove); universe@798: cx_test_register(suite, test_linked_list_size); universe@798: cx_test_register(suite, test_linked_list_sort_empty); universe@798: cx_test_register(suite, test_linked_list_sort); universe@798: cx_test_register(suite, test_linked_list_reverse); universe@798: universe@801: cx_test_register(suite, test_list_ll_create); universe@801: cx_test_register(suite, test_list_ll_create_simple); universe@801: cx_test_register(suite, test_list_ll_store_pointers); universe@801: cx_test_register(suite, test_list_ll_create_simple_for_pointers); universe@801: cx_test_register(suite, test_list_pll_destroy_no_destr); universe@801: cx_test_register(suite, test_list_pll_destroy_simple_destr); universe@801: cx_test_register(suite, test_list_pll_destroy_adv_destr); universe@801: universe@803: cx_test_register(suite, test_list_ll_add); universe@803: cx_test_register(suite, test_list_pll_add); universe@803: cx_test_register(suite, test_list_ll_insert); universe@803: cx_test_register(suite, test_list_pll_insert); universe@803: cx_test_register(suite, test_list_ll_insert_array); universe@803: cx_test_register(suite, test_list_pll_insert_array); universe@803: cx_test_register(suite, test_list_ll_remove); universe@803: cx_test_register(suite, test_list_pll_remove); universe@803: cx_test_register(suite, test_list_ll_find_remove); universe@803: cx_test_register(suite, test_list_pll_find_remove); universe@803: cx_test_register(suite, test_list_ll_clear); universe@803: cx_test_register(suite, test_list_pll_clear); universe@803: cx_test_register(suite, test_list_ll_at); universe@803: cx_test_register(suite, test_list_pll_at); universe@803: cx_test_register(suite, test_list_ll_swap); universe@803: cx_test_register(suite, test_list_pll_swap); universe@803: cx_test_register(suite, test_list_ll_swap_no_sbo); universe@803: cx_test_register(suite, test_list_ll_find); universe@803: cx_test_register(suite, test_list_pll_find); universe@803: cx_test_register(suite, test_list_ll_sort); universe@803: cx_test_register(suite, test_list_pll_sort); universe@803: cx_test_register(suite, test_list_ll_reverse); universe@803: cx_test_register(suite, test_list_pll_reverse); universe@803: cx_test_register(suite, test_list_ll_iterator); universe@803: cx_test_register(suite, test_list_pll_iterator); universe@803: cx_test_register(suite, test_list_ll_insert_with_iterator); universe@803: cx_test_register(suite, test_list_pll_insert_with_iterator); universe@803: cx_test_register(suite, test_list_ll_compare_ll); universe@803: cx_test_register(suite, test_list_ll_compare_arl); universe@803: cx_test_register(suite, test_list_ll_compare_pll); universe@803: cx_test_register(suite, test_list_ll_compare_parl); universe@803: cx_test_register(suite, test_list_pll_compare_ll); universe@803: cx_test_register(suite, test_list_pll_compare_arl); universe@803: cx_test_register(suite, test_list_pll_compare_pll); universe@803: cx_test_register(suite, test_list_pll_compare_parl); universe@803: cx_test_register(suite, test_list_ll_simple_destr); universe@803: cx_test_register(suite, test_list_pll_simple_destr); universe@803: cx_test_register(suite, test_list_ll_advanced_destr); universe@803: cx_test_register(suite, test_list_pll_advanced_destr); universe@803: universe@798: return suite; universe@798: } universe@798: universe@800: CxTestSuite *cx_test_suite_empty_list(void) { universe@800: CxTestSuite *suite = cx_test_suite_new("empty list dummy"); universe@800: universe@800: cx_test_register(suite, test_empty_list_size); universe@800: cx_test_register(suite, test_empty_list_iterator); universe@800: cx_test_register(suite, test_empty_list_noops); universe@800: cx_test_register(suite, test_empty_list_at); universe@800: cx_test_register(suite, test_empty_list_find); universe@800: cx_test_register(suite, test_empty_list_compare); universe@800: universe@800: return suite; universe@800: }