Debian Codegraph @

tests/test_dlinklists.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2008, Steve Murphy
5 *
6 * Steve Murphy <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 *
21 * \brief Doubly-Linked List Tests
22 *
23 * \author\verbatim Steve Murphy <[email protected]> \endverbatim
24 *
25 * This module will run some DLL tests at load time
26 * \ingroup tests
27 */
28
29 /*** MODULEINFO
30 <depend>TEST_FRAMEWORK</depend>
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/app.h"
42
43
44 #include "asterisk/dlinkedlists.h"
45
46 /* Tests for DLLists! We really should, and here is a nice place to do it in asterisk */
47
48 struct test1
49 {
50 char name[10];
51 AST_DLLIST_ENTRY(test1) list;
52 };
53
54 struct test_container
55 {
56 AST_DLLIST_HEAD(entries, test1) entries;
57 int count;
58 };
59
60 static void print_list(struct test_container *x, char *expect)
61 {
62 struct test1 *t1;
63 char buff[1000];
64 buff[0] = 0;
65 AST_DLLIST_TRAVERSE(&x->entries, t1, list) {
66 strcat(buff,t1->name);
67 if (t1 != AST_DLLIST_LAST(&x->entries))
68 strcat(buff," <=> ");
69 }
70
71 ast_debug(1,"Got: %s [expect %s]\n", buff, expect);
72 }
73
74 static void print_list_backwards(struct test_container *x, char *expect)
75 {
76 struct test1 *t1;
77 char buff[1000];
78 buff[0] = 0;
79 AST_DLLIST_TRAVERSE_BACKWARDS(&x->entries, t1, list) {
80 strcat(buff,t1->name);
81 if (t1 != AST_DLLIST_FIRST(&x->entries))
82 strcat(buff," <=> ");
83 }
84
85 ast_debug(1,"Got: %s [expect %s]\n", buff, expect);
86 }
87
88 static struct test_container *make_cont(void)
89 {
90 struct test_container *t = ast_calloc(sizeof(struct test_container),1);
91 return t;
92 }
93
94 static struct test1 *make_test1(char *name)
95 {
96 struct test1 *t1 = ast_calloc(sizeof(struct test1),1);
97 strcpy(t1->name, name);
98 return t1;
99 }
100
101 static void destroy_test_container(struct test_container *x)
102 {
103 /* remove all the test1's */
104 struct test1 *t1;
105 AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&x->entries, t1, list) {
106 AST_DLLIST_REMOVE_CURRENT(list);
107 ast_free(t1);
108 }
109 AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
110 ast_free(x);
111 }
112
113 /* Macros to test:
114 AST_DLLIST_LOCK(head)
115 AST_RWDLLIST_WRLOCK(head)
116 AST_RWDLLIST_WRLOCK(head)
117 AST_RWDLLIST_RDLOCK(head)
118 AST_DLLIST_TRYLOCK(head)
119 AST_RWDLLIST_TRYWRLOCK(head)
120 AST_RWDLLIST_TRYRDLOCK(head)
121 AST_DLLIST_UNLOCK(head)
122 AST_RWDLLIST_UNLOCK(head)
123
124 AST_DLLIST_HEAD(name, type)
125 AST_RWDLLIST_HEAD(name, type)
126 AST_DLLIST_HEAD_NOLOCK(name, type)
127 AST_DLLIST_HEAD_STATIC(name, type)
128 AST_RWDLLIST_HEAD_STATIC(name, type)
129 AST_DLLIST_HEAD_NOLOCK_STATIC(name, type)
130 AST_DLLIST_HEAD_SET(head, entry)
131 AST_RWDLLIST_HEAD_SET(head, entry)
132 AST_DLLIST_HEAD_SET_NOLOCK(head, entry)
133 AST_DLLIST_HEAD_INIT(head)
134 AST_RWDLLIST_HEAD_INIT(head)
135 AST_DLLIST_HEAD_INIT_NOLOCK(head)
136
137 AST_RWDLLIST_HEAD_DESTROY(head)
138
139 AST_DLLIST_ENTRY(type)
140
141 --- the above not going to be dealt with here ---
142
143 AST_DLLIST_INSERT_HEAD(head, elm, field)
144 AST_DLLIST_TRAVERSE(head,var,field)
145 AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(head, var, field)
146 AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END
147 AST_DLLIST_FIRST(head)
148 AST_DLLIST_LAST(head)
149 AST_DLLIST_NEXT(elm, field)
150 AST_DLLIST_PREV(elm, field)
151 AST_DLLIST_EMPTY(head)
152 AST_DLLIST_TRAVERSE_BACKWARDS(head,var,field)
153 AST_DLLIST_INSERT_AFTER(head, listelm, elm, field)
154 AST_DLLIST_INSERT_TAIL(head, elm, field)
155 AST_DLLIST_REMOVE_HEAD(head, field)
156 AST_DLLIST_REMOVE(head, elm, field)
157 AST_DLLIST_TRAVERSE_SAFE_BEGIN(head, var, field)
158 AST_DLLIST_TRAVERSE_SAFE_END
159 AST_DLLIST_REMOVE_CURRENT(field)
160 AST_DLLIST_MOVE_CURRENT(newhead, field)
161 AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field)
162
163
164 AST_DLLIST_MOVE_CURRENT_BACKWARDS(newhead, field)
165 AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(elm, field)
166 AST_DLLIST_HEAD_DESTROY(head)
167
168 AST_DLLIST_APPEND_DLLIST(head, list, field)
169
170 */
171
172 static void dll_tests(void)
173 {
174 struct test_container *tc;
175 struct test1 *a;
176 struct test1 *b;
177 struct test1 *c;
178 struct test1 *d;
179 struct test1 *e;
180
181 ast_debug(1,"Test AST_DLLIST_INSERT_HEAD, AST_DLLIST_TRAVERSE, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END\n");
182 tc = make_cont();
183 a = make_test1("A");
184 b = make_test1("B");
185 c = make_test1("C");
186 d = make_test1("D");
187 AST_DLLIST_INSERT_HEAD(&tc->entries, d, list);
188 AST_DLLIST_INSERT_HEAD(&tc->entries, c, list);
189 AST_DLLIST_INSERT_HEAD(&tc->entries, b, list);
190 AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
191 print_list(tc, "A <=> B <=> C <=> D");
192
193 destroy_test_container(tc);
194
195 tc = make_cont();
196
197 if (AST_DLLIST_EMPTY(&tc->entries))
198 ast_debug(1,"Test AST_DLLIST_EMPTY....OK\n");
199 else
200 ast_log(LOG_ERROR,"Test AST_DLLIST_EMPTY....PROBLEM!!\n");
201
202
203 a = make_test1("A");
204 b = make_test1("B");
205 c = make_test1("C");
206 d = make_test1("D");
207
208 ast_debug(1,"Test AST_DLLIST_INSERT_TAIL\n");
209 AST_DLLIST_INSERT_TAIL(&tc->entries, a, list);
210 AST_DLLIST_INSERT_TAIL(&tc->entries, b, list);
211 AST_DLLIST_INSERT_TAIL(&tc->entries, c, list);
212 AST_DLLIST_INSERT_TAIL(&tc->entries, d, list);
213 print_list(tc, "A <=> B <=> C <=> D");
214
215 if (AST_DLLIST_FIRST(&tc->entries) == a)
216 ast_debug(1,"Test AST_DLLIST_FIRST....OK\n");
217 else
218 ast_log(LOG_ERROR,"Test AST_DLLIST_FIRST....PROBLEM\n");
219
220 if (AST_DLLIST_LAST(&tc->entries) == d)
221 ast_debug(1,"Test AST_DLLIST_LAST....OK\n");
222 else
223 ast_log(LOG_ERROR,"Test AST_DLLIST_LAST....PROBLEM\n");
224
225 if (AST_DLLIST_NEXT(a,list) == b)
226 ast_debug(1,"Test AST_DLLIST_NEXT....OK\n");
227 else
228 ast_log(LOG_ERROR,"Test AST_DLLIST_NEXT....PROBLEM\n");
229
230 if (AST_DLLIST_PREV(d,list) == c)
231 ast_debug(1,"Test AST_DLLIST_PREV....OK\n");
232 else
233 ast_log(LOG_ERROR,"Test AST_DLLIST_PREV....PROBLEM\n");
234
235 destroy_test_container(tc);
236
237 tc = make_cont();
238
239 a = make_test1("A");
240 b = make_test1("B");
241 c = make_test1("C");
242 d = make_test1("D");
243
244 ast_debug(1,"Test AST_DLLIST_INSERT_AFTER, AST_DLLIST_TRAVERSE_BACKWARDS\n");
245 AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
246 AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
247 AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
248 AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
249 print_list_backwards(tc, "D <=> C <=> B <=> A");
250
251 ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD\n");
252 AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
253 print_list_backwards(tc, "D <=> C <=> B");
254 ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD\n");
255 AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
256 print_list_backwards(tc, "D <=> C");
257 ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD\n");
258 AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
259 print_list_backwards(tc, "D");
260 AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
261
262 if (AST_DLLIST_EMPTY(&tc->entries))
263 ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD....OK\n");
264 else
265 ast_log(LOG_ERROR,"Test AST_DLLIST_REMOVE_HEAD....PROBLEM!!\n");
266
267 AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
268 AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
269 AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
270 AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
271
272 ast_debug(1,"Test AST_DLLIST_REMOVE\n");
273 AST_DLLIST_REMOVE(&tc->entries, c, list);
274 print_list(tc, "A <=> B <=> D");
275 AST_DLLIST_REMOVE(&tc->entries, a, list);
276 print_list(tc, "B <=> D");
277 AST_DLLIST_REMOVE(&tc->entries, d, list);
278 print_list(tc, "B");
279 AST_DLLIST_REMOVE(&tc->entries, b, list);
280
281 if (AST_DLLIST_EMPTY(&tc->entries))
282 ast_debug(1,"Test AST_DLLIST_REMOVE....OK\n");
283 else
284 ast_log(LOG_ERROR,"Test AST_DLLIST_REMOVE....PROBLEM!!\n");
285
286 AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
287 AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
288 AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
289 AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
290
291 AST_DLLIST_TRAVERSE_SAFE_BEGIN(&tc->entries, e, list) {
292 AST_DLLIST_REMOVE_CURRENT(list);
293 }
294 AST_DLLIST_TRAVERSE_SAFE_END;
295 if (AST_DLLIST_EMPTY(&tc->entries))
296 ast_debug(1,"Test AST_DLLIST_REMOVE_CURRENT... OK\n");
297 else
298 ast_log(LOG_ERROR,"Test AST_DLLIST_REMOVE_CURRENT... PROBLEM\n");
299
300 ast_debug(1,"Test AST_DLLIST_MOVE_CURRENT, AST_DLLIST_INSERT_BEFORE_CURRENT\n");
301 AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
302 AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
303 AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
304 AST_DLLIST_TRAVERSE_SAFE_BEGIN(&tc->entries, e, list) {
305 if (e == a) {
306 AST_DLLIST_INSERT_BEFORE_CURRENT(d, list); /* D A B C */
307 }
308
309 if (e == b) {
310 AST_DLLIST_MOVE_CURRENT(&tc->entries, list); /* D A C B */
311 }
312
313 }
314 AST_DLLIST_TRAVERSE_SAFE_END;
315 print_list(tc, "D <=> A <=> C <=> B");
316
317 destroy_test_container(tc);
318
319 tc = make_cont();
320
321 a = make_test1("A");
322 b = make_test1("B");
323 c = make_test1("C");
324 d = make_test1("D");
325
326 ast_debug(1,"Test: AST_DLLIST_MOVE_CURRENT_BACKWARDS and AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS\n");
327 AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
328 AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
329 AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
330 AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&tc->entries, e, list) {
331 if (e == c && AST_DLLIST_FIRST(&tc->entries) != c) {
332 AST_DLLIST_MOVE_CURRENT_BACKWARDS(&tc->entries, list); /* C A B */
333 print_list(tc, "C <=> A <=> B");
334 }
335
336 if (e == b) {
337 AST_DLLIST_REMOVE_CURRENT(list); /* C A */
338 ast_free(b);
339 print_list(tc, "C <=> A");
340 }
341 if (e == a) {
342 AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(d, list); /* C A D */
343 print_list(tc, "C <=> A <=> D");
344 }
345
346 }
347 AST_DLLIST_TRAVERSE_SAFE_END;
348 print_list(tc, "C <=> A <=> D");
349
350 destroy_test_container(tc);
351 }
352
353 static int unload_module(void)
354 {
355 return 0;
356 }
357
358 static int load_module(void)
359 {
360 dll_tests();
361 return AST_MODULE_LOAD_SUCCESS;
362 }
363
364 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test Doubly-Linked Lists");