Removed strayed code fragment
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_enumerator.c
1 /*
2 * Copyright (C) 2007 Martin Willi
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 <utils/linked_list.h>
17
18
19 /*******************************************************************************
20 * linked list remove test
21 ******************************************************************************/
22 bool test_list_remove()
23 {
24 void *a = (void*)1, *b = (void*)2;
25 linked_list_t *list;
26
27 list = linked_list_create();
28 list->insert_last(list, a);
29 if (list->remove(list, a, NULL) != 1)
30 {
31 return FALSE;
32 }
33 list->insert_last(list, a);
34 list->insert_first(list, a);
35 list->insert_last(list, a);
36 list->insert_last(list, b);
37 if (list->remove(list, a, NULL) != 3)
38 {
39 return FALSE;
40 }
41 if (list->remove(list, a, NULL) != 0)
42 {
43 return FALSE;
44 }
45 if (list->get_count(list) != 1)
46 {
47 return FALSE;
48 }
49 if (list->remove(list, b, NULL) != 1)
50 {
51 return FALSE;
52 }
53 if (list->remove(list, b, NULL) != 0)
54 {
55 return FALSE;
56 }
57 list->destroy(list);
58 return TRUE;
59 }
60
61 /*******************************************************************************
62 * Simple insert first/last and enumerate test
63 ******************************************************************************/
64 bool test_enumerate()
65 {
66 int round, x;
67 void *a = (void*)4, *b = (void*)3, *c = (void*)2, *d = (void*)5, *e = (void*)1;
68 linked_list_t *list;
69 enumerator_t *enumerator;
70
71 list = linked_list_create();
72
73 list->insert_last(list, a);
74 list->insert_first(list, b);
75 list->insert_first(list, c);
76 list->insert_last(list, d);
77 list->insert_first(list, e);
78
79 round = 1;
80 enumerator = list->create_enumerator(list);
81 while (enumerator->enumerate(enumerator, &x))
82 {
83 if (round != x)
84 {
85 return FALSE;
86 }
87 round++;
88 }
89 enumerator->destroy(enumerator);
90
91 list->destroy(list);
92 return TRUE;
93 }
94
95 /*******************************************************************************
96 * nested enumerator test
97 ******************************************************************************/
98
99 static bool bad_data;
100
101 static enumerator_t* create_inner(linked_list_t *outer, void *data)
102 {
103 if (data != (void*)101)
104 {
105 bad_data = TRUE;
106 }
107 return outer->create_enumerator(outer);
108 }
109
110
111 static void destroy_data(void *data)
112 {
113 if (data != (void*)101)
114 {
115 bad_data = TRUE;
116 }
117 }
118
119 bool test_enumerate_nested()
120 {
121 int round, x;
122 void *a = (void*)1, *b = (void*)2, *c = (void*)3, *d = (void*)4, *e = (void*)5;
123 linked_list_t *list, *l1, *l2, *l3;
124 enumerator_t *enumerator;
125
126 bad_data = FALSE;
127 list = linked_list_create();
128 l1 = linked_list_create();
129 l2 = linked_list_create();
130 l3 = linked_list_create();
131 list->insert_last(list, l1);
132 list->insert_last(list, l2);
133 list->insert_last(list, l3);
134
135 l1->insert_last(l1, a);
136 l1->insert_last(l1, b);
137 l3->insert_last(l3, c);
138 l3->insert_last(l3, d);
139 l3->insert_last(l3, e);
140
141 round = 1;
142 enumerator = enumerator_create_nested(list->create_enumerator(list),
143 (void*)create_inner, (void*)101, destroy_data);
144 while (enumerator->enumerate(enumerator, &x))
145 {
146 if (round != x)
147 {
148 return FALSE;
149 }
150 round++;
151 }
152 enumerator->destroy(enumerator);
153
154 list->destroy(list);
155 l1->destroy(l1);
156 l2->destroy(l2);
157 l3->destroy(l3);
158 return !bad_data;
159 }
160
161
162 /*******************************************************************************
163 * filtered enumerator test
164 ******************************************************************************/
165 static bool filter(void *data, int *v, int *vo, int *w, int *wo,
166 int *x, int *xo, int *y, int *yo, int *z, int *zo)
167 {
168 int val = *v;
169
170 *vo = val++;
171 *wo = val++;
172 *xo = val++;
173 *yo = val++;
174 *zo = val++;
175 if (data != (void*)101)
176 {
177 return FALSE;
178 }
179 return TRUE;
180 }
181
182 bool test_enumerate_filtered()
183 {
184 int round, v, w, x, y, z;
185 void *a = (void*)1, *b = (void*)2, *c = (void*)3, *d = (void*)4, *e = (void*)5;
186 linked_list_t *list;
187 enumerator_t *enumerator;
188
189 bad_data = FALSE;
190 list = linked_list_create();
191
192 list->insert_last(list, a);
193 list->insert_last(list, b);
194 list->insert_last(list, c);
195 list->insert_last(list, d);
196 list->insert_last(list, e);
197
198 round = 1;
199 enumerator = enumerator_create_filter(list->create_enumerator(list),
200 (void*)filter, (void*)101, destroy_data);
201 while (enumerator->enumerate(enumerator, &v, &w, &x, &y, &z))
202 {
203 if (v != round || w != round + 1 || x != round + 2 ||
204 y != round + 3 || z != round + 4)
205 {
206 return FALSE;
207 }
208 round++;
209 }
210 enumerator->destroy(enumerator);
211
212 list->destroy(list);
213 return !bad_data;
214 }
215
216 /*******************************************************************************
217 * token parser test
218 ******************************************************************************/
219
220 bool test_enumerate_token()
221 {
222 enumerator_t *enumerator;
223 char *token;
224 int i, num;
225 struct {
226 char *string;
227 char *sep;
228 char *trim;
229 } tests1[] = {
230 {"abc, cde, efg", ",", " "},
231 {" abc 1:2 cde;3 4efg5. ", ":;.,", " 12345"},
232 {"abc.cde,efg", ",.", ""},
233 {" abc cde efg ", " ", " "},
234 {"a'abc' c 'cde' cefg", " ", " abcd"},
235 {"'abc' abc 'cde'd 'efg'", " ", " abcd"},
236 }, tests2[] = {
237 {"a, b, c", ",", " "},
238 {"a,b,c", ",", " "},
239 {" a 1:2 b;3 4c5. ", ":;.,", " 12345"},
240 {"a.b,c", ",.", ""},
241 {" a b c ", " ", " "},
242 };
243
244 for (num = 0; num < countof(tests1); num++)
245 {
246 i = 0;
247 enumerator = enumerator_create_token(tests1[num].string,
248 tests1[num].sep, tests1[num].trim);
249 while (enumerator->enumerate(enumerator, &token))
250 {
251 switch (i)
252 {
253 case 0:
254 if (!streq(token, "abc")) return FALSE;
255 break;
256 case 1:
257 if (!streq(token, "cde")) return FALSE;
258 break;
259 case 2:
260 if (!streq(token, "efg")) return FALSE;
261 break;
262 default:
263 return FALSE;
264 }
265 i++;
266 }
267 if (i != 3)
268 {
269 return FALSE;
270 }
271 enumerator->destroy(enumerator);
272 }
273
274 for (num = 0; num < countof(tests2); num++)
275 {
276 i = 0;
277 enumerator = enumerator_create_token(tests2[num].string,
278 tests2[num].sep, tests2[num].trim);
279 while (enumerator->enumerate(enumerator, &token))
280 {
281 switch (i)
282 {
283 case 0:
284 if (!streq(token, "a")) return FALSE;
285 break;
286 case 1:
287 if (!streq(token, "b")) return FALSE;
288 break;
289 case 2:
290 if (!streq(token, "c")) return FALSE;
291 break;
292 default:
293 return FALSE;
294 }
295 i++;
296 }
297 if (i != 3)
298 {
299 return FALSE;
300 }
301 enumerator->destroy(enumerator);
302 }
303
304 return TRUE;
305 }
306