unit-tests: Move test suites to its own subfolder
[strongswan.git] / src / libstrongswan / tests / suites / test_linked_list_enumerator.c
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
16 #include "test_suite.h"
17
18 #include <collections/linked_list.h>
19
20 /*******************************************************************************
21 * test fixture
22 */
23
24 static linked_list_t *list;
25
26 START_SETUP(setup_list)
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 }
32 END_SETUP
33
34 START_TEARDOWN(teardown_list)
35 {
36 list->destroy(list);
37 }
38 END_TEARDOWN
39
40 /*******************************************************************************
41 * enumeration
42 */
43
44 START_TEST(test_enumerate)
45 {
46 enumerator_t *enumerator;
47 intptr_t x;
48 int round;
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
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
78 START_TEST(test_reset_enumerator)
79 {
80 enumerator_t *enumerator;
81 intptr_t x;
82 int round;
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
100 /*******************************************************************************
101 * insert before
102 */
103
104 START_TEST(test_insert_before)
105 {
106 enumerator_t *enumerator;
107 intptr_t x;
108 int round;
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++;
116 if (x == _i)
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 {
126 if (round == _i && x != _i)
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;
143 intptr_t x;
144 int round;
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);
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;
169 intptr_t x;
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);
180 ck_assert(enumerator->enumerate(enumerator, &x));
181 ck_assert_int_eq(x, 1);
182 ck_assert(!enumerator->enumerate(enumerator, NULL));
183 enumerator->destroy(enumerator);
184 }
185 END_TEST
186
187 /*******************************************************************************
188 * remove_at
189 */
190
191 START_TEST(test_remove_at)
192 {
193 enumerator_t *enumerator;
194 intptr_t x;
195 int round;
196
197 round = 1;
198 enumerator = list->create_enumerator(list);
199 while (enumerator->enumerate(enumerator, &x))
200 {
201 ck_assert_int_eq(round, x);
202 if (round == 2)
203 {
204 list->remove_at(list, enumerator);
205 }
206 round++;
207 }
208 ck_assert_int_eq(list->get_count(list), 4);
209 list->reset_enumerator(list, enumerator);
210 round = 1;
211 while (enumerator->enumerate(enumerator, &x))
212 {
213 if (round == 2)
214 { /* skip removed item */
215 round++;
216 }
217 ck_assert_int_eq(round, x);
218 round++;
219 }
220 enumerator->destroy(enumerator);
221 }
222 END_TEST
223
224 START_TEST(test_remove_at_ends)
225 {
226 enumerator_t *enumerator;
227 intptr_t x;
228
229 enumerator = list->create_enumerator(list);
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);
233 ck_assert_int_eq(x, 1);
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);
241 enumerator->destroy(enumerator);
242 }
243 END_TEST
244
245 START_TEST(test_insert_before_remove_at)
246 {
247 enumerator_t *enumerator;
248 intptr_t x;
249 int round;
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)
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 */
265 list->remove_at(list, enumerator);
266 list->insert_before(list, enumerator, (void*)21);
267 }
268 round++;
269 }
270 ck_assert_int_eq(list->get_count(list), 5);
271 list->reset_enumerator(list, enumerator);
272 round = 1;
273 while (enumerator->enumerate(enumerator, &x))
274 {
275 if (round == 2)
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);
290 }
291 round++;
292 }
293 enumerator->destroy(enumerator);
294 }
295 END_TEST
296
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
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);
337 tcase_add_test(tc, test_enumerate_null);
338 tcase_add_test(tc, test_reset_enumerator);
339 suite_add_tcase(s, tc);
340
341 tc = tcase_create("insert_before()");
342 tcase_add_checked_fixture(tc, setup_list, teardown_list);
343 tcase_add_loop_test(tc, test_insert_before, 1, 5);
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);
350 tcase_add_test(tc, test_remove_at);
351 tcase_add_test(tc, test_remove_at_ends);
352 tcase_add_test(tc, test_insert_before_remove_at);
353 suite_add_tcase(s, tc);
354
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
360 return s;
361 }