Tue, 28 Sep 2021 18:03:10 +0200
add test for cx_linked_list_add
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "cx/linked_list.h"
30 #include "test_config.h"
31 #include "util_allocator.h"
33 int cmp_int(int const *l, int const *r) {
34 int left = *l, right = *r;
35 return left == right ? 0 : (left < right ? -1 : 1);
36 }
38 void test_linked_list_create(void) {
39 cxTestingAllocatorReset();
41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
43 CU_ASSERT_EQUAL(list->size, 0)
44 CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
45 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
46 CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
47 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
49 // assume this structure for a linked list
50 struct ll_check {
51 cx_list_s base;
52 void *begin;
53 void *end;
54 ptrdiff_t ploc;
55 ptrdiff_t nloc;
56 };
58 struct ll_check *actual = (struct ll_check *) list;
59 CU_ASSERT_PTR_NULL(actual->begin)
60 CU_ASSERT_PTR_NULL(actual->end)
61 CU_ASSERT_EQUAL(0, actual->ploc)
62 CU_ASSERT_EQUAL(sizeof(void *), actual->nloc)
64 cxLinkedListDestroy(list);
66 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
67 }
69 void test_linked_list_at(void) {
70 struct node {
71 void *next;
72 void *prev;
73 };
74 const ptrdiff_t loc_prev = offsetof(struct node, prev);
75 const ptrdiff_t loc_next = offsetof(struct node, next);
77 struct node a, b, c, d;
78 a.prev = NULL;
79 a.next = &b;
80 b.prev = &a;
81 b.next = &c;
82 c.prev = &b;
83 c.next = &d;
84 d.prev = &c;
85 d.next = NULL;
87 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0));
88 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1));
89 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2));
90 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3));
91 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4));
93 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0));
94 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1));
95 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2));
96 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3));
97 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4));
99 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0));
100 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1));
101 }
103 void test_cx_linked_list_add(void) {
104 struct node {
105 void *prev;
106 void *next;
107 int value;
108 };
110 struct node nodes[4];
112 // test with begin, end / prev, next
113 memset(nodes, 0, 4*sizeof(struct node));
114 void *begin = NULL;
115 void *end = NULL;
117 ptrdiff_t loc_prev = offsetof(struct node, prev);
118 ptrdiff_t loc_next = offsetof(struct node, next);
120 int ret;
121 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
122 CU_ASSERT_EQUAL(ret, 0);
123 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
124 CU_ASSERT_PTR_EQUAL(end, &nodes[0]);
125 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL);
126 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL);
128 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
129 CU_ASSERT_EQUAL(ret, 0);
130 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
131 CU_ASSERT_PTR_EQUAL(end, &nodes[1]);
132 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
133 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]);
135 // test with begin only / prev, next
136 memset(nodes, 0, 4*sizeof(struct node));
137 begin = NULL;
138 end = NULL;
140 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
141 CU_ASSERT_EQUAL(ret, 0);
142 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
143 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
144 CU_ASSERT_EQUAL(ret, 0);
145 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
146 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
147 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]);
149 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
150 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]);
151 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]);
153 // test with begin, end / next
154 memset(nodes, 0, 4*sizeof(struct node));
155 begin = NULL;
156 end = NULL;
158 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
159 CU_ASSERT_EQUAL(ret, 0);
160 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]);
161 CU_ASSERT_PTR_EQUAL(end, &nodes[0]);
162 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
163 CU_ASSERT_PTR_EQUAL(end, &nodes[1]);
164 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]);
165 CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL);
166 }
168 int main() {
169 CU_pSuite suite = NULL;
171 if (CUE_SUCCESS != CU_initialize_registry()) {
172 return CU_get_error();
173 }
175 suite = CU_add_suite("linked list suite", NULL, NULL);
176 if (NULL == suite) {
177 CU_cleanup_registry();
178 return CU_get_error();
179 }
181 if (
182 !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) ||
183 !CU_add_test(suite, "linked list: get node at index", test_linked_list_at) ||
184 !CU_add_test(suite, "linked list: add", test_cx_linked_list_add)
185 ) {
186 CU_cleanup_registry();
187 return CU_get_error();
188 }
190 suite = CU_add_suite("array suite", NULL, NULL);
191 if (NULL == suite) {
192 CU_cleanup_registry();
193 return CU_get_error();
194 }
196 /*
197 if (
198 !CU_add_test(suite, "array...", test_array...)
199 ) {
200 CU_cleanup_registry();
201 return CU_get_error();
202 }
203 */
205 CU_basic_set_mode(UCX_CU_BRM);
207 int exitcode;
208 if (CU_basic_run_tests()) {
209 exitcode = CU_get_error();
210 } else {
211 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
212 }
213 CU_cleanup_registry();
214 return exitcode;
215 }