Fri, 09 Aug 2013 11:32:10 +0200
documented test.h and removed duplicated implement/declare macros for UCX_TEST
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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 "list_tests.h"
30 #include "ucx/utils.h"
32 UCX_TEST(test_ucx_list_append) {
33 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
34 UCX_TEST_BEGIN
36 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
37 "failed");
39 list = ucx_list_append(list, (void*)" World!");
41 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
42 "failed");
43 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
44 UCX_TEST_END
46 ucx_list_free(list);
47 }
49 UCX_TEST(test_ucx_list_prepend) {
50 UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
51 UCX_TEST_BEGIN
53 list = ucx_list_prepend(list, (void*)"Hello");
55 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
56 "failed");
57 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
58 "failed");
59 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
61 UCX_TEST_END
62 ucx_list_free(list);
63 }
65 UCX_TEST(test_ucx_list_equals) {
66 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
67 list = ucx_list_append(list, (void*)" World!");
68 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
69 list2 = ucx_list_prepend(list2, (void*)"Hello");
70 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
71 list3 = ucx_list_prepend(list3, (void*)"Hallo");
72 UCX_TEST_BEGIN
74 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
75 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
77 UCX_TEST_END
78 ucx_list_free(list3);
79 ucx_list_free(list2);
80 ucx_list_free(list);
81 }
83 UCX_TEST(test_ucx_list_concat) {
84 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
85 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
86 UCX_TEST_BEGIN
88 list = ucx_list_concat(list, list2);
90 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
91 "failed");
92 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
93 "failed");
94 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
96 UCX_TEST_END
97 ucx_list_free(list);
98 }
100 UCX_TEST(test_ucx_list_size) {
101 UcxList *list = ucx_list_append(NULL, (void*)"This ");
102 list = ucx_list_append(list, (void*)"list ");
103 list = ucx_list_append(list, (void*)"has ");
104 list = ucx_list_append(list, (void*)"size ");
105 list = ucx_list_append(list, (void*)"5!");
107 UCX_TEST_BEGIN
109 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
111 UCX_TEST_END
112 ucx_list_free(list);
113 }
115 UCX_TEST(test_ucx_list_first) {
116 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
117 list = ucx_list_append(list, (void*)"the ");
118 list = ucx_list_append(list, (void*)"first!");
120 UCX_TEST_BEGIN
122 const char* first = (const char*) (ucx_list_first(list)->data);
124 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
126 UCX_TEST_END
127 ucx_list_free(list);
128 }
130 UCX_TEST(test_ucx_list_last) {
131 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
132 list = ucx_list_append(list, (void*)"the ");
133 list = ucx_list_append(list, (void*)"last!");
135 UCX_TEST_BEGIN
137 const char* last = (const char*) (ucx_list_last(list)->data);
139 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
141 UCX_TEST_END
142 ucx_list_free(list);
143 }
145 UCX_TEST(test_ucx_list_get) {
146 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
147 list = ucx_list_append(list, (void*)"the ");
148 list = ucx_list_append(list, (void*)"mid!");
150 UCX_TEST_BEGIN
152 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
154 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
156 UCX_TEST_END
157 ucx_list_free(list);
158 }
160 UCX_TEST(test_ucx_list_indexof) {
161 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
162 list = ucx_list_append(list, (void*)"the ");
163 list = ucx_list_append(list, (void*)"mid!");
165 UCX_TEST_BEGIN
167 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
168 UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed");
169 UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
170 "failed");
172 UcxList *otherlist = ucx_list_append(NULL, (void*) "foobar");
173 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
174 ucx_list_free(otherlist);
176 UCX_TEST_END
177 ucx_list_free(list);
178 }
180 UCX_TEST(test_ucx_list_find) {
181 UcxList *l = ucx_list_append(NULL, (void*)"find ");
182 l = ucx_list_append(l, (void*)"some ");
183 l = ucx_list_append(l, (void*)"string!");
185 UCX_TEST_BEGIN
187 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
188 "doesn't find string");
189 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
190 "finds non-existing string");
192 UCX_TEST_END
193 ucx_list_free(l);
194 }
196 UCX_TEST(test_ucx_list_contains) {
197 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
198 l = ucx_list_append(l, (void*)"a ");
199 l = ucx_list_append(l, (void*)"string!");
201 UCX_TEST_BEGIN
203 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),
204 "false negative");
205 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),
206 "false positive");
208 UCX_TEST_END
209 ucx_list_free(l);
210 }
212 UCX_TEST(test_ucx_list_remove) {
213 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
214 list = ucx_list_append(list, (void*)" fucking");
215 list = ucx_list_append(list, (void*)" World!");
217 UCX_TEST_BEGIN
219 list = ucx_list_remove(list, ucx_list_get(list, 1));
221 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
222 "failed");
223 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
224 "failed");
225 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
227 UCX_TEST_END
228 ucx_list_free(list);
229 }
231 UCX_TEST(test_ucx_list_clone) {
233 char *hello = (char*)malloc(6);
234 char *world = (char*)malloc(8);
236 memcpy(hello, "Hello", 6);
237 memcpy(world, " World!", 8);
239 UcxList *list = ucx_list_append(NULL, hello);
240 list = ucx_list_append(list, world);
242 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
243 UCX_TEST_BEGIN
245 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
246 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
247 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
249 UCX_TEST_END
250 free(copy->next->data);
251 free(copy->data);
253 free(world);
254 free(hello);
255 ucx_list_free(list);
256 ucx_list_free(copy);
257 }
259 UCX_TEST(test_ucx_list_sort) {
260 UcxList *list = ucx_list_append(NULL, (void*)"this");
261 list = ucx_list_append(list, (void*)"is");
262 list = ucx_list_append(list, (void*)"a");
263 list = ucx_list_append(list, (void*)"test");
264 list = ucx_list_append(list, (void*)"for");
265 list = ucx_list_append(list, (void*)"partial");
266 list = ucx_list_append(list, (void*)"correctness");
268 UcxList *expected = ucx_list_append(NULL, (void*)"a");
269 expected = ucx_list_append(expected, (void*)"correctness");
270 expected = ucx_list_append(expected, (void*)"for");
271 expected = ucx_list_append(expected, (void*)"is");
272 expected = ucx_list_append(expected, (void*)"partial");
273 expected = ucx_list_append(expected, (void*)"test");
274 expected = ucx_list_append(expected, (void*)"this");
276 list = ucx_list_sort(list, ucx_strcmp, NULL);
278 UCX_TEST_BEGIN
279 UCX_TEST_ASSERT(
280 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
281 UcxList *l = list;
282 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
283 while (l->next != NULL) {
284 UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
285 l = l->next;
286 }
287 UCX_TEST_END
289 ucx_list_free(expected);
290 ucx_list_free(list);
291 }