Fri, 24 Feb 2012 15:53:50 +0100
some fixes
1 /*
2 * tests of list implementation
3 */
5 #include "list_tests.h"
7 UCX_TEST_BEGIN(test_ucx_list_append) {
8 UcxList *list = ucx_list_append(NULL, "Hello");
10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
12 list = ucx_list_append(list, " World!");
14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
15 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
17 ucx_list_free(list);
19 UCX_TEST_END
20 }
22 UCX_TEST_BEGIN(test_ucx_list_prepend) {
23 UcxList *list = ucx_list_prepend(NULL, " World!");
24 list = ucx_list_prepend(list, "Hello");
26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
28 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
30 ucx_list_free(list);
32 UCX_TEST_END
33 }
35 UCX_TEST_BEGIN(test_ucx_list_equals) {
36 UcxList *list = ucx_list_append(NULL, "Hello");
37 list = ucx_list_append(list, " World!");
38 UcxList *list2 = ucx_list_prepend(NULL, " World!");
39 list2 = ucx_list_prepend(list2, "Hello");
40 UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
41 list3 = ucx_list_prepend(list3, "Hallo");
43 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
44 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
46 ucx_list_free(list3);
47 ucx_list_free(list2);
48 ucx_list_free(list);
50 UCX_TEST_END
51 }
53 UCX_TEST_BEGIN(test_ucx_list_concat) {
54 UcxList *list = ucx_list_append(NULL, "Hello");
55 UcxList *list2 = ucx_list_prepend(NULL, " World!");
57 list = ucx_list_concat(list, list2);
59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
61 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
63 ucx_list_free(list);
65 UCX_TEST_END
66 }
68 UCX_TEST_BEGIN(test_ucx_list_size) {
69 UcxList *list = ucx_list_append(NULL, "This ");
70 list = ucx_list_append(list, "list ");
71 list = ucx_list_append(list, "has ");
72 list = ucx_list_append(list, "size ");
73 list = ucx_list_append(list, "5!");
75 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
77 ucx_list_free(list);
79 UCX_TEST_END
80 }
82 UCX_TEST_BEGIN(test_ucx_list_last) {
83 UcxList *list = ucx_list_append(NULL, "Find ");
84 list = ucx_list_append(list, "the ");
85 list = ucx_list_append(list, "last!");
87 char* last = (char*) (ucx_list_last(list)->data);
89 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
91 ucx_list_free(list);
93 UCX_TEST_END
94 }
96 UCX_TEST_BEGIN(test_ucx_list_get) {
97 UcxList *list = ucx_list_append(NULL, "Find ");
98 list = ucx_list_append(list, "the ");
99 list = ucx_list_append(list, "mid!");
101 char* mid = (char*) (ucx_list_get(list, 1)->data);
103 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
105 ucx_list_free(list);
107 UCX_TEST_END
108 }
110 UCX_TEST_BEGIN(test_ucx_list_remove) {
111 UcxList *list = ucx_list_append(NULL, "Hello");
112 list = ucx_list_append(list, " fucking");
113 list = ucx_list_append(list, " World!");
115 list = ucx_list_remove(list, ucx_list_get(list, 1));
117 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
118 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
119 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
121 ucx_list_free(list);
123 UCX_TEST_END
124 }
126 UCX_TEST_BEGIN(test_ucx_list_clone) {
128 char *hello = (char*)malloc(6);
129 char *world = (char*)malloc(8);
131 memcpy(hello, "Hello", 6);
132 memcpy(world, " World!", 8);
134 UcxList *list = ucx_list_append(NULL, hello);
135 list = ucx_list_append(list, world);
137 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
139 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
140 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
141 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
143 free(copy->next->data);
144 free(copy->data);
146 free(world);
147 free(hello);
148 ucx_list_free(list);
149 ucx_list_free(copy);
151 UCX_TEST_END
152 }