Fri, 12 Oct 2012 10:54:55 +0200
made the code work with VC++ compiler (use make CONF=windows)
1 /*
2 * tests of list implementation
3 */
5 #include "list_tests.h"
7 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
8 UcxList *list = ucx_list_append(NULL, "Hello");
9 UCX_TEST_BEGIN
10 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
11 "failed");
13 list = ucx_list_append(list, " World!");
15 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
16 "failed");
17 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
19 UCX_TEST_END
20 ucx_list_free(list);
21 }
23 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
24 UcxList *list = ucx_list_prepend(NULL, " World!");
25 UCX_TEST_BEGIN
26 list = ucx_list_prepend(list, "Hello");
28 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
29 "failed");
30 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
31 "failed");
32 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
34 UCX_TEST_END
35 ucx_list_free(list);
36 }
38 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
39 UcxList *list = ucx_list_append(NULL, "Hello");
40 list = ucx_list_append(list, " World!");
41 UcxList *list2 = ucx_list_prepend(NULL, " World!");
42 list2 = ucx_list_prepend(list2, "Hello");
43 UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
44 list3 = ucx_list_prepend(list3, "Hallo");
46 UCX_TEST_BEGIN
47 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed");
48 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed");
49 UCX_TEST_END
51 ucx_list_free(list3);
52 ucx_list_free(list2);
53 ucx_list_free(list);
54 }
56 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
57 UcxList *list = ucx_list_append(NULL, "Hello");
58 UcxList *list2 = ucx_list_prepend(NULL, " World!");
60 list = ucx_list_concat(list, list2);
61 UCX_TEST_BEGIN
63 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
64 "failed");
65 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
66 "failed");
67 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
69 UCX_TEST_END
70 if (list->next == NULL) {
71 ucx_list_free(list2);
72 }
73 ucx_list_free(list);
74 }
76 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
77 UcxList *list = ucx_list_append(NULL, "This ");
78 UCX_TEST_BEGIN
79 list = ucx_list_append(list, "list ");
80 list = ucx_list_append(list, "has ");
81 list = ucx_list_append(list, "size ");
82 list = ucx_list_append(list, "5!");
84 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
86 UCX_TEST_END
87 ucx_list_free(list);
88 }
90 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
91 UcxList *list = ucx_list_append(NULL, "Find ");
92 UCX_TEST_BEGIN
93 list = ucx_list_append(list, "the ");
94 list = ucx_list_append(list, "last!");
96 const char* last = (const char*) (ucx_list_last(list)->data);
98 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
100 UCX_TEST_END
101 ucx_list_free(list);
103 }
105 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
106 UcxList *list = ucx_list_append(NULL, "Find ");
107 UCX_TEST_BEGIN
108 list = ucx_list_append(list, "the ");
109 list = ucx_list_append(list, "mid!");
111 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
113 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
115 UCX_TEST_END
116 ucx_list_free(list);
117 }
119 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
120 UcxList *list = ucx_list_append(NULL, "Hello");
121 UCX_TEST_BEGIN
122 list = ucx_list_append(list, " fucking");
123 list = ucx_list_append(list, " World!");
125 list = ucx_list_remove(list, ucx_list_get(list, 1));
127 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
128 "failed");
129 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
130 "failed");
131 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
132 UCX_TEST_END
134 ucx_list_free(list);
135 }
137 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
139 char *hello = (char*)malloc(6);
140 char *world = (char*)malloc(8);
142 memcpy(hello, "Hello", 6);
143 memcpy(world, " World!", 8);
145 UcxList *list = ucx_list_append(NULL, hello);
146 list = ucx_list_append(list, world);
148 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
149 UCX_TEST_BEGIN
151 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed");
152 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
153 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
155 UCX_TEST_END
156 free(copy->next->data);
157 free(copy->data);
159 free(world);
160 free(hello);
161 ucx_list_free(list);
162 ucx_list_free(copy);
163 }
165 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
166 UcxList *list = ucx_list_append(NULL, "this");
167 list = ucx_list_append(list, "is");
168 list = ucx_list_append(list, "a");
169 list = ucx_list_append(list, "test");
170 list = ucx_list_append(list, "for");
171 list = ucx_list_append(list, "partial");
172 list = ucx_list_append(list, "correctness");
174 UcxList *expected = ucx_list_append(NULL, "a");
175 expected = ucx_list_append(expected, "correctness");
176 expected = ucx_list_append(expected, "for");
177 expected = ucx_list_append(expected, "is");
178 expected = ucx_list_append(expected, "partial");
179 expected = ucx_list_append(expected, "test");
180 expected = ucx_list_append(expected, "this");
182 list = ucx_list_sort(list, cmp_string, NULL);
184 UCX_TEST_BEGIN
185 UCX_TEST_ASSERT(
186 ucx_list_equals(list, expected, cmp_string, NULL), "failed");
187 UCX_TEST_END
189 ucx_list_free(expected);
190 ucx_list_free(list);
191 }