test/test_list.c

Mon, 27 Sep 2021 18:33:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 18:33:30 +0200
changeset 438
cd3069757010
parent 435
0fe204d50f54
child 442
310019ddfe4e
permissions
-rw-r--r--

add function cx_linked_list_at()

This commit also makes glue functions static.

universe@390 1 /*
universe@390 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@390 3 *
universe@390 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@390 5 *
universe@390 6 * Redistribution and use in source and binary forms, with or without
universe@390 7 * modification, are permitted provided that the following conditions are met:
universe@390 8 *
universe@390 9 * 1. Redistributions of source code must retain the above copyright
universe@390 10 * notice, this list of conditions and the following disclaimer.
universe@390 11 *
universe@390 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@390 13 * notice, this list of conditions and the following disclaimer in the
universe@390 14 * documentation and/or other materials provided with the distribution.
universe@390 15 *
universe@390 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@390 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@390 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@390 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@390 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@390 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@390 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@390 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@390 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@390 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@390 26 * POSSIBILITY OF SUCH DAMAGE.
universe@390 27 */
universe@390 28
universe@398 29 #include "cx/linked_list.h"
universe@411 30 #include "test_config.h"
universe@422 31 #include "util_allocator.h"
universe@398 32
universe@412 33 int cmp_int(int const *l, int const *r) {
universe@412 34 int left = *l, right = *r;
universe@412 35 return left == right ? 0 : (left < right ? -1 : 1);
universe@412 36 }
universe@412 37
universe@438 38 void test_linked_list_create(void) {
universe@422 39 cxTestingAllocatorReset();
universe@422 40
universe@422 41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
universe@412 42
universe@435 43 CU_ASSERT_EQUAL(list->size, 0)
universe@435 44 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
universe@435 45 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
universe@435 46 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
universe@435 47 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
universe@412 48
universe@435 49 // assume this structure for a linked list
universe@435 50 struct ll_check {
universe@435 51 cx_list_s base;
universe@422 52 void *begin;
universe@422 53 void *end;
universe@422 54 ptrdiff_t ploc;
universe@422 55 ptrdiff_t nloc;
universe@412 56 };
universe@412 57
universe@435 58 struct ll_check *actual = (struct ll_check *) list;
universe@412 59 CU_ASSERT_PTR_NULL(actual->begin)
universe@412 60 CU_ASSERT_PTR_NULL(actual->end)
universe@412 61 CU_ASSERT_EQUAL(0, actual->ploc)
universe@422 62 CU_ASSERT_EQUAL(sizeof(void *), actual->nloc)
universe@412 63
universe@412 64 cxLinkedListDestroy(list);
universe@413 65
universe@422 66 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@411 67 }
universe@398 68
universe@438 69 void test_linked_list_at(void) {
universe@438 70 struct node {
universe@438 71 void *next;
universe@438 72 void *prev;
universe@438 73 };
universe@438 74 const ptrdiff_t loc_prev = offsetof(struct node, prev);
universe@438 75 const ptrdiff_t loc_next = offsetof(struct node, next);
universe@438 76
universe@438 77 struct node a, b, c, d;
universe@438 78 a.prev = NULL;
universe@438 79 a.next = &b;
universe@438 80 b.prev = &a;
universe@438 81 b.next = &c;
universe@438 82 c.prev = &b;
universe@438 83 c.next = &d;
universe@438 84 d.prev = &c;
universe@438 85 d.next = NULL;
universe@438 86
universe@438 87 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0));
universe@438 88 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1));
universe@438 89 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2));
universe@438 90 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3));
universe@438 91 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4));
universe@438 92
universe@438 93 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0));
universe@438 94 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1));
universe@438 95 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2));
universe@438 96 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3));
universe@438 97 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4));
universe@438 98
universe@438 99 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0));
universe@438 100 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1));
universe@438 101 }
universe@438 102
universe@390 103 int main() {
universe@411 104 CU_pSuite suite = NULL;
universe@411 105
universe@411 106 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 107 return CU_get_error();
universe@411 108 }
universe@411 109
universe@413 110 suite = CU_add_suite("linked list suite", NULL, NULL);
universe@411 111 if (NULL == suite) {
universe@411 112 CU_cleanup_registry();
universe@411 113 return CU_get_error();
universe@411 114 }
universe@411 115
universe@411 116 if (
universe@438 117 !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) ||
universe@438 118 !CU_add_test(suite, "linked list: get node at index", test_linked_list_at)
universe@411 119 ) {
universe@411 120 CU_cleanup_registry();
universe@411 121 return CU_get_error();
universe@411 122 }
universe@411 123
universe@413 124 suite = CU_add_suite("array suite", NULL, NULL);
universe@413 125 if (NULL == suite) {
universe@413 126 CU_cleanup_registry();
universe@413 127 return CU_get_error();
universe@413 128 }
universe@413 129
universe@413 130 /*
universe@413 131 if (
universe@413 132 !CU_add_test(suite, "array...", test_array...)
universe@413 133 ) {
universe@413 134 CU_cleanup_registry();
universe@413 135 return CU_get_error();
universe@413 136 }
universe@413 137 */
universe@413 138
universe@411 139 CU_basic_set_mode(UCX_CU_BRM);
universe@411 140
universe@411 141 int exitcode;
universe@411 142 if (CU_basic_run_tests()) {
universe@411 143 exitcode = CU_get_error();
universe@411 144 } else {
universe@411 145 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 146 }
universe@411 147 CU_cleanup_registry();
universe@411 148 return exitcode;
universe@390 149 }

mercurial