Wed, 15 Aug 2012 19:32:29 +0200
added mkdir for build directory to makefile + added qsort for list and dlist
1 /*
2 * tests of dlist implementation
3 */
5 #include "dlist_tests.h"
7 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
8 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
9 UCX_TEST_BEGIN
11 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
13 list = ucx_dlist_append(list, " World!");
15 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
16 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
17 UCX_TEST_END
19 ucx_dlist_free(list);
20 }
22 UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) {
23 UcxDlist *list = ucx_dlist_prepend(NULL, " World!");
24 UCX_TEST_BEGIN
26 list = ucx_dlist_prepend(list, "Hello");
28 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
29 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
30 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
32 UCX_TEST_END
33 ucx_dlist_free(list);
34 }
36 UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) {
37 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
38 list = ucx_dlist_append(list, " World!");
39 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
40 list2 = ucx_dlist_prepend(list2, "Hello");
41 UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!");
42 list3 = ucx_dlist_prepend(list3, "Hallo");
43 UCX_TEST_BEGIN
45 UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed")
46 UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed")
48 UCX_TEST_END
49 ucx_dlist_free(list3);
50 ucx_dlist_free(list2);
51 ucx_dlist_free(list);
52 }
54 UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) {
55 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
56 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
57 UCX_TEST_BEGIN
59 list = ucx_dlist_concat(list, list2);
61 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
62 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
63 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
65 UCX_TEST_END
66 ucx_dlist_free(list);
67 }
69 UCX_TEST_IMPLEMENT(test_ucx_dlist_size) {
70 UcxDlist *list = ucx_dlist_append(NULL, "This ");
71 UCX_TEST_BEGIN
72 list = ucx_dlist_append(list, "list ");
73 list = ucx_dlist_append(list, "has ");
74 list = ucx_dlist_append(list, "size ");
75 list = ucx_dlist_append(list, "5!");
77 UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
79 UCX_TEST_END
80 ucx_dlist_free(list);
81 }
83 UCX_TEST_IMPLEMENT(test_ucx_dlist_first) {
84 UcxDlist *list = ucx_dlist_append(NULL, "Find ");
85 UCX_TEST_BEGIN
86 list = ucx_dlist_append(list, "the ");
87 list = ucx_dlist_append(list, "first!");
89 char* first = (char*) (ucx_dlist_first(list)->data);
91 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
93 UCX_TEST_END
94 ucx_dlist_free(list);
95 }
97 UCX_TEST_IMPLEMENT(test_ucx_dlist_last) {
98 UcxDlist *list = ucx_dlist_append(NULL, "Find ");
99 UCX_TEST_BEGIN
100 list = ucx_dlist_append(list, "the ");
101 list = ucx_dlist_append(list, "last!");
103 char* last = (char*) (ucx_dlist_last(list)->data);
105 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
107 UCX_TEST_END
108 ucx_dlist_free(list);
109 }
111 UCX_TEST_IMPLEMENT(test_ucx_dlist_get) {
112 UcxDlist *list = ucx_dlist_append(NULL, "Find ");
113 UCX_TEST_BEGIN
114 list = ucx_dlist_append(list, "the ");
115 list = ucx_dlist_append(list, "mid!");
117 char* mid = (char*) (ucx_dlist_get(list, 1)->data);
119 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
121 UCX_TEST_END
122 ucx_dlist_free(list);
123 }
125 UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) {
126 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
127 UCX_TEST_BEGIN
128 list = ucx_dlist_append(list, " fucking");
129 list = ucx_dlist_append(list, " World!");
131 list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
133 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
134 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
135 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
137 UCX_TEST_END
138 ucx_dlist_free(list);
139 }
141 UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) {
143 char *hello = (char*)malloc(6);
144 char *world = (char*)malloc(8);
146 memcpy(hello, "Hello", 6);
147 memcpy(world, " World!", 8);
149 UcxDlist *list = ucx_dlist_append(NULL, hello);
150 list = ucx_dlist_append(list, world);
152 UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
153 UCX_TEST_BEGIN
155 UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed")
156 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
157 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
159 UCX_TEST_END
160 free(copy->next->data);
161 free(copy->data);
163 free(world);
164 free(hello);
165 ucx_dlist_free(list);
166 ucx_dlist_free(copy);
167 }
169 UCX_TEST_IMPLEMENT(test_ucx_dlist_qsort) {
170 UcxDlist *list = ucx_dlist_append(NULL, "this");
171 list = ucx_dlist_append(list, "is");
172 list = ucx_dlist_append(list, "a");
173 list = ucx_dlist_append(list, "test");
174 list = ucx_dlist_append(list, "for");
175 list = ucx_dlist_append(list, "partial");
176 list = ucx_dlist_append(list, "correctness");
178 UcxDlist *expected = ucx_dlist_append(NULL, "a");
179 expected = ucx_dlist_append(expected, "correctness");
180 expected = ucx_dlist_append(expected, "for");
181 expected = ucx_dlist_append(expected, "is");
182 expected = ucx_dlist_append(expected, "partial");
183 expected = ucx_dlist_append(expected, "test");
184 expected = ucx_dlist_append(expected, "this");
186 list = ucx_dlist_qsort(list, cmp_string, NULL);
188 UCX_TEST_BEGIN
189 UCX_TEST_ASSERT(
190 ucx_dlist_equals(list, expected, cmp_string, NULL), "failed");
191 UcxDlist *l = list;
192 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
193 while (l->next != NULL) {
194 UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
195 l = l->next;
196 }
197 UCX_TEST_END
199 ucx_dlist_free(expected);
200 ucx_dlist_free(list);
201 }