Mon, 20 Feb 2017 16:04:14 +0100
adds new test case for sstrsplit: string ends with delimiter but empty string exceeds list bound
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 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, *first;
34 list = first = ucx_list_append(NULL, (void*)"Hello");
35 UCX_TEST_BEGIN
37 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
38 "failed");
40 list = ucx_list_append(list, (void*)" World!");
42 UCX_TEST_ASSERT(list == first, "does not return first element");
43 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
44 "failed");
45 UCX_TEST_ASSERT(list->next->prev == list, "failed");
46 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
47 UCX_TEST_END
49 ucx_list_free(list);
50 }
52 UCX_TEST(test_ucx_list_prepend) {
53 UcxList *list, *last;
54 list = last = ucx_list_prepend(NULL, (void*)" World!");
55 UCX_TEST_BEGIN
57 list = ucx_list_prepend(list, (void*)"Hello");
59 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
60 "failed");
61 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
62 "failed");
63 UCX_TEST_ASSERT(list == last->prev, "does not return first element");
64 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
65 UCX_TEST_ASSERT(list->prev == NULL, "failed");
67 UCX_TEST_END
68 ucx_list_free(list);
69 }
71 UCX_TEST(test_ucx_list_append_once) {
72 UcxList *list, *first;
73 list = first = ucx_list_append_once(NULL, (void*)"Hello", ucx_strcmp, NULL);
74 UCX_TEST_BEGIN
76 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
77 "failed");
79 list = ucx_list_append_once(list, (void*)"Hello", ucx_strcmp, NULL);
80 list = ucx_list_append_once(list, (void*)" World!", ucx_strcmp, NULL);
82 UCX_TEST_ASSERT(list == first, "does not return first element");
83 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
84 "'Hello' was not inserted _once_");
85 UCX_TEST_ASSERT(list->next->prev == list, "failed");
86 UCX_TEST_ASSERT(list->next->next == NULL, "right not terminated");
87 UCX_TEST_END
89 ucx_list_free(list);
90 }
92 UCX_TEST(test_ucx_list_equals) {
93 const char *hello = "Hello";
94 const char *world = " World!";
95 UcxList *list = ucx_list_append(NULL, (void*)hello);
96 list = ucx_list_append(list, (void*)world);
97 UcxList *list2 = ucx_list_prepend(NULL, (void*)world);
98 list2 = ucx_list_prepend(list2, (void*)hello);
99 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
100 list3 = ucx_list_prepend(list3, (void*)"Hallo");
101 UcxList *list4 = ucx_list_prepend(NULL, (void*)" World!");
102 list4 = ucx_list_prepend(list4, (void*)"Hello");
103 UCX_TEST_BEGIN
105 UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_strcmp, NULL), "failed");
106 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
107 UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed");
109 UCX_TEST_END
110 ucx_list_free(list4);
111 ucx_list_free(list3);
112 ucx_list_free(list2);
113 ucx_list_free(list);
114 }
116 UCX_TEST(test_ucx_list_concat) {
117 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
118 list = ucx_list_append(list, (void*)" my ");
119 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
120 list2 = ucx_list_prepend(list2, (void*)" sweet ");
121 UCX_TEST_BEGIN
123 list = ucx_list_concat(list, list2);
124 list = ucx_list_concat(list, NULL);
125 list = ucx_list_concat(NULL, list);
127 UCX_TEST_ASSERT(!strncmp((const char*)list->data, "Hello", 5),
128 "failed");
129 UCX_TEST_ASSERT(!strncmp((const char*)list->next->data, " my ", 4),
130 "failed");
131 UCX_TEST_ASSERT(!strncmp((const char*)list->next->next->data, " sweet ", 7),
132 "failed");
133 UCX_TEST_ASSERT(!strncmp((const char*)ucx_list_last(list)->data,
134 " World!", 7), "failed");
136 UCX_TEST_ASSERT(list->prev == NULL, "failed");
138 UCX_TEST_END
139 // don't free list2, as it is freed by freeing list;
140 ucx_list_free(list);
141 }
143 UCX_TEST(test_ucx_list_size) {
144 UcxList *list = ucx_list_append(NULL, (void*)"This ");
145 list = ucx_list_append(list, (void*)"list ");
146 list = ucx_list_append(list, (void*)"has ");
147 list = ucx_list_append(list, (void*)"size ");
148 list = ucx_list_append(list, (void*)"5!");
150 UCX_TEST_BEGIN
152 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
153 list = ucx_list_remove(list, ucx_list_get(list, 2));
154 UCX_TEST_ASSERT(ucx_list_size(list) == 4, "failed after removal");
156 UCX_TEST_END
157 ucx_list_free(list);
158 }
160 UCX_TEST(test_ucx_list_first) {
161 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
162 list = ucx_list_append(list, (void*)"the ");
163 list = ucx_list_append(list, (void*)"first!");
165 UCX_TEST_BEGIN
167 const char* first = (const char*) (ucx_list_first(list)->data);
169 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
170 UCX_TEST_ASSERT(ucx_list_first(list->next->next) == list, "failed");
171 UCX_TEST_ASSERT(!ucx_list_first(NULL),
172 "does not return NULL on an empty list");
174 UCX_TEST_END
175 ucx_list_free(list);
176 }
178 UCX_TEST(test_ucx_list_last) {
179 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
180 list = ucx_list_append(list, (void*)"the ");
181 list = ucx_list_append(list, (void*)"last!");
183 UCX_TEST_BEGIN
185 const char* last = (const char*) (ucx_list_last(list->next->next)->data);
187 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
188 UCX_TEST_ASSERT(ucx_list_last(list) == list->next->next, "failed");
189 UCX_TEST_ASSERT(!ucx_list_last(NULL),
190 "does not return NULL on an empty list");
192 UCX_TEST_END
193 ucx_list_free(list);
194 }
196 UCX_TEST(test_ucx_list_get) {
197 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
198 list = ucx_list_append(list, (void*)"the ");
199 list = ucx_list_append(list, (void*)"mid!");
201 UCX_TEST_BEGIN
203 const char* first = (const char*) (ucx_list_get(list, 0)->data);
204 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
205 const char* last = (const char*) (ucx_list_get(list, 2)->data);
207 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
208 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
209 UCX_TEST_ASSERT(strncmp(last, "mid!", 4) == 0, "failed");
210 UCX_TEST_ASSERT(!ucx_list_get(list, -1), "out of bounds (neg)");
211 UCX_TEST_ASSERT(!ucx_list_get(list, 3), "out of bounds");
212 UCX_TEST_ASSERT(!ucx_list_get(NULL, 0), "empty list");
214 UCX_TEST_END
215 ucx_list_free(list);
216 }
218 UCX_TEST(test_ucx_list_indexof) {
219 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
220 list = ucx_list_append(list, (void*)"the ");
221 list = ucx_list_append(list, (void*)"mid!");
223 UCX_TEST_BEGIN
225 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
226 UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed");
227 UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
228 "failed");
230 UcxList *otherlist = ucx_list_append(NULL, (void*) "the ");
231 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
232 UCX_TEST_ASSERT(ucx_list_indexof(NULL, otherlist) == -1, "empty list");
234 ucx_list_free(otherlist);
236 UCX_TEST_END
237 ucx_list_free(list);
238 }
240 UCX_TEST(test_ucx_list_find) {
241 const char* teststr = "string!";
242 UcxList *l = ucx_list_append(NULL, (void*)"find ");
243 l = ucx_list_append(l, (void*)"some ");
244 l = ucx_list_append(l, (void*)teststr);
246 UCX_TEST_BEGIN
248 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
249 "doesn't find string");
250 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
251 "finds non-existing string");
253 UCX_TEST_ASSERT(ucx_list_find(l,(void*)teststr,NULL,NULL) == 2,
254 "doesn't find integer without cmp_func");
256 UCX_TEST_ASSERT(ucx_list_find(NULL, (void*)"some ",ucx_strcmp,NULL) == -1,
257 "empty list");
259 UCX_TEST_END
260 ucx_list_free(l);
261 }
263 UCX_TEST(test_ucx_list_contains) {
264 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
265 l = ucx_list_append(l, (void*)"a ");
266 l = ucx_list_append(l, (void*)"string!");
268 UCX_TEST_BEGIN
270 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),
271 "false negative");
272 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),
273 "false positive");
275 UCX_TEST_END
276 ucx_list_free(l);
277 }
279 UCX_TEST(test_ucx_list_remove) {
280 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
281 list = ucx_list_append(list, (void*)"fucking");
282 list = ucx_list_append(list, (void*)"World!");
284 UcxList *list2 = ucx_list_append(NULL, (void*)"A");
285 list2 = ucx_list_append(list2, (void*)"B");
286 list2 = ucx_list_append(list2, (void*)"C");
287 list2 = ucx_list_append(list2, (void*)"D");
288 list2 = ucx_list_append(list2, (void*)"E");
289 list2 = ucx_list_append(list2, (void*)"F");
290 list2 = ucx_list_append(list2, (void*)"G");
292 UCX_TEST_BEGIN
294 list = ucx_list_remove(list, ucx_list_get(list, 1));
296 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
297 "failed");
298 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, "World!", 7) == 0,
299 "failed");
300 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
302 // remove first element: B, C, D, E, F, G
303 list2 = ucx_list_remove(list2, list2);
305 UCX_TEST_ASSERT(ucx_list_size(list2) == 6, "list2 has wrong size");
306 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
307 "wrong first element");
308 UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 5)->data, "G", 1)
309 == 0, "wrong last element");
311 // remove second element: B, D, E, F, G
312 list2 = ucx_list_remove(list2, list2->next);
314 UCX_TEST_ASSERT(ucx_list_size(list2) == 5, "list2 has wrong size");
315 UCX_TEST_ASSERT(strncmp((const char*)list2->next->data, "D", 1) == 0,
316 "wrong second element");
318 UcxList *last = ucx_list_get(list2, 4);
319 list2 = ucx_list_remove(list2, last->prev);
321 UCX_TEST_ASSERT(ucx_list_size(list2) == 4, "list2 has wrong size");
322 UCX_TEST_ASSERT(strncmp((const char*)last->prev->data, "E", 1) == 0,
323 "wrong element");
325 // remove last element: B, D, E, F
326 list2 = ucx_list_remove(list2, last);
327 UCX_TEST_ASSERT(ucx_list_size(list2) == 3, "list2 has wrong size");
328 UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 2)->data, "E", 1)
329 == 0, "wrong last element");
331 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
332 "wrong element");
334 list2 = ucx_list_remove(list2, list2);
335 UCX_TEST_ASSERT(ucx_list_size(list2) == 2, "list2 has wrong size");
336 list2 = ucx_list_remove(list2, list2);
337 UCX_TEST_ASSERT(ucx_list_size(list2) == 1, "list2 has wrong size");
338 list2 = ucx_list_remove(list2, list2);
339 UCX_TEST_ASSERT(list2 == NULL, "list2 is not null");
341 UCX_TEST_END
342 ucx_list_free(list);
343 }
345 UCX_TEST(test_ucx_list_clone) {
347 char *hello = (char*)malloc(6);
348 char *world = (char*)malloc(8);
350 memcpy(hello, "Hello", 6);
351 memcpy(world, " World!", 8);
353 UcxList *list = ucx_list_append(NULL, hello);
354 list = ucx_list_append(list, world);
356 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
357 UCX_TEST_BEGIN
359 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
360 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
361 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
363 UCX_TEST_END
365 ucx_list_free_content(copy, free);
367 free(world);
368 free(hello);
369 ucx_list_free(list);
370 ucx_list_free(copy);
371 }
373 UCX_TEST(test_ucx_list_sort) {
374 UcxList *list = ucx_list_append(NULL, (void*)"this");
375 list = ucx_list_append(list, (void*)"is");
376 list = ucx_list_append(list, (void*)"a");
377 list = ucx_list_append(list, (void*)"test");
378 list = ucx_list_append(list, (void*)"for");
379 list = ucx_list_append(list, (void*)"partial");
380 list = ucx_list_append(list, (void*)"correctness");
381 list = ucx_list_append(list, (void*)"of");
382 list = ucx_list_append(list, (void*)"the");
383 list = ucx_list_append(list, (void*)"sort");
384 list = ucx_list_append(list, (void*)"function");
385 list = ucx_list_append(list, (void*)"that");
386 list = ucx_list_append(list, (void*)"shall");
387 list = ucx_list_append(list, (void*)"pass");
388 list = ucx_list_append(list, (void*)"this");
389 list = ucx_list_append(list, (void*)"test");
391 UcxList *expected = ucx_list_append(NULL, (void*)"a");
392 expected = ucx_list_append(expected, (void*)"correctness");
393 expected = ucx_list_append(expected, (void*)"for");
394 expected = ucx_list_append(expected, (void*)"function");
395 expected = ucx_list_append(expected, (void*)"is");
396 expected = ucx_list_append(expected, (void*)"of");
397 expected = ucx_list_append(expected, (void*)"partial");
398 expected = ucx_list_append(expected, (void*)"pass");
399 expected = ucx_list_append(expected, (void*)"shall");
400 expected = ucx_list_append(expected, (void*)"sort");
401 expected = ucx_list_append(expected, (void*)"test");
402 expected = ucx_list_append(expected, (void*)"test");
403 expected = ucx_list_append(expected, (void*)"that");
404 expected = ucx_list_append(expected, (void*)"the");
405 expected = ucx_list_append(expected, (void*)"this");
406 expected = ucx_list_append(expected, (void*)"this");
408 list = ucx_list_sort(list, ucx_strcmp, NULL);
410 UCX_TEST_BEGIN
411 UCX_TEST_ASSERT(
412 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
413 UCX_TEST_ASSERT(ucx_list_size(list) == 16, "list has now a wrong size");
414 UcxList *l = list;
415 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
416 while (l->next != NULL) {
417 UCX_TEST_ASSERT(l->next->prev == l, "next or prev pointer corrupted");
418 l = l->next;
419 }
420 UCX_TEST_ASSERT(!ucx_list_sort(NULL, ucx_strcmp, NULL),
421 "failed to sort empty list");
422 UCX_TEST_END
424 ucx_list_free(expected);
425 ucx_list_free(list);
426 }