[asterisk-commits] rmudgett: branch rmudgett/ao2_red_black r342826 - in /team/rmudgett/ao2_red_b...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Oct 31 13:05:09 CDT 2011


Author: rmudgett
Date: Mon Oct 31 13:05:05 2011
New Revision: 342826

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=342826
Log:
Add double linked list macros and unit tests.

Modified:
    team/rmudgett/ao2_red_black/include/asterisk/linkedlists.h
    team/rmudgett/ao2_red_black/tests/test_linkedlists.c

Modified: team/rmudgett/ao2_red_black/include/asterisk/linkedlists.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/ao2_red_black/include/asterisk/linkedlists.h?view=diff&rev=342826&r1=342825&r2=342826
==============================================================================
--- team/rmudgett/ao2_red_black/include/asterisk/linkedlists.h (original)
+++ team/rmudgett/ao2_red_black/include/asterisk/linkedlists.h Mon Oct 31 13:05:05 2011
@@ -879,4 +879,252 @@
 
 #define AST_RWLIST_REMOVE AST_LIST_REMOVE
 
+#define AST_DBL_LIST_HEAD(name, type)				AST_LIST_HEAD(name, type)
+#define AST_DBL_LIST_HEAD_STATIC(name, type)		AST_LIST_HEAD_STATIC(name, type)
+#define AST_DBL_LIST_HEAD_NOLOCK(name, type)		AST_LIST_HEAD_NOLOCK(name, type)
+#define AST_DBL_LIST_HEAD_NOLOCK_STATIC(name, type)	AST_LIST_HEAD_NOLOCK_STATIC(name, type)
+#define AST_DBL_LIST_HEAD_INIT(head)				AST_LIST_HEAD_INIT(head)
+#define AST_DBL_LIST_HEAD_DESTROY(head)				AST_LIST_HEAD_DESTROY(head)
+
+#define AST_DBL_LIST_ENTRY(type)					AST_LIST_HEAD_NOLOCK(, type)
+
+#define AST_DBL_LIST_FIRST(head)					AST_LIST_FIRST(head)
+#define AST_DBL_LIST_LAST(head)						AST_LIST_LAST(head)
+#define AST_DBL_LIST_NEXT(elm, field, direction)	((elm)->field.direction)
+
+#define AST_DBL_LIST_LOCK(head)						AST_LIST_LOCK(head)
+#define AST_DBL_LIST_TRYLOCK(head)					AST_LIST_TRYLOCK(head)
+#define AST_DBL_LIST_UNLOCK(head)					AST_LIST_UNLOCK(head)
+
+#define AST_DBL_LIST_EMPTY(head)					AST_LIST_EMPTY(head)
+#define AST_DBL_LIST_IS_MEMBER(head, elm, field)	\
+	({												\
+		typeof((head)->first) __cur;				\
+		typeof((elm)) __elm = (elm);				\
+		if (!__elm) {								\
+			__cur = NULL;							\
+		} else {									\
+			__cur = (head)->first;					\
+			while (__cur && __cur != __elm) {		\
+				__cur = __cur->field.first;			\
+			}										\
+		}											\
+		__cur;										\
+	})
+
+/*!
+ * \brief Traverse a doublly linked list using the specified direction list.
+ *
+ * \see AST_LIST_TRAVERSE
+ *
+ * \param head List head structure pointer.
+ * \param var Current list node.
+ * \param field List node field for the next node information.
+ * \param start Specified list node to start traversal: first or last
+ */
+#define AST_DBL_LIST_TRAVERSE(head, var, field, start) 				\
+	for ((var) = (head)->start; (var); (var) = AST_DBL_LIST_NEXT(var, field, start))
+
+/*!
+ * \brief Safe traversal of a doublly linked list using the specified direction list.
+ *
+ * \see AST_LIST_TRAVERSE_SAFE_BEGIN
+ *
+ * \param head List head structure pointer.
+ * \param var Current list node.
+ * \param field List node field for the next node information.
+ * \param start Specified list node to start traversal: first or last
+ */
+#define AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(head, var, field, start)		\
+	do {																\
+		typeof((head)) __list_head = (head);							\
+		typeof(__list_head->first) __list_current;						\
+		typeof(__list_head->first) __list_first;						\
+		typeof(__list_head->first) __list_last;							\
+		typeof(__list_head->first) __list_next;							\
+		for ((var) = __list_head->start,								\
+			__list_current = (var),										\
+			__list_first = (var) ? (var)->field.first : NULL,			\
+			__list_last = (var) ? (var)->field.last : NULL,				\
+			__list_next = (var) ? AST_DBL_LIST_NEXT(var, field, start) : NULL;	\
+			(var);														\
+			(void) __list_current,/* To quiet compiler? */				\
+			(void) __list_first,/* To quiet compiler? */				\
+			(void) __list_last,/* To quiet compiler? */					\
+			(var) = __list_next,										\
+			__list_current = (var),										\
+			__list_first = (var) ? (var)->field.first : NULL,			\
+			__list_last = (var) ? (var)->field.last : NULL,				\
+			__list_next = (var) ? AST_DBL_LIST_NEXT(var, field, start) : NULL	\
+			)
+
+#define AST_DBL_LIST_INSERT_BEFORE_CURRENT(elm, field)					\
+		do {															\
+			typeof((elm)) __elm = (elm);								\
+			__elm->field.last = __list_last;							\
+			__elm->field.first = __list_current;						\
+			if (__list_head->first == __list_current) {					\
+				__list_head->first = __elm;								\
+			} else {													\
+				__list_last->field.first = __elm;						\
+			}															\
+			__list_current->field.last = __elm;							\
+			if (__list_next == __list_last) {							\
+				__list_next = __elm;									\
+			}															\
+			__list_last = __elm;										\
+		} while (0)
+
+#define AST_DBL_LIST_INSERT_AFTER_CURRENT(elm, field)					\
+		do {															\
+			typeof((elm)) __elm = (elm);								\
+			__elm->field.first = __list_first;							\
+			__elm->field.last = __list_current;							\
+			if (__list_head->last == __list_current) {					\
+				__list_head->last = __elm;								\
+			} else {													\
+				__list_first->field.last = __elm;						\
+			}															\
+			__list_current->field.first = __elm;						\
+			if (__list_next == __list_first) {							\
+				__list_next = __elm;									\
+			}															\
+			__list_first = __elm;										\
+		} while (0)
+
+#define AST_DBL_LIST_REMOVE_CURRENT(field)								\
+		do {															\
+			if (__list_first) {											\
+				__list_first->field.last = __list_last;					\
+			} else {													\
+				__list_head->last = __list_last;						\
+			}															\
+			if (__list_last) {											\
+				__list_last->field.first = __list_first;				\
+			} else {													\
+				__list_head->first = __list_first;						\
+			}															\
+			__list_current->field.first = NULL;							\
+			__list_current->field.last = NULL;							\
+			__list_current = NULL;										\
+		} while (0)
+
+#define AST_DBL_LIST_TRAVERSE_SAFE_END	\
+	} while (0)
+
+#define AST_DBL_LIST_INSERT_HEAD(head, elm, field)	\
+	do {											\
+		typeof((elm)) __elm = (elm);				\
+		__elm->field.last = NULL;					\
+		__elm->field.first = (head)->first;			\
+		if (!(head)->first) {						\
+			(head)->last = __elm;					\
+		} else {									\
+			(head)->first->field.last = __elm;		\
+		}											\
+		(head)->first = __elm;						\
+	} while (0)
+
+#define AST_DBL_LIST_INSERT_TAIL(head, elm, field)	\
+	do {											\
+		typeof((elm)) __elm = (elm);				\
+		__elm->field.first = NULL;					\
+		if (!(head)->first) {						\
+			__elm->field.last = NULL;				\
+			(head)->first = __elm;					\
+		} else {									\
+			__elm->field.last = (head)->last;		\
+			(head)->last->field.first = __elm;		\
+		}											\
+		(head)->last = __elm;						\
+	} while (0)
+
+#define AST_DBL_LIST_INSERT_BEFORE(head, listelm, elm, field)	\
+	do {														\
+		typeof((listelm)) __listelm = (listelm);				\
+		typeof((elm)) __elm = (elm);							\
+		__elm->field.last = __listelm->field.last;				\
+		__elm->field.first = __listelm;							\
+		if ((head)->first == __listelm) {						\
+			(head)->first = __elm;								\
+		} else {												\
+			__listelm->field.last->field.first = __elm;			\
+		}														\
+		__listelm->field.last = __elm;							\
+	} while (0)
+
+#define AST_DBL_LIST_INSERT_AFTER(head, listelm, elm, field)	\
+	do {														\
+		typeof((listelm)) __listelm = (listelm);				\
+		typeof((elm)) __elm = (elm);							\
+		__elm->field.first = __listelm->field.first;			\
+		__elm->field.last = __listelm;							\
+		if ((head)->last == __listelm) {						\
+			(head)->last = __elm;								\
+		} else {												\
+			__listelm->field.first->field.last = __elm;			\
+		}														\
+		__listelm->field.first = __elm;							\
+	} while (0)
+
+#define AST_DBL_LIST_REMOVE_HEAD(head, field)		\
+	({												\
+		typeof((head)->first) cur = (head)->first;	\
+		if (cur) {									\
+			(head)->first = cur->field.first;		\
+			if ((head)->first) {					\
+				(head)->first->field.last = NULL;	\
+			}										\
+			cur->field.first = NULL;				\
+			cur->field.last = NULL;					\
+			if ((head)->last == cur) {				\
+				(head)->last = NULL;				\
+			}										\
+		}											\
+		cur;										\
+	})
+
+#define AST_DBL_LIST_REMOVE_TAIL(head, field)		\
+	({												\
+		typeof((head)->last) cur = (head)->last;	\
+		if (cur) {									\
+			(head)->last = cur->field.last;			\
+			if ((head)->last) {						\
+				(head)->last->field.first = NULL;	\
+			}										\
+			cur->field.first = NULL;				\
+			cur->field.last = NULL;					\
+			if ((head)->first == cur) {				\
+				(head)->first = NULL;				\
+			}										\
+		}											\
+		cur;										\
+	})
+
+#define AST_DBL_LIST_REMOVE_QUICK(head, elm, field)						\
+	do {																\
+		typeof((elm)) __elm = (elm);									\
+	 	if (__elm) {													\
+			if (__elm->field.first) {									\
+				__elm->field.first->field.last = __elm->field.last;		\
+			} else {													\
+				(head)->last = __elm->field.last;						\
+			}															\
+			if (__elm->field.last) {									\
+				__elm->field.last->field.first = __elm->field.first;	\
+			} else {													\
+				(head)->first = __elm->field.first;						\
+			}															\
+			__elm->field.first = NULL;									\
+			__elm->field.last = NULL;									\
+		}																\
+	} while (0)
+
+#define AST_DBL_LIST_REMOVE(head, elm, field)							\
+	({																	\
+		typeof((elm)) __res = AST_DBL_LIST_IS_MEMBER(head, elm, field);	\
+		AST_DBL_LIST_REMOVE_QUICK(head, __res, field);					\
+		__res;															\
+	})
+
 #endif /* _ASTERISK_LINKEDLISTS_H */

