Tue, 26 Nov 2024 22:00:03 +0100
missing comma in map iterator docs
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "cx/test.h" #include "util_allocator.h" #include "cx/compare.h" #include "cx/array_list.h" #include "cx/linked_list.h" #include <stdarg.h> CX_TEST(test_array_add) { CX_ARRAY_DECLARE(int, arr); arr = calloc(5, sizeof(int)); arr[0] = 2; arr[1] = 3; arr[2] = 5; arr[3] = 7; arr[4] = 11; arr_size = 3; arr_capacity = 5; int elem = 8, elem2 = 47; int result; CX_TEST_DO { result = cx_array_simple_add(arr, elem); CX_TEST_ASSERT(result == 0); CX_TEST_ASSERT(arr[0] == 2); CX_TEST_ASSERT(arr[1] == 3); CX_TEST_ASSERT(arr[2] == 5); CX_TEST_ASSERT(arr[3] == 8); CX_TEST_ASSERT(arr[4] == 11); CX_TEST_ASSERT(arr_size == 4); CX_TEST_ASSERT(arr_capacity == 5); arr_size = 5; result = cx_array_simple_add(arr, elem2); CX_TEST_ASSERT(result == 0); CX_TEST_ASSERT(arr[0] == 2); CX_TEST_ASSERT(arr[1] == 3); CX_TEST_ASSERT(arr[2] == 5); CX_TEST_ASSERT(arr[3] == 8); CX_TEST_ASSERT(arr[4] == 11); CX_TEST_ASSERT(arr[5] == 47); CX_TEST_ASSERT(arr_size == 6); CX_TEST_ASSERT(arr_capacity >= 6); } free(arr); } CX_TEST(test_array_insert_sorted) { int d1 = 50; int d2 = 80; int d3 = 60; int d4 = 40; int d5 = 70; int d6a[6] = {52, 54, 56, 62, 64, 75}; int d7a[6] = {51, 57, 58, 65, 77, 78}; int d8 = 90; int expected[18] = { 40, 50, 51, 52, 54, 56, 57, 58, 60, 62, 64, 65, 70, 75, 77, 78, 80, 90 }; CX_ARRAY_DECLARE(int, array); cx_array_initialize(array, 4); CX_TEST_DO { CX_TEST_ASSERT(0 == cx_array_simple_add_sorted(array, d1, cx_cmp_int)); CX_TEST_ASSERT(array_size == 1); CX_TEST_ASSERT(array_capacity == 4); CX_TEST_ASSERT(0 == cx_array_simple_add_sorted(array, d2, cx_cmp_int)); CX_TEST_ASSERT(array_size == 2); CX_TEST_ASSERT(array_capacity == 4); CX_TEST_ASSERT(0 == cx_array_simple_add_sorted(array, d3, cx_cmp_int)); CX_TEST_ASSERT(array_size == 3); CX_TEST_ASSERT(array_capacity == 4); CX_TEST_ASSERT(0 == cx_array_simple_add_sorted(array, d4, cx_cmp_int)); CX_TEST_ASSERT(array_size == 4); CX_TEST_ASSERT(array_capacity == 4); CX_TEST_ASSERT(0 == cx_array_simple_add_sorted(array, d5, cx_cmp_int)); CX_TEST_ASSERT(array_size == 5); CX_TEST_ASSERT(array_capacity >= 5); CX_TEST_ASSERT(0 == cx_array_simple_insert_sorted(array, d6a, 6, cx_cmp_int)); CX_TEST_ASSERT(array_size == 11); CX_TEST_ASSERT(array_capacity >= 11); CX_TEST_ASSERT(0 == cx_array_simple_insert_sorted(array, d7a, 6, cx_cmp_int)); CX_TEST_ASSERT(array_size == 17); CX_TEST_ASSERT(array_capacity >= 17); CX_TEST_ASSERT(0 == cx_array_simple_add_sorted(array, d8, cx_cmp_int)); CX_TEST_ASSERT(array_size == 18); CX_TEST_ASSERT(array_capacity >= 18); CX_TEST_ASSERT(0 == memcmp(array, expected, 18 * sizeof(int))); } free(array); } CX_TEST(test_array_binary_search) { int array[18] = { 40, 50, 51, 52, 54, 56, 57, 58, 60, 62, 64, 65, 70, 75, 77, 78, 80, 90 }; CX_TEST_DO { for (size_t i = 0; i < 18; i++) { CX_TEST_ASSERT(i == cx_array_binary_search(array, 18, sizeof(int), &array[i], cx_cmp_int)); } int s = 58; CX_TEST_ASSERT(7 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(7 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); s = 60; CX_TEST_ASSERT(8 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(8 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); s = 59; CX_TEST_ASSERT(7 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(8 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); s = 79; CX_TEST_ASSERT(15 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(16 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); s = 66; CX_TEST_ASSERT(11 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(12 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); s = 69; CX_TEST_ASSERT(11 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(12 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); s = 95; CX_TEST_ASSERT(17 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); s = 30; CX_TEST_ASSERT(18 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(0 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int)); CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); } } typedef struct node { struct node *next; struct node *prev; int data; } node; static int test_cmp_node(const void *l, const void *r) { const node *left = l; const node *right = r; return left->data - right->data; } const ptrdiff_t loc_prev = offsetof(struct node, prev); const ptrdiff_t loc_next = offsetof(struct node, next); const ptrdiff_t loc_data = offsetof(struct node, data); static node *create_nodes_test_data(size_t len) { node *begin = calloc(1, sizeof(node)); void *prev = begin; for (size_t i = 1; i < len; i++) { node *n = calloc(1, sizeof(node)); cx_linked_list_link(prev, n, loc_prev, loc_next); prev = n; } return begin; } void assign_nodes_test_data(node *n, ...) { va_list ap; va_start(ap, n); while (n != NULL) { n->data = va_arg(ap, int); n = n->next; } va_end(ap); } static void destroy_nodes_test_data(node *n) { while (n != NULL) { void *next = n->next; free(n); n = next; } } static int *int_test_data(size_t len) { int *data = malloc(len*sizeof(int)); for (size_t i = 0 ; i < len ; i++) { data[i] = rand(); // NOLINT(*-msc50-cpp) } return data; } CX_TEST(test_linked_list_link_unlink) { node a = {0}, b = {0}, c = {0}; CX_TEST_DO { cx_linked_list_link(&a, &b, loc_prev, loc_next); CX_TEST_ASSERT(a.prev == NULL); CX_TEST_ASSERT(a.next == &b); CX_TEST_ASSERT(b.prev == &a); CX_TEST_ASSERT(b.next == NULL); cx_linked_list_unlink(&a, &b, loc_prev, loc_next); CX_TEST_ASSERT(a.prev == NULL); CX_TEST_ASSERT(a.next == NULL); CX_TEST_ASSERT(b.prev == NULL); CX_TEST_ASSERT(b.next == NULL); cx_linked_list_link(&b, &c, loc_prev, loc_next); cx_linked_list_link(&a, &b, loc_prev, loc_next); cx_linked_list_unlink(&b, &c, loc_prev, loc_next); CX_TEST_ASSERT(a.prev == NULL); CX_TEST_ASSERT(a.next == &b); CX_TEST_ASSERT(b.prev == &a); CX_TEST_ASSERT(b.next == NULL); CX_TEST_ASSERT(c.prev == NULL); CX_TEST_ASSERT(c.next == NULL); } } CX_TEST(test_linked_list_at) { node a = {0}, b = {0}, c = {0}, d = {0}; cx_linked_list_link(&a, &b, loc_prev, loc_next); cx_linked_list_link(&b, &c, loc_prev, loc_next); cx_linked_list_link(&c, &d, loc_prev, loc_next); CX_TEST_DO { CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 0) == &a); CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 1) == &b); CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 2) == &c); CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 3) == &d); CX_TEST_ASSERT(cx_linked_list_at(&a, 0, loc_next, 4) == NULL); CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_prev, 0) == &a); CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 1) == &b); CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 2) == &c); CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 3) == &d); CX_TEST_ASSERT(cx_linked_list_at(&b, 1, loc_next, 4) == NULL); CX_TEST_ASSERT(cx_linked_list_at(&d, 3, loc_prev, 0) == &a); CX_TEST_ASSERT(cx_linked_list_at(&d, 3, loc_prev, 1) == &b); } } CX_TEST(test_linked_list_find) { void *list = create_nodes_test_data(4); assign_nodes_test_data(list, 2, 4, 6, 8); CX_TEST_DO { int s; s = 2; CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 0); s = 4; CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 1); s = 6; CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 2); s = 8; CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 3); s = 10; CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0); s = -2; CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0); } destroy_nodes_test_data(list); } CX_TEST(test_linked_list_compare) { void *la = create_nodes_test_data(4); void *lb = create_nodes_test_data(3); void *lc = create_nodes_test_data(4); assign_nodes_test_data(la, 2, 4, 6, 8); assign_nodes_test_data(lb, 2, 4, 6); assign_nodes_test_data(lc, 2, 4, 6, 9); CX_TEST_DO { CX_TEST_ASSERT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int) > 0); CX_TEST_ASSERT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int) < 0); CX_TEST_ASSERT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int) > 0); CX_TEST_ASSERT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int) < 0); CX_TEST_ASSERT(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int) == 0); } destroy_nodes_test_data(la); destroy_nodes_test_data(lb); destroy_nodes_test_data(lc); } CX_TEST(test_linked_list_add) { node nodes[4]; void *begin, *end; CX_TEST_DO { // test with begin, end / prev, next memset(nodes, 0, sizeof(node)*4); end = begin = NULL; cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[0]); CX_TEST_ASSERT(nodes[0].prev == NULL); CX_TEST_ASSERT(nodes[0].next == NULL); cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[1]); CX_TEST_ASSERT(nodes[0].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == &nodes[0]); // test with begin only / prev, next memset(nodes, 0, sizeof(node)*4); end = begin = NULL; cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); CX_TEST_ASSERT(begin == &nodes[0]); cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(nodes[0].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == &nodes[0]); cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); CX_TEST_ASSERT(nodes[1].next == &nodes[2]); CX_TEST_ASSERT(nodes[2].prev == &nodes[1]); // test with end only / prev, next memset(nodes, 0, sizeof(node)*4); end = begin = NULL; cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]); CX_TEST_ASSERT(end == &nodes[0]); cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]); CX_TEST_ASSERT(end == &nodes[1]); CX_TEST_ASSERT(nodes[0].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == &nodes[0]); cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]); CX_TEST_ASSERT(end == &nodes[2]); CX_TEST_ASSERT(nodes[1].next == &nodes[2]); CX_TEST_ASSERT(nodes[2].prev == &nodes[1]); // test with begin, end / next memset(nodes, 0, sizeof(node)*4); end = begin = NULL; cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[0]); cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); CX_TEST_ASSERT(end == &nodes[1]); CX_TEST_ASSERT(nodes[0].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == NULL); } } CX_TEST(test_linked_list_prepend) { node nodes[4]; void *begin, *end; CX_TEST_DO { // test with begin, end / prev, next memset(nodes, 0, sizeof(node) * 4); end = begin = NULL; cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[0]); CX_TEST_ASSERT(nodes[0].prev == NULL); CX_TEST_ASSERT(nodes[0].next == NULL); cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]); CX_TEST_ASSERT(begin == &nodes[1]); CX_TEST_ASSERT(end == &nodes[0]); CX_TEST_ASSERT(nodes[1].next == &nodes[0]); CX_TEST_ASSERT(nodes[0].prev == &nodes[1]); // test with begin only / prev, next memset(nodes, 0, sizeof(node) * 4); end = begin = NULL; cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]); CX_TEST_ASSERT(begin == &nodes[0]); cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]); CX_TEST_ASSERT(begin == &nodes[1]); CX_TEST_ASSERT(nodes[1].next == &nodes[0]); CX_TEST_ASSERT(nodes[0].prev == &nodes[1]); cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]); CX_TEST_ASSERT(begin == &nodes[2]); CX_TEST_ASSERT(nodes[2].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == &nodes[2]); // test with end only / prev, next memset(nodes, 0, sizeof(node) * 4); end = begin = NULL; cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]); CX_TEST_ASSERT(end == &nodes[0]); cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]); CX_TEST_ASSERT(end == &nodes[0]); CX_TEST_ASSERT(nodes[1].next == &nodes[0]); CX_TEST_ASSERT(nodes[0].prev == &nodes[1]); cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]); CX_TEST_ASSERT(end == &nodes[0]); CX_TEST_ASSERT(nodes[2].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == &nodes[2]); // test with begin, end / next memset(nodes, 0, sizeof(node) * 4); end = begin = NULL; cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[0]); cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]); cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]); CX_TEST_ASSERT(begin == &nodes[2]); CX_TEST_ASSERT(end == &nodes[0]); CX_TEST_ASSERT(nodes[1].next == &nodes[0]); CX_TEST_ASSERT(nodes[2].next == &nodes[1]); CX_TEST_ASSERT(nodes[1].prev == NULL); CX_TEST_ASSERT(nodes[0].prev == NULL); } } CX_TEST(test_linked_list_insert) { node nodes[4]; void *begin, *end; CX_TEST_DO { // insert mid list memset(nodes, 0, sizeof(node) * 4); begin = &nodes[0]; end = &nodes[2]; cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[2]); CX_TEST_ASSERT(nodes[1].next == &nodes[3]); CX_TEST_ASSERT(nodes[2].prev == &nodes[3]); CX_TEST_ASSERT(nodes[3].prev == &nodes[1]); CX_TEST_ASSERT(nodes[3].next == &nodes[2]); // insert end memset(nodes, 0, sizeof(node) * 4); begin = &nodes[0]; end = &nodes[2]; cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[3]); CX_TEST_ASSERT(nodes[2].next == &nodes[3]); CX_TEST_ASSERT(nodes[3].prev == &nodes[2]); CX_TEST_ASSERT(nodes[3].next == NULL); // insert begin memset(nodes, 0, sizeof(node) * 4); begin = &nodes[0]; end = &nodes[2]; cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]); CX_TEST_ASSERT(begin == &nodes[3]); CX_TEST_ASSERT(end == &nodes[2]); CX_TEST_ASSERT(nodes[0].prev == &nodes[3]); CX_TEST_ASSERT(nodes[3].prev == NULL); CX_TEST_ASSERT(nodes[3].next == &nodes[0]); } } CX_TEST(test_linked_list_insert_chain) { node nodes[5]; void *begin, *end; CX_TEST_DO { // insert mid list memset(nodes, 0, sizeof(node) * 5); begin = &nodes[0]; end = &nodes[2]; cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[2]); CX_TEST_ASSERT(nodes[1].next == &nodes[3]); CX_TEST_ASSERT(nodes[2].prev == &nodes[4]); CX_TEST_ASSERT(nodes[3].prev == &nodes[1]); CX_TEST_ASSERT(nodes[4].next == &nodes[2]); // insert end memset(nodes, 0, sizeof(node) * 5); begin = &nodes[0]; end = &nodes[2]; cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[4]); CX_TEST_ASSERT(nodes[2].next == &nodes[3]); CX_TEST_ASSERT(nodes[3].prev == &nodes[2]); CX_TEST_ASSERT(nodes[4].next == NULL); // insert begin memset(nodes, 0, sizeof(node) * 5); begin = &nodes[0]; end = &nodes[2]; cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next); cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next); cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next); cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL); CX_TEST_ASSERT(begin == &nodes[3]); CX_TEST_ASSERT(end == &nodes[2]); CX_TEST_ASSERT(nodes[0].prev == &nodes[4]); CX_TEST_ASSERT(nodes[3].prev == NULL); CX_TEST_ASSERT(nodes[4].next == &nodes[0]); } } CX_TEST(test_linked_list_insert_sorted) { node nodes[5] = {0}; void *begin, *end; nodes[0].data = 3; nodes[1].data = 6; nodes[2].data = 10; nodes[3].data = 11; nodes[4].data = 15; for (unsigned i = 0 ; i < 4 ; i++) { cx_linked_list_link(&nodes[i], &nodes[i+1], loc_prev, loc_next); } begin = &nodes[0]; end = &nodes[4]; CX_TEST_DO { // insert a single node node new_node = {0}; new_node.data = 5; cx_linked_list_insert_sorted( &begin, &end, loc_prev, loc_next, &new_node, test_cmp_node ); CX_TEST_ASSERT(new_node.prev == &nodes[0]); CX_TEST_ASSERT(new_node.next == &nodes[1]); CX_TEST_ASSERT(nodes[0].next == &new_node); CX_TEST_ASSERT(nodes[1].prev == &new_node); CX_TEST_ASSERT(begin == &nodes[0]); CX_TEST_ASSERT(end == &nodes[4]); // insert a new beginning node node new_begin = {0}; new_begin.data = 1; cx_linked_list_insert_sorted( &begin, &end, loc_prev, loc_next, &new_begin, test_cmp_node ); CX_TEST_ASSERT(new_begin.prev == NULL); CX_TEST_ASSERT(new_begin.next == &nodes[0]); CX_TEST_ASSERT(nodes[0].prev == &new_begin); CX_TEST_ASSERT(begin == &new_begin); CX_TEST_ASSERT(end == &nodes[4]); // now insert a chain node chain_start = {NULL, NULL, 8}; node chain_mid = {NULL, NULL, 14}; node chain_end = {NULL, NULL, 17}; cx_linked_list_link(&chain_start, &chain_mid, loc_prev, loc_next); cx_linked_list_link(&chain_mid, &chain_end, loc_prev, loc_next); cx_linked_list_insert_sorted_chain( &begin, &end, loc_prev, loc_next, &chain_start, test_cmp_node ); CX_TEST_ASSERT(chain_start.prev == &nodes[1]); CX_TEST_ASSERT(chain_start.next == &nodes[2]); CX_TEST_ASSERT(chain_mid.prev == &nodes[3]); CX_TEST_ASSERT(chain_mid.next == &nodes[4]); CX_TEST_ASSERT(chain_end.prev == &nodes[4]); CX_TEST_ASSERT(chain_end.next == NULL); CX_TEST_ASSERT(begin == &new_begin); CX_TEST_ASSERT(end == &chain_end); } } CX_TEST(test_linked_list_first) { node *testdata = create_nodes_test_data(3); void *begin = testdata; CX_TEST_DO { CX_TEST_ASSERT(begin == cx_linked_list_first(testdata, loc_prev)); CX_TEST_ASSERT(begin == cx_linked_list_first(testdata->next, loc_prev)); CX_TEST_ASSERT(begin == cx_linked_list_first(testdata->next->next, loc_prev)); } destroy_nodes_test_data(testdata); } CX_TEST(test_linked_list_last) { node *testdata = create_nodes_test_data(3); void *end = testdata->next->next; CX_TEST_DO { CX_TEST_ASSERT(end == cx_linked_list_last(testdata, loc_next)); CX_TEST_ASSERT(end == cx_linked_list_last(testdata->next, loc_next)); CX_TEST_ASSERT(end == cx_linked_list_last(testdata->next->next, loc_next)); } destroy_nodes_test_data(testdata); } CX_TEST(test_linked_list_prev) { node *testdata = create_nodes_test_data(3); CX_TEST_DO { CX_TEST_ASSERT(cx_linked_list_prev(testdata, loc_next, testdata) == NULL); CX_TEST_ASSERT(cx_linked_list_prev(testdata, loc_next, testdata->next) == testdata); CX_TEST_ASSERT(cx_linked_list_prev(testdata, loc_next, testdata->next->next) == testdata->next); } destroy_nodes_test_data(testdata); } CX_TEST(test_linked_list_remove) { node *testdata = create_nodes_test_data(3); assign_nodes_test_data(testdata, 2, 4, 6); node *first = testdata; node *second = first->next; node *third = second->next; void *begin = testdata; void *end = third; CX_TEST_DO { cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second); CX_TEST_ASSERT(begin == first); CX_TEST_ASSERT(end == third); CX_TEST_ASSERT(first->prev == NULL); CX_TEST_ASSERT(first->next == third); CX_TEST_ASSERT(third->prev == first); CX_TEST_ASSERT(third->next == NULL); cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third); CX_TEST_ASSERT(begin == first); CX_TEST_ASSERT(end == first); CX_TEST_ASSERT(first->prev == NULL); CX_TEST_ASSERT(first->next == NULL); cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first); CX_TEST_ASSERT(begin == NULL); CX_TEST_ASSERT(end == NULL); } // list is not intact anymore, we have to free nodes individually free(first); free(second); free(third); } CX_TEST(test_linked_list_remove_chain) { node *testdata = create_nodes_test_data(5); assign_nodes_test_data(testdata, 2, 4, 6, 8, 10); void *begin = testdata; void *end = cx_linked_list_last(testdata, loc_next); void *orig_end = end; // remember what we need to free node *kill_list[5]; kill_list[0] = testdata; for (unsigned i = 1 ; i < 5 ; i++) { kill_list[i] = kill_list[i-1]->next; } CX_TEST_DO { // remove chain, but pretend that we don't have a prev pointer! size_t result = cx_linked_list_remove_chain( &begin, &end, -1, loc_next, cx_linked_list_at(begin, 0, loc_next, 2), 2 ); CX_TEST_ASSERT(result == 2); CX_TEST_ASSERT(begin == testdata); CX_TEST_ASSERT(end == orig_end); node *new_idx2 = cx_linked_list_at(begin, 0, loc_next, 2); CX_TEST_ASSERT(new_idx2->data == 10); CX_TEST_ASSERT(new_idx2->next == NULL); // we pretended we don't have prev, so it still points to 8! CX_TEST_ASSERT(new_idx2->prev->data == 8); // remove last elements and try to remove more than we have result = cx_linked_list_remove_chain( &begin, &end, -1, loc_next, cx_linked_list_at(begin, 0, loc_next, 2), 2 ); CX_TEST_ASSERT(result == 1); CX_TEST_ASSERT(begin == testdata); CX_TEST_ASSERT(end == testdata->next); CX_TEST_ASSERT(NULL == testdata->next->next); } for (unsigned i = 0 ; i < 5 ; i++) { free(kill_list[i]); } } CX_TEST(test_linked_list_size) { node *td5 = create_nodes_test_data(5); node *td13 = create_nodes_test_data(13); CX_TEST_DO { CX_TEST_ASSERT(cx_linked_list_size(NULL, loc_next) == 0); CX_TEST_ASSERT(cx_linked_list_size(td5, loc_next) == 5); CX_TEST_ASSERT(cx_linked_list_size(td13, loc_next) == 13); } destroy_nodes_test_data(td5); destroy_nodes_test_data(td13); } CX_TEST(test_linked_list_sort_empty) { void *begin = NULL; CX_TEST_DO { // cannot assert something, we can just test that it does not crash cx_linked_list_sort(&begin, NULL, loc_prev, loc_next, loc_data, cx_cmp_int); CX_TEST_ASSERT(true); } } CX_TEST(test_linked_list_sort) { const size_t len = 1500; int *testdata = int_test_data(len); void *scrambled = create_nodes_test_data(len); node *n = scrambled; for (size_t i = 0; i < len; i++) { n->data = testdata[i]; n = n->next; } int *sorted = malloc(len*sizeof(int)); memcpy(sorted, testdata, len*sizeof(int)); qsort(sorted, len, sizeof(int), cx_cmp_int); void *begin = scrambled; void *end = cx_linked_list_last(begin, loc_next); CX_TEST_DO { cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, cx_cmp_int); node *check = begin; node *check_last = NULL; for (size_t i = 0; i < len; i++) { CX_TEST_ASSERT(check->data == sorted[i]); CX_TEST_ASSERT(check->prev == check_last); if (i < len - 1) { CX_TEST_ASSERT(check->next != NULL); } check_last = check; check = check->next; } CX_TEST_ASSERT(check == NULL); CX_TEST_ASSERT(end == check_last); } destroy_nodes_test_data(begin); free(sorted); free(testdata); } CX_TEST(test_linked_list_reverse) { void *testdata = create_nodes_test_data(4); void *expected = create_nodes_test_data(4); assign_nodes_test_data(testdata, 2, 4, 6, 8); assign_nodes_test_data(expected, 8, 6, 4, 2); void *begin = testdata; CX_TEST_DO { void *end = cx_linked_list_last(begin, loc_next); void *orig_begin = begin, *orig_end = end; cx_linked_list_reverse(&begin, &end, loc_prev, loc_next); CX_TEST_ASSERT(end == orig_begin); CX_TEST_ASSERT(begin == orig_end); CX_TEST_ASSERT(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data, cx_cmp_int)); } destroy_nodes_test_data(begin); destroy_nodes_test_data(expected); } CX_TEST(test_empty_list_size) { CX_TEST_DO { CX_TEST_ASSERT(cxEmptyList->collection.size == 0); CX_TEST_ASSERT(cxListSize(cxEmptyList) == 0); } } CX_TEST(test_empty_list_iterator) { CxList *list = cxEmptyList; CxIterator it1 = cxListIterator(list); CxIterator it2 = cxListBackwardsIterator(list); CxIterator it3 = cxListMutIterator(list); CxIterator it4 = cxListMutBackwardsIterator(list); CX_TEST_DO { CX_TEST_ASSERT(!cxIteratorValid(it1)); CX_TEST_ASSERT(!cxIteratorValid(it2)); CX_TEST_ASSERT(!cxIteratorValid(it3)); CX_TEST_ASSERT(!cxIteratorValid(it4)); int c = 0; cx_foreach(void*, data, it1) c++; cx_foreach(void*, data, it2) c++; cx_foreach(void*, data, it3) c++; cx_foreach(void*, data, it4) c++; CX_TEST_ASSERT(c == 0); } } CX_TEST(test_empty_list_noops) { CX_TEST_DO { CxList copy = *cxEmptyList; cxListSort(cxEmptyList); cxListClear(cxEmptyList); cxListDestroy(cxEmptyList); CX_TEST_ASSERT(0 == memcmp(©, cxEmptyList, sizeof(CxList))); // NOLINT(*-suspicious-memory-comparison) } } CX_TEST(test_empty_list_at) { CX_TEST_DO { CX_TEST_ASSERT(cxListAt(cxEmptyList, 0) == NULL); CX_TEST_ASSERT(cxListAt(cxEmptyList, 1) == NULL); } } CX_TEST(test_empty_list_find) { int x = 42, y = 1337; CX_TEST_DO { CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) < 0); CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) < 0); } } CX_TEST(test_empty_list_compare) { CxList *empty = cxEmptyList; CxList *ll = cxLinkedListCreateSimple(sizeof(int)); CxList *al = cxArrayListCreateSimple(sizeof(int), 8); int x = 5; CX_TEST_DO { CX_TEST_ASSERT(0 == cxListCompare(empty, cxEmptyList)); CX_TEST_ASSERT(0 == cxListCompare(ll, cxEmptyList)); CX_TEST_ASSERT(0 == cxListCompare(al, cxEmptyList)); CX_TEST_ASSERT(0 == cxListCompare(cxEmptyList, ll)); CX_TEST_ASSERT(0 == cxListCompare(cxEmptyList, al)); cxListAdd(ll, &x); cxListAdd(al, &x); CX_TEST_ASSERT(0 < cxListCompare(ll, cxEmptyList)); CX_TEST_ASSERT(0 < cxListCompare(al, cxEmptyList)); CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, ll)); CX_TEST_ASSERT(0 > cxListCompare(cxEmptyList, al)); } cxListDestroy(ll); cxListDestroy(al); } CX_TEST(test_list_ll_create) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; CX_TEST_DO { CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); CX_TEST_ASSERT(list != NULL); CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); CX_TEST_ASSERT(list->collection.simple_destructor == NULL); CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); CX_TEST_ASSERT(list->collection.destructor_data == NULL); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(list->collection.allocator == alloc); CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_int); CX_TEST_ASSERT(!cxListIsStoringPointers(list)); cxListDestroy(list); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } CX_TEST(test_list_ll_create_simple) { CxList *list = cxLinkedListCreateSimple(sizeof(int)); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); CX_TEST_ASSERT(list->collection.simple_destructor == NULL); CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); CX_TEST_ASSERT(list->collection.destructor_data == NULL); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); CX_TEST_ASSERT(list->collection.cmpfunc == NULL); CX_TEST_ASSERT(!cxListIsStoringPointers(list)); } cxListDestroy(list); } CX_TEST(test_list_ll_store_pointers) { CxList *list = cxLinkedListCreateSimple(47); CX_TEST_DO { CX_TEST_ASSERT(!cxListIsStoringPointers(list)); cxListStorePointers(list); CX_TEST_ASSERT(list->collection.elem_size == sizeof(void *)); CX_TEST_ASSERT(list->cl != NULL); CX_TEST_ASSERT(list->climpl != NULL); CX_TEST_ASSERT(cxListIsStoringPointers(list)); cxListStoreObjects(list); CX_TEST_ASSERT(list->cl != NULL); CX_TEST_ASSERT(list->climpl == NULL); CX_TEST_ASSERT(!cxListIsStoringPointers(list)); } cxListDestroy(list); } CX_TEST(test_list_ll_create_simple_for_pointers) { CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*)); CX_TEST_ASSERT(list->collection.simple_destructor == NULL); CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); CX_TEST_ASSERT(list->collection.destructor_data == NULL); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_ptr); CX_TEST_ASSERT(cxListIsStoringPointers(list)); } cxListDestroy(list); } CX_TEST(test_list_arl_create) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; CX_TEST_DO { CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); CX_TEST_ASSERT(list != NULL); CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); CX_TEST_ASSERT(list->collection.simple_destructor == NULL); CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); CX_TEST_ASSERT(list->collection.destructor_data == NULL); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(list->collection.allocator == alloc); CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_int); CX_TEST_ASSERT(!cxListIsStoringPointers(list)); cxListDestroy(list); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } CX_TEST(test_list_arl_create_simple) { CxList *list = cxArrayListCreateSimple(sizeof(int), 8); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); CX_TEST_ASSERT(list->collection.simple_destructor == NULL); CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); CX_TEST_ASSERT(list->collection.destructor_data == NULL); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); CX_TEST_ASSERT(list->collection.cmpfunc == NULL); CX_TEST_ASSERT(!cxListIsStoringPointers(list)); } cxListDestroy(list); } CX_TEST(test_list_arl_create_simple_for_pointers) { CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8); CX_TEST_DO { CX_TEST_ASSERT(list != NULL); CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*)); CX_TEST_ASSERT(list->collection.simple_destructor == NULL); CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); CX_TEST_ASSERT(list->collection.destructor_data == NULL); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator); CX_TEST_ASSERT(list->collection.cmpfunc == cx_cmp_ptr); CX_TEST_ASSERT(cxListIsStoringPointers(list)); } cxListDestroy(list); } static void test_fake_simple_int_destr(void *elem) { *(int *) elem = 42; } CX_TEST(test_list_pll_destroy_no_destr) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; CX_TEST_DO { void *item = cxMalloc(alloc, sizeof(int)); CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); cxListAdd(list, item); CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); cxListDestroy(list); // item is not yet freed CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); cxFree(alloc, item); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } CX_TEST(test_list_pll_destroy_simple_destr) { CX_TEST_DO { int item = 0; CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); list->collection.simple_destructor = test_fake_simple_int_destr; cxListAdd(list, &item); cxListDestroy(list); CX_TEST_ASSERT(item == 42); } } CX_TEST(test_list_pll_destroy_adv_destr) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; CX_TEST_DO { void *item = cxMalloc(alloc, sizeof(int)); CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); list->collection.destructor_data = alloc; list->collection.advanced_destructor = (cx_destructor_func2) cxFree; cxListAdd(list, item); CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); cxListDestroy(list); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } CX_TEST(test_list_parl_destroy_no_destr) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; CX_TEST_DO { void *item = cxMalloc(alloc, sizeof(int)); CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); cxListAdd(list, item); CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); cxListDestroy(list); // item is not yet freed CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); cxFree(alloc, item); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } CX_TEST(test_list_parl_destroy_simple_destr) { CX_TEST_DO { int item = 0; CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); list->collection.simple_destructor = test_fake_simple_int_destr; cxListAdd(list, &item); cxListDestroy(list); CX_TEST_ASSERT(item == 42); } } CX_TEST(test_list_parl_destroy_adv_destr) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; CX_TEST_DO { void *item = cxMalloc(alloc, sizeof(int)); CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); list->collection.destructor_data = alloc; list->collection.advanced_destructor = (cx_destructor_func2) cxFree; cxListAdd(list, item); CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); cxListDestroy(list); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } cx_testing_allocator_destroy(&talloc); } #define set_up_combo \ CxTestingAllocator talloc; \ cx_testing_allocator_init(&talloc); \ CxAllocator *alloc = &talloc.base; \ CX_TEST_DO { #define tear_down_combo \ cxListDestroy(list); \ CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\ } \ cx_testing_allocator_destroy(&talloc); #define roll_out_test_invokers(name) \ CX_TEST(test_list_ll_##name) { \ set_up_combo \ CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ tear_down_combo \ } \ CX_TEST(test_list_arl_##name) { \ set_up_combo \ CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ tear_down_combo \ } \ CX_TEST(test_list_pll_##name) { \ set_up_combo \ CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ tear_down_combo \ } \ CX_TEST(test_list_parl_##name) { \ set_up_combo \ CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ tear_down_combo \ } #define roll_out_test_combos(name, body) \ static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ cx_attr_unused bool isptrlist) body \ roll_out_test_invokers(name) static void set_default_class_funcs(CxList *list, cx_list_class *defaulted_cl) { const cx_list_class *cl = list->climpl == NULL ? list->cl : list->climpl; memcpy(defaulted_cl, cl, sizeof(cx_list_class)); defaulted_cl->insert_array = cx_list_default_insert_array; defaulted_cl->insert_sorted = cx_list_default_insert_sorted; defaulted_cl->sort = cx_list_default_sort; defaulted_cl->swap = cx_list_default_swap; defaulted_cl->compare = NULL; if (list->climpl == NULL) { list->cl = defaulted_cl; } else { list->climpl = defaulted_cl; } } #define do_set_default_class_funcs(list) \ cx_list_class defaulted_cl; \ set_default_class_funcs(list, &defaulted_cl) #define roll_out_test_combos_with_defaulted_funcs(name, body) \ static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ cx_attr_unused bool isptrlist) body \ roll_out_test_invokers(name) \ CX_TEST(test_list_llm_##name) { \ set_up_combo \ CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ do_set_default_class_funcs(list); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ tear_down_combo \ } \ CX_TEST(test_list_arlm_##name) { \ set_up_combo \ CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \ do_set_default_class_funcs(list); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ tear_down_combo \ } \ CX_TEST(test_list_pllm_##name) { \ set_up_combo \ CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ do_set_default_class_funcs(list); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ tear_down_combo \ } \ CX_TEST(test_list_parlm_##name) { \ set_up_combo \ CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ do_set_default_class_funcs(list); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ tear_down_combo \ } #define array_init(...) {__VA_ARGS__} static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) { int *testdata = int_test_data(len); if (isptrlist) { for (size_t i = 0; i < len; i++) { cxListAdd(list, &testdata[i]); } } else { cxListAddArray(list, testdata, len); } return testdata; } roll_out_test_combos(add, { const size_t len = 250; int *testdata = int_test_data(len); for (size_t i = 0; i < len; i++) { CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0); } CX_TEST_ASSERT(cxListSize(list) == len); for (size_t i = 0; i < len; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); } for (size_t i = 0; i < len; i++) { ++testdata[i]; } if (isptrlist) { for (size_t i = 0; i < len; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); } } else { for (size_t i = 0; i < len; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1); } } free(testdata); }) roll_out_test_combos(insert, { int a = 5; int b = 47; int c = 13; int d = 42; CX_TEST_ASSERT(cxListInsert(list, 1, &a) != 0); CX_TEST_ASSERT(cxListSize(list) == 0); CX_TEST_ASSERT(cxListInsert(list, 0, &a) == 0); CX_TEST_ASSERT(cxListSize(list) == 1); CX_TEST_ASSERT(cxListInsert(list, 0, &b) == 0); CX_TEST_ASSERT(cxListSize(list) == 2); CX_TEST_ASSERT(cxListInsert(list, 1, &c) == 0); CX_TEST_ASSERT(cxListSize(list) == 3); CX_TEST_ASSERT(cxListInsert(list, 3, &d) == 0); CX_TEST_ASSERT(cxListSize(list) == 4); CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 47); CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 13); CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 5); CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 42); }) roll_out_test_combos_with_defaulted_funcs(insert_array, { int a[5] = array_init(5, 47, 11, 13, 42); int b[5] = array_init(9, 18, 72, 50, 7); int *aptr[5]; int *bptr[5]; for (size_t i = 0; i < 5; i++) { aptr[i] = &a[i]; bptr[i] = &b[i]; } size_t inserted; if (isptrlist) { inserted = cxListInsertArray(list, 0, aptr, 5); } else { inserted = cxListInsertArray(list, 0, a, 5); } CX_TEST_ASSERT(inserted == 5); CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 5); CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 47); CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 11); CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 13); CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == 42); if (isptrlist) { inserted = cxListInsertArray(list, 3, bptr, 5); } else { inserted = cxListInsertArray(list, 3, b, 5); } CX_TEST_ASSERT(inserted == 5); CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 5); CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 47); CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 11); CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 9); CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == 18); CX_TEST_ASSERT(*(int *) cxListAt(list, 5) == 72); CX_TEST_ASSERT(*(int *) cxListAt(list, 6) == 50); CX_TEST_ASSERT(*(int *) cxListAt(list, 7) == 7); CX_TEST_ASSERT(*(int *) cxListAt(list, 8) == 13); CX_TEST_ASSERT(*(int *) cxListAt(list, 9) == 42); }) roll_out_test_combos_with_defaulted_funcs(insert_sorted, { int d1 = 50; int d2 = 80; int d3 = 60; int d4 = 40; int d5 = 70; int d6a[6] = array_init(52, 54, 56, 62, 64, 75); int d7a[6] = array_init(51, 57, 58, 65, 77, 78); int d8 = 90; int *d6ptr[6]; int *d7ptr[6]; for (size_t i = 0; i < 6; i++) { d6ptr[i] = &d6a[i]; d7ptr[i] = &d7a[i]; } size_t inserted; int expected[18] = array_init( 40, 50, 51, 52, 54, 56, 57, 58, 60, 62, 64, 65, 70, 75, 77, 78, 80, 90 ); CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d1)); CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d2)); CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d3)); CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d4)); CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d5)); if (isptrlist) { inserted = cxListInsertSortedArray(list, d6ptr, 6); } else { inserted = cxListInsertSortedArray(list, d6a, 6); } CX_TEST_ASSERT(inserted == 6); if (isptrlist) { inserted = cxListInsertSortedArray(list, d7ptr, 6); } else { inserted = cxListInsertSortedArray(list, d7a, 6); } CX_TEST_ASSERT(inserted == 6); CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d8)); CX_TEST_ASSERT(cxListSize(list) == 18); for (size_t i = 0; i < 18; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]); } }) roll_out_test_combos(remove, { const size_t testdata_len = 32; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); CX_TEST_ASSERT(cxListRemove(list, 2) == 0); CX_TEST_ASSERT(cxListRemove(list, 4) == 0); CX_TEST_ASSERT(cxListSize(list) == testdata_len - 2); CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == testdata[0]); CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == testdata[1]); CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == testdata[3]); CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == testdata[4]); CX_TEST_ASSERT(*(int *) cxListAt(list, 4) == testdata[6]); CX_TEST_ASSERT(cxListRemove(list, 0) == 0); CX_TEST_ASSERT(cxListSize(list) == testdata_len - 3); CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == testdata[1]); CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == testdata[3]); CX_TEST_ASSERT(cxListRemove(list, testdata_len) != 0); free(testdata); }) static unsigned test_remove_array_destr_ctr; static void test_remove_array_destr(cx_attr_unused void *d) { test_remove_array_destr_ctr++; } roll_out_test_combos(remove_array, { const size_t testdata_len = 32; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); cxDefineDestructor(list, test_remove_array_destr); test_remove_array_destr_ctr = 0; // first, remove and get - no destructor must be called int targete[8]; int *targetp[8]; memset(targete, 0, sizeof(targete)); memset(targetp, 0, sizeof(targetp)); void *target = isptrlist ? (void*) targetp : targete; CX_TEST_ASSERT(8 == cxListRemoveArrayAndGet(list, 8, 8, target)); CX_TEST_ASSERT(0 == test_remove_array_destr_ctr); CX_TEST_ASSERT(24 == cxListSize(list)); for (unsigned int i = 0 ; i < 8 ; i++) { CX_TEST_ASSERT((*(int*)cxListAt(list, i)) == testdata[i]); } for (unsigned int i = 8 ; i < 24 ; i++) { CX_TEST_ASSERT((*(int*)cxListAt(list, i)) == testdata[8+i]); } for (unsigned int i = 0 ; i < 8 ; i++) { if (isptrlist) { CX_TEST_ASSERT(targetp[i] == &testdata[8 + i]); } else { CX_TEST_ASSERT(targete[i] == testdata[8 + i]); } } // now, just remove - destructor must be called CX_TEST_ASSERT(8 == cxListRemoveArray(list, 8, 8)); CX_TEST_ASSERT(8 == test_remove_array_destr_ctr); CX_TEST_ASSERT(16 == cxListSize(list)); for (unsigned int i = 0 ; i < 8 ; i++) { CX_TEST_ASSERT((*(int*)cxListAt(list, i)) == testdata[i]); } for (unsigned int i = 8 ; i < 16 ; i++) { CX_TEST_ASSERT((*(int*)cxListAt(list, i)) == testdata[16+i]); } // finally, remove and get out of bounds test_remove_array_destr_ctr = 0; memset(targete, 0, sizeof(targete)); memset(targetp, 0, sizeof(targetp)); CX_TEST_ASSERT(4 == cxListRemoveArrayAndGet(list, 12, 8, target)); CX_TEST_ASSERT(0 == test_remove_array_destr_ctr); CX_TEST_ASSERT(12 == cxListSize(list)); for (unsigned int i = 0 ; i < 8 ; i++) { CX_TEST_ASSERT((*(int*)cxListAt(list, i)) == testdata[i]); } for (unsigned int i = 8 ; i < 12 ; i++) { CX_TEST_ASSERT((*(int*)cxListAt(list, i)) == testdata[16+i]); } for (unsigned int i = 0 ; i < 4 ; i++) { if (isptrlist) { CX_TEST_ASSERT(targetp[i] == &testdata[28 + i]); } else { CX_TEST_ASSERT(targete[i] == testdata[28 + i]); } } for (unsigned int i = 4 ; i < 8 ; i++) { if (isptrlist) { CX_TEST_ASSERT(targetp[i] == 0); } else { CX_TEST_ASSERT(targete[i] == 0); } } free(testdata); }) roll_out_test_combos(find_remove, { const size_t testdata_len = 250; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) int val = testdata[exp]; // randomly picked number could occur earlier in list - find first position for (int i = 0 ; i < exp ; i++) { if (testdata[i] == val) { exp = i; break; } } CX_TEST_ASSERT(cxListSize(list) == testdata_len); CX_TEST_ASSERT(cxListFind(list, &val) == exp); CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp); CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); CX_TEST_ASSERT(cxListFind(list, &val) != exp); int notinlist = -1; CX_TEST_ASSERT(cxListFindRemove(list, ¬inlist) < 0); CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); free(testdata); }) roll_out_test_combos(clear, { int *testdata = int_test_data_added_to_list(list, isptrlist, 8); CX_TEST_ASSERT(cxListSize(list) > 0); cxListClear(list); CX_TEST_ASSERT(cxListSize(list) == 0); free(testdata); }) roll_out_test_combos(at, { size_t len = 128; int *testdata = int_test_data_added_to_list(list, isptrlist, 128); CX_TEST_ASSERT(cxListSize(list) == len); for (size_t i = 0; i < len; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); } CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL); free(testdata); }) roll_out_test_combos_with_defaulted_funcs(swap, { int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13); for (size_t i = 0; i < 16; i++) { cxListAdd(list, &original[i]); } CX_TEST_ASSERT(0 == cxListSwap(list, 1, 4)); CX_TEST_ASSERT(0 == cxListSwap(list, 2, 14)); CX_TEST_ASSERT(0 == cxListSwap(list, 9, 6)); CX_TEST_ASSERT(0 == cxListSwap(list, 3, 3)); CX_TEST_ASSERT(0 == cxListSwap(list, 10, 11)); CX_TEST_ASSERT(0 == cxListSwap(list, 8, 0)); CX_TEST_ASSERT(0 == cxListSwap(list, 7, 12)); CX_TEST_ASSERT(0 == cxListSwap(list, 13, 15)); CX_TEST_ASSERT(0 != cxListSwap(list, 5, 16)); CX_TEST_ASSERT(0 != cxListSwap(list, 16, 6)); CX_TEST_ASSERT(0 != cxListSwap(list, 16, 17)); CxIterator iter = cxListIterator(list); cx_foreach(int*, e, iter) { CX_TEST_ASSERT(*e == swapped[iter.index]); } iter = cxListBackwardsIterator(list); cx_foreach(int*, e, iter) { CX_TEST_ASSERT(*e == swapped[iter.index]); } }) CX_TEST(test_list_ll_swap_no_sbo) { set_up_combo CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, 2*cx_linked_list_swap_sbo_size); CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, false); tear_down_combo } CX_TEST(test_list_arl_swap_no_sbo) { set_up_combo CxList *list = cxArrayListCreate(alloc, cx_cmp_int, 2*cx_array_swap_sbo_size, 8); CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, false); tear_down_combo } roll_out_test_combos(find, { const size_t testdata_len = 500; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); for (size_t attempt = 0; attempt < 25; attempt++) { int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) int val = testdata[exp]; // randomly picked number could occur earlier in list - find first position for (int i = 0 ; i < exp ; i++) { if (testdata[i] == val) { exp = i; break; } } CX_TEST_ASSERT(cxListFind(list, &val) == exp); } int notinlist = -1; CX_TEST_ASSERT(cxListFind(list, ¬inlist) < 0); free(testdata); }) roll_out_test_combos_with_defaulted_funcs(sort, { const size_t testdata_len = 250; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); int *expected = malloc(testdata_len*sizeof(int)); memcpy(expected, testdata, testdata_len*sizeof(int)); qsort(expected, testdata_len, sizeof(int), cx_cmp_int); cxListSort(list); for (size_t i = 0; i < testdata_len; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]); } free(expected); free(testdata); }) roll_out_test_combos(reverse, { const size_t testdata_len = 50; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); cxListReverse(list); for (size_t i = 0; i < testdata_len; i++) { CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[testdata_len - 1 - i]); } free(testdata); }) roll_out_test_combos(iterator, { const size_t len = 50; int *testdata = int_test_data_added_to_list(list, isptrlist, len); CxIterator iter = cxListIterator(list); CX_TEST_ASSERT(iter.elem_size == list->collection.elem_size); CX_TEST_ASSERT(iter.elem_count == list->collection.size); size_t i = 0; cx_foreach(int*, x, iter) { CX_TEST_ASSERT(i == iter.index); CX_TEST_ASSERT(*x == testdata[iter.index]); i++; } CX_TEST_ASSERT(i == cxListSize(list)); iter = cxListBackwardsIterator(list); cx_foreach(int*, x, iter) { CX_TEST_ASSERT(i - 1 == iter.index); CX_TEST_ASSERT(*x == testdata[iter.index]); i--; } CX_TEST_ASSERT(i == 0); i = len / 2; CxIterator mut_iter = cxListMutIteratorAt(list, i); CX_TEST_ASSERT(mut_iter.elem_size == list->collection.elem_size); CX_TEST_ASSERT(mut_iter.elem_count == list->collection.size); size_t j = 0; cx_foreach(int*, x, mut_iter) { CX_TEST_ASSERT(mut_iter.index == len / 2 + j / 2); CX_TEST_ASSERT(*x == testdata[i]); if (i % 2 == 1) cxIteratorFlagRemoval(mut_iter); i++; j++; } CX_TEST_ASSERT(i == len); i = len / 2; j = 0; mut_iter = cxListMutBackwardsIteratorAt(list, i - 1); cx_foreach(int*, x, mut_iter) { CX_TEST_ASSERT(mut_iter.index == len / 2 - 1 - j); CX_TEST_ASSERT(*x == testdata[i - 1]); if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter); i--; j++; } CX_TEST_ASSERT(i == 0); CX_TEST_ASSERT(cxListSize(list) == len / 2); for (size_t k = 0; k < len / 2; k++) { CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]); } free(testdata); }) roll_out_test_combos(insert_with_iterator, { int fivenums[] = array_init(0, 1, 2, 3, 4); for (size_t i = 0; i < 5; i++) { cxListAdd(list, &fivenums[i]); } int newdata[] = array_init(10, 20, 30, 40, 50); CxIterator iter = cxListMutIteratorAt(list, 2); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 2); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); CX_TEST_ASSERT(iter.elem_count == 5); cxListInsertAfter(&iter, &newdata[0]); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 2); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); CX_TEST_ASSERT(iter.elem_count == 6); cxListInsertBefore(&iter, &newdata[1]); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 3); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); CX_TEST_ASSERT(iter.elem_count == 7); iter = cxListMutIterator(list); cxListInsertBefore(&iter, &newdata[2]); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 1); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 0); CX_TEST_ASSERT(iter.elem_count == 8); iter = cxListMutIteratorAt(list, cxListSize(list)); cxListInsertBefore(&iter, &newdata[3]); CX_TEST_ASSERT(!cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 9); CX_TEST_ASSERT(iter.elem_count == 9); iter = cxListMutIteratorAt(list, cxListSize(list)); cxListInsertAfter(&iter, &newdata[4]); CX_TEST_ASSERT(!cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 10); CX_TEST_ASSERT(iter.elem_count == 10); int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50); for (size_t j = 0; j < 10; j++) { CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]); } }) static CX_TEST_SUBROUTINE(test_list_verify_compare, CxList *left, CxList *right) { CX_TEST_ASSERTM(cxListCompare(left, right) == 0, "lists don't start identical"); int x = 42; cxListAdd(left, &x); CX_TEST_ASSERT(cxListSize(left) > cxListSize(right)); CX_TEST_ASSERT(cxListCompare(left, right) > 0); CX_TEST_ASSERT(cxListCompare(right, left) < 0); cxListAdd(right, &x); CX_TEST_ASSERT(cxListSize(left) == cxListSize(right)); CX_TEST_ASSERT(cxListCompare(left, right) == 0); int a = 5, b = 10; cxListInsert(left, 15, &a); cxListInsert(right, 15, &b); CX_TEST_ASSERT(cxListSize(left) == cxListSize(right)); CX_TEST_ASSERT(cxListCompare(left, right) < 0); CX_TEST_ASSERT(cxListCompare(right, left) > 0); *(int *) cxListAt(left, 15) = 10; CX_TEST_ASSERT(cxListCompare(left, right) == 0); } #define roll_out_compare_tests(suffix, otherctr) \ roll_out_test_combos(compare_##suffix, { \ const size_t len = 47; \ int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ CxList *other = otherctr; \ for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \ CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ cxListDestroy(other); \ free(testdata); \ }) roll_out_compare_tests( ll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)) ) roll_out_compare_tests( pll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS) ) roll_out_compare_tests( arl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50) ) roll_out_compare_tests( parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50) ) roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, { const size_t len = 33; int *testdata = int_test_data_added_to_list(list, isptrlist, len); CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); do_set_default_class_funcs(other); for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); cxListDestroy(other); free(testdata); }) static unsigned destr_test_ctr; static int destr_last_value; static void simple_destr_test_fun(void *data) { int *ptr = data; destr_last_value = *ptr; *ptr = destr_last_value + 1; destr_test_ctr++; } static void advanced_destr_test_fun(cx_attr_unused void *u, void *data) { simple_destr_test_fun(data); } static CX_TEST_SUBROUTINE(test_list_verify_destructor, CxList *list, const int *testdata, size_t testdata_len) { destr_test_ctr = 0; int off = cxListIsStoringPointers(list) ? 1 : 0; cxListRemove(list, 15); CX_TEST_ASSERT(1 == destr_test_ctr); CX_TEST_ASSERT(testdata[15] == destr_last_value + off); CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); cxListRemove(list, 47); CX_TEST_ASSERT(2 == destr_test_ctr); CX_TEST_ASSERT(testdata[48] == destr_last_value + off); CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); CxIterator iter = cxListMutIteratorAt(list, 7); cxIteratorNext(iter); CX_TEST_ASSERT(2 == destr_test_ctr); CX_TEST_ASSERT(testdata[48] == destr_last_value + off); CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); cxIteratorFlagRemoval(iter); cxIteratorNext(iter); CX_TEST_ASSERT(3 == destr_test_ctr); CX_TEST_ASSERT(testdata[8] == destr_last_value + off); CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); iter = cxListMutBackwardsIteratorAt(list, 5); cxIteratorNext(iter); CX_TEST_ASSERT(3 == destr_test_ctr); CX_TEST_ASSERT(testdata[8] == destr_last_value + off); CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); cxIteratorFlagRemoval(iter); cxIteratorNext(iter); CX_TEST_ASSERT(4 == destr_test_ctr); CX_TEST_ASSERT(testdata[4] == destr_last_value + off); CX_TEST_ASSERT(testdata_len - destr_test_ctr == cxListSize(list)); cxListClear(list); CX_TEST_ASSERT(testdata_len == destr_test_ctr); CX_TEST_ASSERT(testdata[testdata_len - 1] == destr_last_value + off); } roll_out_test_combos(simple_destr, { const size_t len = 60; int *testdata = int_test_data_added_to_list(list, isptrlist, len); cxDefineDestructor(list, simple_destr_test_fun); CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); free(testdata); }) roll_out_test_combos(advanced_destr, { const size_t len = 75; int *testdata = int_test_data_added_to_list(list, isptrlist, len); cxDefineAdvancedDestructor(list, advanced_destr_test_fun, NULL); CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); free(testdata); }) CxTestSuite *cx_test_suite_array_list(void) { CxTestSuite *suite = cx_test_suite_new("array_list"); cx_test_register(suite, test_array_add); cx_test_register(suite, test_array_insert_sorted); cx_test_register(suite, test_array_binary_search); cx_test_register(suite, test_list_arl_create); cx_test_register(suite, test_list_arl_create_simple); cx_test_register(suite, test_list_arl_create_simple_for_pointers); cx_test_register(suite, test_list_parl_destroy_no_destr); cx_test_register(suite, test_list_parl_destroy_simple_destr); cx_test_register(suite, test_list_parl_destroy_adv_destr); cx_test_register(suite, test_list_arl_add); cx_test_register(suite, test_list_parl_add); cx_test_register(suite, test_list_arl_insert); cx_test_register(suite, test_list_parl_insert); cx_test_register(suite, test_list_arl_insert_array); cx_test_register(suite, test_list_parl_insert_array); cx_test_register(suite, test_list_arl_insert_sorted); cx_test_register(suite, test_list_parl_insert_sorted); cx_test_register(suite, test_list_arl_remove); cx_test_register(suite, test_list_parl_remove); cx_test_register(suite, test_list_arl_remove_array); cx_test_register(suite, test_list_parl_remove_array); cx_test_register(suite, test_list_arl_find_remove); cx_test_register(suite, test_list_parl_find_remove); cx_test_register(suite, test_list_arl_clear); cx_test_register(suite, test_list_parl_clear); cx_test_register(suite, test_list_arl_at); cx_test_register(suite, test_list_parl_at); cx_test_register(suite, test_list_arl_swap); cx_test_register(suite, test_list_parl_swap); cx_test_register(suite, test_list_arl_swap_no_sbo); cx_test_register(suite, test_list_arl_find); cx_test_register(suite, test_list_parl_find); cx_test_register(suite, test_list_arl_sort); cx_test_register(suite, test_list_parl_sort); cx_test_register(suite, test_list_arl_reverse); cx_test_register(suite, test_list_parl_reverse); cx_test_register(suite, test_list_arl_iterator); cx_test_register(suite, test_list_parl_iterator); cx_test_register(suite, test_list_arl_insert_with_iterator); cx_test_register(suite, test_list_parl_insert_with_iterator); cx_test_register(suite, test_list_arl_compare_ll); cx_test_register(suite, test_list_arl_compare_arl); cx_test_register(suite, test_list_arl_compare_pll); cx_test_register(suite, test_list_arl_compare_parl); cx_test_register(suite, test_list_parl_compare_ll); cx_test_register(suite, test_list_parl_compare_arl); cx_test_register(suite, test_list_parl_compare_pll); cx_test_register(suite, test_list_parl_compare_parl); cx_test_register(suite, test_list_arl_simple_destr); cx_test_register(suite, test_list_parl_simple_destr); cx_test_register(suite, test_list_arl_advanced_destr); cx_test_register(suite, test_list_parl_advanced_destr); return suite; } CxTestSuite *cx_test_suite_array_list_defaulted_funcs(void) { CxTestSuite *suite = cx_test_suite_new( "array_list with defaulted functions"); cx_test_register(suite, test_list_arlm_insert_array); cx_test_register(suite, test_list_parlm_insert_array); cx_test_register(suite, test_list_arlm_insert_sorted); cx_test_register(suite, test_list_parlm_insert_sorted); cx_test_register(suite, test_list_arlm_swap); cx_test_register(suite, test_list_parlm_swap); cx_test_register(suite, test_list_arlm_sort); cx_test_register(suite, test_list_parlm_sort); cx_test_register(suite, test_list_arl_compare_unoptimized); cx_test_register(suite, test_list_parl_compare_unoptimized); cx_test_register(suite, test_list_arlm_compare_unoptimized); cx_test_register(suite, test_list_parlm_compare_unoptimized); return suite; } CxTestSuite *cx_test_suite_linked_list(void) { CxTestSuite *suite = cx_test_suite_new("linked_list"); cx_test_register(suite, test_linked_list_link_unlink); cx_test_register(suite, test_linked_list_at); cx_test_register(suite, test_linked_list_find); cx_test_register(suite, test_linked_list_compare); cx_test_register(suite, test_linked_list_add); cx_test_register(suite, test_linked_list_prepend); cx_test_register(suite, test_linked_list_insert); cx_test_register(suite, test_linked_list_insert_chain); cx_test_register(suite, test_linked_list_insert_sorted); cx_test_register(suite, test_linked_list_first); cx_test_register(suite, test_linked_list_last); cx_test_register(suite, test_linked_list_prev); cx_test_register(suite, test_linked_list_remove); cx_test_register(suite, test_linked_list_remove_chain); cx_test_register(suite, test_linked_list_size); cx_test_register(suite, test_linked_list_sort_empty); cx_test_register(suite, test_linked_list_sort); cx_test_register(suite, test_linked_list_reverse); cx_test_register(suite, test_list_ll_create); cx_test_register(suite, test_list_ll_create_simple); cx_test_register(suite, test_list_ll_store_pointers); cx_test_register(suite, test_list_ll_create_simple_for_pointers); cx_test_register(suite, test_list_pll_destroy_no_destr); cx_test_register(suite, test_list_pll_destroy_simple_destr); cx_test_register(suite, test_list_pll_destroy_adv_destr); cx_test_register(suite, test_list_ll_add); cx_test_register(suite, test_list_pll_add); cx_test_register(suite, test_list_ll_insert); cx_test_register(suite, test_list_pll_insert); cx_test_register(suite, test_list_ll_insert_array); cx_test_register(suite, test_list_pll_insert_array); cx_test_register(suite, test_list_ll_insert_sorted); cx_test_register(suite, test_list_pll_insert_sorted); cx_test_register(suite, test_list_ll_remove); cx_test_register(suite, test_list_pll_remove); cx_test_register(suite, test_list_ll_remove_array); cx_test_register(suite, test_list_pll_remove_array); cx_test_register(suite, test_list_ll_find_remove); cx_test_register(suite, test_list_pll_find_remove); cx_test_register(suite, test_list_ll_clear); cx_test_register(suite, test_list_pll_clear); cx_test_register(suite, test_list_ll_at); cx_test_register(suite, test_list_pll_at); cx_test_register(suite, test_list_ll_swap); cx_test_register(suite, test_list_pll_swap); cx_test_register(suite, test_list_ll_swap_no_sbo); cx_test_register(suite, test_list_ll_find); cx_test_register(suite, test_list_pll_find); cx_test_register(suite, test_list_ll_sort); cx_test_register(suite, test_list_pll_sort); cx_test_register(suite, test_list_ll_reverse); cx_test_register(suite, test_list_pll_reverse); cx_test_register(suite, test_list_ll_iterator); cx_test_register(suite, test_list_pll_iterator); cx_test_register(suite, test_list_ll_insert_with_iterator); cx_test_register(suite, test_list_pll_insert_with_iterator); cx_test_register(suite, test_list_ll_compare_ll); cx_test_register(suite, test_list_ll_compare_arl); cx_test_register(suite, test_list_ll_compare_pll); cx_test_register(suite, test_list_ll_compare_parl); cx_test_register(suite, test_list_pll_compare_ll); cx_test_register(suite, test_list_pll_compare_arl); cx_test_register(suite, test_list_pll_compare_pll); cx_test_register(suite, test_list_pll_compare_parl); cx_test_register(suite, test_list_ll_simple_destr); cx_test_register(suite, test_list_pll_simple_destr); cx_test_register(suite, test_list_ll_advanced_destr); cx_test_register(suite, test_list_pll_advanced_destr); return suite; } CxTestSuite *cx_test_suite_linked_list_defaulted_funcs(void) { CxTestSuite *suite = cx_test_suite_new( "linked_list with defaulted functions"); cx_test_register(suite, test_list_llm_insert_array); cx_test_register(suite, test_list_pllm_insert_array); cx_test_register(suite, test_list_llm_insert_sorted); cx_test_register(suite, test_list_pllm_insert_sorted); cx_test_register(suite, test_list_llm_swap); cx_test_register(suite, test_list_pllm_swap); cx_test_register(suite, test_list_llm_sort); cx_test_register(suite, test_list_pllm_sort); cx_test_register(suite, test_list_ll_compare_unoptimized); cx_test_register(suite, test_list_pll_compare_unoptimized); cx_test_register(suite, test_list_llm_compare_unoptimized); cx_test_register(suite, test_list_pllm_compare_unoptimized); return suite; } CxTestSuite *cx_test_suite_empty_list(void) { CxTestSuite *suite = cx_test_suite_new("empty list dummy"); cx_test_register(suite, test_empty_list_size); cx_test_register(suite, test_empty_list_iterator); cx_test_register(suite, test_empty_list_noops); cx_test_register(suite, test_empty_list_at); cx_test_register(suite, test_empty_list_find); cx_test_register(suite, test_empty_list_compare); return suite; }