Wed, 27 Feb 2013 10:28:02 +0100
added tests for ucx_(d)list_contains
1 /*
2 * tests of list implementation
3 */
5 #include "list_tests.h"
6 #include "ucx/comparator.h"
8 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
9 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
10 UCX_TEST_BEGIN
11 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
12 "failed");
14 list = ucx_list_append(list, (void*)" World!");
16 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
17 "failed");
18 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
20 UCX_TEST_END
21 ucx_list_free(list);
22 }
24 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
25 UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
26 UCX_TEST_BEGIN
27 list = ucx_list_prepend(list, (void*)"Hello");
29 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
30 "failed");
31 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
32 "failed");
33 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
35 UCX_TEST_END
36 ucx_list_free(list);
37 }
39 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
40 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
41 list = ucx_list_append(list, (void*)" World!");
42 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
43 list2 = ucx_list_prepend(list2, (void*)"Hello");
44 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
45 list3 = ucx_list_prepend(list3, (void*)"Hallo");
47 UCX_TEST_BEGIN
48 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
49 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
50 UCX_TEST_END
52 ucx_list_free(list3);
53 ucx_list_free(list2);
54 ucx_list_free(list);
55 }
57 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
58 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
59 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
61 list = ucx_list_concat(list, list2);
62 UCX_TEST_BEGIN
64 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
65 "failed");
66 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
67 "failed");
68 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
70 UCX_TEST_END
71 if (list->next == NULL) {
72 ucx_list_free(list2);
73 }
74 ucx_list_free(list);
75 }
77 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
78 UcxList *list = ucx_list_append(NULL, (void*)"This ");
79 UCX_TEST_BEGIN
80 list = ucx_list_append(list, (void*)"list ");
81 list = ucx_list_append(list, (void*)"has ");
82 list = ucx_list_append(list, (void*)"size ");
83 list = ucx_list_append(list, (void*)"5!");
85 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
87 UCX_TEST_END
88 ucx_list_free(list);
89 }
91 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
92 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
93 UCX_TEST_BEGIN
94 list = ucx_list_append(list, (void*)"the ");
95 list = ucx_list_append(list, (void*)"last!");
97 const char* last = (const char*) (ucx_list_last(list)->data);
99 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
101 UCX_TEST_END
102 ucx_list_free(list);
104 }
106 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
107 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
108 UCX_TEST_BEGIN
109 list = ucx_list_append(list, (void*)"the ");
110 list = ucx_list_append(list, (void*)"mid!");
112 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
114 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
116 UCX_TEST_END
117 ucx_list_free(list);
118 }
120 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
121 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
122 UCX_TEST_BEGIN
123 l = ucx_list_append(l, (void*)"a ");
124 l = ucx_list_append(l, (void*)"string!");
126 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), "failed");
127 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), "failed");
129 UCX_TEST_END
130 ucx_list_free(l);
131 }
133 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
134 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
135 UCX_TEST_BEGIN
136 list = ucx_list_append(list, (void*)" fucking");
137 list = ucx_list_append(list, (void*)" World!");
139 list = ucx_list_remove(list, ucx_list_get(list, 1));
141 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
142 "failed");
143 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
144 "failed");
145 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
146 UCX_TEST_END
148 ucx_list_free(list);
149 }
151 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
153 char *hello = (char*)malloc(6);
154 char *world = (char*)malloc(8);
156 memcpy(hello, "Hello", 6);
157 memcpy(world, " World!", 8);
159 UcxList *list = ucx_list_append(NULL, hello);
160 list = ucx_list_append(list, world);
162 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
163 UCX_TEST_BEGIN
165 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
166 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
167 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
169 UCX_TEST_END
170 free(copy->next->data);
171 free(copy->data);
173 free(world);
174 free(hello);
175 ucx_list_free(list);
176 ucx_list_free(copy);
177 }
179 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
180 UcxList *list = ucx_list_append(NULL, (void*)"this");
181 list = ucx_list_append(list, (void*)"is");
182 list = ucx_list_append(list, (void*)"a");
183 list = ucx_list_append(list, (void*)"test");
184 list = ucx_list_append(list, (void*)"for");
185 list = ucx_list_append(list, (void*)"partial");
186 list = ucx_list_append(list, (void*)"correctness");
188 UcxList *expected = ucx_list_append(NULL, (void*)"a");
189 expected = ucx_list_append(expected, (void*)"correctness");
190 expected = ucx_list_append(expected, (void*)"for");
191 expected = ucx_list_append(expected, (void*)"is");
192 expected = ucx_list_append(expected, (void*)"partial");
193 expected = ucx_list_append(expected, (void*)"test");
194 expected = ucx_list_append(expected, (void*)"this");
196 list = ucx_list_sort(list, ucx_strcmp, NULL);
198 UCX_TEST_BEGIN
199 UCX_TEST_ASSERT(
200 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
201 UCX_TEST_END
203 ucx_list_free(expected);
204 ucx_list_free(list);
205 }