]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/tests/suites/test_linked_list.c
2 * Copyright (C) 2013 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "test_suite.h"
19 #include <collections/linked_list.h>
21 /*******************************************************************************
25 static linked_list_t
*list
;
27 START_SETUP(setup_list
)
31 list
= linked_list_create();
32 ck_assert_int_eq(list
->get_count(list
), 0);
33 ck_assert(list
->get_first(list
, &x
) == NOT_FOUND
);
34 ck_assert(list
->get_last(list
, &x
) == NOT_FOUND
);
38 START_TEARDOWN(teardown_list
)
44 /*******************************************************************************
48 START_TEST(test_insert_first
)
50 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
52 list
->insert_first(list
, a
);
53 ck_assert_int_eq(list
->get_count(list
), 1);
54 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
56 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
59 list
->insert_first(list
, b
);
60 ck_assert_int_eq(list
->get_count(list
), 2);
61 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
63 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
68 START_TEST(test_insert_last
)
70 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
72 list
->insert_last(list
, a
);
73 ck_assert_int_eq(list
->get_count(list
), 1);
74 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
76 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
79 list
->insert_last(list
, b
);
80 ck_assert_int_eq(list
->get_count(list
), 2);
81 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
83 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
88 /*******************************************************************************
92 START_TEST(test_remove_first
)
94 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
96 list
->insert_first(list
, a
);
97 list
->insert_first(list
, b
);
98 ck_assert(list
->remove_first(list
, &x
) == SUCCESS
);
99 ck_assert_int_eq(list
->get_count(list
), 1);
101 ck_assert(list
->remove_first(list
, &x
) == SUCCESS
);
102 ck_assert_int_eq(list
->get_count(list
), 0);
104 ck_assert(list
->remove_first(list
, &x
) == NOT_FOUND
);
105 ck_assert(list
->remove_last(list
, &x
) == NOT_FOUND
);
109 START_TEST(test_remove_last
)
111 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
113 list
->insert_first(list
, a
);
114 list
->insert_first(list
, b
);
115 ck_assert(list
->remove_last(list
, &x
) == SUCCESS
);
116 ck_assert_int_eq(list
->get_count(list
), 1);
118 ck_assert(list
->remove_last(list
, &x
) == SUCCESS
);
119 ck_assert_int_eq(list
->get_count(list
), 0);
121 ck_assert(list
->remove_first(list
, &x
) == NOT_FOUND
);
122 ck_assert(list
->remove_last(list
, &x
) == NOT_FOUND
);
126 /*******************************************************************************
127 * helper function for remove and find tests
130 static bool match_a(void *item
, void *a
)
132 ck_assert(a
== (void*)1);
136 static bool match_b(void *item
, void *b
)
138 ck_assert(b
== (void*)2);
142 /*******************************************************************************
146 START_TEST(test_remove
)
148 void *a
= (void*)1, *b
= (void*)2;
150 list
->insert_first(list
, a
);
151 ck_assert(list
->remove(list
, a
, NULL
) == 1);
152 ck_assert_int_eq(list
->get_count(list
), 0);
154 list
->insert_last(list
, a
);
155 list
->insert_last(list
, a
);
156 list
->insert_last(list
, a
);
157 list
->insert_last(list
, b
);
158 ck_assert(list
->remove(list
, a
, NULL
) == 3);
159 ck_assert(list
->remove(list
, a
, NULL
) == 0);
160 ck_assert_int_eq(list
->get_count(list
), 1);
161 ck_assert(list
->remove(list
, b
, NULL
) == 1);
162 ck_assert(list
->remove(list
, b
, NULL
) == 0);
166 START_TEST(test_remove_callback
)
168 void *a
= (void*)1, *b
= (void*)2;
170 list
->insert_last(list
, a
);
171 list
->insert_last(list
, b
);
172 list
->insert_last(list
, a
);
173 list
->insert_last(list
, b
);
174 ck_assert(list
->remove(list
, a
, match_a
) == 2);
175 ck_assert(list
->remove(list
, a
, match_a
) == 0);
176 ck_assert_int_eq(list
->get_count(list
), 2);
177 ck_assert(list
->remove(list
, b
, match_b
) == 2);
178 ck_assert(list
->remove(list
, b
, match_b
) == 0);
179 ck_assert_int_eq(list
->get_count(list
), 0);
183 /*******************************************************************************
187 CALLBACK(find_a_b
, bool,
188 void *item
, va_list args
)
192 VA_ARGS_VGET(args
, a
, b
);
193 ck_assert(a
== (void*)1);
194 ck_assert(b
== (void*)2);
195 return item
== a
|| item
== b
;
198 CALLBACK(find_a
, bool,
199 void *item
, va_list args
)
203 VA_ARGS_VGET(args
, a
);
204 return match_a(item
, a
);
207 CALLBACK(find_b
, bool,
208 void *item
, va_list args
)
212 VA_ARGS_VGET(args
, b
);
213 return match_b(item
, b
);
216 START_TEST(test_find
)
218 void *a
= (void*)1, *b
= (void*)2;
220 ck_assert(!list
->find_first(list
, NULL
, &a
));
221 list
->insert_last(list
, a
);
222 ck_assert(list
->find_first(list
, NULL
, &a
));
223 ck_assert(!list
->find_first(list
, NULL
, &b
));
224 list
->insert_last(list
, b
);
225 ck_assert(list
->find_first(list
, NULL
, &a
));
226 ck_assert(list
->find_first(list
, NULL
, &b
));
228 ck_assert(!list
->find_first(list
, NULL
, NULL
));
232 START_TEST(test_find_callback
)
234 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
236 ck_assert(!list
->find_first(list
, find_a_b
, &x
, a
, b
));
237 list
->insert_last(list
, a
);
238 ck_assert(list
->find_first(list
, find_a
, NULL
, a
));
240 ck_assert(list
->find_first(list
, find_a
, &x
, a
));
242 ck_assert(!list
->find_first(list
, find_b
, &x
, b
));
245 ck_assert(list
->find_first(list
, find_a_b
, &x
, a
, b
));
248 list
->insert_last(list
, b
);
249 ck_assert(list
->find_first(list
, find_a
, &x
, a
));
251 ck_assert(list
->find_first(list
, find_b
, &x
, b
));
254 ck_assert(list
->find_first(list
, find_a_b
, &x
, a
, b
));
259 CALLBACK(find_args
, bool,
260 void *item
, va_list args
)
266 VA_ARGS_VGET(args
, a
, b
, c
, d
, e
);
267 ck_assert_int_eq(*a
, 1);
268 ck_assert_int_eq(b
, 2);
269 ck_assert_int_eq(c
, LEVEL_PRIVATE
);
270 ck_assert_int_eq(d
, UINT64_MAX
);
271 ck_assert_int_eq(e
, UINT64_MAX
-1);
275 START_TEST(test_find_callback_args
)
277 int a
= 1, b
= 2, *x
;
278 uint64_t d
= UINT64_MAX
;
280 list
->insert_last(list
, &a
);
281 ck_assert(list
->find_first(list
, find_args
, (void**)&x
, &a
, b
,
282 LEVEL_PRIVATE
, d
, UINT64_MAX
-1));
283 ck_assert_int_eq(a
, *x
);
287 /*******************************************************************************
291 typedef struct invoke_t invoke_t
;
295 void (*invoke
)(invoke_t
*item
);
298 CALLBACK(invoke
, void,
299 intptr_t item
, va_list args
)
304 VA_ARGS_VGET(args
, a
, b
, c
, d
, sum
);
305 ck_assert_int_eq((uintptr_t)a
, 1);
306 ck_assert_int_eq((uintptr_t)b
, 2);
307 ck_assert_int_eq((uintptr_t)c
, 3);
308 ck_assert_int_eq((uintptr_t)d
, 4);
312 static void invoke_offset(invoke_t
*item
)
317 START_TEST(test_invoke_function
)
321 list
->insert_last(list
, (void*)1);
322 list
->insert_last(list
, (void*)2);
323 list
->insert_last(list
, (void*)3);
324 list
->insert_last(list
, (void*)4);
325 list
->insert_last(list
, (void*)5);
326 list
->invoke_function(list
, invoke
, (uintptr_t)1, (uintptr_t)2,
327 (uintptr_t)3, (uintptr_t)4, &sum
);
328 ck_assert_int_eq(sum
, 15);
332 START_TEST(test_invoke_offset
)
335 { .val
= 1, .invoke
= invoke_offset
, },
336 { .val
= 2, .invoke
= invoke_offset
, },
337 { .val
= 3, .invoke
= invoke_offset
, },
338 { .val
= 4, .invoke
= invoke_offset
, },
339 { .val
= 5, .invoke
= invoke_offset
, },
343 for (i
= 0; i
< countof(items
); i
++)
345 list
->insert_last(list
, &items
[i
]);
347 list
->invoke_offset(list
, offsetof(invoke_t
, invoke
));
349 while (list
->remove_first(list
, (void**)&item
) == SUCCESS
)
351 ck_assert_int_eq(item
->val
, i
++);
356 /*******************************************************************************
360 typedef struct clone_t clone_t
;
364 void *(*clone
)(clone_t
*item
);
367 static void *clonefn(clone_t
*item
)
372 static void test_clone(linked_list_t
*list
)
377 ck_assert_int_eq(list
->get_count(list
), 5);
378 while (list
->remove_first(list
, (void*)&x
) == SUCCESS
)
380 ck_assert_int_eq(round
, x
);
383 ck_assert_int_eq(round
, 6);
386 START_TEST(test_clone_offset
)
388 linked_list_t
*other
;
390 { .val
= (void*)1, .clone
= clonefn
, },
391 { .val
= (void*)2, .clone
= clonefn
, },
392 { .val
= (void*)3, .clone
= clonefn
, },
393 { .val
= (void*)4, .clone
= clonefn
, },
394 { .val
= (void*)5, .clone
= clonefn
, },
398 for (i
= 0; i
< countof(items
); i
++)
400 list
->insert_last(list
, &items
[i
]);
402 other
= list
->clone_offset(list
, offsetof(clone_t
, clone
));
404 other
->destroy(other
);
409 /*******************************************************************************
413 typedef struct equals_t equals_t
;
417 bool (*equals
)(equals_t
*a
, equals_t
*b
);
420 static bool equalsfn(equals_t
*a
, equals_t
*b
)
422 return a
->val
== b
->val
;
425 START_TEST(test_equals_offset
)
427 linked_list_t
*other
;
428 equals_t
*x
, items
[] = {
429 { .val
= 1, .equals
= equalsfn
, },
430 { .val
= 2, .equals
= equalsfn
, },
431 { .val
= 3, .equals
= equalsfn
, },
432 { .val
= 4, .equals
= equalsfn
, },
433 { .val
= 5, .equals
= equalsfn
, },
437 for (i
= 0; i
< countof(items
); i
++)
439 list
->insert_last(list
, &items
[i
]);
441 ck_assert(list
->equals_offset(list
, list
, offsetof(equals_t
, equals
)));
442 other
= linked_list_create_from_enumerator(list
->create_enumerator(list
));
443 ck_assert(list
->equals_offset(list
, other
, offsetof(equals_t
, equals
)));
444 other
->remove_last(other
, (void**)&x
);
445 ck_assert(!list
->equals_offset(list
, other
, offsetof(equals_t
, equals
)));
446 list
->remove_last(list
, (void**)&x
);
447 ck_assert(list
->equals_offset(list
, other
, offsetof(equals_t
, equals
)));
448 other
->remove_first(other
, (void**)&x
);
449 ck_assert(!list
->equals_offset(list
, other
, offsetof(equals_t
, equals
)));
450 list
->remove_first(list
, (void**)&x
);
451 ck_assert(list
->equals_offset(list
, other
, offsetof(equals_t
, equals
)));
452 while (list
->remove_first(list
, (void**)&x
) == SUCCESS
);
453 while (other
->remove_first(other
, (void**)&x
) == SUCCESS
);
454 ck_assert(list
->equals_offset(list
, other
, offsetof(equals_t
, equals
)));
455 other
->destroy(other
);
459 START_TEST(test_equals_function
)
461 linked_list_t
*other
;
462 equals_t
*x
, items
[] = {
471 for (i
= 0; i
< countof(items
); i
++)
473 list
->insert_last(list
, &items
[i
]);
475 ck_assert(list
->equals_function(list
, list
, (void*)equalsfn
));
476 other
= linked_list_create_from_enumerator(list
->create_enumerator(list
));
477 ck_assert(list
->equals_function(list
, other
, (void*)equalsfn
));
478 other
->remove_last(other
, (void**)&x
);
479 ck_assert(!list
->equals_function(list
, other
, (void*)equalsfn
));
480 list
->remove_last(list
, (void**)&x
);
481 ck_assert(list
->equals_function(list
, other
, (void*)equalsfn
));
482 other
->remove_first(other
, (void**)&x
);
483 ck_assert(!list
->equals_function(list
, other
, (void*)equalsfn
));
484 list
->remove_first(list
, (void**)&x
);
485 ck_assert(list
->equals_function(list
, other
, (void*)equalsfn
));
486 while (list
->remove_first(list
, (void**)&x
) == SUCCESS
);
487 while (other
->remove_first(other
, (void**)&x
) == SUCCESS
);
488 ck_assert(list
->equals_function(list
, other
, (void*)equalsfn
));
489 other
->destroy(other
);
493 Suite
*linked_list_suite_create()
498 s
= suite_create("linked list");
500 tc
= tcase_create("insert/get");
501 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
502 tcase_add_test(tc
, test_insert_first
);
503 tcase_add_test(tc
, test_insert_last
);
504 suite_add_tcase(s
, tc
);
506 tc
= tcase_create("remove");
507 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
508 tcase_add_test(tc
, test_remove_first
);
509 tcase_add_test(tc
, test_remove_last
);
510 tcase_add_test(tc
, test_remove
);
511 tcase_add_test(tc
, test_remove_callback
);
512 suite_add_tcase(s
, tc
);
514 tc
= tcase_create("find");
515 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
516 tcase_add_test(tc
, test_find
);
517 tcase_add_test(tc
, test_find_callback
);
518 tcase_add_test(tc
, test_find_callback_args
);
519 suite_add_tcase(s
, tc
);
521 tc
= tcase_create("invoke");
522 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
523 tcase_add_test(tc
, test_invoke_function
);
524 tcase_add_test(tc
, test_invoke_offset
);
525 suite_add_tcase(s
, tc
);
527 tc
= tcase_create("clone");
528 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
529 tcase_add_test(tc
, test_clone_offset
);
530 suite_add_tcase(s
, tc
);
532 tc
= tcase_create("equals");
533 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
534 tcase_add_test(tc
, test_equals_offset
);
535 tcase_add_test(tc
, test_equals_function
);
536 suite_add_tcase(s
, tc
);