test/list_tests.c

changeset 390
d345541018fa
parent 371
365b24f20f98
child 471
e9ef2637e101
equal deleted inserted replaced
389:92e482410453 390:d345541018fa
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 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 */
28
29 #include "list_tests.h"
30 #include <ucx/utils.h>
31
32 UCX_TEST(test_ucx_list_append) {
33 UcxList *list, *first;
34 list = first = ucx_list_append(NULL, (void*)"Hello");
35 UCX_TEST_BEGIN
36
37 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
38 "failed");
39
40 list = ucx_list_append(list, (void*)" World!");
41
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
48
49 ucx_list_free(list);
50 }
51
52 UCX_TEST(test_ucx_list_prepend) {
53 UcxList *list, *last;
54 list = last = ucx_list_prepend(NULL, (void*)" World!");
55 UCX_TEST_BEGIN
56
57 list = ucx_list_prepend(list, (void*)"Hello");
58
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");
66
67 UCX_TEST_END
68 ucx_list_free(list);
69 }
70
71 UCX_TEST(test_ucx_list_equals) {
72 const char *hello = "Hello";
73 const char *world = " World!";
74 UcxList *list = ucx_list_append(NULL, (void*)hello);
75 list = ucx_list_append(list, (void*)world);
76 UcxList *list2 = ucx_list_prepend(NULL, (void*)world);
77 list2 = ucx_list_prepend(list2, (void*)hello);
78 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
79 list3 = ucx_list_prepend(list3, (void*)"Hallo");
80 UcxList *list4 = ucx_list_prepend(NULL, (void*)" World!");
81 list4 = ucx_list_prepend(list4, (void*)"Hello");
82 UCX_TEST_BEGIN
83
84 UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_cmp_str, NULL), "failed");
85 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_cmp_str, NULL), "failed");
86 UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed");
87
88 UCX_TEST_END
89 ucx_list_free(list4);
90 ucx_list_free(list3);
91 ucx_list_free(list2);
92 ucx_list_free(list);
93 }
94
95 UCX_TEST(test_ucx_list_concat) {
96 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
97 list = ucx_list_append(list, (void*)" my ");
98 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
99 list2 = ucx_list_prepend(list2, (void*)" sweet ");
100 UCX_TEST_BEGIN
101
102 list = ucx_list_concat(list, list2);
103 list = ucx_list_concat(list, NULL);
104 list = ucx_list_concat(NULL, list);
105
106 UCX_TEST_ASSERT(!strncmp((const char*)list->data, "Hello", 5),
107 "failed");
108 UCX_TEST_ASSERT(!strncmp((const char*)list->next->data, " my ", 4),
109 "failed");
110 UCX_TEST_ASSERT(!strncmp((const char*)list->next->next->data, " sweet ", 7),
111 "failed");
112 UCX_TEST_ASSERT(!strncmp((const char*)ucx_list_last(list)->data,
113 " World!", 7), "failed");
114
115 UCX_TEST_ASSERT(list->prev == NULL, "failed");
116
117 UCX_TEST_END
118 // don't free list2, as it is freed by freeing list;
119 ucx_list_free(list);
120 }
121
122 UCX_TEST(test_ucx_list_size) {
123 UcxList *list = ucx_list_append(NULL, (void*)"This ");
124 list = ucx_list_append(list, (void*)"list ");
125 list = ucx_list_append(list, (void*)"has ");
126 list = ucx_list_append(list, (void*)"size ");
127 list = ucx_list_append(list, (void*)"5!");
128
129 UCX_TEST_BEGIN
130
131 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
132 list = ucx_list_remove(list, ucx_list_get(list, 2));
133 UCX_TEST_ASSERT(ucx_list_size(list) == 4, "failed after removal");
134
135 UCX_TEST_END
136 ucx_list_free(list);
137 }
138
139 UCX_TEST(test_ucx_list_first) {
140 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
141 list = ucx_list_append(list, (void*)"the ");
142 list = ucx_list_append(list, (void*)"first!");
143
144 UCX_TEST_BEGIN
145
146 const char* first = (const char*) (ucx_list_first(list)->data);
147
148 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
149 UCX_TEST_ASSERT(ucx_list_first(list->next->next) == list, "failed");
150 UCX_TEST_ASSERT(!ucx_list_first(NULL),
151 "does not return NULL on an empty list");
152
153 UCX_TEST_END
154 ucx_list_free(list);
155 }
156
157 UCX_TEST(test_ucx_list_last) {
158 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
159 list = ucx_list_append(list, (void*)"the ");
160 list = ucx_list_append(list, (void*)"last!");
161
162 UCX_TEST_BEGIN
163
164 const char* last = (const char*) (ucx_list_last(list->next->next)->data);
165
166 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
167 UCX_TEST_ASSERT(ucx_list_last(list) == list->next->next, "failed");
168 UCX_TEST_ASSERT(!ucx_list_last(NULL),
169 "does not return NULL on an empty list");
170
171 UCX_TEST_END
172 ucx_list_free(list);
173 }
174
175 UCX_TEST(test_ucx_list_get) {
176 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
177 list = ucx_list_append(list, (void*)"the ");
178 list = ucx_list_append(list, (void*)"mid!");
179
180 UCX_TEST_BEGIN
181
182 const char* first = (const char*) (ucx_list_get(list, 0)->data);
183 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
184 const char* last = (const char*) (ucx_list_get(list, 2)->data);
185
186 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
187 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
188 UCX_TEST_ASSERT(strncmp(last, "mid!", 4) == 0, "failed");
189 UCX_TEST_ASSERT(!ucx_list_get(list, -1), "out of bounds (neg)");
190 UCX_TEST_ASSERT(!ucx_list_get(list, 3), "out of bounds");
191 UCX_TEST_ASSERT(!ucx_list_get(NULL, 0), "empty list");
192
193 UCX_TEST_END
194 ucx_list_free(list);
195 }
196
197 UCX_TEST(test_ucx_list_indexof) {
198 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
199 list = ucx_list_append(list, (void*)"the ");
200 list = ucx_list_append(list, (void*)"mid!");
201
202 UCX_TEST_BEGIN
203
204 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
205 UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed");
206 UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
207 "failed");
208
209 UcxList *otherlist = ucx_list_append(NULL, (void*) "the ");
210 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
211 UCX_TEST_ASSERT(ucx_list_indexof(NULL, otherlist) == -1, "empty list");
212
213 ucx_list_free(otherlist);
214
215 UCX_TEST_END
216 ucx_list_free(list);
217 }
218
219 UCX_TEST(test_ucx_list_find) {
220 const char* teststr = "string!";
221 UcxList *l = ucx_list_append(NULL, (void*)"find ");
222 l = ucx_list_append(l, (void*)"some ");
223 l = ucx_list_append(l, (void*)teststr);
224
225 UCX_TEST_BEGIN
226
227 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_cmp_str,NULL) == 1,
228 "doesn't find string");
229 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_cmp_str,NULL) == -1,
230 "finds non-existing string");
231
232 UCX_TEST_ASSERT(ucx_list_find(l,(void*)teststr,NULL,NULL) == 2,
233 "doesn't find integer without cmp_func");
234
235 UCX_TEST_ASSERT(ucx_list_find(NULL, (void*)"some ",ucx_cmp_str,NULL) == -1,
236 "empty list");
237
238 UCX_TEST_END
239 ucx_list_free(l);
240 }
241
242 UCX_TEST(test_ucx_list_contains) {
243 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
244 l = ucx_list_append(l, (void*)"a ");
245 l = ucx_list_append(l, (void*)"string!");
246
247 UCX_TEST_BEGIN
248
249 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_cmp_str,NULL),
250 "false negative");
251 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_cmp_str,NULL),
252 "false positive");
253
254 UCX_TEST_END
255 ucx_list_free(l);
256 }
257
258 UCX_TEST(test_ucx_list_remove) {
259 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
260 list = ucx_list_append(list, (void*)"fucking");
261 list = ucx_list_append(list, (void*)"World!");
262
263 UcxList *list2 = ucx_list_append(NULL, (void*)"A");
264 list2 = ucx_list_append(list2, (void*)"B");
265 list2 = ucx_list_append(list2, (void*)"C");
266 list2 = ucx_list_append(list2, (void*)"D");
267 list2 = ucx_list_append(list2, (void*)"E");
268 list2 = ucx_list_append(list2, (void*)"F");
269 list2 = ucx_list_append(list2, (void*)"G");
270
271 UCX_TEST_BEGIN
272
273 list = ucx_list_remove(list, ucx_list_get(list, 1));
274
275 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
276 "failed");
277 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, "World!", 7) == 0,
278 "failed");
279 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
280
281 // remove first element: B, C, D, E, F, G
282 list2 = ucx_list_remove(list2, list2);
283
284 UCX_TEST_ASSERT(ucx_list_size(list2) == 6, "list2 has wrong size");
285 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
286 "wrong first element");
287 UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 5)->data, "G", 1)
288 == 0, "wrong last element");
289
290 // remove second element: B, D, E, F, G
291 list2 = ucx_list_remove(list2, list2->next);
292
293 UCX_TEST_ASSERT(ucx_list_size(list2) == 5, "list2 has wrong size");
294 UCX_TEST_ASSERT(strncmp((const char*)list2->next->data, "D", 1) == 0,
295 "wrong second element");
296
297 UcxList *last = ucx_list_get(list2, 4);
298 list2 = ucx_list_remove(list2, last->prev);
299
300 UCX_TEST_ASSERT(ucx_list_size(list2) == 4, "list2 has wrong size");
301 UCX_TEST_ASSERT(strncmp((const char*)last->prev->data, "E", 1) == 0,
302 "wrong element");
303
304 // remove last element: B, D, E, F
305 list2 = ucx_list_remove(list2, last);
306 UCX_TEST_ASSERT(ucx_list_size(list2) == 3, "list2 has wrong size");
307 UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 2)->data, "E", 1)
308 == 0, "wrong last element");
309
310 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
311 "wrong element");
312
313 list2 = ucx_list_remove(list2, list2);
314 UCX_TEST_ASSERT(ucx_list_size(list2) == 2, "list2 has wrong size");
315 list2 = ucx_list_remove(list2, list2);
316 UCX_TEST_ASSERT(ucx_list_size(list2) == 1, "list2 has wrong size");
317 list2 = ucx_list_remove(list2, list2);
318 UCX_TEST_ASSERT(list2 == NULL, "list2 is not null");
319
320 UCX_TEST_END
321 ucx_list_free(list);
322 }
323
324 UCX_TEST(test_ucx_list_clone) {
325
326 char *hello = (char*)malloc(6);
327 char *world = (char*)malloc(8);
328
329 memcpy(hello, "Hello", 6);
330 memcpy(world, " World!", 8);
331
332 UcxList *list = ucx_list_append(NULL, hello);
333 list = ucx_list_append(list, world);
334
335 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
336 UCX_TEST_BEGIN
337
338 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_cmp_str, NULL), "failed");
339 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
340 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
341
342 UCX_TEST_END
343
344 ucx_list_free_content(copy, free);
345
346 free(world);
347 free(hello);
348 ucx_list_free(list);
349 ucx_list_free(copy);
350 }
351
352 UCX_TEST(test_ucx_list_sort) {
353 UcxList *list = ucx_list_append(NULL, (void*)"this");
354 list = ucx_list_append(list, (void*)"is");
355 list = ucx_list_append(list, (void*)"a");
356 list = ucx_list_append(list, (void*)"test");
357 list = ucx_list_append(list, (void*)"for");
358 list = ucx_list_append(list, (void*)"partial");
359 list = ucx_list_append(list, (void*)"correctness");
360 list = ucx_list_append(list, (void*)"of");
361 list = ucx_list_append(list, (void*)"the");
362 list = ucx_list_append(list, (void*)"sort");
363 list = ucx_list_append(list, (void*)"function");
364 list = ucx_list_append(list, (void*)"that");
365 list = ucx_list_append(list, (void*)"shall");
366 list = ucx_list_append(list, (void*)"pass");
367 list = ucx_list_append(list, (void*)"this");
368 list = ucx_list_append(list, (void*)"test");
369
370 UcxList *expected = ucx_list_append(NULL, (void*)"a");
371 expected = ucx_list_append(expected, (void*)"correctness");
372 expected = ucx_list_append(expected, (void*)"for");
373 expected = ucx_list_append(expected, (void*)"function");
374 expected = ucx_list_append(expected, (void*)"is");
375 expected = ucx_list_append(expected, (void*)"of");
376 expected = ucx_list_append(expected, (void*)"partial");
377 expected = ucx_list_append(expected, (void*)"pass");
378 expected = ucx_list_append(expected, (void*)"shall");
379 expected = ucx_list_append(expected, (void*)"sort");
380 expected = ucx_list_append(expected, (void*)"test");
381 expected = ucx_list_append(expected, (void*)"test");
382 expected = ucx_list_append(expected, (void*)"that");
383 expected = ucx_list_append(expected, (void*)"the");
384 expected = ucx_list_append(expected, (void*)"this");
385 expected = ucx_list_append(expected, (void*)"this");
386
387 list = ucx_list_sort(list, ucx_cmp_str, NULL);
388
389 UCX_TEST_BEGIN
390 UCX_TEST_ASSERT(
391 ucx_list_equals(list, expected, ucx_cmp_str, NULL), "failed");
392 UCX_TEST_ASSERT(ucx_list_size(list) == 16, "list has now a wrong size");
393 UcxList *l = list;
394 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
395 while (l->next != NULL) {
396 UCX_TEST_ASSERT(l->next->prev == l, "next or prev pointer corrupted");
397 l = l->next;
398 }
399 UCX_TEST_ASSERT(!ucx_list_sort(NULL, ucx_cmp_str, NULL),
400 "failed to sort empty list");
401 UCX_TEST_END
402
403 ucx_list_free(expected);
404 ucx_list_free(list);
405 }
406
407 UCX_TEST(test_ucx_list_union) {
408 UcxList *left = ucx_list_append(NULL, (void*)"this");
409 left = ucx_list_append(left, (void*)"is");
410 left = ucx_list_append(left, (void*)"a");
411 left = ucx_list_append(left, (void*)"test");
412
413 UcxList *right = ucx_list_append(NULL, (void*)"to");
414 right = ucx_list_append(right, (void*)"test");
415 right = ucx_list_append(right, (void*)"set");
416 right = ucx_list_append(right, (void*)"operations");
417
418 UcxList *expected = ucx_list_append(NULL, (void*)"this");
419 expected = ucx_list_append(expected, (void*)"is");
420 expected = ucx_list_append(expected, (void*)"a");
421 expected = ucx_list_append(expected, (void*)"test");
422 expected = ucx_list_append(expected, (void*)"to");
423 expected = ucx_list_append(expected, (void*)"set");
424 expected = ucx_list_append(expected, (void*)"operations");
425
426 UcxList* result = ucx_list_union(left, right, ucx_cmp_str,
427 NULL, NULL, NULL);
428
429 UCX_TEST_BEGIN
430 UCX_TEST_ASSERT(ucx_list_equals(result, expected,
431 ucx_cmp_str, NULL), "failed");
432 UCX_TEST_END
433
434 ucx_list_free(result);
435 ucx_list_free(expected);
436 ucx_list_free(right);
437 ucx_list_free(left);
438 }
439
440 UCX_TEST(test_ucx_list_intersection) {
441 UcxList *left = ucx_list_append(NULL, (void*)"this");
442 left = ucx_list_append(left, (void*)"is");
443 left = ucx_list_append(left, (void*)"a");
444 left = ucx_list_append(left, (void*)"test");
445
446 UcxList *right = ucx_list_append(NULL, (void*)"to");
447 right = ucx_list_append(right, (void*)"test");
448 right = ucx_list_append(right, (void*)"a");
449 right = ucx_list_append(right, (void*)"set");
450 right = ucx_list_append(right, (void*)"operation");
451
452 UcxList *expected = ucx_list_append(NULL, (void*)"a");
453 expected = ucx_list_append(expected, (void*)"test");
454
455 UcxList* result = ucx_list_intersection(left, right, ucx_cmp_str,
456 NULL, NULL, NULL);
457
458 UCX_TEST_BEGIN
459 UCX_TEST_ASSERT(ucx_list_equals(result, expected,
460 ucx_cmp_str, NULL), "failed");
461 UCX_TEST_END
462
463 ucx_list_free(result);
464 ucx_list_free(expected);
465 ucx_list_free(right);
466 ucx_list_free(left);
467 }
468
469 UCX_TEST(test_ucx_list_difference) {
470 UcxList *left = ucx_list_append(NULL, (void*)"this");
471 left = ucx_list_append(left, (void*)"is");
472 left = ucx_list_append(left, (void*)"a");
473 left = ucx_list_append(left, (void*)"test");
474
475 UcxList *right = ucx_list_append(NULL, (void*)"to");
476 right = ucx_list_append(right, (void*)"test");
477 right = ucx_list_append(right, (void*)"this");
478 right = ucx_list_append(right, (void*)"set");
479 right = ucx_list_append(right, (void*)"operations");
480
481 UcxList *expected = ucx_list_append(NULL, (void*)"is");
482 expected = ucx_list_append(expected, (void*)"a");
483
484 UcxList* result = ucx_list_difference(left, right, ucx_cmp_str,
485 NULL, NULL, NULL);
486
487 UCX_TEST_BEGIN
488 UCX_TEST_ASSERT(ucx_list_equals(result, expected,
489 ucx_cmp_str, NULL), "failed");
490 UCX_TEST_END
491
492 ucx_list_free(result);
493 ucx_list_free(expected);
494 ucx_list_free(right);
495 ucx_list_free(left);
496 }

mercurial