Wed, 27 Feb 2013 10:09:23 +0100
comparator module
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_remove) {
121 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
122 UCX_TEST_BEGIN
123 list = ucx_list_append(list, (void*)" fucking");
124 list = ucx_list_append(list, (void*)" World!");
126 list = ucx_list_remove(list, ucx_list_get(list, 1));
128 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
129 "failed");
130 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
131 "failed");
132 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
133 UCX_TEST_END
135 ucx_list_free(list);
136 }
138 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
140 char *hello = (char*)malloc(6);
141 char *world = (char*)malloc(8);
143 memcpy(hello, "Hello", 6);
144 memcpy(world, " World!", 8);
146 UcxList *list = ucx_list_append(NULL, hello);
147 list = ucx_list_append(list, world);
149 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
150 UCX_TEST_BEGIN
152 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
153 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
154 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
156 UCX_TEST_END
157 free(copy->next->data);
158 free(copy->data);
160 free(world);
161 free(hello);
162 ucx_list_free(list);
163 ucx_list_free(copy);
164 }
166 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
167 UcxList *list = ucx_list_append(NULL, (void*)"this");
168 list = ucx_list_append(list, (void*)"is");
169 list = ucx_list_append(list, (void*)"a");
170 list = ucx_list_append(list, (void*)"test");
171 list = ucx_list_append(list, (void*)"for");
172 list = ucx_list_append(list, (void*)"partial");
173 list = ucx_list_append(list, (void*)"correctness");
175 UcxList *expected = ucx_list_append(NULL, (void*)"a");
176 expected = ucx_list_append(expected, (void*)"correctness");
177 expected = ucx_list_append(expected, (void*)"for");
178 expected = ucx_list_append(expected, (void*)"is");
179 expected = ucx_list_append(expected, (void*)"partial");
180 expected = ucx_list_append(expected, (void*)"test");
181 expected = ucx_list_append(expected, (void*)"this");
183 list = ucx_list_sort(list, ucx_strcmp, NULL);
185 UCX_TEST_BEGIN
186 UCX_TEST_ASSERT(
187 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
188 UCX_TEST_END
190 ucx_list_free(expected);
191 ucx_list_free(list);
192 }