]>
Commit | Line | Data |
---|---|---|
26e8375b TB |
1 | /* |
2 | * Copyright (C) 2013 Tobias Brunner | |
3 | * Hochschule fuer Technik Rapperswil | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | */ | |
15 | ||
95e99150 | 16 | #include "test_suite.h" |
26e8375b TB |
17 | |
18 | #include <collections/linked_list.h> | |
19 | ||
20 | /******************************************************************************* | |
21 | * test fixture | |
22 | */ | |
23 | ||
24 | static linked_list_t *list; | |
25 | ||
95e99150 | 26 | START_SETUP(setup_list) |
26e8375b TB |
27 | { |
28 | list = linked_list_create_with_items((void*)1, (void*)2, (void*)3, (void*)4, | |
29 | (void*)5, NULL); | |
30 | ck_assert_int_eq(list->get_count(list), 5); | |
31 | } | |
95e99150 | 32 | END_SETUP |
26e8375b | 33 | |
95e99150 | 34 | START_TEARDOWN(teardown_list) |
26e8375b TB |
35 | { |
36 | list->destroy(list); | |
37 | } | |
95e99150 | 38 | END_TEARDOWN |
26e8375b TB |
39 | |
40 | /******************************************************************************* | |
41 | * enumeration | |
42 | */ | |
43 | ||
44 | START_TEST(test_enumerate) | |
45 | { | |
46 | enumerator_t *enumerator; | |
952073b8 TB |
47 | intptr_t x; |
48 | int round; | |
26e8375b TB |
49 | |
50 | round = 1; | |
51 | enumerator = list->create_enumerator(list); | |
52 | while (enumerator->enumerate(enumerator, &x)) | |
53 | { | |
54 | ck_assert_int_eq(round, x); | |
55 | round++; | |
56 | } | |
57 | ck_assert_int_eq(round, 6); | |
58 | enumerator->destroy(enumerator); | |
59 | } | |
60 | END_TEST | |
61 | ||
1a9528f9 TB |
62 | START_TEST(test_enumerate_null) |
63 | { | |
64 | enumerator_t *enumerator; | |
65 | int round; | |
66 | ||
67 | round = 1; | |
68 | enumerator = list->create_enumerator(list); | |
69 | while (enumerator->enumerate(enumerator, NULL)) | |
70 | { | |
71 | round++; | |
72 | } | |
73 | ck_assert_int_eq(round, 6); | |
74 | enumerator->destroy(enumerator); | |
75 | } | |
76 | END_TEST | |
77 | ||
26e8375b TB |
78 | START_TEST(test_reset_enumerator) |
79 | { | |
80 | enumerator_t *enumerator; | |
952073b8 TB |
81 | intptr_t x; |
82 | int round; | |
26e8375b TB |
83 | |
84 | enumerator = list->create_enumerator(list); | |
85 | while (enumerator->enumerate(enumerator, &x)) | |
86 | { | |
87 | } | |
88 | list->reset_enumerator(list, enumerator); | |
89 | round = 1; | |
90 | while (enumerator->enumerate(enumerator, &x)) | |
91 | { | |
92 | ck_assert_int_eq(round, x); | |
93 | round++; | |
94 | } | |
95 | ck_assert_int_eq(round, 6); | |
96 | enumerator->destroy(enumerator); | |
97 | } | |
98 | END_TEST | |
99 | ||
26e8375b TB |
100 | /******************************************************************************* |
101 | * insert before | |
102 | */ | |
103 | ||
104 | START_TEST(test_insert_before) | |
105 | { | |
106 | enumerator_t *enumerator; | |
952073b8 TB |
107 | intptr_t x; |
108 | int round; | |
26e8375b TB |
109 | |
110 | round = 1; | |
111 | enumerator = list->create_enumerator(list); | |
112 | while (enumerator->enumerate(enumerator, &x)) | |
113 | { | |
114 | ck_assert_int_eq(round, x); | |
115 | round++; | |
bc90b3dd | 116 | if (x == _i) |
26e8375b TB |
117 | { |
118 | list->insert_before(list, enumerator, (void*)6); | |
119 | } | |
120 | } | |
121 | ck_assert_int_eq(list->get_count(list), 6); | |
122 | list->reset_enumerator(list, enumerator); | |
123 | round = 1; | |
124 | while (enumerator->enumerate(enumerator, &x)) | |
125 | { | |
bc90b3dd | 126 | if (round == _i && x != _i) |
26e8375b TB |
127 | { |
128 | ck_assert_int_eq(6, x); | |
129 | } | |
130 | else | |
131 | { | |
132 | ck_assert_int_eq(round, x); | |
133 | round++; | |
134 | } | |
135 | } | |
136 | enumerator->destroy(enumerator); | |
137 | } | |
138 | END_TEST | |
139 | ||
140 | START_TEST(test_insert_before_ends) | |
141 | { | |
142 | enumerator_t *enumerator; | |
952073b8 TB |
143 | intptr_t x; |
144 | int round; | |
26e8375b TB |
145 | |
146 | enumerator = list->create_enumerator(list); | |
147 | list->insert_before(list, enumerator, (void*)0); | |
148 | ck_assert_int_eq(list->get_count(list), 6); | |
149 | ck_assert(list->get_first(list, (void*)&x) == SUCCESS); | |
150 | ck_assert_int_eq(x, 0); | |
151 | round = 0; | |
152 | while (enumerator->enumerate(enumerator, &x)) | |
153 | { | |
154 | ck_assert_int_eq(round, x); | |
155 | round++; | |
156 | } | |
157 | list->insert_before(list, enumerator, (void*)6); | |
158 | ck_assert_int_eq(list->get_count(list), 7); | |
159 | ck_assert(list->get_last(list, (void*)&x) == SUCCESS); | |
160 | ck_assert_int_eq(x, 6); | |
26e8375b TB |
161 | ck_assert(!enumerator->enumerate(enumerator, &x)); |
162 | enumerator->destroy(enumerator); | |
163 | } | |
164 | END_TEST | |
165 | ||
166 | START_TEST(test_insert_before_empty) | |
167 | { | |
168 | enumerator_t *enumerator; | |
952073b8 | 169 | intptr_t x; |
26e8375b TB |
170 | |
171 | list->destroy(list); | |
172 | list = linked_list_create(); | |
173 | enumerator = list->create_enumerator(list); | |
174 | list->insert_before(list, enumerator, (void*)1); | |
175 | ck_assert_int_eq(list->get_count(list), 1); | |
176 | ck_assert(list->get_first(list, (void*)&x) == SUCCESS); | |
177 | ck_assert_int_eq(x, 1); | |
178 | ck_assert(list->get_last(list, (void*)&x) == SUCCESS); | |
179 | ck_assert_int_eq(x, 1); | |
26e8375b TB |
180 | ck_assert(enumerator->enumerate(enumerator, &x)); |
181 | ck_assert_int_eq(x, 1); | |
84693a3d | 182 | ck_assert(!enumerator->enumerate(enumerator, NULL)); |
26e8375b TB |
183 | enumerator->destroy(enumerator); |
184 | } | |
185 | END_TEST | |
186 | ||
187 | /******************************************************************************* | |
be3c09d0 | 188 | * remove_at |
26e8375b TB |
189 | */ |
190 | ||
be3c09d0 | 191 | START_TEST(test_remove_at) |
26e8375b TB |
192 | { |
193 | enumerator_t *enumerator; | |
be3c09d0 | 194 | intptr_t x; |
952073b8 | 195 | int round; |
26e8375b TB |
196 | |
197 | round = 1; | |
198 | enumerator = list->create_enumerator(list); | |
199 | while (enumerator->enumerate(enumerator, &x)) | |
200 | { | |
201 | ck_assert_int_eq(round, x); | |
be3c09d0 TB |
202 | if (round == 2) |
203 | { | |
204 | list->remove_at(list, enumerator); | |
205 | } | |
26e8375b TB |
206 | round++; |
207 | } | |
be3c09d0 | 208 | ck_assert_int_eq(list->get_count(list), 4); |
26e8375b | 209 | list->reset_enumerator(list, enumerator); |
be3c09d0 | 210 | round = 1; |
26e8375b TB |
211 | while (enumerator->enumerate(enumerator, &x)) |
212 | { | |
be3c09d0 TB |
213 | if (round == 2) |
214 | { /* skip removed item */ | |
215 | round++; | |
216 | } | |
26e8375b | 217 | ck_assert_int_eq(round, x); |
be3c09d0 | 218 | round++; |
26e8375b TB |
219 | } |
220 | enumerator->destroy(enumerator); | |
221 | } | |
222 | END_TEST | |
223 | ||
be3c09d0 | 224 | START_TEST(test_remove_at_ends) |
afb6d9c3 TB |
225 | { |
226 | enumerator_t *enumerator; | |
227 | intptr_t x; | |
228 | ||
229 | enumerator = list->create_enumerator(list); | |
be3c09d0 TB |
230 | list->remove_at(list, enumerator); |
231 | ck_assert_int_eq(list->get_count(list), 5); | |
232 | ck_assert(list->get_first(list, (void*)&x) == SUCCESS); | |
afb6d9c3 | 233 | ck_assert_int_eq(x, 1); |
be3c09d0 TB |
234 | while (enumerator->enumerate(enumerator, &x)) |
235 | { | |
236 | } | |
237 | list->remove_at(list, enumerator); | |
238 | ck_assert_int_eq(list->get_count(list), 5); | |
239 | ck_assert(list->get_last(list, (void*)&x) == SUCCESS); | |
240 | ck_assert_int_eq(x, 5); | |
afb6d9c3 TB |
241 | enumerator->destroy(enumerator); |
242 | } | |
243 | END_TEST | |
244 | ||
be3c09d0 | 245 | START_TEST(test_insert_before_remove_at) |
26e8375b TB |
246 | { |
247 | enumerator_t *enumerator; | |
952073b8 TB |
248 | intptr_t x; |
249 | int round; | |
26e8375b TB |
250 | |
251 | round = 1; | |
252 | enumerator = list->create_enumerator(list); | |
253 | while (enumerator->enumerate(enumerator, &x)) | |
254 | { | |
255 | ck_assert_int_eq(round, x); | |
256 | if (round == 2) | |
be3c09d0 TB |
257 | { /* this replaces the current item, as insert_before does not change |
258 | * the enumerator position */ | |
259 | list->insert_before(list, enumerator, (void*)42); | |
260 | list->remove_at(list, enumerator); | |
261 | } | |
262 | else if (round == 4) | |
263 | { /* this does not replace the item, as remove_at moves the enumerator | |
264 | * position to the previous item */ | |
26e8375b | 265 | list->remove_at(list, enumerator); |
be3c09d0 | 266 | list->insert_before(list, enumerator, (void*)21); |
26e8375b TB |
267 | } |
268 | round++; | |
269 | } | |
be3c09d0 | 270 | ck_assert_int_eq(list->get_count(list), 5); |
26e8375b TB |
271 | list->reset_enumerator(list, enumerator); |
272 | round = 1; | |
273 | while (enumerator->enumerate(enumerator, &x)) | |
274 | { | |
275 | if (round == 2) | |
be3c09d0 TB |
276 | { /* check replaced item */ |
277 | ck_assert_int_eq(42, x); | |
278 | } | |
279 | else if (round == 3) | |
280 | { /* check misplaced item */ | |
281 | ck_assert_int_eq(21, x); | |
282 | } | |
283 | else if (round == 4) | |
284 | { /* check misplaced item */ | |
285 | ck_assert_int_eq(3, x); | |
286 | } | |
287 | else | |
288 | { | |
289 | ck_assert_int_eq(round, x); | |
26e8375b | 290 | } |
26e8375b TB |
291 | round++; |
292 | } | |
293 | enumerator->destroy(enumerator); | |
294 | } | |
295 | END_TEST | |
296 | ||
cca70ed3 TB |
297 | /******************************************************************************* |
298 | * create list from enumerator | |
299 | */ | |
300 | ||
301 | START_TEST(test_create_from_enumerator) | |
302 | { | |
303 | enumerator_t *enumerator, *enumerator_other; | |
304 | linked_list_t *other; | |
305 | intptr_t x, y; | |
306 | int count = 0; | |
307 | ||
308 | enumerator = list->create_enumerator(list); | |
309 | other = linked_list_create_from_enumerator(enumerator); | |
310 | ck_assert_int_eq(other->get_count(list), 5); | |
311 | ||
312 | enumerator = list->create_enumerator(list); | |
313 | enumerator_other = other->create_enumerator(other); | |
314 | while (enumerator->enumerate(enumerator, &x) && | |
315 | enumerator_other->enumerate(enumerator_other, &y)) | |
316 | { | |
317 | ck_assert_int_eq(x, y); | |
318 | count++; | |
319 | } | |
320 | ck_assert_int_eq(count, 5); | |
321 | enumerator_other->destroy(enumerator_other); | |
322 | enumerator->destroy(enumerator); | |
323 | other->destroy(other); | |
324 | } | |
325 | END_TEST | |
326 | ||
26e8375b TB |
327 | Suite *linked_list_enumerator_suite_create() |
328 | { | |
329 | Suite *s; | |
330 | TCase *tc; | |
331 | ||
332 | s = suite_create("linked list and enumerators"); | |
333 | ||
334 | tc = tcase_create("enumerate"); | |
335 | tcase_add_checked_fixture(tc, setup_list, teardown_list); | |
336 | tcase_add_test(tc, test_enumerate); | |
1a9528f9 | 337 | tcase_add_test(tc, test_enumerate_null); |
26e8375b | 338 | tcase_add_test(tc, test_reset_enumerator); |
26e8375b TB |
339 | suite_add_tcase(s, tc); |
340 | ||
341 | tc = tcase_create("insert_before()"); | |
342 | tcase_add_checked_fixture(tc, setup_list, teardown_list); | |
bc90b3dd | 343 | tcase_add_loop_test(tc, test_insert_before, 1, 5); |
26e8375b TB |
344 | tcase_add_test(tc, test_insert_before_ends); |
345 | tcase_add_test(tc, test_insert_before_empty); | |
346 | suite_add_tcase(s, tc); | |
347 | ||
348 | tc = tcase_create("modify"); | |
349 | tcase_add_checked_fixture(tc, setup_list, teardown_list); | |
26e8375b TB |
350 | tcase_add_test(tc, test_remove_at); |
351 | tcase_add_test(tc, test_remove_at_ends); | |
be3c09d0 | 352 | tcase_add_test(tc, test_insert_before_remove_at); |
26e8375b TB |
353 | suite_add_tcase(s, tc); |
354 | ||
cca70ed3 TB |
355 | tc = tcase_create("create_from_enumerator"); |
356 | tcase_add_checked_fixture(tc, setup_list, teardown_list); | |
357 | tcase_add_test(tc, test_create_from_enumerator); | |
358 | suite_add_tcase(s, tc); | |
359 | ||
26e8375b TB |
360 | return s; |
361 | } |