test/test_list.c

Tue, 28 Sep 2021 18:03:10 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 28 Sep 2021 18:03:10 +0200
changeset 442
310019ddfe4e
parent 438
cd3069757010
child 443
d6d8712e15bc
permissions
-rw-r--r--

add test for cx_linked_list_add

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
olaf@442 103 void test_cx_linked_list_add(void) {
olaf@442 104 struct node {
olaf@442 105 void *prev;
olaf@442 106 void *next;
olaf@442 107 int value;
olaf@442 108 };
olaf@442 109
olaf@442 110 struct node nodes[4];
olaf@442 111
olaf@442 112 // test with begin, end / prev, next
olaf@442 113 memset(nodes, 0, 4*sizeof(struct node));
olaf@442 114 void *begin = NULL;
olaf@442 115 void *end = NULL;
olaf@442 116
olaf@442 117 ptrdiff_t loc_prev = offsetof(struct node, prev);
olaf@442 118 ptrdiff_t loc_next = offsetof(struct node, next);
olaf@442 119
olaf@442 120 int ret;
olaf@442 121 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
olaf@442 122 CU_ASSERT_EQUAL(ret, 0);
olaf@442 123 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
olaf@442 124 CU_ASSERT_PTR_EQUAL(end, &nodes[0]);
olaf@442 125 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL);
olaf@442 126 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL);
olaf@442 127
olaf@442 128 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
olaf@442 129 CU_ASSERT_EQUAL(ret, 0);
olaf@442 130 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
olaf@442 131 CU_ASSERT_PTR_EQUAL(end, &nodes[1]);
olaf@442 132 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
olaf@442 133 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]);
olaf@442 134
olaf@442 135 // test with begin only / prev, next
olaf@442 136 memset(nodes, 0, 4*sizeof(struct node));
olaf@442 137 begin = NULL;
olaf@442 138 end = NULL;
olaf@442 139
olaf@442 140 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
olaf@442 141 CU_ASSERT_EQUAL(ret, 0);
olaf@442 142 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
olaf@442 143 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
olaf@442 144 CU_ASSERT_EQUAL(ret, 0);
olaf@442 145 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
olaf@442 146 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
olaf@442 147 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]);
olaf@442 148
olaf@442 149 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
olaf@442 150 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]);
olaf@442 151 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]);
olaf@442 152
olaf@442 153 // test with begin, end / next
olaf@442 154 memset(nodes, 0, 4*sizeof(struct node));
olaf@442 155 begin = NULL;
olaf@442 156 end = NULL;
olaf@442 157
olaf@442 158 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
olaf@442 159 CU_ASSERT_EQUAL(ret, 0);
olaf@442 160 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
olaf@442 161 CU_ASSERT_PTR_EQUAL(end, &nodes[0]);
olaf@442 162 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
olaf@442 163 CU_ASSERT_PTR_EQUAL(end, &nodes[1]);
olaf@442 164 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
olaf@442 165 CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL);
olaf@442 166 }
olaf@442 167
universe@390 168 int main() {
universe@411 169 CU_pSuite suite = NULL;
olaf@442 170
universe@411 171 if (CUE_SUCCESS != CU_initialize_registry()) {
universe@411 172 return CU_get_error();
universe@411 173 }
universe@411 174
universe@413 175 suite = CU_add_suite("linked list suite", NULL, NULL);
universe@411 176 if (NULL == suite) {
universe@411 177 CU_cleanup_registry();
universe@411 178 return CU_get_error();
universe@411 179 }
universe@411 180
universe@411 181 if (
universe@438 182 !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) ||
olaf@442 183 !CU_add_test(suite, "linked list: get node at index", test_linked_list_at) ||
olaf@442 184 !CU_add_test(suite, "linked list: add", test_cx_linked_list_add)
universe@411 185 ) {
universe@411 186 CU_cleanup_registry();
universe@411 187 return CU_get_error();
universe@411 188 }
universe@411 189
universe@413 190 suite = CU_add_suite("array suite", NULL, NULL);
universe@413 191 if (NULL == suite) {
universe@413 192 CU_cleanup_registry();
universe@413 193 return CU_get_error();
universe@413 194 }
universe@413 195
universe@413 196 /*
universe@413 197 if (
universe@413 198 !CU_add_test(suite, "array...", test_array...)
universe@413 199 ) {
universe@413 200 CU_cleanup_registry();
universe@413 201 return CU_get_error();
universe@413 202 }
universe@413 203 */
universe@413 204
universe@411 205 CU_basic_set_mode(UCX_CU_BRM);
universe@411 206
universe@411 207 int exitcode;
universe@411 208 if (CU_basic_run_tests()) {
universe@411 209 exitcode = CU_get_error();
universe@411 210 } else {
universe@411 211 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
universe@411 212 }
universe@411 213 CU_cleanup_registry();
universe@411 214 return exitcode;
universe@390 215 }

mercurial