test/list_tests.c

changeset 172
7084e8e8433c
parent 162
52dfe5f4ecd7
child 177
11ad03783baf
equal deleted inserted replaced
171:49cebb8eceff 172:7084e8e8433c
28 28
29 #include "list_tests.h" 29 #include "list_tests.h"
30 #include "ucx/utils.h" 30 #include "ucx/utils.h"
31 31
32 UCX_TEST(test_ucx_list_append) { 32 UCX_TEST(test_ucx_list_append) {
33 UcxList *list = ucx_list_append(NULL, (void*)"Hello"); 33 UcxList *list, *first;
34 list = first = ucx_list_append(NULL, (void*)"Hello");
34 UCX_TEST_BEGIN 35 UCX_TEST_BEGIN
35 36
36 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, 37 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
37 "failed"); 38 "failed");
38 39
39 list = ucx_list_append(list, (void*)" World!"); 40 list = ucx_list_append(list, (void*)" World!");
40 41
42 UCX_TEST_ASSERT(list == first, "does not return first element");
41 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, 43 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
42 "failed"); 44 "failed");
45 UCX_TEST_ASSERT(list->next->prev == list, "failed");
43 UCX_TEST_ASSERT(list->next->next == NULL, "failed"); 46 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
44 UCX_TEST_END 47 UCX_TEST_END
45 48
46 ucx_list_free(list); 49 ucx_list_free(list);
47 } 50 }
48 51
49 UCX_TEST(test_ucx_list_prepend) { 52 UCX_TEST(test_ucx_list_prepend) {
50 UcxList *list = ucx_list_prepend(NULL, (void*)" World!"); 53 UcxList *list, *last;
54 list = last = ucx_list_prepend(NULL, (void*)" World!");
51 UCX_TEST_BEGIN 55 UCX_TEST_BEGIN
52 56
53 list = ucx_list_prepend(list, (void*)"Hello"); 57 list = ucx_list_prepend(list, (void*)"Hello");
54 58
55 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, 59 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
56 "failed"); 60 "failed");
57 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, 61 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
58 "failed"); 62 "failed");
63 UCX_TEST_ASSERT(list == last->prev, "does not return first element");
59 UCX_TEST_ASSERT(list->next->next == NULL, "failed"); 64 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
65 UCX_TEST_ASSERT(list->prev == NULL, "failed");
60 66
61 UCX_TEST_END 67 UCX_TEST_END
62 ucx_list_free(list); 68 ucx_list_free(list);
63 } 69 }
64 70
65 UCX_TEST(test_ucx_list_equals) { 71 UCX_TEST(test_ucx_list_equals) {
66 UcxList *list = ucx_list_append(NULL, (void*)"Hello"); 72 const char *hello = "Hello";
67 list = ucx_list_append(list, (void*)" World!"); 73 const char *world = " World!";
68 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!"); 74 UcxList *list = ucx_list_append(NULL, (void*)hello);
69 list2 = ucx_list_prepend(list2, (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);
70 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!"); 78 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
71 list3 = ucx_list_prepend(list3, (void*)"Hallo"); 79 list3 = ucx_list_prepend(list3, (void*)"Hallo");
72 UCX_TEST_BEGIN 80 UcxList *list4 = ucx_list_prepend(NULL, (void*)" World!");
73 81 list4 = ucx_list_prepend(list4, (void*)"Hello");
74 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed"); 82 UCX_TEST_BEGIN
83
84 UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_strcmp, NULL), "failed");
75 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed"); 85 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
76 86 UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed");
77 UCX_TEST_END 87
88 UCX_TEST_END
89 ucx_list_free(list4);
78 ucx_list_free(list3); 90 ucx_list_free(list3);
79 ucx_list_free(list2); 91 ucx_list_free(list2);
80 ucx_list_free(list); 92 ucx_list_free(list);
81 } 93 }
82 94
83 UCX_TEST(test_ucx_list_concat) { 95 UCX_TEST(test_ucx_list_concat) {
84 UcxList *list = ucx_list_append(NULL, (void*)"Hello"); 96 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
97 list = ucx_list_append(list, (void*)" my ");
85 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!"); 98 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
99 list2 = ucx_list_prepend(list2, (void*)" sweet ");
86 UCX_TEST_BEGIN 100 UCX_TEST_BEGIN
87 101
88 list = ucx_list_concat(list, list2); 102 list = ucx_list_concat(list, list2);
89 103 list = ucx_list_concat(list, NULL);
90 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, 104 list = ucx_list_concat(NULL, list);
91 "failed"); 105
92 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, 106 UCX_TEST_ASSERT(!strncmp((const char*)list->data, "Hello", 5),
93 "failed"); 107 "failed");
94 UCX_TEST_ASSERT(list->next->next == NULL, "failed"); 108 UCX_TEST_ASSERT(!strncmp((const char*)list->next->data, " my ", 4),
95 109 "failed");
96 UCX_TEST_END 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;
97 ucx_list_free(list); 119 ucx_list_free(list);
98 } 120 }
99 121
100 UCX_TEST(test_ucx_list_size) { 122 UCX_TEST(test_ucx_list_size) {
101 UcxList *list = ucx_list_append(NULL, (void*)"This "); 123 UcxList *list = ucx_list_append(NULL, (void*)"This ");
105 list = ucx_list_append(list, (void*)"5!"); 127 list = ucx_list_append(list, (void*)"5!");
106 128
107 UCX_TEST_BEGIN 129 UCX_TEST_BEGIN
108 130
109 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); 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");
110 134
111 UCX_TEST_END 135 UCX_TEST_END
112 ucx_list_free(list); 136 ucx_list_free(list);
113 } 137 }
114 138
120 UCX_TEST_BEGIN 144 UCX_TEST_BEGIN
121 145
122 const char* first = (const char*) (ucx_list_first(list)->data); 146 const char* first = (const char*) (ucx_list_first(list)->data);
123 147
124 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); 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");
125 152
126 UCX_TEST_END 153 UCX_TEST_END
127 ucx_list_free(list); 154 ucx_list_free(list);
128 } 155 }
129 156
132 list = ucx_list_append(list, (void*)"the "); 159 list = ucx_list_append(list, (void*)"the ");
133 list = ucx_list_append(list, (void*)"last!"); 160 list = ucx_list_append(list, (void*)"last!");
134 161
135 UCX_TEST_BEGIN 162 UCX_TEST_BEGIN
136 163
137 const char* last = (const char*) (ucx_list_last(list)->data); 164 const char* last = (const char*) (ucx_list_last(list->next->next)->data);
138 165
139 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); 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");
140 170
141 UCX_TEST_END 171 UCX_TEST_END
142 ucx_list_free(list); 172 ucx_list_free(list);
143 } 173 }
144 174
147 list = ucx_list_append(list, (void*)"the "); 177 list = ucx_list_append(list, (void*)"the ");
148 list = ucx_list_append(list, (void*)"mid!"); 178 list = ucx_list_append(list, (void*)"mid!");
149 179
150 UCX_TEST_BEGIN 180 UCX_TEST_BEGIN
151 181
182 const char* first = (const char*) (ucx_list_get(list, 0)->data);
152 const char* mid = (const char*) (ucx_list_get(list, 1)->data); 183 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
153 184 const char* last = (const char*) (ucx_list_get(list, 2)->data);
185
186 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
154 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 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");
155 192
156 UCX_TEST_END 193 UCX_TEST_END
157 ucx_list_free(list); 194 ucx_list_free(list);
158 } 195 }
159 196
167 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed"); 204 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
168 UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed"); 205 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, 206 UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
170 "failed"); 207 "failed");
171 208
172 UcxList *otherlist = ucx_list_append(NULL, (void*) "foobar"); 209 UcxList *otherlist = ucx_list_append(NULL, (void*) "the ");
173 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed"); 210 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
211 UCX_TEST_ASSERT(ucx_list_indexof(NULL, otherlist) == -1, "empty list");
212
174 ucx_list_free(otherlist); 213 ucx_list_free(otherlist);
175 214
176 UCX_TEST_END 215 UCX_TEST_END
177 ucx_list_free(list); 216 ucx_list_free(list);
178 } 217 }
179 218
180 UCX_TEST(test_ucx_list_find) { 219 UCX_TEST(test_ucx_list_find) {
220 const char* teststr = "string!";
181 UcxList *l = ucx_list_append(NULL, (void*)"find "); 221 UcxList *l = ucx_list_append(NULL, (void*)"find ");
182 l = ucx_list_append(l, (void*)"some "); 222 l = ucx_list_append(l, (void*)"some ");
183 l = ucx_list_append(l, (void*)"string!"); 223 l = ucx_list_append(l, (void*)teststr);
184 224
185 UCX_TEST_BEGIN 225 UCX_TEST_BEGIN
186 226
187 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1, 227 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
188 "doesn't find string"); 228 "doesn't find string");
189 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1, 229 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
190 "finds non-existing string"); 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_strcmp,NULL) == -1,
236 "empty list");
191 237
192 UCX_TEST_END 238 UCX_TEST_END
193 ucx_list_free(l); 239 ucx_list_free(l);
194 } 240 }
195 241
309 list = ucx_list_append(list, (void*)"a"); 355 list = ucx_list_append(list, (void*)"a");
310 list = ucx_list_append(list, (void*)"test"); 356 list = ucx_list_append(list, (void*)"test");
311 list = ucx_list_append(list, (void*)"for"); 357 list = ucx_list_append(list, (void*)"for");
312 list = ucx_list_append(list, (void*)"partial"); 358 list = ucx_list_append(list, (void*)"partial");
313 list = ucx_list_append(list, (void*)"correctness"); 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");
314 369
315 UcxList *expected = ucx_list_append(NULL, (void*)"a"); 370 UcxList *expected = ucx_list_append(NULL, (void*)"a");
316 expected = ucx_list_append(expected, (void*)"correctness"); 371 expected = ucx_list_append(expected, (void*)"correctness");
317 expected = ucx_list_append(expected, (void*)"for"); 372 expected = ucx_list_append(expected, (void*)"for");
373 expected = ucx_list_append(expected, (void*)"function");
318 expected = ucx_list_append(expected, (void*)"is"); 374 expected = ucx_list_append(expected, (void*)"is");
375 expected = ucx_list_append(expected, (void*)"of");
319 expected = ucx_list_append(expected, (void*)"partial"); 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");
320 expected = ucx_list_append(expected, (void*)"test"); 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");
321 expected = ucx_list_append(expected, (void*)"this"); 385 expected = ucx_list_append(expected, (void*)"this");
322 386
323 list = ucx_list_sort(list, ucx_strcmp, NULL); 387 list = ucx_list_sort(list, ucx_strcmp, NULL);
324 388
325 UCX_TEST_BEGIN 389 UCX_TEST_BEGIN
326 UCX_TEST_ASSERT( 390 UCX_TEST_ASSERT(
327 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed"); 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");
328 UcxList *l = list; 393 UcxList *l = list;
329 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null"); 394 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
330 while (l->next != NULL) { 395 while (l->next != NULL) {
331 UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted"); 396 UCX_TEST_ASSERT(l->next->prev == l, "next or prev pointer corrupted");
332 l = l->next; 397 l = l->next;
333 } 398 }
399 UCX_TEST_ASSERT(!ucx_list_sort(NULL, ucx_strcmp, NULL),
400 "failed to sort empty list");
334 UCX_TEST_END 401 UCX_TEST_END
335 402
336 ucx_list_free(expected); 403 ucx_list_free(expected);
337 ucx_list_free(list); 404 ucx_list_free(list);
338 } 405 }

mercurial