test_dlinklists.c 9.7 KB

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