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