- iterator for linked list implemented
[strongswan.git] / Source / charon / tests / linked_list_test.c
1 /**
2 * @file linked_list_test.c
3 *
4 * @brief Tests to test the Linked List type linked_list_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "../tester.h"
26 #include "../linked_list.h"
27
28 /*
29 * Description in header-file
30 */
31 void test_linked_list(tester_t *tester)
32 {
33 void *test_value = NULL;
34 int count;
35
36 linked_list_t *linked_list = linked_list_create();
37 linked_list->get_count(linked_list,&count);
38 tester->assert_true(tester,(count == 0), "count check");
39
40 linked_list->insert_first(linked_list,"one");
41 linked_list->get_count(linked_list,&count);
42 tester->assert_true(tester,(count == 1), "count check");
43
44 linked_list->insert_first(linked_list,"two");
45 linked_list->get_count(linked_list,&count);
46 tester->assert_true(tester,(count == 2), "count check");
47
48 linked_list->insert_first(linked_list,"three");
49 linked_list->get_count(linked_list,&count);
50 tester->assert_true(tester,(count == 3), "count check");
51
52 linked_list->insert_first(linked_list,"four");
53 linked_list->get_count(linked_list,&count);
54 tester->assert_true(tester,(count == 4), "count check");
55
56 linked_list->insert_first(linked_list,"five");
57 linked_list->get_count(linked_list,&count);
58 tester->assert_true(tester,(count == 5), "count check");
59
60 tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
61 tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "get_first value check");
62 linked_list->get_count(linked_list,&count);
63 tester->assert_true(tester,(count == 5), "count check");
64
65 tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
66 tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check");
67 linked_list->get_count(linked_list,&count);
68 tester->assert_true(tester,(count == 5), "count check");
69
70 tester->assert_true(tester,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check");
71 tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "remove_first value check");
72 linked_list->get_count(linked_list,&count);
73 tester->assert_true(tester,(count == 4), "count check");
74
75 tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
76 tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
77 linked_list->get_count(linked_list,&count);
78 tester->assert_true(tester,(count == 4), "count check");
79
80 tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
81 tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check");
82 linked_list->get_count(linked_list,&count);
83 tester->assert_true(tester,(count == 4), "count check");
84
85 tester->assert_true(tester,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check");
86 tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "remove_last value check");
87 linked_list->get_count(linked_list,&count);
88 tester->assert_true(tester,(count == 3), "count check");
89
90 tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
91 tester->assert_true(tester,(strcmp((char *) test_value,"two") == 0), "get_last value check");
92 linked_list->get_count(linked_list,&count);
93 tester->assert_true(tester,(count == 3), "count check");
94
95 tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
96 tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
97 linked_list->get_count(linked_list,&count);
98 tester->assert_true(tester,(count == 3), "count check");
99
100 tester->assert_true(tester,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check");
101 }
102
103 /*
104 * Description in header-file
105 */
106 void test_linked_list_forward_iterator(tester_t *tester)
107 {
108 bool has_next;
109 linked_list_element_t * element;
110
111 linked_list_t *linked_list = linked_list_create();
112 linked_list->insert_first(linked_list,"one");
113 linked_list->insert_first(linked_list,"two");
114 linked_list->insert_first(linked_list,"three");
115 linked_list->insert_first(linked_list,"four");
116 linked_list->insert_first(linked_list,"five");
117
118 linked_list_iterator_t * iterator;
119
120
121 tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator,TRUE) == SUCCESS), "create_iterator call check");
122
123 iterator->has_next(iterator,&has_next);
124 tester->assert_true(tester,has_next, "has_next value check");
125 iterator->current(iterator,&element);
126 tester->assert_true(tester,(strcmp((char *) element->value,"five") == 0), "current value check");
127
128 iterator->has_next(iterator,&has_next);
129 tester->assert_true(tester,has_next, "has_next value check");
130 iterator->current(iterator,&element);
131 tester->assert_true(tester,(strcmp((char *) element->value,"four") == 0), "current value check");
132
133 iterator->has_next(iterator,&has_next);
134 tester->assert_true(tester,has_next, "has_next value check");
135 iterator->current(iterator,&element);
136 tester->assert_true(tester,(strcmp((char *) element->value,"three") == 0), "current value check");
137
138 iterator->has_next(iterator,&has_next);
139 tester->assert_true(tester,has_next, "has_next value check");
140 iterator->current(iterator,&element);
141 tester->assert_true(tester,(strcmp((char *) element->value,"two") == 0), "current value check");
142
143 iterator->has_next(iterator,&has_next);
144 tester->assert_true(tester,has_next, "has_next value check");
145 iterator->current(iterator,&element);
146 tester->assert_true(tester,(strcmp((char *) element->value,"one") == 0), "current value check");
147
148 iterator->has_next(iterator,&has_next);
149 tester->assert_false(tester,has_next, "has_next value check");
150
151 tester->assert_true(tester,(iterator->destroy(iterator) == SUCCESS), "destroy call check");
152
153 linked_list->destroy(linked_list);
154 }