]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/tests/test_linked_list.c
9e85c58d8c3ca3daf35419b48a2a6ddff4770cf2
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "test_suite.h"
18 #include <collections/linked_list.h>
20 /*******************************************************************************
24 static linked_list_t
*list
;
26 START_SETUP(setup_list
)
30 list
= linked_list_create();
31 ck_assert_int_eq(list
->get_count(list
), 0);
32 ck_assert(list
->get_first(list
, &x
) == NOT_FOUND
);
33 ck_assert(list
->get_last(list
, &x
) == NOT_FOUND
);
37 START_TEARDOWN(teardown_list
)
43 /*******************************************************************************
47 START_TEST(test_insert_first
)
49 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
51 list
->insert_first(list
, a
);
52 ck_assert_int_eq(list
->get_count(list
), 1);
53 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
55 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
58 list
->insert_first(list
, b
);
59 ck_assert_int_eq(list
->get_count(list
), 2);
60 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
62 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
67 START_TEST(test_insert_last
)
69 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
71 list
->insert_last(list
, a
);
72 ck_assert_int_eq(list
->get_count(list
), 1);
73 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
75 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
78 list
->insert_last(list
, b
);
79 ck_assert_int_eq(list
->get_count(list
), 2);
80 ck_assert(list
->get_first(list
, &x
) == SUCCESS
);
82 ck_assert(list
->get_last(list
, &x
) == SUCCESS
);
87 /*******************************************************************************
91 START_TEST(test_remove_first
)
93 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
95 list
->insert_first(list
, a
);
96 list
->insert_first(list
, b
);
97 ck_assert(list
->remove_first(list
, &x
) == SUCCESS
);
98 ck_assert_int_eq(list
->get_count(list
), 1);
100 ck_assert(list
->remove_first(list
, &x
) == SUCCESS
);
101 ck_assert_int_eq(list
->get_count(list
), 0);
103 ck_assert(list
->remove_first(list
, &x
) == NOT_FOUND
);
104 ck_assert(list
->remove_last(list
, &x
) == NOT_FOUND
);
108 START_TEST(test_remove_last
)
110 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
112 list
->insert_first(list
, a
);
113 list
->insert_first(list
, b
);
114 ck_assert(list
->remove_last(list
, &x
) == SUCCESS
);
115 ck_assert_int_eq(list
->get_count(list
), 1);
117 ck_assert(list
->remove_last(list
, &x
) == SUCCESS
);
118 ck_assert_int_eq(list
->get_count(list
), 0);
120 ck_assert(list
->remove_first(list
, &x
) == NOT_FOUND
);
121 ck_assert(list
->remove_last(list
, &x
) == NOT_FOUND
);
125 /*******************************************************************************
126 * helper function for remove and find tests
129 static bool match_a(void *item
, void *a
)
131 ck_assert(a
== (void*)1);
135 static bool match_b(void *item
, void *b
)
137 ck_assert(b
== (void*)2);
141 /*******************************************************************************
145 START_TEST(test_remove
)
147 void *a
= (void*)1, *b
= (void*)2;
149 list
->insert_first(list
, a
);
150 ck_assert(list
->remove(list
, a
, NULL
) == 1);
151 ck_assert_int_eq(list
->get_count(list
), 0);
153 list
->insert_last(list
, a
);
154 list
->insert_last(list
, a
);
155 list
->insert_last(list
, a
);
156 list
->insert_last(list
, b
);
157 ck_assert(list
->remove(list
, a
, NULL
) == 3);
158 ck_assert(list
->remove(list
, a
, NULL
) == 0);
159 ck_assert_int_eq(list
->get_count(list
), 1);
160 ck_assert(list
->remove(list
, b
, NULL
) == 1);
161 ck_assert(list
->remove(list
, b
, NULL
) == 0);
165 START_TEST(test_remove_callback
)
167 void *a
= (void*)1, *b
= (void*)2;
169 list
->insert_last(list
, a
);
170 list
->insert_last(list
, b
);
171 list
->insert_last(list
, a
);
172 list
->insert_last(list
, b
);
173 ck_assert(list
->remove(list
, a
, match_a
) == 2);
174 ck_assert(list
->remove(list
, a
, match_a
) == 0);
175 ck_assert_int_eq(list
->get_count(list
), 2);
176 ck_assert(list
->remove(list
, b
, match_b
) == 2);
177 ck_assert(list
->remove(list
, b
, match_b
) == 0);
178 ck_assert_int_eq(list
->get_count(list
), 0);
182 /*******************************************************************************
186 static bool match_a_b(void *item
, void *a
, void *b
)
188 ck_assert(a
== (void*)1);
189 ck_assert(b
== (void*)2);
190 return item
== a
|| item
== b
;
193 START_TEST(test_find
)
195 void *a
= (void*)1, *b
= (void*)2;
197 ck_assert(list
->find_first(list
, NULL
, &a
) == NOT_FOUND
);
198 list
->insert_last(list
, a
);
199 ck_assert(list
->find_first(list
, NULL
, &a
) == SUCCESS
);
200 ck_assert(list
->find_first(list
, NULL
, &b
) == NOT_FOUND
);
201 list
->insert_last(list
, b
);
202 ck_assert(list
->find_first(list
, NULL
, &a
) == SUCCESS
);
203 ck_assert(list
->find_first(list
, NULL
, &b
) == SUCCESS
);
205 ck_assert(list
->find_first(list
, NULL
, NULL
) == NOT_FOUND
);
209 START_TEST(test_find_callback
)
211 void *a
= (void*)1, *b
= (void*)2, *x
= NULL
;
213 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_a_b
, &x
, a
, b
) == NOT_FOUND
);
214 list
->insert_last(list
, a
);
215 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_a
, NULL
, a
) == SUCCESS
);
217 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_a
, &x
, a
) == SUCCESS
);
219 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_b
, &x
, b
) == NOT_FOUND
);
222 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_a_b
, &x
, a
, b
) == SUCCESS
);
225 list
->insert_last(list
, b
);
226 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_a
, &x
, a
) == SUCCESS
);
228 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_b
, &x
, b
) == SUCCESS
);
231 ck_assert(list
->find_first(list
, (linked_list_match_t
)match_a_b
, &x
, a
, b
) == SUCCESS
);
236 /*******************************************************************************
240 typedef struct invoke_t invoke_t
;
244 void (*invoke
)(invoke_t
*item
, void *a
, void *b
, void *c
, void *d
, int *sum
);
247 static void invoke(intptr_t item
, void *a
, void *b
, void *c
, void *d
, int *sum
)
249 ck_assert(a
== (void*)1);
250 ck_assert(b
== (void*)2);
251 ck_assert(c
== (void*)3);
252 ck_assert(d
== (void*)4);
256 static void invoke_offset(invoke_t
*item
, void *a
, void *b
, void *c
, void *d
, int *sum
)
258 invoke(item
->val
, a
, b
, c
, d
, sum
);
261 START_TEST(test_invoke_function
)
265 list
->insert_last(list
, (void*)1);
266 list
->insert_last(list
, (void*)2);
267 list
->insert_last(list
, (void*)3);
268 list
->insert_last(list
, (void*)4);
269 list
->insert_last(list
, (void*)5);
270 list
->invoke_function(list
, (linked_list_invoke_t
)invoke
, 1, 2, 3, 4, &sum
);
271 ck_assert_int_eq(sum
, 15);
275 START_TEST(test_invoke_offset
)
278 { .val
= 1, .invoke
= invoke_offset
, },
279 { .val
= 2, .invoke
= invoke_offset
, },
280 { .val
= 3, .invoke
= invoke_offset
, },
281 { .val
= 4, .invoke
= invoke_offset
, },
282 { .val
= 5, .invoke
= invoke_offset
, },
286 for (i
= 0; i
< countof(items
); i
++)
288 list
->insert_last(list
, &items
[i
]);
290 list
->invoke_offset(list
, offsetof(invoke_t
, invoke
), 1, 2, 3, 4, &sum
);
291 ck_assert_int_eq(sum
, 15);
295 /*******************************************************************************
299 typedef struct clone_t clone_t
;
303 void *(*clone
)(clone_t
*item
);
306 static void *clone(clone_t
*item
)
311 static void test_clone(linked_list_t
*list
)
316 ck_assert_int_eq(list
->get_count(list
), 5);
317 while (list
->remove_first(list
, (void*)&x
) == SUCCESS
)
319 ck_assert_int_eq(round
, x
);
322 ck_assert_int_eq(round
, 6);
325 START_TEST(test_clone_offset
)
327 linked_list_t
*other
;
329 { .val
= (void*)1, .clone
= clone
, },
330 { .val
= (void*)2, .clone
= clone
, },
331 { .val
= (void*)3, .clone
= clone
, },
332 { .val
= (void*)4, .clone
= clone
, },
333 { .val
= (void*)5, .clone
= clone
, },
337 for (i
= 0; i
< countof(items
); i
++)
339 list
->insert_last(list
, &items
[i
]);
341 other
= list
->clone_offset(list
, offsetof(clone_t
, clone
));
343 other
->destroy(other
);
347 Suite
*linked_list_suite_create()
352 s
= suite_create("linked list");
354 tc
= tcase_create("insert/get");
355 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
356 tcase_add_test(tc
, test_insert_first
);
357 tcase_add_test(tc
, test_insert_last
);
358 suite_add_tcase(s
, tc
);
360 tc
= tcase_create("remove");
361 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
362 tcase_add_test(tc
, test_remove_first
);
363 tcase_add_test(tc
, test_remove_last
);
364 tcase_add_test(tc
, test_remove
);
365 tcase_add_test(tc
, test_remove_callback
);
366 suite_add_tcase(s
, tc
);
368 tc
= tcase_create("find");
369 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
370 tcase_add_test(tc
, test_find
);
371 tcase_add_test(tc
, test_find_callback
);
372 suite_add_tcase(s
, tc
);
374 tc
= tcase_create("invoke");
375 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
376 tcase_add_test(tc
, test_invoke_function
);
377 tcase_add_test(tc
, test_invoke_offset
);
378 suite_add_tcase(s
, tc
);
380 tc
= tcase_create("clone");
381 tcase_add_checked_fixture(tc
, setup_list
, teardown_list
);
382 tcase_add_test(tc
, test_clone_offset
);
383 suite_add_tcase(s
, tc
);