test/list_tests.c

changeset 122
540d99722f1f
parent 103
08018864fb91
child 123
7fb0f74517c5
equal deleted inserted replaced
121:311cac04d079 122:540d99722f1f
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "list_tests.h"
30 #include "ucx/utils.h"
31
32 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
33 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
34 UCX_TEST_BEGIN
35
36 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
37 "failed");
38
39 list = ucx_list_append(list, (void*)" World!");
40
41 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
42 "failed");
43 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
44 UCX_TEST_END
45
46 ucx_list_free(list);
47 }
48
49 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
50 UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
51 UCX_TEST_BEGIN
52
53 list = ucx_list_prepend(list, (void*)"Hello");
54
55 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
56 "failed");
57 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
58 "failed");
59 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
60
61 UCX_TEST_END
62 ucx_list_free(list);
63 }
64
65 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
66 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
67 list = ucx_list_append(list, (void*)" World!");
68 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
69 list2 = ucx_list_prepend(list2, (void*)"Hello");
70 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
71 list3 = ucx_list_prepend(list3, (void*)"Hallo");
72 UCX_TEST_BEGIN
73
74 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
75 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
76
77 UCX_TEST_END
78 ucx_list_free(list3);
79 ucx_list_free(list2);
80 ucx_list_free(list);
81 }
82
83 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
84 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
85 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
86 UCX_TEST_BEGIN
87
88 list = ucx_list_concat(list, list2);
89
90 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
91 "failed");
92 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
93 "failed");
94 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
95
96 UCX_TEST_END
97 ucx_list_free(list);
98 }
99
100 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
101 UcxList *list = ucx_list_append(NULL, (void*)"This ");
102 UCX_TEST_BEGIN
103 list = ucx_list_append(list, (void*)"list ");
104 list = ucx_list_append(list, (void*)"has ");
105 list = ucx_list_append(list, (void*)"size ");
106 list = ucx_list_append(list, (void*)"5!");
107
108 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
109
110 UCX_TEST_END
111 ucx_list_free(list);
112 }
113
114 UCX_TEST_IMPLEMENT(test_ucx_list_first) {
115 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
116 UCX_TEST_BEGIN
117 list = ucx_list_append(list, (void*)"the ");
118 list = ucx_list_append(list, (void*)"first!");
119
120 const char* first = (const char*) (ucx_list_first(list)->data);
121
122 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
123
124 UCX_TEST_END
125 ucx_list_free(list);
126 }
127
128 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
129 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
130 UCX_TEST_BEGIN
131 list = ucx_list_append(list, (void*)"the ");
132 list = ucx_list_append(list, (void*)"last!");
133
134 const char* last = (const char*) (ucx_list_last(list)->data);
135
136 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
137
138 UCX_TEST_END
139 ucx_list_free(list);
140 }
141
142 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
143 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
144 UCX_TEST_BEGIN
145 list = ucx_list_append(list, (void*)"the ");
146 list = ucx_list_append(list, (void*)"mid!");
147
148 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
149
150 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
151
152 UCX_TEST_END
153 ucx_list_free(list);
154 }
155
156 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
157 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
158 UCX_TEST_BEGIN
159 l = ucx_list_append(l, (void*)"a ");
160 l = ucx_list_append(l, (void*)"string!");
161
162 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed");
163 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),"failed");
164
165 UCX_TEST_END
166 ucx_list_free(l);
167 }
168
169 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
170 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
171 UCX_TEST_BEGIN
172 list = ucx_list_append(list, (void*)" fucking");
173 list = ucx_list_append(list, (void*)" World!");
174
175 list = ucx_list_remove(list, ucx_list_get(list, 1));
176
177 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
178 "failed");
179 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
180 "failed");
181 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
182
183 UCX_TEST_END
184 ucx_list_free(list);
185 }
186
187 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
188
189 char *hello = (char*)malloc(6);
190 char *world = (char*)malloc(8);
191
192 memcpy(hello, "Hello", 6);
193 memcpy(world, " World!", 8);
194
195 UcxList *list = ucx_list_append(NULL, hello);
196 list = ucx_list_append(list, world);
197
198 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
199 UCX_TEST_BEGIN
200
201 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
202 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
203 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
204
205 UCX_TEST_END
206 free(copy->next->data);
207 free(copy->data);
208
209 free(world);
210 free(hello);
211 ucx_list_free(list);
212 ucx_list_free(copy);
213 }
214
215 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
216 UcxList *list = ucx_list_append(NULL, (void*)"this");
217 list = ucx_list_append(list, (void*)"is");
218 list = ucx_list_append(list, (void*)"a");
219 list = ucx_list_append(list, (void*)"test");
220 list = ucx_list_append(list, (void*)"for");
221 list = ucx_list_append(list, (void*)"partial");
222 list = ucx_list_append(list, (void*)"correctness");
223
224 UcxList *expected = ucx_list_append(NULL, (void*)"a");
225 expected = ucx_list_append(expected, (void*)"correctness");
226 expected = ucx_list_append(expected, (void*)"for");
227 expected = ucx_list_append(expected, (void*)"is");
228 expected = ucx_list_append(expected, (void*)"partial");
229 expected = ucx_list_append(expected, (void*)"test");
230 expected = ucx_list_append(expected, (void*)"this");
231
232 list = ucx_list_sort(list, ucx_strcmp, NULL);
233
234 UCX_TEST_BEGIN
235 UCX_TEST_ASSERT(
236 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
237 UcxList *l = list;
238 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
239 while (l->next != NULL) {
240 UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
241 l = l->next;
242 }
243 UCX_TEST_END
244
245 ucx_list_free(expected);
246 ucx_list_free(list);
247 }

mercurial