aa6d1d6004a6f55663b4b00da1714daf0ee984cd
[strongswan.git] / Source / charon / testcases / linked_list_test.c
1 /**
2 * @file linked_list_test.c
3 *
4 * @brief Tests for the linked_list_t class.
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 "linked_list_test.h"
26
27 #include <utils/linked_list.h>
28
29 /*
30 * Description in header-file
31 */
32 void test_linked_list(tester_t *tester)
33 {
34 void *test_value = NULL;
35
36 linked_list_t *linked_list = linked_list_create();
37
38 tester->assert_true(tester,(linked_list->get_count(linked_list) == 0), "count check");
39
40 linked_list->insert_first(linked_list,"one");
41 tester->assert_true(tester,(linked_list->get_count(linked_list) == 1), "count check");
42
43 linked_list->insert_first(linked_list,"two");
44 tester->assert_true(tester,(linked_list->get_count(linked_list) == 2), "count check");
45
46 linked_list->insert_first(linked_list,"three");
47 tester->assert_true(tester,(linked_list->get_count(linked_list) == 3), "count check");
48
49 linked_list->insert_first(linked_list,"four");
50 tester->assert_true(tester,(linked_list->get_count(linked_list) == 4), "count check");
51
52 linked_list->insert_first(linked_list,"five");
53 tester->assert_true(tester,(linked_list->get_count(linked_list) == 5), "count check");
54
55 tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
56 tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "get_first value check");
57 tester->assert_true(tester,(linked_list->get_count(linked_list) == 5), "count check");
58
59 tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
60 tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check");
61 tester->assert_true(tester,( linked_list->get_count(linked_list) == 5), "count check");
62
63 tester->assert_true(tester,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check");
64 tester->assert_true(tester,(strcmp((char *) test_value,"five") == 0), "remove_first value check");
65 tester->assert_true(tester,(linked_list->get_count(linked_list) == 4), "count check");
66
67 tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
68 tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
69 tester->assert_true(tester,(linked_list->get_count(linked_list) == 4), "count check");
70
71 tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
72 tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "get_last value check");
73 tester->assert_true(tester,(linked_list->get_count(linked_list) == 4), "count check");
74
75 tester->assert_true(tester,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check");
76 tester->assert_true(tester,(strcmp((char *) test_value,"one") == 0), "remove_last value check");
77 tester->assert_true(tester,(linked_list->get_count(linked_list) == 3), "count check");
78
79 tester->assert_true(tester,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
80 tester->assert_true(tester,(strcmp((char *) test_value,"two") == 0), "get_last value check");
81 tester->assert_true(tester,(linked_list->get_count(linked_list) == 3), "count check");
82
83 tester->assert_true(tester,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
84 tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
85 tester->assert_true(tester,(linked_list->get_count(linked_list) == 3), "count check");
86
87 linked_list->destroy(linked_list);
88 }
89
90 /*
91 * Description in header-file
92 */
93 void test_linked_list_iterator(tester_t *tester)
94 {
95 void * value;
96
97 linked_list_t *linked_list = linked_list_create();
98 linked_list->insert_first(linked_list,"one");
99 linked_list->insert_first(linked_list,"two");
100 linked_list->insert_first(linked_list,"three");
101 linked_list->insert_first(linked_list,"four");
102 linked_list->insert_first(linked_list,"five");
103
104 iterator_t * iterator;
105 iterator_t * iterator2;
106
107
108 linked_list->create_iterator(linked_list,&iterator,TRUE);
109
110 tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
111 iterator->current(iterator,&value);
112 tester->assert_true(tester,(strcmp((char *) value,"five") == 0), "it 1 current value check");
113
114 tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
115 iterator->current(iterator,&value);
116 tester->assert_true(tester,(strcmp((char *) value,"four") == 0), "it 1 current value check");
117
118 linked_list->create_iterator(linked_list,&iterator2,FALSE);
119
120 tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
121 iterator2->current(iterator2,&value);
122 tester->assert_true(tester,(strcmp((char *) value,"one") == 0), "it 2 current value check");
123
124 tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
125 iterator->current(iterator,&value);
126 tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "it 1 current value check");
127
128 tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
129 iterator2->current(iterator2,&value);
130 tester->assert_true(tester,(strcmp((char *) value,"two") == 0), "it 2 current value check");
131
132 tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
133 iterator->current(iterator,&value);
134 tester->assert_true(tester,(strcmp((char *) value,"two") == 0), "it 1 current value check");
135
136 tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
137 iterator2->current(iterator2,&value);
138 tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "it 2 current value check");
139
140 tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
141 iterator->current(iterator,&value);
142 tester->assert_true(tester,(strcmp((char *) value,"one") == 0), "it 1 current value check");
143
144 tester->assert_false(tester,iterator->has_next(iterator), "it 1 has_next value check");
145
146 tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
147 tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
148 tester->assert_false(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
149
150 iterator->destroy(iterator);
151 iterator2->destroy(iterator2);
152 linked_list->destroy(linked_list);
153 }
154
155 /*
156 * Description in header-file
157 */
158 void test_linked_list_insert_and_remove(tester_t *tester)
159 {
160 void *value;
161 iterator_t * iterator;
162
163 linked_list_t *linked_list = linked_list_create();
164 linked_list->insert_first(linked_list,"one");
165 linked_list->insert_first(linked_list,"two");
166
167 linked_list->insert_first(linked_list,"three");
168 linked_list->insert_first(linked_list,"four");
169 linked_list->insert_first(linked_list,"five");
170
171
172
173 linked_list->create_iterator(linked_list,&iterator,TRUE);
174
175 iterator->has_next(iterator);
176 iterator->has_next(iterator);
177 iterator->has_next(iterator);
178 iterator->current(iterator,&value);
179 tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
180
181 iterator->insert_before(iterator,"before_three");
182 iterator->current(iterator,&value);
183 tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
184
185
186 iterator->insert_after(iterator,"after_three");
187 iterator->current(iterator,&value);
188 tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
189
190
191 tester->assert_true(tester,(iterator->remove(iterator) == SUCCESS), "remove call check");
192 iterator->current(iterator,&value);
193 tester->assert_true(tester,(strcmp((char *) value,"before_three") == 0), "current value check");
194
195 iterator->reset(iterator);
196
197 iterator->has_next(iterator);
198 iterator->has_next(iterator);
199 iterator->has_next(iterator);
200 iterator->current(iterator,&value);
201 tester->assert_true(tester,(strcmp((char *) value,"before_three") == 0), "current value check");
202 iterator->has_next(iterator);
203 iterator->current(iterator,&value);
204 tester->assert_true(tester,(strcmp((char *) value,"after_three") == 0), "current value check");
205
206 iterator->destroy(iterator);
207
208 linked_list->destroy(linked_list);
209 }