[asterisk-commits] rmudgett: branch rmudgett/ao2_enhancements r371275 - /team/rmudgett/ao2_enhan...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Aug 15 15:18:21 CDT 2012
Author: rmudgett
Date: Wed Aug 15 15:18:16 2012
New Revision: 371275
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=371275
Log:
* Extracted subtests from astobj2_test_1().
* Created astobj2_test_4() to test the new traversal options. Still need
to complete tests for duplicate handling in sorted containers.
Modified:
team/rmudgett/ao2_enhancements/tests/test_astobj2.c
Modified: team/rmudgett/ao2_enhancements/tests/test_astobj2.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/ao2_enhancements/tests/test_astobj2.c?view=diff&rev=371275&r1=371274&r2=371275
==============================================================================
--- team/rmudgett/ao2_enhancements/tests/test_astobj2.c (original)
+++ team/rmudgett/ao2_enhancements/tests/test_astobj2.c Wed Aug 15 15:18:16 2012
@@ -37,11 +37,43 @@
#include "asterisk/test.h"
#include "asterisk/astobj2.h"
+enum test_container_type {
+ TEST_CONTAINER_LIST,
+ TEST_CONTAINER_HASH,
+};
+
+/*!
+ * \internal
+ * \brief Convert the container type enum to string.
+ * \since 12.0
+ *
+ * \param type Container type value to convert to string.
+ *
+ * \return String value of container type.
+ */
+static const char *test_container2str(enum test_container_type type)
+{
+ const char *c_type;
+
+ c_type = "Unknown";
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ c_type = "List";
+ break;
+ case TEST_CONTAINER_HASH:
+ c_type = "Hash";
+ break;
+ }
+ return c_type;
+}
+
struct test_obj {
- /*! What to increment when object is destroyed. */
+ /*! What to decrement when object is destroyed. */
int *destructor_count;
/*! Container object key */
int i;
+ /*! Identifier for duplicate object key tests. */
+ int dup_number;
};
/*! Partial search key +/- matching range. */
@@ -55,7 +87,9 @@
{
struct test_obj *obj = (struct test_obj *) v_obj;
- --*obj->destructor_count;
+ if (obj->destructor_count) {
+ --*obj->destructor_count;
+ }
}
static int increment_cb(void *obj, void *arg, int flag)
@@ -165,105 +199,43 @@
}
}
-static int astobj2_test_1_helper(int tst_num, int use_hash, int use_sort, int use_cmp, unsigned int lim, struct ast_test *test)
-{
- struct ao2_container *c1;
- struct ao2_container *c2;
- struct ao2_container *c3 = NULL;
- struct ao2_iterator it;
- struct ao2_iterator *mult_it;
+/*!
+ * \internal
+ * \brief Test container cloning.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param orig Container to clone.
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_container_clone(int res, struct ao2_container *orig, struct ast_test *test)
+{
+ struct ao2_container *clone;
struct test_obj *obj;
struct test_obj *obj2;
- struct test_obj tmp_obj;
- int n_buckets;
- int increment = 0;
- int destructor_count = 0;
- int count;
- int num;
- int res = AST_TEST_PASS;
-
- ast_test_status_update(test, "Test %d, %s hash_cb, sorted %s, and %s cmp_cb.\n",
- tst_num,
- use_hash ? "custom" : "default",
- use_sort ? "yes" : "no",
- use_cmp ? "custom" : "default");
-
- /* Need at least 12 objects for the special iax2 OBJ_CONTINUE test. */
- if (lim < 12) {
- lim = 12;
- }
-
- if (use_hash) {
- n_buckets = (ast_random() % ((lim / 4) + 1)) + 1;
- if (n_buckets < 6) {
- /* Need at least 6 buckets for the special iax2 OBJ_CONTINUE test. */
- n_buckets = 6;
- }
+ struct ao2_iterator iter;
+
+ clone = ao2_container_clone(orig, 0);
+ if (!clone) {
+ ast_test_status_update(test, "ao2_container_clone failed.\n");
+ return AST_TEST_FAIL;
+ }
+ if (ao2_container_check(clone, 0)) {
+ ast_test_status_update(test, "container integrity check failed\n");
+ res = AST_TEST_FAIL;
+ } else if (ao2_container_count(orig) != ao2_container_count(clone)) {
+ ast_test_status_update(test, "Cloned container does not have the same number of objects.\n");
+ res = AST_TEST_FAIL;
} else {
- /* Without a hash function, the container is just a linked list. */
- n_buckets = 1;
- }
- c1 = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, n_buckets,
- use_hash ? test_hash_cb : NULL,
- use_sort ? test_sort_cb : NULL,
- use_cmp ? test_cmp_cb : NULL,
- "test");
- c2 = ao2_t_container_alloc(1, NULL, NULL, "test");
-
- if (!c1 || !c2) {
- ast_test_status_update(test, "ao2_container_alloc failed.\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
-
- /* Create objects and link into container */
- destructor_count = lim;
- for (num = 0; num < lim; ++num) {
- if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
- ast_test_status_update(test, "ao2_alloc failed.\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
- obj->destructor_count = &destructor_count;
- obj->i = num;
- ao2_link(c1, obj);
- ao2_t_ref(obj, -1, "test");
- if (ao2_container_count(c1) != num + 1) {
- ast_test_status_update(test, "container did not link correctly\n");
- res = AST_TEST_FAIL;
- }
- }
- if (ao2_container_check(c1, 0)) {
- ast_test_status_update(test, "container integrity check failed\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
-
- ast_test_status_update(test, "Container created: buckets %d: items: %d\n", n_buckets, lim);
-
- /* Testing ao2_container_clone */
- c3 = ao2_container_clone(c1, 0);
- if (!c3) {
- ast_test_status_update(test, "ao2_container_clone failed.\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
- if (ao2_container_check(c3, 0)) {
- ast_test_status_update(test, "container integrity check failed\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
- if (ao2_container_count(c1) != ao2_container_count(c3)) {
- ast_test_status_update(test, "Cloned container does not have the same number of objects.\n");
- res = AST_TEST_FAIL;
- } else {
- it = ao2_iterator_init(c1, 0);
- for (; (obj = ao2_t_iterator_next(&it, "test orig")); ao2_t_ref(obj, -1, "test orig")) {
+ iter = ao2_iterator_init(orig, 0);
+ for (; (obj = ao2_t_iterator_next(&iter, "test orig")); ao2_t_ref(obj, -1, "test orig")) {
/*
* Unlink the matching object from the cloned container to make
* the next search faster. This is a big speed optimization!
*/
- obj2 = ao2_t_callback(c3, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
+ obj2 = ao2_t_callback(clone, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
"test clone");
if (obj2) {
ao2_t_ref(obj2, -1, "test clone");
@@ -273,89 +245,267 @@
"Orig container has an object %p not in the clone container.\n", obj);
res = AST_TEST_FAIL;
}
- ao2_iterator_destroy(&it);
- if (ao2_container_count(c3)) {
+ ao2_iterator_destroy(&iter);
+ if (ao2_container_count(clone)) {
ast_test_status_update(test, "Cloned container still has objects.\n");
res = AST_TEST_FAIL;
}
- if (ao2_container_check(c3, 0)) {
+ if (ao2_container_check(clone, 0)) {
ast_test_status_update(test, "container integrity check failed\n");
res = AST_TEST_FAIL;
}
}
- ao2_t_ref(c3, -1, "bye c3");
- c3 = NULL;
-
- /* Testing ao2_find with no flags */
+ ao2_t_ref(clone, -1, "bye clone");
+
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Test ao2_find with no flags.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param look_in Container to search.
+ * \param limit Container contains objects 0 - (limit - 1).
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_ao2_find_w_no_flags(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
+{
+ int i;
+ int num;
+ struct test_obj tmp_obj = { 0, };
+ struct test_obj *obj;
+
for (num = 100; num--;) {
- int i = ast_random() % lim; /* find a random object */
+ i = ast_random() % limit; /* find a random object */
tmp_obj.i = i;
- if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
- res = AST_TEST_FAIL;
+ obj = ao2_find(look_in, &tmp_obj, 0);
+ if (!obj) {
ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
+ res = AST_TEST_FAIL;
} else {
- /* a correct match will only take place when the custom cmp function is used */
- if (use_cmp && obj->i != i) {
- ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
+ if (obj->i != i) {
+ ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
res = AST_TEST_FAIL;
}
ao2_t_ref(obj, -1, "test");
}
}
- /* Testing ao2_find with OBJ_POINTER */
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Test ao2_find with OBJ_POINTER.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param look_in Container to search.
+ * \param limit Container contains objects 0 - (limit - 1).
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_ao2_find_w_OBJ_POINTER(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
+{
+ int i;
+ int num;
+ struct test_obj tmp_obj = { 0, };
+ struct test_obj *obj;
+
for (num = 75; num--;) {
- int i = ast_random() % lim; /* find a random object */
+ i = ast_random() % limit; /* find a random object */
tmp_obj.i = i;
- if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
- res = AST_TEST_FAIL;
+ obj = ao2_find(look_in, &tmp_obj, OBJ_POINTER);
+ if (!obj) {
ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
+ res = AST_TEST_FAIL;
} else {
- /* a correct match will only take place when the custom cmp function is used */
- if (use_cmp && obj->i != i) {
- ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
+ if (obj->i != i) {
+ ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
res = AST_TEST_FAIL;
}
ao2_t_ref(obj, -1, "test");
}
}
- /* Testing ao2_find with OBJ_KEY */
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Test ao2_find with OBJ_KEY.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param look_in Container to search.
+ * \param limit Container contains objects 0 - (limit - 1).
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_ao2_find_w_OBJ_KEY(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
+{
+ int i;
+ int num;
+ struct test_obj *obj;
+
for (num = 75; num--;) {
- int i = ast_random() % lim; /* find a random object */
-
- if (!(obj = ao2_find(c1, &i, OBJ_KEY))) {
- res = AST_TEST_FAIL;
+ i = ast_random() % limit; /* find a random object */
+
+ obj = ao2_find(look_in, &i, OBJ_KEY);
+ if (!obj) {
ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
+ res = AST_TEST_FAIL;
} else {
- /* a correct match will only take place when the custom cmp function is used */
- if (use_cmp && obj->i != i) {
- ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
+ if (obj->i != i) {
+ ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
res = AST_TEST_FAIL;
}
ao2_t_ref(obj, -1, "test");
}
}
- /* Testing ao2_find with OBJ_PARTIAL_KEY */
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Test ao2_find with OBJ_PARTIAL_KEY.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param look_in Container to search.
+ * \param limit Container contains objects 0 - (limit - 1).
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_ao2_find_w_OBJ_PARTIAL_KEY(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
+{
+ int i;
+ int num;
+ struct test_obj *obj;
+
+ /* Set partial match to find exactly. */
partial_key_match_range = 0;
+
for (num = 100; num--;) {
- int i = ast_random() % lim; /* find a random object */
-
- if (!(obj = ao2_find(c1, &i, OBJ_PARTIAL_KEY))) {
- res = AST_TEST_FAIL;
+ i = ast_random() % limit; /* find a random object */
+
+ obj = ao2_find(look_in, &i, OBJ_PARTIAL_KEY);
+ if (!obj) {
ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_PARTIAL_KEY flag failed.\n", i);
+ res = AST_TEST_FAIL;
} else {
- /* a correct match will only take place when the custom cmp function is used */
- if (use_cmp && obj->i != i) {
- ast_test_status_update(test, "object %d does not match object %d\n", obj->i, tmp_obj.i);
+ if (obj->i != i) {
+ ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
res = AST_TEST_FAIL;
}
ao2_t_ref(obj, -1, "test");
}
}
+
+ return res;
+}
+
+static int astobj2_test_1_helper(int tst_num, enum test_container_type type, int use_sort, unsigned int lim, struct ast_test *test)
+{
+ const char *c_type;
+ struct ao2_container *c1;
+ struct ao2_container *c2;
+ struct ao2_iterator it;
+ struct ao2_iterator *mult_it;
+ struct test_obj *obj;
+ ao2_callback_fn *cmp_fn;
+ int n_buckets;
+ int increment = 0;
+ int destructor_count = 0;
+ int count;
+ int num;
+ int res = AST_TEST_PASS;
+
+ c_type = test_container2str(type);
+ ast_test_status_update(test, "Test %d, %s containers (%s).\n",
+ tst_num, c_type, use_sort ? "sorted" : "non-sorted");
+
+ /* Need at least 12 objects for the special iax2 OBJ_CONTINUE test. */
+ if (lim < 12) {
+ lim = 12;
+ }
+
+ c1 = NULL;
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ /* Lists just have one bucket. */
+ n_buckets = 1;
+ c1 = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
+ use_sort ? test_sort_cb : NULL, test_cmp_cb, "test");
+ break;
+ case TEST_CONTAINER_HASH:
+ n_buckets = (ast_random() % ((lim / 4) + 1)) + 1;
+ if (n_buckets < 6) {
+ /* Need at least 6 buckets for the special iax2 OBJ_CONTINUE test. */
+ n_buckets = 6;
+ }
+ c1 = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, n_buckets,
+ test_hash_cb, use_sort ? test_sort_cb : NULL, test_cmp_cb, "test");
+ break;
+ }
+ c2 = ao2_t_container_alloc(1, NULL, NULL, "test");
+
+ if (!c1 || !c2) {
+ ast_test_status_update(test, "ao2_container_alloc failed.\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+
+ /* Create objects and link into container */
+ destructor_count = lim;
+ for (num = 0; num < lim; ++num) {
+ if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
+ ast_test_status_update(test, "ao2_alloc failed.\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+ obj->destructor_count = &destructor_count;
+ obj->i = num;
+ ao2_link(c1, obj);
+ ao2_t_ref(obj, -1, "test");
+ if (ao2_container_count(c1) != num + 1) {
+ ast_test_status_update(test, "container did not link correctly\n");
+ res = AST_TEST_FAIL;
+ }
+ }
+ if (ao2_container_check(c1, 0)) {
+ ast_test_status_update(test, "container integrity check failed\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+
+ ast_test_status_update(test, "%s container created: buckets: %d, items: %d\n",
+ c_type, n_buckets, lim);
+
+ /* Testing ao2_container_clone */
+ res = test_container_clone(res, c1, test);
+
+ /* Testing ao2_find with no flags */
+ res = test_ao2_find_w_no_flags(res, c1, lim, test);
+
+ /* Testing ao2_find with OBJ_POINTER */
+ res = test_ao2_find_w_OBJ_POINTER(res, c1, lim, test);
+
+ /* Testing ao2_find with OBJ_KEY */
+ res = test_ao2_find_w_OBJ_KEY(res, c1, lim, test);
+
+ /* Testing ao2_find with OBJ_PARTIAL_KEY */
+ res = test_ao2_find_w_OBJ_PARTIAL_KEY(res, c1, lim, test);
/*
* Testing ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE.
@@ -365,83 +515,95 @@
* For this module and set of custom hash/cmp functions, an object
* should only be found if the astobj2 default cmp function is used.
* This test is designed to mimic the chan_iax.c call number use case.
+ *
+ * Must test the custom cmp_cb case first since it should never
+ * find and thus unlink anything for this test.
*/
- num = lim;
- for (count = 0; num && count < 100; ++count) {
- --num;
-
- /* This special manipulation is needed for sorted buckets. */
- special_bucket = num;
- switch (count) {
- case 0:
- /* Beyond end of bucket list. */
- special_match = lim;
+ for (cmp_fn = test_cmp_cb; ; cmp_fn = NULL) {
+ num = lim;
+ for (count = 0; num && count < 100; ++count) {
+ --num;
+
+ /* This special manipulation is needed for sorted hash buckets. */
+ special_bucket = num;
+ switch (count) {
+ case 0:
+ /* Beyond end of bucket list. */
+ special_match = lim;
+ break;
+ case 1:
+ /* At end of bucket list. */
+ special_match = num;
+ break;
+ case 2:
+ /* In between in middle of bucket list. */
+ special_match = num - 1;
+ break;
+ case 3:
+ /* Beginning of bucket list. */
+ special_match = num % n_buckets;
+ break;
+ case 4:
+ /* Before bucket list. */
+ special_match = -1;
+ break;
+ default:
+ /* Empty bucket list. (If possible to empty it.) */
+ special_match = -1;
+ special_bucket = lim - 1;
+ break;
+ }
+
+ /* ao2_find is just a shortcut notation for ao2_callback(). */
+ obj = ao2_callback(c1, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE, cmp_fn, NULL);
+ if (!obj) {
+ if (!cmp_fn) {
+ ast_test_status_update(test,
+ "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default cmp_cb.\n");
+ res = AST_TEST_FAIL;
+ }
+ } else {
+ if (cmp_fn) {
+ ast_test_status_update(test,
+ "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom cmp_cb.\n");
+ res = AST_TEST_FAIL;
+ }
+ ao2_link(c2, obj);
+ ao2_t_ref(obj, -1, "test");
+ }
+ }
+ if (ao2_container_check(c1, 0)) {
+ ast_test_status_update(test, "container integrity check failed\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+ if (ao2_container_check(c2, 0)) {
+ ast_test_status_update(test, "container integrity check failed\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+ it = ao2_iterator_init(c2, 0);
+ while ((obj = ao2_t_iterator_next(&it, "test"))) {
+ ao2_t_unlink(c2, obj, "test");
+ ao2_t_link(c1, obj, "test");
+ ao2_t_ref(obj, -1, "test");
+ }
+ ao2_iterator_destroy(&it);
+ if (ao2_container_check(c1, 0)) {
+ ast_test_status_update(test, "container integrity check failed\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+ if (ao2_container_check(c2, 0)) {
+ ast_test_status_update(test, "container integrity check failed\n");
+ res = AST_TEST_FAIL;
+ goto cleanup;
+ }
+
+ if (!cmp_fn) {
+ /* Completed testing with custom cmp_cb and default cmp_cb */
break;
- case 1:
- /* At end of bucket list. */
- special_match = num;
- break;
- case 2:
- /* In between in middle of bucket list. */
- special_match = num - 1;
- break;
- case 3:
- /* Beginning of bucket list. */
- special_match = num % n_buckets;
- break;
- case 4:
- /* Before bucket list. */
- special_match = -1;
- break;
- default:
- /* Empty bucket list. (If possible to empty it.) */
- special_match = -1;
- special_bucket = lim - 1;
- break;
- }
-
- if (!(obj = ao2_find(c1, NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE))) {
- if (!use_cmp) {
- ast_test_status_update(test,
- "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with default cmp_cb.\n");
- res = AST_TEST_FAIL;
- }
- } else {
- if (use_cmp) {
- ast_test_status_update(test,
- "ao2_find with OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE failed with custom cmp_cb.\n");
- res = AST_TEST_FAIL;
- }
- ao2_link(c2, obj);
- ao2_t_ref(obj, -1, "test");
- }
- }
- if (ao2_container_check(c1, 0)) {
- ast_test_status_update(test, "container integrity check failed\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
- if (ao2_container_check(c2, 0)) {
- ast_test_status_update(test, "container integrity check failed\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
- it = ao2_iterator_init(c2, 0);
- while ((obj = ao2_t_iterator_next(&it, "test"))) {
- ao2_t_unlink(c2, obj, "test");
- ao2_t_link(c1, obj, "test");
- ao2_t_ref(obj, -1, "test");
- }
- ao2_iterator_destroy(&it);
- if (ao2_container_check(c1, 0)) {
- ast_test_status_update(test, "container integrity check failed\n");
- res = AST_TEST_FAIL;
- goto cleanup;
- }
- if (ao2_container_check(c2, 0)) {
- ast_test_status_update(test, "container integrity check failed\n");
- res = AST_TEST_FAIL;
- goto cleanup;
+ }
}
/* Test Callback with no flags. */
@@ -520,6 +682,13 @@
/* Testing iterator. Unlink a single object and break. do not add item back */
it = ao2_iterator_init(c1, 0);
num = ast_random() % lim; /* remove a random object */
+ if (!num) {
+ /*
+ * Well we cannot remove object zero because of test with
+ * all_but_one_cb later.
+ */
+ num = 1;
+ }
while ((obj = ao2_t_iterator_next(&it, "test"))) {
if (obj->i == num) {
ao2_t_unlink(c1, obj, "test");
@@ -560,9 +729,6 @@
}
if (c2) {
ao2_t_ref(c2, -1, "bye c2");
- }
- if (c3) {
- ao2_t_ref(c3, -1, "bye c3");
}
if (destructor_count > 0) {
@@ -594,36 +760,20 @@
break;
}
- /* Test number, use_hash, use_sort, use_cmp, number of objects. */
- if ((res = astobj2_test_1_helper(1, 0, 0, 0, 500, test)) == AST_TEST_FAIL) {
+ /* Test number, container_type, use_sort, number of objects. */
+ if ((res = astobj2_test_1_helper(1, TEST_CONTAINER_LIST, 0, 50, test)) == AST_TEST_FAIL) {
return res;
}
- if ((res = astobj2_test_1_helper(2, 0, 0, 1, 500, test)) == AST_TEST_FAIL) {
+ if ((res = astobj2_test_1_helper(2, TEST_CONTAINER_LIST, 1, 50, test)) == AST_TEST_FAIL) {
return res;
}
- if ((res = astobj2_test_1_helper(3, 0, 1, 0, 500, test)) == AST_TEST_FAIL) {
+ if ((res = astobj2_test_1_helper(3, TEST_CONTAINER_HASH, 0, 1000, test)) == AST_TEST_FAIL) {
return res;
}
- if ((res = astobj2_test_1_helper(4, 0, 1, 1, 500, test)) == AST_TEST_FAIL) {
- return res;
- }
-
- if ((res = astobj2_test_1_helper(5, 1, 0, 0, 1000, test)) == AST_TEST_FAIL) {
- return res;
- }
-
- if ((res = astobj2_test_1_helper(6, 1, 0, 1, 1000, test)) == AST_TEST_FAIL) {
- return res;
- }
-
- if ((res = astobj2_test_1_helper(7, 1, 1, 0, 1000, test)) == AST_TEST_FAIL) {
- return res;
- }
-
- if ((res = astobj2_test_1_helper(8, 1, 1, 1, 1000, test)) == AST_TEST_FAIL) {
+ if ((res = astobj2_test_1_helper(4, TEST_CONTAINER_HASH, 1, 1000, test)) == AST_TEST_FAIL) {
return res;
}
@@ -896,11 +1046,638 @@
return res;
}
+/*!
+ * \internal
+ * \brief Make a nonsorted container for astobj2 testing.
+ * \since 12.0
+ *
+ * \param type Container type to create.
+ * \param options Container options
+ *
+ * \retval container on success.
+ * \retval NULL on error.
+ */
+static struct ao2_container *test_make_nonsorted(enum test_container_type type, int options)
+{
+ struct ao2_container *container;
+
+ container = NULL;
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, options,
+ NULL, test_cmp_cb);
+ break;
+ case TEST_CONTAINER_HASH:
+ container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, options, 5,
+ test_hash_cb, NULL, test_cmp_cb);
+ break;
+ }
+
+ return container;
+}
+
+/*!
+ * \internal
+ * \brief Make a sorted container for astobj2 testing.
+ * \since 12.0
+ *
+ * \param type Container type to create.
+ * \param options Container options
+ *
+ * \retval container on success.
+ * \retval NULL on error.
+ */
+static struct ao2_container *test_make_sorted(enum test_container_type type, int options)
+{
+ struct ao2_container *container;
+
+ container = NULL;
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ container = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, options,
+ test_sort_cb, test_cmp_cb, "test");
+ break;
+ case TEST_CONTAINER_HASH:
+ container = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, options, 5,
+ test_hash_cb, test_sort_cb, test_cmp_cb, "test");
+ break;
+ }
+
+ return container;
+}
+
+/*!
+ * \internal
+ * \brief Insert the given test vector into the given container.
+ * \since 12.0
+ *
+ * \note The given test vector must not have any duplicates.
+ *
+ * \param container Container to insert the test vector.
+ * \param destroy_counter What to increment when the object is destroyed.
+ * \param vector Test vector to insert.
+ * \param count Number of objects in the vector.
+ * \param prefix Test output prefix string.
+ * \param test Test output controller.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+static int insert_test_vector(struct ao2_container *container, int *destroy_counter, const int *vector, int count, const char *prefix, struct ast_test *test)
+{
+ int idx;
+ struct test_obj *obj;
+
+ for (idx = 0; idx < count; ++idx) {
+ obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
+ if (!obj) {
+ ast_test_status_update(test, "%s: ao2_alloc failed.\n", prefix);
+ return -1;
+ }
+ if (destroy_counter) {
+ /* This object ultimately needs to be destroyed. */
+ ++*destroy_counter;
+ }
+ obj->destructor_count = destroy_counter;
+ obj->i = vector[idx];
+ ao2_link(container, obj);
+ ao2_t_ref(obj, -1, "test");
+
+ if (ao2_container_count(container) != idx + 1) {
+ ast_test_status_update(test,
+ "%s: Unexpected container count. Expected:%d Got:%d\n",
+ prefix, idx + 1, ao2_container_count(container));
+ return -1;
+ }
+ }
+ if (ao2_container_check(container, 0)) {
+ ast_test_status_update(test, "%s: Container integrity check failed\n", prefix);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Iterate over the container and compare the objects with the given vector.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param container Container to iterate.
+ * \param flags Flags controlling the iteration.
+ * \param vector Expected vector to find.
+ * \param count Number of objects in the vector.
+ * \param prefix Test output prefix string.
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_ao2_iteration(int res, struct ao2_container *container,
+ enum ao2_iterator_flags flags,
+ const int *vector, int count, const char *prefix, struct ast_test *test)
+{
+ struct ao2_iterator iter;
+ struct test_obj *obj = NULL;
+ int idx;
+
+ if (ao2_container_count(container) != count) {
+ ast_test_status_update(test, "%s: Container count doesn't match vector count.\n",
+ prefix);
+ res = AST_TEST_FAIL;
+ }
+
+ iter = ao2_iterator_init(container, flags);
+
+ /* Check iterated objects against the given vector. */
+ for (idx = 0; idx < count; ++idx) {
+ obj = ao2_iterator_next(&iter);
+ if (!obj) {
+ ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
+ res = AST_TEST_FAIL;
+ break;
+ }
+ if (vector[idx] != obj->i) {
+ ast_test_status_update(test, "%s: Object %d != vector[%d] %d.\n",
+ prefix, obj->i, idx, vector[idx]);
+ res = AST_TEST_FAIL;
+ }
+ ao2_ref(obj, -1); /* remove ref from iterator */
+ }
+ if (obj) {
+ obj = ao2_iterator_next(&iter);
+ if (obj) {
+ ast_test_status_update(test, "%s: Too many objects found. Object %d\n",
+ prefix, obj->i);
+ ao2_ref(obj, -1); /* remove ref from iterator */
+ res = AST_TEST_FAIL;
+ }
+ }
+
+ ao2_iterator_destroy(&iter);
+
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Run an ao2_callback() and compare the returned vector with the given vector.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param container Container to traverse.
+ * \param flags Callback flags controlling the traversal.
+ * \param cmp_fn Compare function to select objects.
+ * \param arg Optional argument.
+ * \param vector Expected vector to find.
+ * \param count Number of objects in the vector.
+ * \param prefix Test output prefix string.
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_ao2_callback_traversal(int res, struct ao2_container *container,
+ enum search_flags flags, ao2_callback_fn *cmp_fn, void *arg,
+ const int *vector, int count, const char *prefix, struct ast_test *test)
+{
+ struct ao2_iterator *mult_iter;
+ struct test_obj *obj;
+ int idx;
+
+ mult_iter = ao2_callback(container, flags | OBJ_MULTIPLE, cmp_fn, arg);
+ if (!mult_iter) {
+ ast_test_status_update(test, "%s: Did not return iterator.\n", prefix);
+ return AST_TEST_FAIL;
+ }
+
+ /* Check matching objects against the given vector. */
+ for (idx = 0; idx < count; ++idx) {
+ obj = ao2_iterator_next(mult_iter);
+ if (!obj) {
+ ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
+ res = AST_TEST_FAIL;
+ break;
+ }
+ if (vector[idx] != obj->i) {
+ ast_test_status_update(test, "%s: Object %d != vector[%d] %d.\n",
+ prefix, obj->i, idx, vector[idx]);
+ res = AST_TEST_FAIL;
+ }
+ ao2_ref(obj, -1); /* remove ref from iterator */
+ }
+ obj = ao2_iterator_next(mult_iter);
+ if (obj) {
+ ast_test_status_update(test, "%s: Too many objects found. Object %d\n",
+ prefix, obj->i);
+ ao2_ref(obj, -1); /* remove ref from iterator */
+ res = AST_TEST_FAIL;
+ }
+ ao2_iterator_destroy(mult_iter);
+
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Test nonsorted container traversal.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param tst_num Test number.
+ * \param type Container type to test.
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_traversal_nonsorted(int res, int tst_num, enum test_container_type type, struct ast_test *test)
+{
+ struct ao2_container *c1;
+ struct ao2_container *c2 = NULL;
+ int partial;
+ int destructor_count = 0;
+
+ /*! Container object insertion vector. */
+ static const int test_initial[] = {
+ 1, 0, 2, 6, 4, 7, 5, 3, 9, 8
+ };
+
+ /*! Container object insertion vector reversed. */
+ static const int test_reverse[] = {
+ 8, 9, 3, 5, 7, 4, 6, 2, 0, 1
+ };
+ static const int test_list_partial_forward[] = {
+ 6, 7, 5
+ };
+ static const int test_list_partial_backward[] = {
+ 5, 7, 6
+ };
+
+ /* The hash orders assume that there are 5 buckets. */
+ static const int test_hash_end_forward[] = {
+ 0, 5, 1, 6, 2, 7, 3, 8, 4, 9
+ };
+ static const int test_hash_end_backward[] = {
+ 9, 4, 8, 3, 7, 2, 6, 1, 5, 0
+ };
+ static const int test_hash_begin_forward[] = {
+ 5, 0, 6, 1, 7, 2, 8, 3, 9, 4
+ };
+ static const int test_hash_begin_backward[] = {
+ 4, 9, 3, 8, 2, 7, 1, 6, 0, 5
+ };
+ static const int test_hash_partial_forward[] = {
+ 5, 6, 7
+ };
+ static const int test_hash_partial_backward[] = {
+ 7, 6, 5
+ };
+
+ ast_test_status_update(test, "Test %d, %s containers.\n",
+ tst_num, test_container2str(type));
+
+ /* Create container that inserts objects at the end. */
+ c1 = test_make_nonsorted(type, 0);
+ if (!c1) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+ if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1", test)) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+
+ /* Create container that inserts objects at the beginning. */
+ c2 = test_make_nonsorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN);
+ if (!c2) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+ if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2", test)) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+
+ /* Check container iteration directions */
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ res = test_ao2_iteration(res, c1, 0,
+ test_initial, ARRAY_LEN(test_initial),
+ "Iteration (ascending, insert end)", test);
+ res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
+ test_reverse, ARRAY_LEN(test_reverse),
+ "Iteration (descending, insert end)", test);
+
+ res = test_ao2_iteration(res, c2, 0,
+ test_reverse, ARRAY_LEN(test_reverse),
+ "Iteration (ascending, insert begin)", test);
+ res = test_ao2_iteration(res, c2, AO2_ITERATOR_DESCENDING,
+ test_initial, ARRAY_LEN(test_initial),
+ "Iteration (descending, insert begin)", test);
+ break;
+ case TEST_CONTAINER_HASH:
+ res = test_ao2_iteration(res, c1, 0,
+ test_hash_end_forward, ARRAY_LEN(test_hash_end_forward),
+ "Iteration (ascending, insert end)", test);
+ res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
+ test_hash_end_backward, ARRAY_LEN(test_hash_end_backward),
+ "Iteration (descending, insert end)", test);
+
+ res = test_ao2_iteration(res, c2, 0,
+ test_hash_begin_forward, ARRAY_LEN(test_hash_begin_forward),
+ "Iteration (ascending, insert begin)", test);
+ res = test_ao2_iteration(res, c2, AO2_ITERATOR_DESCENDING,
+ test_hash_begin_backward, ARRAY_LEN(test_hash_begin_backward),
+ "Iteration (descending, insert begin)", test);
+ break;
+ }
+
+ /* Check container traversal directions */
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
+ test_initial, ARRAY_LEN(test_initial),
+ "Traversal (ascending, insert end)", test);
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
+ test_reverse, ARRAY_LEN(test_reverse),
+ "Traversal (descending, insert end)", test);
+
+ res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_ASCENDING, NULL, NULL,
+ test_reverse, ARRAY_LEN(test_reverse),
+ "Traversal (ascending, insert begin)", test);
+ res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_DESCENDING, NULL, NULL,
+ test_initial, ARRAY_LEN(test_initial),
+ "Traversal (descending, insert begin)", test);
+ break;
+ case TEST_CONTAINER_HASH:
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
+ test_hash_end_forward, ARRAY_LEN(test_hash_end_forward),
+ "Traversal (ascending, insert end)", test);
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
+ test_hash_end_backward, ARRAY_LEN(test_hash_end_backward),
+ "Traversal (descending, insert end)", test);
+
+ res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_ASCENDING, NULL, NULL,
+ test_hash_begin_forward, ARRAY_LEN(test_hash_begin_forward),
+ "Traversal (ascending, insert begin)", test);
+ res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_DESCENDING, NULL, NULL,
+ test_hash_begin_backward, ARRAY_LEN(test_hash_begin_backward),
+ "Traversal (descending, insert begin)", test);
+ break;
+ }
+
+ /* Check traversal with OBJ_PARTIAL_KEY search range. */
+ partial = 6;
+ partial_key_match_range = 1;
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
+ test_cmp_cb, &partial,
+ test_list_partial_forward, ARRAY_LEN(test_list_partial_forward),
+ "Traversal OBJ_PARTIAL_KEY (ascending)", test);
+ res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
+ test_cmp_cb, &partial,
+ test_list_partial_backward, ARRAY_LEN(test_list_partial_backward),
+ "Traversal OBJ_PARTIAL_KEY (descending)", test);
+ break;
+ case TEST_CONTAINER_HASH:
+ res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
+ test_cmp_cb, &partial,
+ test_hash_partial_forward, ARRAY_LEN(test_hash_partial_forward),
+ "Traversal OBJ_PARTIAL_KEY (ascending)", test);
+ res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
+ test_cmp_cb, &partial,
+ test_hash_partial_backward, ARRAY_LEN(test_hash_partial_backward),
+ "Traversal OBJ_PARTIAL_KEY (descending)", test);
+ break;
+ }
+
+test_cleanup:
+ /* destroy containers */
+ if (c1) {
+ ao2_t_ref(c1, -1, "bye c1");
+ }
+ if (c2) {
+ ao2_t_ref(c2, -1, "bye c2");
+ }
+
+ if (destructor_count > 0) {
+ ast_test_status_update(test,
+ "all destructors were not called, destructor count is %d\n",
+ destructor_count);
+ res = AST_TEST_FAIL;
+ } else if (destructor_count < 0) {
+ ast_test_status_update(test,
+ "Destructor was called too many times, destructor count is %d\n",
+ destructor_count);
+ res = AST_TEST_FAIL;
+ }
+
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Test sorted container traversal.
+ * \since 12.0
+ *
+ * \param res Passed in enum ast_test_result_state.
+ * \param tst_num Test number.
+ * \param type Container type to test.
+ * \param test Test output controller.
+ *
+ * \return enum ast_test_result_state
+ */
+static int test_traversal_sorted(int res, int tst_num, enum test_container_type type, struct ast_test *test)
+{
+ struct ao2_container *c1;
+ struct ao2_container *c2 = NULL;
+ int partial;
+ int destructor_count = 0;
+
+ /*! Container object insertion vector. */
+ static const int test_initial[] = {
+ 1, 0, 2, 6, 4, 7, 5, 3, 9, 8
+ };
+
+ /*! Container forward traversal/iteration. */
+ static const int test_forward[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+ };
+ /*! Container backward traversal/iteration. */
+ static const int test_backward[] = {
+ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
+ };
+
+ static const int test_partial_forward[] = {
+ 5, 6, 7
+ };
+ static const int test_partial_backward[] = {
+ 7, 6, 5
+ };
+
+ /* The hash orders assume that there are 5 buckets. */
+ static const int test_hash_forward[] = {
+ 0, 5, 1, 6, 2, 7, 3, 8, 4, 9
+ };
+ static const int test_hash_backward[] = {
+ 9, 4, 8, 3, 7, 2, 6, 1, 5, 0
+ };
+ static const int test_hash_partial_forward[] = {
+ 5, 6, 7
+ };
+ static const int test_hash_partial_backward[] = {
+ 7, 6, 5
+ };
+
+ ast_test_status_update(test, "Test %d, %s containers.\n",
+ tst_num, test_container2str(type));
+
+ /* Create container that inserts objects at the end. */
+ c1 = test_make_sorted(type, 0);
+ if (!c1) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+ if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1", test)) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+
+ /* Create container that inserts objects at the beginning. */
+ c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN);
+ if (!c2) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+ if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2", test)) {
+ res = AST_TEST_FAIL;
+ goto test_cleanup;
+ }
+
+ /* Check container iteration directions */
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ res = test_ao2_iteration(res, c1, 0,
+ test_forward, ARRAY_LEN(test_forward),
+ "Iteration (ascending)", test);
+ res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
+ test_backward, ARRAY_LEN(test_backward),
+ "Iteration (descending)", test);
+ break;
+ case TEST_CONTAINER_HASH:
+ res = test_ao2_iteration(res, c1, 0,
+ test_hash_forward, ARRAY_LEN(test_hash_forward),
+ "Iteration (ascending)", test);
+ res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
+ test_hash_backward, ARRAY_LEN(test_hash_backward),
+ "Iteration (descending)", test);
+ break;
+ }
+
+ /* Check container traversal directions */
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
+ test_forward, ARRAY_LEN(test_forward),
+ "Traversal (ascending)", test);
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
+ test_backward, ARRAY_LEN(test_backward),
+ "Traversal (descending)", test);
+ break;
+ case TEST_CONTAINER_HASH:
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
+ test_hash_forward, ARRAY_LEN(test_hash_forward),
+ "Traversal (ascending, insert end)", test);
+ res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
+ test_hash_backward, ARRAY_LEN(test_hash_backward),
+ "Traversal (descending)", test);
+ break;
+ }
+
+ /* Check traversal with OBJ_PARTIAL_KEY search range. */
+ partial = 6;
+ partial_key_match_range = 1;
+ switch (type) {
+ case TEST_CONTAINER_LIST:
+ res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
+ test_cmp_cb, &partial,
+ test_partial_forward, ARRAY_LEN(test_partial_forward),
[... 88 lines stripped ...]
More information about the asterisk-commits
mailing list