test/list_tests.c

changeset 121
311cac04d079
parent 103
08018864fb91
equal deleted inserted replaced
120:8170f658f017 121:311cac04d079
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 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
36 "failed");
37
38 list = ucx_list_append(list, (void*)" World!");
39
40 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
41 "failed");
42 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
43
44 UCX_TEST_END
45 ucx_list_free(list);
46 }
47
48 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
49 UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
50 UCX_TEST_BEGIN
51 list = ucx_list_prepend(list, (void*)"Hello");
52
53 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
54 "failed");
55 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
56 "failed");
57 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
58
59 UCX_TEST_END
60 ucx_list_free(list);
61 }
62
63 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
64 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
65 list = ucx_list_append(list, (void*)" World!");
66 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
67 list2 = ucx_list_prepend(list2, (void*)"Hello");
68 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
69 list3 = ucx_list_prepend(list3, (void*)"Hallo");
70
71 UCX_TEST_BEGIN
72 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
73 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
74 UCX_TEST_END
75
76 ucx_list_free(list3);
77 ucx_list_free(list2);
78 ucx_list_free(list);
79 }
80
81 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
82 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
83 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
84
85 list = ucx_list_concat(list, list2);
86 UCX_TEST_BEGIN
87
88 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
89 "failed");
90 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
91 "failed");
92 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
93
94 UCX_TEST_END
95 if (list->next == NULL) {
96 ucx_list_free(list2);
97 }
98 ucx_list_free(list);
99 }
100
101 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
102 UcxList *list = ucx_list_append(NULL, (void*)"This ");
103 UCX_TEST_BEGIN
104 list = ucx_list_append(list, (void*)"list ");
105 list = ucx_list_append(list, (void*)"has ");
106 list = ucx_list_append(list, (void*)"size ");
107 list = ucx_list_append(list, (void*)"5!");
108
109 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
110
111 UCX_TEST_END
112 ucx_list_free(list);
113 }
114
115 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
116 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
117 UCX_TEST_BEGIN
118 list = ucx_list_append(list, (void*)"the ");
119 list = ucx_list_append(list, (void*)"last!");
120
121 const char* last = (const char*) (ucx_list_last(list)->data);
122
123 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
124
125 UCX_TEST_END
126 ucx_list_free(list);
127
128 }
129
130 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
131 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
132 UCX_TEST_BEGIN
133 list = ucx_list_append(list, (void*)"the ");
134 list = ucx_list_append(list, (void*)"mid!");
135
136 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
137
138 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
139
140 UCX_TEST_END
141 ucx_list_free(list);
142 }
143
144 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
145 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
146 UCX_TEST_BEGIN
147 l = ucx_list_append(l, (void*)"a ");
148 l = ucx_list_append(l, (void*)"string!");
149
150 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), "failed");
151 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), "failed");
152
153 UCX_TEST_END
154 ucx_list_free(l);
155 }
156
157 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
158 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
159 UCX_TEST_BEGIN
160 list = ucx_list_append(list, (void*)" fucking");
161 list = ucx_list_append(list, (void*)" World!");
162
163 list = ucx_list_remove(list, ucx_list_get(list, 1));
164
165 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
166 "failed");
167 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
168 "failed");
169 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
170 UCX_TEST_END
171
172 ucx_list_free(list);
173 }
174
175 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
176
177 char *hello = (char*)malloc(6);
178 char *world = (char*)malloc(8);
179
180 memcpy(hello, "Hello", 6);
181 memcpy(world, " World!", 8);
182
183 UcxList *list = ucx_list_append(NULL, hello);
184 list = ucx_list_append(list, world);
185
186 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
187 UCX_TEST_BEGIN
188
189 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
190 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
191 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
192
193 UCX_TEST_END
194 free(copy->next->data);
195 free(copy->data);
196
197 free(world);
198 free(hello);
199 ucx_list_free(list);
200 ucx_list_free(copy);
201 }
202
203 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
204 UcxList *list = ucx_list_append(NULL, (void*)"this");
205 list = ucx_list_append(list, (void*)"is");
206 list = ucx_list_append(list, (void*)"a");
207 list = ucx_list_append(list, (void*)"test");
208 list = ucx_list_append(list, (void*)"for");
209 list = ucx_list_append(list, (void*)"partial");
210 list = ucx_list_append(list, (void*)"correctness");
211
212 UcxList *expected = ucx_list_append(NULL, (void*)"a");
213 expected = ucx_list_append(expected, (void*)"correctness");
214 expected = ucx_list_append(expected, (void*)"for");
215 expected = ucx_list_append(expected, (void*)"is");
216 expected = ucx_list_append(expected, (void*)"partial");
217 expected = ucx_list_append(expected, (void*)"test");
218 expected = ucx_list_append(expected, (void*)"this");
219
220 list = ucx_list_sort(list, ucx_strcmp, NULL);
221
222 UCX_TEST_BEGIN
223 UCX_TEST_ASSERT(
224 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
225 UCX_TEST_END
226
227 ucx_list_free(expected);
228 ucx_list_free(list);
229 }

mercurial