test/test_list.c

Sun, 03 Oct 2021 10:43:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 10:43:31 +0200
changeset 449
68ad5750ba6b
parent 446
668757098b73
child 453
bb144d08cd44
permissions
-rw-r--r--

minor code changes

These changes do not affect program behavior.

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@412 54 };
universe@412 55
universe@435 56 struct ll_check *actual = (struct ll_check *) list;
universe@412 57 CU_ASSERT_PTR_NULL(actual->begin)
universe@412 58 CU_ASSERT_PTR_NULL(actual->end)
universe@412 59
universe@412 60 cxLinkedListDestroy(list);
universe@413 61
universe@422 62 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
universe@411 63 }
universe@398 64
universe@438 65 void test_linked_list_at(void) {
universe@438 66 struct node {
universe@438 67 void *next;
universe@438 68 void *prev;
universe@438 69 };
universe@438 70 const ptrdiff_t loc_prev = offsetof(struct node, prev);
universe@438 71 const ptrdiff_t loc_next = offsetof(struct node, next);
universe@438 72
universe@438 73 struct node a, b, c, d;
universe@438 74 a.prev = NULL;
universe@438 75 a.next = &b;
universe@438 76 b.prev = &a;
universe@438 77 b.next = &c;
universe@438 78 c.prev = &b;
universe@438 79 c.next = &d;
universe@438 80 d.prev = &c;
universe@438 81 d.next = NULL;
universe@438 82
universe@449 83 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 0), &a)
universe@449 84 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 1), &b)
universe@449 85 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 2), &c)
universe@449 86 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&a, 0, loc_next, 3), &d)
universe@449 87 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4))
universe@438 88
universe@449 89 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_prev, 0), &a)
universe@449 90 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 1), &b)
universe@449 91 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 2), &c)
universe@449 92 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&b, 1, loc_next, 3), &d)
universe@449 93 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4))
universe@438 94
universe@449 95 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 0), &a)
universe@449 96 CU_ASSERT_PTR_EQUAL(cx_linked_list_at(&d, 3, loc_prev, 1), &b)
universe@438 97 }
universe@438 98
olaf@444 99 void test_linked_list_add(void) {
olaf@442 100 struct node {
olaf@442 101 void *prev;
olaf@442 102 void *next;
olaf@442 103 };
universe@449 104
olaf@442 105 struct node nodes[4];
universe@449 106
olaf@442 107 // test with begin, end / prev, next
universe@449 108 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 109 void *begin = NULL;
olaf@442 110 void *end = NULL;
universe@449 111
olaf@442 112 ptrdiff_t loc_prev = offsetof(struct node, prev);
olaf@442 113 ptrdiff_t loc_next = offsetof(struct node, next);
universe@449 114
olaf@442 115 int ret;
olaf@442 116 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
universe@449 117 CU_ASSERT_EQUAL(ret, 0)
universe@449 118 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 119 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
universe@449 120 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL)
universe@449 121 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL)
universe@449 122
olaf@442 123 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
universe@449 124 CU_ASSERT_EQUAL(ret, 0)
universe@449 125 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 126 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
universe@449 127 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 128 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
universe@449 129
olaf@442 130 // test with begin only / prev, next
universe@449 131 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 132 begin = NULL;
olaf@442 133 end = NULL;
universe@449 134
olaf@442 135 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
universe@449 136 CU_ASSERT_EQUAL(ret, 0)
universe@449 137 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
olaf@442 138 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
universe@449 139 CU_ASSERT_EQUAL(ret, 0)
universe@449 140 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 141 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 142 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
universe@449 143
olaf@442 144 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
universe@449 145 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
universe@449 146 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
universe@449 147
olaf@442 148 // test with begin, end / next
universe@449 149 memset(nodes, 0, 4 * sizeof(struct node));
olaf@442 150 begin = NULL;
olaf@442 151 end = NULL;
universe@449 152
olaf@442 153 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
universe@449 154 CU_ASSERT_EQUAL(ret, 0)
universe@449 155 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
universe@449 156 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
olaf@442 157 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
universe@449 158 CU_ASSERT_EQUAL(ret, 0)
universe@449 159 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
universe@449 160 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
universe@449 161 CU_ASSERT_PTR_NULL(nodes[1].prev)
olaf@442 162 }
olaf@442 163
universe@390 164 int main() {
universe@411 165 CU_pSuite suite = NULL;
universe@449 166
universe@411 167 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 168 return CU_get_error();
universe@411 169 }
universe@411 170
universe@413 171 suite = CU_add_suite("linked list suite", NULL, NULL);
universe@449 172
olaf@443 173 CU_add_test(suite, "linked list: create and destroy", test_linked_list_create);
olaf@443 174 CU_add_test(suite, "linked list: get node at index", test_linked_list_at);
olaf@444 175 CU_add_test(suite, "linked list: add", test_linked_list_add);
universe@413 176
universe@411 177 CU_basic_set_mode(UCX_CU_BRM);
universe@411 178
universe@411 179 int exitcode;
universe@411 180 if (CU_basic_run_tests()) {
universe@411 181 exitcode = CU_get_error();
universe@411 182 } else {
universe@411 183 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 184 }
universe@411 185 CU_cleanup_registry();
universe@411 186 return exitcode;
universe@390 187 }

mercurial