test_linkedlists.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Terry Wilson
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Linked List Tests
  19. *
  20. * \author Terry Wilson <twilson@digium.com>
  21. *
  22. * \ingroup tests
  23. */
  24. /*** MODULEINFO
  25. <depend>TEST_FRAMEWORK</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/module.h"
  31. #include "asterisk/test.h"
  32. #include "asterisk/strings.h"
  33. #include "asterisk/logger.h"
  34. #include "asterisk/linkedlists.h"
  35. #include "asterisk/dlinkedlists.h"
  36. struct test_val {
  37. const char *name;
  38. AST_LIST_ENTRY(test_val) list;
  39. AST_DLLIST_ENTRY(test_val) dbl_list;
  40. };
  41. static struct test_val a = { "A" };
  42. static struct test_val b = { "B" };
  43. static struct test_val c = { "C" };
  44. static struct test_val d = { "D" };
  45. AST_LIST_HEAD_NOLOCK(test_llist, test_val);
  46. AST_DLLIST_HEAD_NOLOCK(test_dbl_llist, test_val);
  47. static int list_expect(struct test_llist *test_list, const char *expect, struct ast_str **buf)
  48. {
  49. struct test_val *i;
  50. ast_str_reset(*buf);
  51. AST_LIST_TRAVERSE(test_list, i, list) {
  52. ast_str_append(buf, 0, "%s", i->name);
  53. }
  54. return strcmp(expect, ast_str_buffer(*buf));
  55. }
  56. static int dbl_list_expect_forward(struct test_dbl_llist *test_list, const char *expect, struct ast_str **buf)
  57. {
  58. struct test_val *i;
  59. ast_str_reset(*buf);
  60. AST_DLLIST_TRAVERSE(test_list, i, dbl_list) {
  61. ast_str_append(buf, 0, "%s", i->name);
  62. }
  63. return strcmp(expect, ast_str_buffer(*buf));
  64. }
  65. static int dbl_list_expect_reverse(struct test_dbl_llist *test_list, const char *expect, struct ast_str **buf)
  66. {
  67. struct test_val *i;
  68. char *str;
  69. int len = strlen(expect);
  70. int idx;
  71. ast_str_reset(*buf);
  72. AST_DLLIST_TRAVERSE_BACKWARDS(test_list, i, dbl_list) {
  73. ast_str_append(buf, 0, "%s", i->name);
  74. }
  75. /* Check reverse string. */
  76. str = ast_str_buffer(*buf);
  77. if (len != strlen(str)) {
  78. return 1;
  79. }
  80. for (idx = 0; idx < len; ++idx) {
  81. if (expect[idx] != str[len - idx - 1]) {
  82. return 1;
  83. }
  84. }
  85. return 0;
  86. }
  87. #define MATCH_OR_FAIL(list, val, retbuf) \
  88. if (list_expect(list, val, &retbuf)) { \
  89. ast_test_status_update(test, "Expected: %s, Got: %s\n", val, ast_str_buffer(retbuf)); \
  90. return AST_TEST_FAIL; \
  91. }
  92. #define MATCH_OR_FAIL_DBL(list, val, retbuf) \
  93. if (dbl_list_expect_forward(list, val, &retbuf)) { \
  94. ast_test_status_update(test, "Expected: %s, Got: %s\n", val, ast_str_buffer(retbuf)); \
  95. return AST_TEST_FAIL; \
  96. } \
  97. if (dbl_list_expect_reverse(list, val, &retbuf)) { \
  98. ast_test_status_update(test, "Expected reverse of: %s, Got: %s\n", val, ast_str_buffer(retbuf)); \
  99. return AST_TEST_FAIL; \
  100. }
  101. #define ELEM_OR_FAIL(x,y) \
  102. if ((x) != (y)) { \
  103. ast_test_status_update(test, "Expected: %s, Got: %s\n", (x)->name, (y)->name); \
  104. return AST_TEST_FAIL; \
  105. }
  106. AST_TEST_DEFINE(single_ll_tests)
  107. {
  108. RAII_VAR(struct ast_str *, buf, NULL, ast_free);
  109. struct test_llist test_list = { 0, };
  110. struct test_llist other_list = { 0, };
  111. struct test_val *bogus;
  112. switch (cmd) {
  113. case TEST_INIT:
  114. info->name = "ll_tests";
  115. info->category = "/main/linkedlists/";
  116. info->summary = "single linked list unit test";
  117. info->description =
  118. "Test the single linked list API";
  119. return AST_TEST_NOT_RUN;
  120. case TEST_EXECUTE:
  121. break;
  122. }
  123. if (!(buf = ast_str_create(16))) {
  124. return AST_TEST_FAIL;
  125. }
  126. if (!(bogus = ast_alloca(sizeof(*bogus)))) {
  127. return AST_TEST_FAIL;
  128. }
  129. if (AST_LIST_REMOVE(&test_list, bogus, list)) {
  130. ast_test_status_update(test, "AST_LIST_REMOVE should safely return NULL for missing element from empty list\n");
  131. return AST_TEST_FAIL;
  132. }
  133. /* INSERT_HEAD and REMOVE_HEAD tests */
  134. AST_LIST_INSERT_HEAD(&test_list, &a, list);
  135. MATCH_OR_FAIL(&test_list, "A", buf);
  136. AST_LIST_INSERT_HEAD(&test_list, &b, list);
  137. MATCH_OR_FAIL(&test_list, "BA", buf);
  138. AST_LIST_REMOVE_HEAD(&test_list, list);
  139. MATCH_OR_FAIL(&test_list, "A", buf);
  140. AST_LIST_REMOVE_HEAD(&test_list, list);
  141. MATCH_OR_FAIL(&test_list, "", buf);
  142. if (AST_LIST_REMOVE_HEAD(&test_list, list)) {
  143. ast_test_status_update(test, "Somehow removed an item from the head of a list that didn't exist\n");
  144. return AST_TEST_FAIL;
  145. }
  146. MATCH_OR_FAIL(&test_list, "", buf);
  147. /* Check empty list test */
  148. if (!AST_LIST_EMPTY(&test_list)) {
  149. ast_test_status_update(test, "List should be empty\n");
  150. return AST_TEST_FAIL;
  151. }
  152. /* Insert tail and remove specific item tests. */
  153. AST_LIST_INSERT_TAIL(&test_list, &a, list);
  154. MATCH_OR_FAIL(&test_list, "A", buf);
  155. AST_LIST_INSERT_TAIL(&test_list, &b, list);
  156. MATCH_OR_FAIL(&test_list, "AB", buf);
  157. AST_LIST_INSERT_TAIL(&test_list, &c, list);
  158. MATCH_OR_FAIL(&test_list, "ABC", buf);
  159. AST_LIST_INSERT_TAIL(&test_list, &d, list);
  160. MATCH_OR_FAIL(&test_list, "ABCD", buf);
  161. if (AST_LIST_REMOVE(&test_list, bogus, list)) {
  162. ast_test_status_update(test, "AST_LIST_REMOVE should safely return NULL for missing element\n");
  163. return AST_TEST_FAIL;
  164. }
  165. bogus = NULL;
  166. if (AST_LIST_REMOVE(&test_list, bogus, list)) {
  167. ast_test_status_update(test, "AST_LIST_REMOVE should safely return NULL for element set to NULL\n");
  168. return AST_TEST_FAIL;
  169. }
  170. AST_LIST_REMOVE(&test_list, &b, list);
  171. MATCH_OR_FAIL(&test_list, "ACD", buf);
  172. AST_LIST_REMOVE(&test_list, &d, list);
  173. MATCH_OR_FAIL(&test_list, "AC", buf);
  174. AST_LIST_REMOVE(&test_list, &a, list);
  175. MATCH_OR_FAIL(&test_list, "C", buf);
  176. AST_LIST_REMOVE(&test_list, &c, list);
  177. MATCH_OR_FAIL(&test_list, "", buf);
  178. if (!AST_LIST_EMPTY(&test_list)) {
  179. ast_test_status_update(test, "List should be empty\n");
  180. return AST_TEST_FAIL;
  181. }
  182. if (AST_LIST_REMOVE(&test_list, bogus, list)) {
  183. ast_test_status_update(test, "AST_LIST_REMOVE should safely return NULL asked to remove a NULL pointer from an empty list\n");
  184. return AST_TEST_FAIL;
  185. }
  186. /* Insert item after specific item tests */
  187. AST_LIST_INSERT_HEAD(&test_list, &a, list);
  188. MATCH_OR_FAIL(&test_list, "A", buf);
  189. AST_LIST_INSERT_TAIL(&test_list, &c, list);
  190. MATCH_OR_FAIL(&test_list, "AC", buf);
  191. AST_LIST_INSERT_AFTER(&test_list, &a, &b, list);
  192. MATCH_OR_FAIL(&test_list, "ABC", buf);
  193. AST_LIST_INSERT_AFTER(&test_list, &c, &d, list);
  194. MATCH_OR_FAIL(&test_list, "ABCD", buf);
  195. ELEM_OR_FAIL(AST_LIST_FIRST(&test_list), &a);
  196. ELEM_OR_FAIL(AST_LIST_LAST(&test_list), &d);
  197. ELEM_OR_FAIL(AST_LIST_NEXT(&a, list), &b);
  198. AST_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, list) {
  199. AST_LIST_REMOVE_CURRENT(list);
  200. }
  201. AST_LIST_TRAVERSE_SAFE_END;
  202. if (!AST_LIST_EMPTY(&test_list)) {
  203. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  204. return AST_TEST_FAIL;
  205. }
  206. /* Append list test */
  207. AST_LIST_INSERT_HEAD(&test_list, &a, list);
  208. MATCH_OR_FAIL(&test_list, "A", buf);
  209. AST_LIST_INSERT_TAIL(&test_list, &b, list);
  210. MATCH_OR_FAIL(&test_list, "AB", buf);
  211. AST_LIST_INSERT_HEAD(&other_list, &c, list);
  212. MATCH_OR_FAIL(&other_list, "C", buf);
  213. AST_LIST_INSERT_TAIL(&other_list, &d, list);
  214. MATCH_OR_FAIL(&other_list, "CD", buf);
  215. AST_LIST_APPEND_LIST(&test_list, &other_list, list);
  216. MATCH_OR_FAIL(&test_list, "ABCD", buf);
  217. MATCH_OR_FAIL(&other_list, "", buf);
  218. AST_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, list) {
  219. AST_LIST_REMOVE_CURRENT(list);
  220. }
  221. AST_LIST_TRAVERSE_SAFE_END;
  222. if (!AST_LIST_EMPTY(&test_list)) {
  223. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  224. return AST_TEST_FAIL;
  225. }
  226. /* Insert list after specific item in middle test */
  227. AST_LIST_INSERT_HEAD(&test_list, &a, list);
  228. MATCH_OR_FAIL(&test_list, "A", buf);
  229. AST_LIST_INSERT_TAIL(&test_list, &d, list);
  230. MATCH_OR_FAIL(&test_list, "AD", buf);
  231. AST_LIST_INSERT_HEAD(&other_list, &b, list);
  232. MATCH_OR_FAIL(&other_list, "B", buf);
  233. AST_LIST_INSERT_TAIL(&other_list, &c, list);
  234. MATCH_OR_FAIL(&other_list, "BC", buf);
  235. AST_LIST_INSERT_LIST_AFTER(&test_list, &other_list, &a, list);
  236. MATCH_OR_FAIL(&test_list, "ABCD", buf);
  237. MATCH_OR_FAIL(&other_list, "", buf);
  238. AST_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, list) {
  239. AST_LIST_REMOVE_CURRENT(list);
  240. }
  241. AST_LIST_TRAVERSE_SAFE_END;
  242. if (!AST_LIST_EMPTY(&test_list)) {
  243. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  244. return AST_TEST_FAIL;
  245. }
  246. /* Insert list after specific item on end test */
  247. AST_LIST_INSERT_HEAD(&test_list, &a, list);
  248. MATCH_OR_FAIL(&test_list, "A", buf);
  249. AST_LIST_INSERT_TAIL(&test_list, &b, list);
  250. MATCH_OR_FAIL(&test_list, "AB", buf);
  251. AST_LIST_INSERT_HEAD(&other_list, &c, list);
  252. MATCH_OR_FAIL(&other_list, "C", buf);
  253. AST_LIST_INSERT_TAIL(&other_list, &d, list);
  254. MATCH_OR_FAIL(&other_list, "CD", buf);
  255. AST_LIST_INSERT_LIST_AFTER(&test_list, &other_list, &b, list);
  256. MATCH_OR_FAIL(&test_list, "ABCD", buf);
  257. MATCH_OR_FAIL(&other_list, "", buf);
  258. AST_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, list) {
  259. AST_LIST_REMOVE_CURRENT(list);
  260. }
  261. AST_LIST_TRAVERSE_SAFE_END;
  262. if (!AST_LIST_EMPTY(&test_list)) {
  263. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  264. return AST_TEST_FAIL;
  265. }
  266. /* Safe traversal list modification tests */
  267. AST_LIST_INSERT_HEAD(&test_list, &a, list);
  268. MATCH_OR_FAIL(&test_list, "A", buf);
  269. AST_LIST_INSERT_TAIL(&test_list, &d, list);
  270. MATCH_OR_FAIL(&test_list, "AD", buf);
  271. AST_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, list) {
  272. if (bogus == &d) {
  273. AST_LIST_INSERT_BEFORE_CURRENT(&b, list);
  274. MATCH_OR_FAIL(&test_list, "ABD", buf);
  275. AST_LIST_INSERT_BEFORE_CURRENT(&c, list);
  276. MATCH_OR_FAIL(&test_list, "ABCD", buf);
  277. AST_LIST_REMOVE_CURRENT(list);
  278. MATCH_OR_FAIL(&test_list, "ABC", buf);
  279. }
  280. }
  281. AST_LIST_TRAVERSE_SAFE_END;
  282. MATCH_OR_FAIL(&test_list, "ABC", buf);
  283. AST_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, list) {
  284. AST_LIST_REMOVE_CURRENT(list);
  285. }
  286. AST_LIST_TRAVERSE_SAFE_END;
  287. if (!AST_LIST_EMPTY(&test_list)) {
  288. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  289. return AST_TEST_FAIL;
  290. }
  291. return AST_TEST_PASS;
  292. }
  293. AST_TEST_DEFINE(double_ll_tests)
  294. {
  295. RAII_VAR(struct ast_str *, buf, NULL, ast_free);
  296. struct test_dbl_llist test_list = { 0, };
  297. struct test_dbl_llist other_list = { 0, };
  298. struct test_val *bogus;
  299. switch (cmd) {
  300. case TEST_INIT:
  301. info->name = "double_ll_tests";
  302. info->category = "/main/linkedlists/";
  303. info->summary = "double linked list unit test";
  304. info->description =
  305. "Test the double linked list API";
  306. return AST_TEST_NOT_RUN;
  307. case TEST_EXECUTE:
  308. break;
  309. }
  310. if (!(buf = ast_str_create(16))) {
  311. return AST_TEST_FAIL;
  312. }
  313. bogus = ast_alloca(sizeof(*bogus));
  314. if (AST_DLLIST_REMOVE_VERIFY(&test_list, bogus, dbl_list)) {
  315. ast_test_status_update(test, "AST_DLLIST_REMOVE_VERIFY should safely return NULL for missing element from empty list\n");
  316. return AST_TEST_FAIL;
  317. }
  318. /* INSERT_HEAD and REMOVE_HEAD tests */
  319. AST_DLLIST_INSERT_HEAD(&test_list, &a, dbl_list);
  320. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  321. AST_DLLIST_INSERT_HEAD(&test_list, &b, dbl_list);
  322. MATCH_OR_FAIL_DBL(&test_list, "BA", buf);
  323. AST_DLLIST_REMOVE_HEAD(&test_list, dbl_list);
  324. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  325. AST_DLLIST_REMOVE_HEAD(&test_list, dbl_list);
  326. MATCH_OR_FAIL_DBL(&test_list, "", buf);
  327. if (AST_DLLIST_REMOVE_HEAD(&test_list, dbl_list)) {
  328. ast_test_status_update(test, "Somehow removed an item from the head of a list that didn't exist\n");
  329. return AST_TEST_FAIL;
  330. }
  331. MATCH_OR_FAIL_DBL(&test_list, "", buf);
  332. /* Check empty list test */
  333. if (!AST_DLLIST_EMPTY(&test_list)) {
  334. ast_test_status_update(test, "List should be empty\n");
  335. return AST_TEST_FAIL;
  336. }
  337. /* Insert tail and remove specific item tests. */
  338. AST_DLLIST_INSERT_TAIL(&test_list, &a, dbl_list);
  339. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  340. AST_DLLIST_INSERT_TAIL(&test_list, &b, dbl_list);
  341. MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
  342. AST_DLLIST_INSERT_TAIL(&test_list, &c, dbl_list);
  343. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  344. AST_DLLIST_INSERT_TAIL(&test_list, &d, dbl_list);
  345. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  346. if (AST_DLLIST_REMOVE_VERIFY(&test_list, bogus, dbl_list)) {
  347. ast_test_status_update(test, "AST_DLLIST_REMOVE_VERIFY should safely return NULL for missing element\n");
  348. return AST_TEST_FAIL;
  349. }
  350. bogus = NULL;
  351. if (AST_DLLIST_REMOVE_VERIFY(&test_list, bogus, dbl_list)) {
  352. ast_test_status_update(test, "AST_DLLIST_REMOVE_VERIFY should safely return NULL for element set to NULL\n");
  353. return AST_TEST_FAIL;
  354. }
  355. AST_DLLIST_REMOVE(&test_list, &b, dbl_list);
  356. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  357. AST_DLLIST_REMOVE(&test_list, &d, dbl_list);
  358. MATCH_OR_FAIL_DBL(&test_list, "AC", buf);
  359. AST_DLLIST_REMOVE(&test_list, &a, dbl_list);
  360. MATCH_OR_FAIL_DBL(&test_list, "C", buf);
  361. AST_DLLIST_REMOVE(&test_list, &c, dbl_list);
  362. MATCH_OR_FAIL_DBL(&test_list, "", buf);
  363. if (!AST_DLLIST_EMPTY(&test_list)) {
  364. ast_test_status_update(test, "List should be empty\n");
  365. return AST_TEST_FAIL;
  366. }
  367. if (AST_DLLIST_REMOVE_VERIFY(&test_list, bogus, dbl_list)) {
  368. ast_test_status_update(test, "AST_DLLIST_REMOVE_VERIFY should safely return NULL asked to remove a NULL pointer from an empty list\n");
  369. return AST_TEST_FAIL;
  370. }
  371. /* Insert item after and before specific item tests */
  372. AST_DLLIST_INSERT_HEAD(&test_list, &a, dbl_list);
  373. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  374. AST_DLLIST_INSERT_TAIL(&test_list, &c, dbl_list);
  375. MATCH_OR_FAIL_DBL(&test_list, "AC", buf);
  376. AST_DLLIST_INSERT_AFTER(&test_list, &a, &b, dbl_list);
  377. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  378. AST_DLLIST_INSERT_AFTER(&test_list, &c, &d, dbl_list);
  379. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  380. AST_DLLIST_REMOVE_TAIL(&test_list, dbl_list);
  381. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  382. AST_DLLIST_REMOVE_TAIL(&test_list, dbl_list);
  383. MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
  384. AST_DLLIST_INSERT_TAIL(&test_list, &d, dbl_list);
  385. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  386. AST_DLLIST_INSERT_BEFORE(&test_list, &d, &c, dbl_list);
  387. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  388. AST_DLLIST_REMOVE_HEAD(&test_list, dbl_list);
  389. MATCH_OR_FAIL_DBL(&test_list, "BCD", buf);
  390. AST_DLLIST_INSERT_BEFORE(&test_list, &b, &a, dbl_list);
  391. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  392. ELEM_OR_FAIL(AST_DLLIST_FIRST(&test_list), &a);
  393. ELEM_OR_FAIL(AST_DLLIST_LAST(&test_list), &d);
  394. ELEM_OR_FAIL(AST_DLLIST_NEXT(&a, dbl_list), &b);
  395. ELEM_OR_FAIL(AST_DLLIST_PREV(&b, dbl_list), &a);
  396. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  397. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  398. }
  399. AST_DLLIST_TRAVERSE_SAFE_END;
  400. if (!AST_DLLIST_EMPTY(&test_list)) {
  401. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  402. return AST_TEST_FAIL;
  403. }
  404. /* Append list test */
  405. AST_DLLIST_INSERT_HEAD(&test_list, &a, dbl_list);
  406. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  407. AST_DLLIST_INSERT_TAIL(&test_list, &b, dbl_list);
  408. MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
  409. AST_DLLIST_INSERT_HEAD(&other_list, &c, dbl_list);
  410. MATCH_OR_FAIL_DBL(&other_list, "C", buf);
  411. AST_DLLIST_INSERT_TAIL(&other_list, &d, dbl_list);
  412. MATCH_OR_FAIL_DBL(&other_list, "CD", buf);
  413. AST_DLLIST_APPEND_DLLIST(&test_list, &other_list, dbl_list);
  414. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  415. MATCH_OR_FAIL_DBL(&other_list, "", buf);
  416. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  417. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  418. }
  419. AST_DLLIST_TRAVERSE_SAFE_END;
  420. if (!AST_DLLIST_EMPTY(&test_list)) {
  421. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  422. return AST_TEST_FAIL;
  423. }
  424. /*
  425. * Safe traversal list modification tests
  426. * Traverse starting from first element
  427. */
  428. AST_DLLIST_INSERT_HEAD(&test_list, &a, dbl_list);
  429. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  430. AST_DLLIST_INSERT_TAIL(&test_list, &d, dbl_list);
  431. MATCH_OR_FAIL_DBL(&test_list, "AD", buf);
  432. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  433. if (bogus == &d) {
  434. AST_DLLIST_INSERT_BEFORE_CURRENT(&b, dbl_list);
  435. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  436. AST_DLLIST_INSERT_BEFORE_CURRENT(&c, dbl_list);
  437. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  438. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  439. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  440. }
  441. }
  442. AST_DLLIST_TRAVERSE_SAFE_END;
  443. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  444. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  445. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  446. }
  447. AST_DLLIST_TRAVERSE_SAFE_END;
  448. if (!AST_DLLIST_EMPTY(&test_list)) {
  449. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  450. return AST_TEST_FAIL;
  451. }
  452. AST_DLLIST_INSERT_HEAD(&test_list, &b, dbl_list);
  453. MATCH_OR_FAIL_DBL(&test_list, "B", buf);
  454. AST_DLLIST_INSERT_TAIL(&test_list, &d, dbl_list);
  455. MATCH_OR_FAIL_DBL(&test_list, "BD", buf);
  456. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  457. if (bogus == &b) {
  458. AST_DLLIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
  459. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  460. AST_DLLIST_INSERT_AFTER_CURRENT(&c, dbl_list);
  461. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  462. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  463. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  464. }
  465. }
  466. AST_DLLIST_TRAVERSE_SAFE_END;
  467. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  468. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  469. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  470. }
  471. AST_DLLIST_TRAVERSE_SAFE_END;
  472. if (!AST_DLLIST_EMPTY(&test_list)) {
  473. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  474. return AST_TEST_FAIL;
  475. }
  476. AST_DLLIST_INSERT_HEAD(&test_list, &b, dbl_list);
  477. MATCH_OR_FAIL_DBL(&test_list, "B", buf);
  478. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  479. if (bogus == &b) {
  480. AST_DLLIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
  481. MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
  482. AST_DLLIST_INSERT_AFTER_CURRENT(&d, dbl_list);
  483. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  484. AST_DLLIST_INSERT_AFTER_CURRENT(&c, dbl_list);
  485. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  486. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  487. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  488. }
  489. }
  490. AST_DLLIST_TRAVERSE_SAFE_END;
  491. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  492. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  493. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  494. }
  495. AST_DLLIST_TRAVERSE_SAFE_END;
  496. if (!AST_DLLIST_EMPTY(&test_list)) {
  497. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  498. return AST_TEST_FAIL;
  499. }
  500. /*
  501. * Safe traversal list modification tests
  502. * Traverse starting from last element
  503. */
  504. AST_DLLIST_INSERT_HEAD(&test_list, &a, dbl_list);
  505. MATCH_OR_FAIL_DBL(&test_list, "A", buf);
  506. AST_DLLIST_INSERT_TAIL(&test_list, &d, dbl_list);
  507. MATCH_OR_FAIL_DBL(&test_list, "AD", buf);
  508. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  509. if (bogus == &d) {
  510. AST_DLLIST_INSERT_BEFORE_CURRENT(&b, dbl_list);
  511. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  512. AST_DLLIST_INSERT_BEFORE_CURRENT(&c, dbl_list);
  513. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  514. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  515. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  516. }
  517. }
  518. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
  519. MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
  520. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  521. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  522. }
  523. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
  524. if (!AST_DLLIST_EMPTY(&test_list)) {
  525. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  526. return AST_TEST_FAIL;
  527. }
  528. AST_DLLIST_INSERT_HEAD(&test_list, &b, dbl_list);
  529. MATCH_OR_FAIL_DBL(&test_list, "B", buf);
  530. AST_DLLIST_INSERT_TAIL(&test_list, &d, dbl_list);
  531. MATCH_OR_FAIL_DBL(&test_list, "BD", buf);
  532. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  533. if (bogus == &b) {
  534. AST_DLLIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
  535. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  536. AST_DLLIST_INSERT_AFTER_CURRENT(&c, dbl_list);
  537. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  538. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  539. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  540. }
  541. }
  542. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
  543. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  544. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  545. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  546. }
  547. AST_DLLIST_TRAVERSE_SAFE_END;
  548. if (!AST_DLLIST_EMPTY(&test_list)) {
  549. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  550. return AST_TEST_FAIL;
  551. }
  552. AST_DLLIST_INSERT_HEAD(&test_list, &b, dbl_list);
  553. MATCH_OR_FAIL_DBL(&test_list, "B", buf);
  554. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  555. if (bogus == &b) {
  556. AST_DLLIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
  557. MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
  558. AST_DLLIST_INSERT_AFTER_CURRENT(&d, dbl_list);
  559. MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
  560. AST_DLLIST_INSERT_AFTER_CURRENT(&c, dbl_list);
  561. MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
  562. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  563. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  564. }
  565. }
  566. AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
  567. MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
  568. AST_DLLIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list) {
  569. AST_DLLIST_REMOVE_CURRENT(dbl_list);
  570. }
  571. AST_DLLIST_TRAVERSE_SAFE_END;
  572. if (!AST_DLLIST_EMPTY(&test_list)) {
  573. ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
  574. return AST_TEST_FAIL;
  575. }
  576. return AST_TEST_PASS;
  577. }
  578. static int unload_module(void)
  579. {
  580. AST_TEST_UNREGISTER(single_ll_tests);
  581. AST_TEST_UNREGISTER(double_ll_tests);
  582. return 0;
  583. }
  584. static int load_module(void)
  585. {
  586. AST_TEST_REGISTER(single_ll_tests);
  587. AST_TEST_REGISTER(double_ll_tests);
  588. return AST_MODULE_LOAD_SUCCESS;
  589. }
  590. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test Linked Lists");