diff options
Diffstat (limited to 'libraries/eina/src/tests/eina_test_iterator.c')
-rw-r--r-- | libraries/eina/src/tests/eina_test_iterator.c | 465 |
1 files changed, 0 insertions, 465 deletions
diff --git a/libraries/eina/src/tests/eina_test_iterator.c b/libraries/eina/src/tests/eina_test_iterator.c deleted file mode 100644 index 84bb336..0000000 --- a/libraries/eina/src/tests/eina_test_iterator.c +++ /dev/null | |||
@@ -1,465 +0,0 @@ | |||
1 | /* EINA - EFL data type library | ||
2 | * Copyright (C) 2008 Cedric Bail | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library; | ||
16 | * if not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include "config.h" | ||
21 | #endif | ||
22 | |||
23 | #include <stdio.h> | ||
24 | |||
25 | #include "eina_suite.h" | ||
26 | #include "Eina.h" | ||
27 | |||
28 | static Eina_Bool | ||
29 | eina_iterator_array_check(__UNUSED__ const Eina_Array *array, | ||
30 | int *data, int *fdata) | ||
31 | { | ||
32 | fail_if(*fdata > *data); | ||
33 | *fdata = *data; | ||
34 | |||
35 | return EINA_TRUE; | ||
36 | } | ||
37 | |||
38 | START_TEST(eina_iterator_array_simple) | ||
39 | { | ||
40 | Eina_Iterator *it; | ||
41 | Eina_Array *ea; | ||
42 | int *tmp; | ||
43 | int i; | ||
44 | |||
45 | eina_init(); | ||
46 | |||
47 | ea = eina_array_new(11); | ||
48 | fail_if(!ea); | ||
49 | |||
50 | for (i = 0; i < 200; ++i) | ||
51 | { | ||
52 | tmp = malloc(sizeof(int)); | ||
53 | fail_if(!tmp); | ||
54 | *tmp = i; | ||
55 | |||
56 | eina_array_push(ea, tmp); | ||
57 | } | ||
58 | |||
59 | it = eina_array_iterator_new(ea); | ||
60 | fail_if(!it); | ||
61 | |||
62 | i = -1; | ||
63 | eina_iterator_foreach(it, EINA_EACH_CB(eina_iterator_array_check), &i); | ||
64 | fail_if(i != 199); | ||
65 | |||
66 | fail_if(eina_iterator_container_get(it) != ea); | ||
67 | fail_if(eina_iterator_next(it, (void **)&tmp) != EINA_FALSE); | ||
68 | |||
69 | eina_iterator_free(it); | ||
70 | |||
71 | eina_array_free(ea); | ||
72 | |||
73 | eina_shutdown(); | ||
74 | } | ||
75 | END_TEST | ||
76 | |||
77 | static Eina_Bool | ||
78 | eina_iterator_hash_key_check(const Eina_Hash *hash, | ||
79 | const char *key, | ||
80 | __UNUSED__ void *fdata) | ||
81 | { | ||
82 | fail_if(eina_hash_find(hash, key) == NULL); | ||
83 | |||
84 | return EINA_TRUE; | ||
85 | } | ||
86 | static Eina_Bool | ||
87 | eina_iterator_hash_data_check(const Eina_Hash *hash, | ||
88 | int *data, | ||
89 | __UNUSED__ void *fdata) | ||
90 | { | ||
91 | char tmp[10]; | ||
92 | |||
93 | snprintf(tmp, 10, "%i", *data); | ||
94 | fail_if(eina_hash_find(hash, tmp) != data); | ||
95 | |||
96 | return EINA_TRUE; | ||
97 | } | ||
98 | static Eina_Bool | ||
99 | eina_iterator_hash_tuple_check(__UNUSED__ const Eina_Hash *hash, | ||
100 | Eina_Hash_Tuple *tuple, | ||
101 | __UNUSED__ void *fdata) | ||
102 | { | ||
103 | fail_if(atoi((char *)tuple->key) != *((int *)tuple->data)); | ||
104 | |||
105 | return EINA_TRUE; | ||
106 | } | ||
107 | |||
108 | START_TEST(eina_iterator_hash_simple) | ||
109 | { | ||
110 | Eina_Iterator *it; | ||
111 | Eina_Hash *hash; | ||
112 | int array[] = { 1, 42, 7, 8, 6 }; | ||
113 | |||
114 | eina_init(); | ||
115 | |||
116 | hash = eina_hash_string_superfast_new(NULL); | ||
117 | fail_if(hash == NULL); | ||
118 | |||
119 | fail_if(eina_hash_add(hash, "1", &array[0]) != EINA_TRUE); | ||
120 | fail_if(eina_hash_add(hash, "42", &array[1]) != EINA_TRUE); | ||
121 | fail_if(eina_hash_add(hash, "7", &array[2]) != EINA_TRUE); | ||
122 | fail_if(eina_hash_add(hash, "8", &array[3]) != EINA_TRUE); | ||
123 | fail_if(eina_hash_add(hash, "6", &array[4]) != EINA_TRUE); | ||
124 | |||
125 | it = eina_hash_iterator_key_new(hash); | ||
126 | eina_iterator_foreach(it, EINA_EACH_CB(eina_iterator_hash_key_check), NULL); | ||
127 | eina_iterator_free(it); | ||
128 | |||
129 | it = eina_hash_iterator_data_new(hash); | ||
130 | eina_iterator_foreach(it, EINA_EACH_CB(eina_iterator_hash_data_check), NULL); | ||
131 | eina_iterator_free(it); | ||
132 | |||
133 | it = eina_hash_iterator_tuple_new(hash); | ||
134 | eina_iterator_foreach(it, EINA_EACH_CB(eina_iterator_hash_tuple_check), NULL); | ||
135 | eina_iterator_free(it); | ||
136 | |||
137 | eina_hash_free(hash); | ||
138 | |||
139 | eina_shutdown(); | ||
140 | } | ||
141 | END_TEST | ||
142 | |||
143 | typedef struct _Eina_Test_Inlist Eina_Test_Inlist; | ||
144 | struct _Eina_Test_Inlist | ||
145 | { | ||
146 | EINA_INLIST; | ||
147 | int i; | ||
148 | }; | ||
149 | |||
150 | static Eina_Test_Inlist * | ||
151 | _eina_test_inlist_build(int i) | ||
152 | { | ||
153 | Eina_Test_Inlist *tmp; | ||
154 | |||
155 | tmp = malloc(sizeof(Eina_Test_Inlist)); | ||
156 | fail_if(!tmp); | ||
157 | tmp->i = i; | ||
158 | |||
159 | return tmp; | ||
160 | } | ||
161 | |||
162 | static Eina_Bool | ||
163 | eina_iterator_inlist_data_check(__UNUSED__ const Eina_Inlist *in_list, | ||
164 | Eina_Test_Inlist *data, | ||
165 | int *fdata) | ||
166 | { | ||
167 | switch (*fdata) | ||
168 | { | ||
169 | case 0: fail_if(data->i != 27); break; | ||
170 | |||
171 | case 1: fail_if(data->i != 42); break; | ||
172 | |||
173 | case 2: fail_if(data->i != 3227); break; | ||
174 | |||
175 | case 3: fail_if(data->i != 1664); break; | ||
176 | |||
177 | case 4: fail_if(data->i != 81); break; | ||
178 | } | ||
179 | |||
180 | (*fdata)++; | ||
181 | |||
182 | return EINA_TRUE; | ||
183 | } | ||
184 | |||
185 | START_TEST(eina_iterator_inlist_simple) | ||
186 | { | ||
187 | Eina_Inlist *lst = NULL; | ||
188 | Eina_Test_Inlist *tmp; | ||
189 | Eina_Test_Inlist *prev; | ||
190 | Eina_Iterator *it; | ||
191 | int i = 0; | ||
192 | |||
193 | tmp = _eina_test_inlist_build(42); | ||
194 | lst = eina_inlist_append(lst, EINA_INLIST_GET(tmp)); | ||
195 | fail_if(!lst); | ||
196 | |||
197 | tmp = _eina_test_inlist_build(1664); | ||
198 | lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), lst); | ||
199 | fail_if(!lst); | ||
200 | fail_if(((Eina_Test_Inlist *)lst)->i != 42); | ||
201 | |||
202 | prev = tmp; | ||
203 | tmp = _eina_test_inlist_build(3227); | ||
204 | lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET( | ||
205 | tmp), EINA_INLIST_GET(prev)); | ||
206 | fail_if(!lst); | ||
207 | fail_if(((Eina_Test_Inlist *)lst)->i != 42); | ||
208 | |||
209 | tmp = _eina_test_inlist_build(27); | ||
210 | lst = eina_inlist_prepend_relative(lst, EINA_INLIST_GET(tmp), NULL); | ||
211 | |||
212 | tmp = _eina_test_inlist_build(81); | ||
213 | lst = eina_inlist_append_relative(lst, EINA_INLIST_GET(tmp), NULL); | ||
214 | |||
215 | it = eina_inlist_iterator_new(lst); | ||
216 | fail_if(!it); | ||
217 | |||
218 | eina_iterator_foreach(it, EINA_EACH_CB(eina_iterator_inlist_data_check), &i); | ||
219 | eina_iterator_free(it); | ||
220 | |||
221 | fail_if(i != 5); | ||
222 | } | ||
223 | END_TEST | ||
224 | |||
225 | static Eina_Bool | ||
226 | eina_iterator_list_data_check(__UNUSED__ const Eina_List *list, | ||
227 | int *data, | ||
228 | int *fdata) | ||
229 | { | ||
230 | switch (*fdata) | ||
231 | { | ||
232 | case 0: fail_if(*data != 81); break; | ||
233 | |||
234 | case 1: fail_if(*data != 7); break; | ||
235 | |||
236 | case 2: fail_if(*data != 9); break; | ||
237 | |||
238 | case 3: fail_if(*data != 6); break; | ||
239 | |||
240 | case 4: fail_if(*data != 42); break; | ||
241 | |||
242 | case 5: fail_if(*data != 1); break; | ||
243 | |||
244 | case 6: fail_if(*data != 1337); break; | ||
245 | } | ||
246 | |||
247 | (*fdata)++; | ||
248 | |||
249 | return EINA_TRUE; | ||
250 | } | ||
251 | |||
252 | START_TEST(eina_iterator_list_simple) | ||
253 | { | ||
254 | Eina_List *list = NULL; | ||
255 | Eina_Iterator *it; | ||
256 | int data[] = { 6, 9, 42, 1, 7, 1337, 81, 1664 }; | ||
257 | int i = 0; | ||
258 | |||
259 | eina_init(); | ||
260 | |||
261 | list = eina_list_append(list, &data[0]); | ||
262 | fail_if(list == NULL); | ||
263 | |||
264 | list = eina_list_prepend(list, &data[1]); | ||
265 | fail_if(list == NULL); | ||
266 | |||
267 | list = eina_list_append(list, &data[2]); | ||
268 | fail_if(list == NULL); | ||
269 | |||
270 | list = eina_list_append(list, &data[3]); | ||
271 | fail_if(list == NULL); | ||
272 | |||
273 | list = eina_list_prepend(list, &data[4]); | ||
274 | fail_if(list == NULL); | ||
275 | |||
276 | list = eina_list_append(list, &data[5]); | ||
277 | fail_if(list == NULL); | ||
278 | |||
279 | list = eina_list_prepend(list, &data[6]); | ||
280 | fail_if(list == NULL); | ||
281 | |||
282 | it = eina_list_iterator_new(list); | ||
283 | fail_if(!it); | ||
284 | |||
285 | eina_iterator_foreach(it, EINA_EACH_CB(eina_iterator_list_data_check), &i); | ||
286 | eina_iterator_free(it); | ||
287 | } | ||
288 | END_TEST | ||
289 | |||
290 | typedef struct _Eina_Rbtree_Int Eina_Rbtree_Int; | ||
291 | struct _Eina_Rbtree_Int | ||
292 | { | ||
293 | Eina_Rbtree node; | ||
294 | int value; | ||
295 | }; | ||
296 | |||
297 | static Eina_Rbtree_Direction | ||
298 | eina_rbtree_int_cmp(const Eina_Rbtree_Int *left, const Eina_Rbtree_Int *right) | ||
299 | { | ||
300 | fail_if(!left); | ||
301 | fail_if(!right); | ||
302 | |||
303 | if (left->value < right->value) | ||
304 | return EINA_RBTREE_LEFT; | ||
305 | |||
306 | return EINA_RBTREE_RIGHT; | ||
307 | } | ||
308 | |||
309 | static Eina_Rbtree * | ||
310 | _eina_rbtree_int_new(int value) | ||
311 | { | ||
312 | Eina_Rbtree_Int *it; | ||
313 | |||
314 | it = malloc(sizeof (Eina_Rbtree_Int)); | ||
315 | fail_if(!it); | ||
316 | |||
317 | it->value = value; | ||
318 | |||
319 | return &it->node; | ||
320 | } | ||
321 | |||
322 | static Eina_Bool | ||
323 | eina_iterator_rbtree_data_check_sorted(__UNUSED__ const Eina_List *list, | ||
324 | Eina_Rbtree_Int *data, | ||
325 | int *fdata) | ||
326 | { | ||
327 | switch (*fdata) | ||
328 | { | ||
329 | case 0: fail_if(data->value != 10); break; | ||
330 | |||
331 | case 1: fail_if(data->value != 27); break; | ||
332 | |||
333 | case 2: fail_if(data->value != 42); break; | ||
334 | |||
335 | case 3: fail_if(data->value != 69); break; | ||
336 | |||
337 | case 4: fail_if(data->value != 1337); break; | ||
338 | } | ||
339 | |||
340 | (*fdata)++; | ||
341 | |||
342 | return EINA_TRUE; | ||
343 | } | ||
344 | |||
345 | static Eina_Bool | ||
346 | eina_iterator_rbtree_data_check_prefix(__UNUSED__ const Eina_List *list, | ||
347 | Eina_Rbtree_Int *data, | ||
348 | int *fdata) | ||
349 | { | ||
350 | switch (*fdata) | ||
351 | { | ||
352 | case 0: fail_if(data->value != 27); break; | ||
353 | |||
354 | case 1: fail_if(data->value != 10); break; | ||
355 | |||
356 | case 2: fail_if(data->value != 69); break; | ||
357 | |||
358 | case 3: fail_if(data->value != 42); break; | ||
359 | |||
360 | case 4: fail_if(data->value != 1337); break; | ||
361 | } | ||
362 | |||
363 | (*fdata)++; | ||
364 | |||
365 | return EINA_TRUE; | ||
366 | } | ||
367 | |||
368 | static Eina_Bool | ||
369 | eina_iterator_rbtree_data_check_postfix(__UNUSED__ const Eina_List *list, | ||
370 | Eina_Rbtree_Int *data, | ||
371 | int *fdata) | ||
372 | { | ||
373 | switch (*fdata) | ||
374 | { | ||
375 | case 0: fail_if(data->value != 10); break; | ||
376 | |||
377 | case 1: fail_if(data->value != 42); break; | ||
378 | |||
379 | case 2: fail_if(data->value != 1337); break; | ||
380 | |||
381 | case 3: fail_if(data->value != 69); break; | ||
382 | |||
383 | case 4: fail_if(data->value != 27); break; | ||
384 | } | ||
385 | |||
386 | (*fdata)++; | ||
387 | |||
388 | return EINA_TRUE; | ||
389 | } | ||
390 | |||
391 | START_TEST(eina_iterator_rbtree_simple) | ||
392 | { | ||
393 | Eina_Rbtree *root = NULL; | ||
394 | Eina_Iterator *it; | ||
395 | int i; | ||
396 | |||
397 | root = eina_rbtree_inline_insert(NULL, | ||
398 | _eina_rbtree_int_new(10), | ||
399 | EINA_RBTREE_CMP_NODE_CB(eina_rbtree_int_cmp), | ||
400 | NULL); | ||
401 | fail_if(!root); | ||
402 | |||
403 | root = eina_rbtree_inline_insert(root, | ||
404 | _eina_rbtree_int_new(1337), | ||
405 | EINA_RBTREE_CMP_NODE_CB(eina_rbtree_int_cmp), | ||
406 | NULL); | ||
407 | fail_if(!root); | ||
408 | |||
409 | root = eina_rbtree_inline_insert(root, | ||
410 | _eina_rbtree_int_new(27), | ||
411 | EINA_RBTREE_CMP_NODE_CB(eina_rbtree_int_cmp), | ||
412 | NULL); | ||
413 | fail_if(!root); | ||
414 | |||
415 | root = eina_rbtree_inline_insert(root, | ||
416 | _eina_rbtree_int_new(69), | ||
417 | EINA_RBTREE_CMP_NODE_CB(eina_rbtree_int_cmp), | ||
418 | NULL); | ||
419 | fail_if(!root); | ||
420 | |||
421 | root = eina_rbtree_inline_insert(root, | ||
422 | _eina_rbtree_int_new(42), | ||
423 | EINA_RBTREE_CMP_NODE_CB(eina_rbtree_int_cmp), | ||
424 | NULL); | ||
425 | fail_if(!root); | ||
426 | |||
427 | i = 0; | ||
428 | it = eina_rbtree_iterator_prefix(root); | ||
429 | fail_if(!it); | ||
430 | |||
431 | eina_iterator_foreach(it, | ||
432 | EINA_EACH_CB(eina_iterator_rbtree_data_check_prefix), | ||
433 | &i); | ||
434 | eina_iterator_free(it); | ||
435 | |||
436 | /* This will return the item sorted. */ | ||
437 | i = 0; | ||
438 | it = eina_rbtree_iterator_infix(root); | ||
439 | fail_if(!it); | ||
440 | |||
441 | eina_iterator_foreach(it, | ||
442 | EINA_EACH_CB(eina_iterator_rbtree_data_check_sorted), | ||
443 | &i); | ||
444 | eina_iterator_free(it); | ||
445 | |||
446 | i = 0; | ||
447 | it = eina_rbtree_iterator_postfix(root); | ||
448 | fail_if(!it); | ||
449 | |||
450 | eina_iterator_foreach(it, | ||
451 | EINA_EACH_CB(eina_iterator_rbtree_data_check_postfix), | ||
452 | &i); | ||
453 | eina_iterator_free(it); | ||
454 | } | ||
455 | END_TEST | ||
456 | |||
457 | void | ||
458 | eina_test_iterator(TCase *tc) | ||
459 | { | ||
460 | tcase_add_test(tc, eina_iterator_array_simple); | ||
461 | tcase_add_test(tc, eina_iterator_hash_simple); | ||
462 | tcase_add_test(tc, eina_iterator_inlist_simple); | ||
463 | tcase_add_test(tc, eina_iterator_list_simple); | ||
464 | tcase_add_test(tc, eina_iterator_rbtree_simple); | ||
465 | } | ||