Modified: team/rmudgett/ao2_red_black/tests/test_linkedlists.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/ao2_red_black/tests/test_linkedlists.c?view=diff&rev=342826&r1=342825&r2=342826
==============================================================================
--- team/rmudgett/ao2_red_black/tests/test_linkedlists.c (original)
+++ team/rmudgett/ao2_red_black/tests/test_linkedlists.c Mon Oct 31 13:05:05 2011
@@ -41,6 +41,7 @@
 struct test_val {
 	const char *name;
 	AST_LIST_ENTRY(test_val) list;
+	AST_DBL_LIST_ENTRY(test_val) dbl_list;
 };
 
 static struct test_val a = { "A" };
@@ -49,6 +50,7 @@
 static struct test_val d = { "D" };
 
 AST_LIST_HEAD_NOLOCK(test_llist, test_val);
+AST_DBL_LIST_HEAD_NOLOCK(test_dbl_llist, test_val);
 
 static int list_expect(struct test_llist *test_list, const char *expect, struct ast_str **buf)
 {
@@ -62,9 +64,58 @@
 	return strcmp(expect, ast_str_buffer(*buf));
 }
 
+static int dbl_list_expect_forward(struct test_dbl_llist *test_list, const char *expect, struct ast_str **buf)
+{
+	struct test_val *i;
+
+	ast_str_reset(*buf);
+	AST_DBL_LIST_TRAVERSE(test_list, i, dbl_list, first) {
+		ast_str_append(buf, 0, "%s", i->name);
+	}
+
+	return strcmp(expect, ast_str_buffer(*buf));
+}
+
+static int dbl_list_expect_reverse(struct test_dbl_llist *test_list, const char *expect, struct ast_str **buf)
+{
+	struct test_val *i;
+	char *str;
+	int len = strlen(expect);
+	int idx;
+
+	ast_str_reset(*buf);
+	AST_DBL_LIST_TRAVERSE(test_list, i, dbl_list, last) {
+		ast_str_append(buf, 0, "%s", i->name);
+	}
+
+	/* Check reverse string. */
+	str = ast_str_buffer(*buf);
+	if (len != strlen(str)) {
+		return 1;
+	}
+	for (idx = 0; idx < len; ++idx) {
+		if (expect[idx] != str[len - idx - 1]) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
 #define MATCH_OR_FAIL(list, val, retbuf) \
 	if (list_expect(list, val, &retbuf)) { \
 		ast_test_status_update(test, "Expected: %s, Got: %s\n", val, ast_str_buffer(retbuf)); \
+		ast_free(retbuf); \
+		return AST_TEST_FAIL; \
+	}
+
+#define MATCH_OR_FAIL_DBL(list, val, retbuf) \
+	if (dbl_list_expect_forward(list, val, &retbuf)) { \
+		ast_test_status_update(test, "Expected: %s, Got: %s\n", val, ast_str_buffer(retbuf)); \
+		ast_free(retbuf); \
+		return AST_TEST_FAIL; \
+	} \
+	if (dbl_list_expect_reverse(list, val, &retbuf)) { \
+		ast_test_status_update(test, "Expected reverse of: %s, Got: %s\n", val, ast_str_buffer(retbuf)); \
 		ast_free(retbuf); \
 		return AST_TEST_FAIL; \
 	}
@@ -286,15 +337,301 @@
 	return AST_TEST_PASS;
 }
 
+AST_TEST_DEFINE(double_ll_tests)
+{
+	struct ast_str *buf;
+	struct test_dbl_llist test_list = { 0, };
+	struct test_val *bogus;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "double_ll_tests";
+		info->category = "/main/linkedlists";
+		info->summary = "double linked list unit test";
+		info->description =
+			"Test the double linked list API";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (!(buf = ast_str_create(16))) {
+		return AST_TEST_FAIL;
+	}
+
+	if (!(bogus = alloca(sizeof(*bogus)))) {
+		return AST_TEST_FAIL;
+	}
+
+	if (AST_DBL_LIST_REMOVE(&test_list, bogus, dbl_list)) {
+		ast_test_status_update(test, "AST_DBL_LIST_REMOVE should safely return NULL for missing element from empty list\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* INSERT_HEAD and REMOVE_HEAD tests */
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "A", buf);
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "BA", buf);
+	AST_DBL_LIST_REMOVE_HEAD(&test_list, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "A", buf);
+	AST_DBL_LIST_REMOVE_HEAD(&test_list, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "", buf);
+	if (AST_DBL_LIST_REMOVE_HEAD(&test_list, dbl_list)) {
+		ast_test_status_update(test, "Somehow removed an item from the head of a list that didn't exist\n");
+		return AST_TEST_FAIL;
+	}
+	MATCH_OR_FAIL_DBL(&test_list, "", buf);
+
+	/* ** */
+
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* ** */
+
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "A", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &c, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+	if (AST_DBL_LIST_REMOVE(&test_list, bogus, dbl_list)) {
+		ast_test_status_update(test, "AST_DBL_LIST_REMOVE should safely return NULL for missing element\n");
+		return AST_TEST_FAIL;
+	}
+	bogus = NULL;
+	if (AST_DBL_LIST_REMOVE(&test_list, bogus, dbl_list)) {
+		ast_test_status_update(test, "AST_DBL_LIST_REMOVE should safely return NULL for element set to NULL\n");
+		return AST_TEST_FAIL;
+	}
+	AST_DBL_LIST_REMOVE(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+	AST_DBL_LIST_REMOVE(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "AC", buf);
+	AST_DBL_LIST_REMOVE(&test_list, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "C", buf);
+	AST_DBL_LIST_REMOVE(&test_list, &c, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "", buf);
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty\n");
+		return AST_TEST_FAIL;
+	}
+	if (AST_DBL_LIST_REMOVE(&test_list, bogus, dbl_list)) {
+		ast_test_status_update(test, "AST_DBL_LIST_REMOVE should safely return NULL asked to remove a NULL pointer from an empty list\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* ** */
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "A", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &c, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "AC", buf);
+	AST_DBL_LIST_INSERT_AFTER(&test_list, &a, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+	AST_DBL_LIST_INSERT_AFTER(&test_list, &c, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+	AST_DBL_LIST_REMOVE_TAIL(&test_list, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+	AST_DBL_LIST_REMOVE_TAIL(&test_list, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+	AST_DBL_LIST_INSERT_BEFORE(&test_list, &d, &c, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+	AST_DBL_LIST_REMOVE_HEAD(&test_list, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "BCD", buf);
+	AST_DBL_LIST_INSERT_BEFORE(&test_list, &b, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+
+	ELEM_OR_FAIL(AST_DBL_LIST_FIRST(&test_list), &a);
+	ELEM_OR_FAIL(AST_DBL_LIST_LAST(&test_list), &d);
+	ELEM_OR_FAIL(AST_DBL_LIST_NEXT(&a, dbl_list, first), &b);
+	ELEM_OR_FAIL(AST_DBL_LIST_NEXT(&b, dbl_list, last), &a);
+
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* Traverse starting from first element */
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "A", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "AD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		if (bogus == &d) {
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&b, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&c, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+			AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+		}
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "B", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "BD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		if (bogus == &b) {
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+			AST_DBL_LIST_INSERT_AFTER_CURRENT(&c, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+			AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+		}
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "B", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		if (bogus == &b) {
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
+			AST_DBL_LIST_INSERT_AFTER_CURRENT(&d, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+			AST_DBL_LIST_INSERT_AFTER_CURRENT(&c, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+			AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+		}
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* Traverse starting from last element */
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &a, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "A", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "AD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, last) {
+		if (bogus == &d) {
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&b, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&c, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+			AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+		}
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	MATCH_OR_FAIL_DBL(&test_list, "ABC", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, last) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "B", buf);
+	AST_DBL_LIST_INSERT_TAIL(&test_list, &d, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "BD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, last) {
+		if (bogus == &b) {
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+			AST_DBL_LIST_INSERT_AFTER_CURRENT(&c, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+			AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+		}
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	AST_DBL_LIST_INSERT_HEAD(&test_list, &b, dbl_list);
+	MATCH_OR_FAIL_DBL(&test_list, "B", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, last) {
+		if (bogus == &b) {
+			AST_DBL_LIST_INSERT_BEFORE_CURRENT(&a, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "AB", buf);
+			AST_DBL_LIST_INSERT_AFTER_CURRENT(&d, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABD", buf);
+			AST_DBL_LIST_INSERT_AFTER_CURRENT(&c, dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ABCD", buf);
+			AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+			MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+		}
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	MATCH_OR_FAIL_DBL(&test_list, "ACD", buf);
+	AST_DBL_LIST_TRAVERSE_SAFE_BEGIN(&test_list, bogus, dbl_list, first) {
+		AST_DBL_LIST_REMOVE_CURRENT(dbl_list);
+	}
+	AST_DBL_LIST_TRAVERSE_SAFE_END;
+	if (!AST_DBL_LIST_EMPTY(&test_list)) {
+		ast_test_status_update(test, "List should be empty after traversing and removal. It wasn't.\n");
+		return AST_TEST_FAIL;
+	}
+
+	return AST_TEST_PASS;
+}
+
 static int unload_module(void)
 {
 	AST_TEST_UNREGISTER(single_ll_tests);
+	AST_TEST_UNREGISTER(double_ll_tests);
 	return 0;
 }
 
 static int load_module(void)
 {
 	AST_TEST_REGISTER(single_ll_tests);
+	AST_TEST_REGISTER(double_ll_tests);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 




More information about the asterisk-commits mailing list