Add tests combining linked_list_t and enumerators
[strongswan.git] / src / libstrongswan / tests / 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 <check.h>
17
18 #include <collections/linked_list.h>
19
20 /*******************************************************************************
21 * test fixture
22 */
23
24 static linked_list_t *list;
25
26 static void 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
33 static void teardown_list()
34 {
35 list->destroy(list);
36 }
37
38 /*******************************************************************************
39 * enumeration
40 */
41
42 START_TEST(test_enumerate)
43 {
44 enumerator_t *enumerator;
45 int round, x;
46
47 round = 1;
48 enumerator = list->create_enumerator(list);
49 while (enumerator->enumerate(enumerator, &x))
50 {
51 ck_assert_int_eq(round, x);
52 round++;
53 }
54 ck_assert_int_eq(round, 6);
55 enumerator->destroy(enumerator);
56 }
57 END_TEST
58
59 START_TEST(test_reset_enumerator)
60 {
61 enumerator_t *enumerator;
62 int round, x;
63
64 enumerator = list->create_enumerator(list);
65 while (enumerator->enumerate(enumerator, &x))
66 {
67 }
68 list->reset_enumerator(list, enumerator);
69 round = 1;
70 while (enumerator->enumerate(enumerator, &x))
71 {
72 ck_assert_int_eq(round, x);
73 round++;
74 }
75 ck_assert_int_eq(round, 6);
76 enumerator->destroy(enumerator);
77 }
78 END_TEST
79
80 START_TEST(test_has_more)
81 {
82 enumerator_t *enumerator;
83 int round, x;
84
85 round = 1;
86 enumerator = list->create_enumerator(list);
87 while (enumerator->enumerate(enumerator, &x))
88 {
89 ck_assert_int_eq(round, x);
90 round++;
91 if (x == 2)
92 {
93 break;
94 }
95 }
96 ck_assert(list->has_more(list, enumerator));
97 while (enumerator->enumerate(enumerator, &x))
98 {
99 ck_assert_int_eq(round, x);
100 round++;
101 }
102 ck_assert(!list->has_more(list, enumerator));
103 ck_assert_int_eq(round, 6);
104 enumerator->destroy(enumerator);
105 }
106 END_TEST
107
108 /*******************************************************************************
109 * insert before
110 */
111
112 START_TEST(test_insert_before)
113 {
114 enumerator_t *enumerator;
115 int round, x;
116
117 round = 1;
118 enumerator = list->create_enumerator(list);
119 while (enumerator->enumerate(enumerator, &x))
120 {
121 ck_assert_int_eq(round, x);
122 round++;
123 if (x == 2)
124 {
125 list->insert_before(list, enumerator, (void*)6);
126 }
127 }
128 ck_assert_int_eq(list->get_count(list), 6);
129 list->reset_enumerator(list, enumerator);
130 round = 1;
131 while (enumerator->enumerate(enumerator, &x))
132 {
133 if (round == 2 && x != 2)
134 {
135 ck_assert_int_eq(6, x);
136 }
137 else
138 {
139 ck_assert_int_eq(round, x);
140 round++;
141 }
142 }
143 enumerator->destroy(enumerator);
144 }
145 END_TEST
146
147 START_TEST(test_insert_before_ends)
148 {
149 enumerator_t *enumerator;
150 int round, x;
151
152 enumerator = list->create_enumerator(list);
153 list->insert_before(list, enumerator, (void*)0);
154 ck_assert_int_eq(list->get_count(list), 6);
155 ck_assert(list->get_first(list, (void*)&x) == SUCCESS);
156 ck_assert_int_eq(x, 0);
157 round = 0;
158 while (enumerator->enumerate(enumerator, &x))
159 {
160 ck_assert_int_eq(round, x);
161 round++;
162 }
163 list->insert_before(list, enumerator, (void*)6);
164 ck_assert_int_eq(list->get_count(list), 7);
165 ck_assert(list->get_last(list, (void*)&x) == SUCCESS);
166 ck_assert_int_eq(x, 6);
167 ck_assert(!list->has_more(list, enumerator));
168 ck_assert(!enumerator->enumerate(enumerator, &x));
169 enumerator->destroy(enumerator);
170 }
171 END_TEST
172
173 START_TEST(test_insert_before_empty)
174 {
175 enumerator_t *enumerator;
176 int x;
177
178 list->destroy(list);
179 list = linked_list_create();
180 enumerator = list->create_enumerator(list);
181 list->insert_before(list, enumerator, (void*)1);
182 ck_assert_int_eq(list->get_count(list), 1);
183 ck_assert(list->get_first(list, (void*)&x) == SUCCESS);
184 ck_assert_int_eq(x, 1);
185 ck_assert(list->get_last(list, (void*)&x) == SUCCESS);
186 ck_assert_int_eq(x, 1);
187 ck_assert(list->has_more(list, enumerator));
188 ck_assert(enumerator->enumerate(enumerator, &x));
189 ck_assert_int_eq(x, 1);
190 ck_assert(!list->has_more(list, enumerator));
191 enumerator->destroy(enumerator);
192 }
193 END_TEST
194
195 /*******************************************************************************
196 * replace / remove_at
197 */
198
199 START_TEST(test_replace)
200 {
201 enumerator_t *enumerator;
202 int round, x;
203
204 round = 1;
205 enumerator = list->create_enumerator(list);
206 while (enumerator->enumerate(enumerator, &x))
207 {
208 ck_assert_int_eq(round, x);
209 list->replace(list, enumerator, (void*)(uintptr_t)(6 - round));
210 round++;
211 }
212 list->reset_enumerator(list, enumerator);
213 round = 5;
214 while (enumerator->enumerate(enumerator, &x))
215 {
216 ck_assert_int_eq(round, x);
217 round--;
218 }
219 enumerator->destroy(enumerator);
220 }
221 END_TEST
222
223 START_TEST(test_remove_at)
224 {
225 enumerator_t *enumerator;
226 int round, x;
227
228 round = 1;
229 enumerator = list->create_enumerator(list);
230 while (enumerator->enumerate(enumerator, &x))
231 {
232 ck_assert_int_eq(round, x);
233 if (round == 2)
234 {
235 list->remove_at(list, enumerator);
236 }
237 round++;
238 }
239 ck_assert_int_eq(list->get_count(list), 4);
240 list->reset_enumerator(list, enumerator);
241 round = 1;
242 while (enumerator->enumerate(enumerator, &x))
243 {
244 if (round == 2)
245 { /* skip removed item */
246 round++;
247 }
248 ck_assert_int_eq(round, x);
249 round++;
250 }
251 enumerator->destroy(enumerator);
252 }
253 END_TEST
254
255 START_TEST(test_remove_at_ends)
256 {
257 enumerator_t *enumerator;
258 int x;
259
260 enumerator = list->create_enumerator(list);
261 list->remove_at(list, enumerator);
262 ck_assert_int_eq(list->get_count(list), 5);
263 ck_assert(list->get_first(list, (void*)&x) == SUCCESS);
264 ck_assert_int_eq(x, 1);
265 while (enumerator->enumerate(enumerator, &x))
266 {
267 }
268 list->remove_at(list, enumerator);
269 ck_assert_int_eq(list->get_count(list), 5);
270 ck_assert(list->get_last(list, (void*)&x) == SUCCESS);
271 ck_assert_int_eq(x, 5);
272 enumerator->destroy(enumerator);
273 }
274 END_TEST
275
276 Suite *linked_list_enumerator_suite_create()
277 {
278 Suite *s;
279 TCase *tc;
280
281 s = suite_create("linked list and enumerators");
282
283 tc = tcase_create("enumerate");
284 tcase_add_checked_fixture(tc, setup_list, teardown_list);
285 tcase_add_test(tc, test_enumerate);
286 tcase_add_test(tc, test_reset_enumerator);
287 tcase_add_test(tc, test_has_more);
288 suite_add_tcase(s, tc);
289
290 tc = tcase_create("insert_before()");
291 tcase_add_checked_fixture(tc, setup_list, teardown_list);
292 tcase_add_test(tc, test_insert_before);
293 tcase_add_test(tc, test_insert_before_ends);
294 tcase_add_test(tc, test_insert_before_empty);
295 suite_add_tcase(s, tc);
296
297 tc = tcase_create("modify");
298 tcase_add_checked_fixture(tc, setup_list, teardown_list);
299 tcase_add_test(tc, test_replace);
300 tcase_add_test(tc, test_remove_at);
301 tcase_add_test(tc, test_remove_at_ends);
302 suite_add_tcase(s, tc);
303
304 return s;
305 }