test/dlist_tests.c

changeset 27
22644e2572bc
parent 24
e04822101291
child 30
23bb65cbf7a4
equal deleted inserted replaced
26:59f147baea31 27:22644e2572bc
1 /*
2 * tests of dlist implementation
3 */
4
5 #include "dlist_tests.h"
6
7 UCX_TEST_BEGIN(test_ucx_dlist_append) {
8 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
9
10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
11
12 list = ucx_dlist_append(list, " World!");
13
14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
15 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
16
17 ucx_dlist_free(list);
18
19 UCX_TEST_END
20 }
21
22 UCX_TEST_BEGIN(test_ucx_dlist_prepend) {
23 UcxDlist *list = ucx_dlist_prepend(NULL, " World!");
24 list = ucx_dlist_prepend(list, "Hello");
25
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")
29
30 ucx_dlist_free(list);
31
32 UCX_TEST_END
33 }
34
35 UCX_TEST_BEGIN(test_ucx_dlist_equals) {
36 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
37 list = ucx_dlist_append(list, " World!");
38 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
39 list2 = ucx_dlist_prepend(list2, "Hello");
40 UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!");
41 list3 = ucx_dlist_prepend(list3, "Hallo");
42
43 UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed")
44 UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed")
45
46 ucx_dlist_free(list3);
47 ucx_dlist_free(list2);
48 ucx_dlist_free(list);
49
50 UCX_TEST_END
51 }
52
53 UCX_TEST_BEGIN(test_ucx_dlist_concat) {
54 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
55 UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
56
57 list = ucx_dlist_concat(list, list2);
58
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")
62
63 ucx_dlist_free(list2);
64 ucx_dlist_free(list);
65
66 UCX_TEST_END
67 }
68
69 UCX_TEST_BEGIN(test_ucx_dlist_size) {
70 UcxDlist *list = ucx_dlist_append(NULL, "This ");
71 list = ucx_dlist_append(list, "list ");
72 list = ucx_dlist_append(list, "has ");
73 list = ucx_dlist_append(list, "size ");
74 list = ucx_dlist_append(list, "5!");
75
76 UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
77
78 ucx_dlist_free(list);
79
80 UCX_TEST_END
81 }
82
83 UCX_TEST_BEGIN(test_ucx_dlist_first) {
84 UcxDlist *list = ucx_dlist_append(NULL, "Find ");
85 list = ucx_dlist_append(list, "the ");
86 list = ucx_dlist_append(list, "first!");
87
88 char* first = (char*) (ucx_dlist_first(list)->data);
89
90 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
91
92 ucx_dlist_free(list);
93
94 UCX_TEST_END
95 }
96
97 UCX_TEST_BEGIN(test_ucx_dlist_last) {
98 UcxDlist *list = ucx_dlist_append(NULL, "Find ");
99 list = ucx_dlist_append(list, "the ");
100 list = ucx_dlist_append(list, "last!");
101
102 char* last = (char*) (ucx_dlist_last(list)->data);
103
104 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
105
106 ucx_dlist_free(list);
107
108 UCX_TEST_END
109 }
110
111 UCX_TEST_BEGIN(test_ucx_dlist_get) {
112 UcxDlist *list = ucx_dlist_append(NULL, "Find ");
113 list = ucx_dlist_append(list, "the ");
114 list = ucx_dlist_append(list, "mid!");
115
116 char* mid = (char*) (ucx_dlist_get(list, 1)->data);
117
118 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
119
120 ucx_dlist_free(list);
121
122 UCX_TEST_END
123 }
124
125 UCX_TEST_BEGIN(test_ucx_dlist_remove) {
126 UcxDlist *list = ucx_dlist_append(NULL, "Hello");
127 list = ucx_dlist_append(list, " fucking");
128 list = ucx_dlist_append(list, " World!");
129
130 list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
131
132 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
133 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
134 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
135
136 ucx_dlist_free(list);
137
138 UCX_TEST_END
139 }
140
141 UCX_TEST_BEGIN(test_ucx_dlist_clone) {
142
143 char *hello = (char*)malloc(6);
144 char *world = (char*)malloc(8);
145
146 memcpy(hello, "Hello", 6);
147 memcpy(world, " World!", 8);
148
149 UcxDlist *list = ucx_dlist_append(NULL, hello);
150 list = ucx_dlist_append(list, world);
151
152 UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
153
154 UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed")
155 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
156 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
157
158 free(copy->next->data);
159 free(copy->data);
160
161 free(world);
162 free(hello);
163 free(list);
164 free(copy);
165
166 UCX_TEST_END
167 }

mercurial