2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2013 Martin Willi
6 * Copyright (C) 2013 revosec AG
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include "test_suite.h"
21 #include <collections/array.h>
23 START_TEST(test_append_ptr
)
29 array
= array_create(0, 0);
31 for (i
= 0; i
< 4; i
++)
33 ck_assert_int_eq(array_count(array
), 0);
35 array_insert(array
, ARRAY_HEAD
, (void*)(uintptr_t)3);
36 array_insert(array
, ARRAY_TAIL
, (void*)(uintptr_t)4);
37 ck_assert_int_eq(array_count(array
), 2);
41 ck_assert(array_get(array
, ARRAY_HEAD
, &x
));
42 ck_assert_int_eq(x
, 3);
43 ck_assert(array_get(array
, 1, &x
));
44 ck_assert_int_eq(x
, 4);
45 ck_assert(array_get(array
, ARRAY_TAIL
, &x
));
46 ck_assert_int_eq(x
, 4);
47 ck_assert(!array_get(array
, 3, &x
));
49 array_insert(array
, ARRAY_HEAD
, (void*)(uintptr_t)1);
50 array_insert(array
, 1, (void*)(uintptr_t)2);
51 ck_assert_int_eq(array_count(array
), 4);
55 array_insert(array
, ARRAY_TAIL
, (void*)(uintptr_t)5);
56 array_insert(array
, ARRAY_HEAD
, (void*)(uintptr_t)0);
57 ck_assert_int_eq(array_count(array
), 6);
59 /* 0, 1, 2, 3, 4, 5 */
61 ck_assert(array_remove(array
, ARRAY_TAIL
, &x
));
62 ck_assert_int_eq(x
, 5);
63 ck_assert(array_remove(array
, 4, &x
));
64 ck_assert_int_eq(x
, 4);
68 array_compress(array
);
73 ck_assert(array_remove(array
, 1, &x
));
74 ck_assert_int_eq(x
, 1);
75 ck_assert(array_remove(array
, ARRAY_HEAD
, &x
));
76 ck_assert_int_eq(x
, 0);
80 array_compress(array
);
85 ck_assert(array_remove(array
, ARRAY_TAIL
, &x
));
86 ck_assert_int_eq(x
, 3);
87 ck_assert(array_remove(array
, ARRAY_TAIL
, &x
));
88 ck_assert_int_eq(x
, 2);
92 array_compress(array
);
95 ck_assert_int_eq(array_count(array
), 0);
97 ck_assert(array_remove(array
, ARRAY_HEAD
, NULL
) == FALSE
);
98 ck_assert(array_remove(array
, ARRAY_TAIL
, NULL
) == FALSE
);
101 array_destroy(array
);
105 START_TEST(test_append_obj
)
108 int i
, x
, y
[6] = {0, 1, 2, 3, 4, 5};
110 array
= array_create(sizeof(y
[0]), 0);
112 for (i
= 0; i
< 4; i
++)
114 ck_assert_int_eq(array_count(array
), 0);
116 array_insert(array
, ARRAY_HEAD
, &y
[3]);
117 array_insert(array
, ARRAY_TAIL
, &y
[4]);
118 ck_assert_int_eq(array_count(array
), 2);;
122 ck_assert(array_get(array
, ARRAY_HEAD
, &x
));
123 ck_assert_int_eq(x
, 3);
124 ck_assert(array_get(array
, 1, &x
));
125 ck_assert_int_eq(x
, 4);
126 ck_assert(array_get(array
, ARRAY_TAIL
, &x
));
127 ck_assert_int_eq(x
, 4);
128 ck_assert(!array_get(array
, 3, &x
));
130 array_insert(array
, ARRAY_HEAD
, &y
[1]);
131 array_insert(array
, 1, &y
[2]);
132 ck_assert_int_eq(array_count(array
), 4);
136 array_insert(array
, ARRAY_TAIL
, &y
[5]);
137 array_insert(array
, ARRAY_HEAD
, &y
[0]);
138 ck_assert_int_eq(array_count(array
), 6);
140 /* 0, 1, 2, 3, 4, 5 */
142 ck_assert(array_remove(array
, ARRAY_TAIL
, &x
));
143 ck_assert_int_eq(x
, 5);
144 ck_assert(array_remove(array
, 4, &x
));
145 ck_assert_int_eq(x
, 4);
149 array_compress(array
);
154 ck_assert(array_remove(array
, ARRAY_HEAD
, &x
));
155 ck_assert_int_eq(x
, 0);
156 ck_assert(array_remove(array
, ARRAY_HEAD
, &x
));
157 ck_assert_int_eq(x
, 1);
161 array_compress(array
);
166 ck_assert(array_remove(array
, ARRAY_TAIL
, &x
));
167 ck_assert_int_eq(x
, 3);
168 ck_assert(array_remove(array
, ARRAY_HEAD
, &x
));
169 ck_assert_int_eq(x
, 2);
173 array_compress(array
);
176 ck_assert_int_eq(array_count(array
), 0);
178 ck_assert(array_remove(array
, ARRAY_HEAD
, NULL
) == FALSE
);
179 ck_assert(array_remove(array
, ARRAY_TAIL
, NULL
) == FALSE
);
182 array_destroy(array
);
186 START_TEST(test_enumerate
)
189 int i
, *x
, y
[6] = {0, 1, 2, 3, 4, 5};
190 enumerator_t
*enumerator
;
192 array
= array_create(sizeof(y
[0]), 0);
194 array_insert(array
, ARRAY_TAIL
, &y
[0]);
195 array_insert(array
, ARRAY_TAIL
, &y
[1]);
196 array_insert(array
, ARRAY_TAIL
, &y
[2]);
197 array_insert(array
, ARRAY_TAIL
, &y
[3]);
198 array_insert(array
, ARRAY_TAIL
, &y
[4]);
199 array_insert(array
, ARRAY_TAIL
, &y
[5]);
201 ck_assert_int_eq(array_count(array
), 6);
203 /* 0, 1, 2, 3, 4, 5 */
206 enumerator
= array_create_enumerator(array
);
207 while (enumerator
->enumerate(enumerator
, &x
))
209 ck_assert_int_eq(*x
, y
[i
]);
212 enumerator
->destroy(enumerator
);
213 ck_assert_int_eq(i
, 6);
216 enumerator
= array_create_enumerator(array
);
217 while (enumerator
->enumerate(enumerator
, &x
))
219 ck_assert_int_eq(*x
, y
[i
]);
220 if (i
== 0 || i
== 3 || i
== 5)
222 array_remove_at(array
, enumerator
);
226 enumerator
->destroy(enumerator
);
227 ck_assert_int_eq(i
, 6);
228 ck_assert_int_eq(array_count(array
), 3);
233 enumerator
= array_create_enumerator(array
);
234 while (enumerator
->enumerate(enumerator
, &x
))
239 ck_assert_int_eq(*x
, y
[1]);
242 ck_assert_int_eq(*x
, y
[2]);
245 ck_assert_int_eq(*x
, y
[4]);
251 enumerator
->destroy(enumerator
);
253 array_compress(array
);
256 enumerator
= array_create_enumerator(array
);
257 while (enumerator
->enumerate(enumerator
, &x
))
262 ck_assert_int_eq(*x
, y
[1]);
265 ck_assert_int_eq(*x
, y
[2]);
268 ck_assert_int_eq(*x
, y
[4]);
274 enumerator
->destroy(enumerator
);
276 array_destroy(array
);
280 static int comp_obj(const void *a
, const void *b
, void *arg
)
282 ck_assert_str_eq(arg
, "arg");
283 return *(int*)a
- *(int*)b
;
286 START_TEST(test_sort_obj
)
300 for (i
= 0; i
< countof(x
); i
++)
302 array
= array_create(sizeof(x
[i
][0]), 0);
303 array_insert(array
, ARRAY_TAIL
, &x
[i
][0]);
304 array_insert(array
, ARRAY_TAIL
, &x
[i
][1]);
305 array_insert(array
, ARRAY_TAIL
, &x
[i
][2]);
307 array_sort(array
, comp_obj
, arg
);
309 ck_assert(array_get(array
, 0, &v
));
310 ck_assert_int_eq(v
, 1);
311 ck_assert(array_get(array
, 1, &v
));
312 ck_assert_int_eq(v
, 2);
313 ck_assert(array_get(array
, 2, &v
));
314 ck_assert_int_eq(v
, 3);
316 array_destroy(array
);
321 static int comp_ptr(const void *a
, const void *b
, void *arg
)
323 ck_assert_str_eq(arg
, "arg");
327 START_TEST(test_sort_ptr
)
338 char *v
, *arg
= "arg";
341 for (i
= 0; i
< countof(x
); i
++)
343 array
= array_create(0, 0);
344 array_insert(array
, ARRAY_TAIL
, x
[i
][0]);
345 array_insert(array
, ARRAY_TAIL
, x
[i
][1]);
346 array_insert(array
, ARRAY_TAIL
, x
[i
][2]);
348 array_sort(array
, comp_ptr
, arg
);
350 ck_assert(array_get(array
, 0, &v
));
351 ck_assert_str_eq(v
, "a");
352 ck_assert(array_get(array
, 1, &v
));
353 ck_assert_str_eq(v
, "b");
354 ck_assert(array_get(array
, 2, &v
));
355 ck_assert_str_eq(v
, "c");
357 array_destroy(array
);
362 static void invoke(void *data
, int idx
, void *user
)
364 int *y
= user
, *x
= data
;
368 ck_assert_int_eq(y
[idx
], *x
);
372 START_TEST(test_invoke
)
377 array
= array_create(sizeof(y
[0]), 0);
379 array_insert(array
, ARRAY_TAIL
, &y
[0]);
380 array_insert(array
, ARRAY_TAIL
, &y
[1]);
381 array_insert(array
, ARRAY_TAIL
, &y
[2]);
383 array_invoke(array
, invoke
, y
);
385 ck_assert_int_eq(y
[0], 0);
386 ck_assert_int_eq(y
[0], 0);
387 ck_assert_int_eq(y
[0], 0);
389 array_destroy(array
);
393 typedef struct obj_t obj_t
;
396 void (*fun
)(obj_t
*obj
);
401 static void fun(obj_t
*obj
)
403 ck_assert(obj
->x
== (*obj
->counter
)++);
406 START_TEST(test_invoke_offset
)
412 array
= array_create(0, 0);
414 for (i
= 0; i
< countof(objs
); i
++)
417 objs
[i
].counter
= &counter
;
420 array_insert(array
, ARRAY_TAIL
, &objs
[i
]);
423 ck_assert_int_eq(countof(objs
), array_count(array
));
425 array_invoke_offset(array
, offsetof(obj_t
, fun
));
427 ck_assert_int_eq(counter
, countof(objs
));
429 array_destroy(array
);
433 Suite
*array_suite_create()
438 s
= suite_create("array");
440 tc
= tcase_create("add/get/remove ptr");
441 tcase_add_test(tc
, test_append_ptr
);
442 suite_add_tcase(s
, tc
);
444 tc
= tcase_create("add/get/remove obj");
445 tcase_add_test(tc
, test_append_obj
);
446 suite_add_tcase(s
, tc
);
448 tc
= tcase_create("enumerate");
449 tcase_add_test(tc
, test_enumerate
);
450 suite_add_tcase(s
, tc
);
452 tc
= tcase_create("sort");
453 tcase_add_test(tc
, test_sort_obj
);
454 tcase_add_test(tc
, test_sort_ptr
);
455 suite_add_tcase(s
, tc
);
457 tc
= tcase_create("invoke");
458 tcase_add_test(tc
, test_invoke
);
459 suite_add_tcase(s
, tc
);
461 tc
= tcase_create("invoke offset");
462 tcase_add_test(tc
, test_invoke_offset
);
463 suite_add_tcase(s
, tc
);