Mon, 14 Jul 2014 13:20:03 +0200
fixed stream copy bug + fixed doc issues in mempool
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 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_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
84 UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_strcmp, NULL), "failed");
85 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
86 UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed");
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 }
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
102 list = ucx_list_concat(list, list2);
103 list = ucx_list_concat(list, NULL);
104 list = ucx_list_concat(NULL, list);
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");
115 UCX_TEST_ASSERT(list->prev == NULL, "failed");
117 UCX_TEST_END
118 // don't free list2, as it is freed by freeing list;
119 ucx_list_free(list);
120 }
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!");
129 UCX_TEST_BEGIN
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");
135 UCX_TEST_END
136 ucx_list_free(list);
137 }
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!");
144 UCX_TEST_BEGIN
146 const char* first = (const char*) (ucx_list_first(list)->data);
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");
153 UCX_TEST_END
154 ucx_list_free(list);
155 }
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!");
162 UCX_TEST_BEGIN
164 const char* last = (const char*) (ucx_list_last(list->next->next)->data);
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");
171 UCX_TEST_END
172 ucx_list_free(list);
173 }
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!");
180 UCX_TEST_BEGIN
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);
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");
193 UCX_TEST_END
194 ucx_list_free(list);
195 }
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!");
202 UCX_TEST_BEGIN
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");
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");
213 ucx_list_free(otherlist);
215 UCX_TEST_END
216 ucx_list_free(list);
217 }
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);
225 UCX_TEST_BEGIN
227 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
228 "doesn't find string");
229 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
230 "finds non-existing string");
232 UCX_TEST_ASSERT(ucx_list_find(l,(void*)teststr,NULL,NULL) == 2,
233 "doesn't find integer without cmp_func");
235 UCX_TEST_ASSERT(ucx_list_find(NULL, (void*)"some ",ucx_strcmp,NULL) == -1,
236 "empty list");
238 UCX_TEST_END
239 ucx_list_free(l);
240 }
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!");
247 UCX_TEST_BEGIN
249 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),
250 "false negative");
251 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),
252 "false positive");
254 UCX_TEST_END
255 ucx_list_free(l);
256 }
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!");
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");
271 UCX_TEST_BEGIN
273 list = ucx_list_remove(list, ucx_list_get(list, 1));
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");
281 // remove first element: B, C, D, E, F, G
282 list2 = ucx_list_remove(list2, list2);
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");
290 // remove second element: B, D, E, F, G
291 list2 = ucx_list_remove(list2, list2->next);
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");
297 UcxList *last = ucx_list_get(list2, 4);
298 list2 = ucx_list_remove(list2, last->prev);
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");
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");
310 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
311 "wrong element");
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");
320 UCX_TEST_END
321 ucx_list_free(list);
322 }
324 UCX_TEST(test_ucx_list_clone) {
326 char *hello = (char*)malloc(6);
327 char *world = (char*)malloc(8);
329 memcpy(hello, "Hello", 6);
330 memcpy(world, " World!", 8);
332 UcxList *list = ucx_list_append(NULL, hello);
333 list = ucx_list_append(list, world);
335 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
336 UCX_TEST_BEGIN
338 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, 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");
342 UCX_TEST_END
343 free(copy->next->data);
344 free(copy->data);
346 free(world);
347 free(hello);
348 ucx_list_free(list);
349 ucx_list_free(copy);
350 }
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");
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");
387 list = ucx_list_sort(list, ucx_strcmp, NULL);
389 UCX_TEST_BEGIN
390 UCX_TEST_ASSERT(
391 ucx_list_equals(list, expected, ucx_strcmp, 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_strcmp, NULL),
400 "failed to sort empty list");
401 UCX_TEST_END
403 ucx_list_free(expected);
404 ucx_list_free(list);
405 }