[asterisk-commits] rmudgett: branch rmudgett/ao2_enhancements r371248 - /team/rmudgett/ao2_enhan...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Aug 14 15:30:12 CDT 2012
Author: rmudgett
Date: Tue Aug 14 15:30:08 2012
New Revision: 371248
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=371248
Log:
Adjust astobj2_test_1() tests to handle sorted list/hash containers.
Add OBJ_PARTIAL_KEY test to astobj2_test_1().
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=371248&r1=371247&r2=371248
==============================================================================
--- team/rmudgett/ao2_enhancements/tests/test_astobj2.c (original)
+++ team/rmudgett/ao2_enhancements/tests/test_astobj2.c Tue Aug 14 15:30:08 2012
@@ -38,14 +38,24 @@
#include "asterisk/astobj2.h"
struct test_obj {
+ /*! What to increment when object is destroyed. */
+ int *destructor_count;
+ /*! Container object key */
int i;
- int *destructor_count;
};
-static void test_obj_destructor(void *obj)
-{
- struct test_obj *test_obj = (struct test_obj *) obj;
- *test_obj->destructor_count = *test_obj->destructor_count - 1;
+/*! Partial search key +/- matching range. */
+int partial_key_match_range;
+/*! Special iax2 OBJ_CONTINUE test. Bucket selected. */
+int special_bucket;
+/*! Special iax2 OBJ_CONTINUE test. Object number select. */
+int special_match;
+
+static void test_obj_destructor(void *v_obj)
+{
+ struct test_obj *obj = (struct test_obj *) v_obj;
+
+ --*obj->destructor_count;
}
static int increment_cb(void *obj, void *arg, int flag)
@@ -58,54 +68,104 @@
static int all_but_one_cb(void *obj, void *arg, int flag)
{
- struct test_obj *test_obj = (struct test_obj *) obj;
-
- return (test_obj->i > 1) ? CMP_MATCH : 0;
+ struct test_obj *cmp_obj = (struct test_obj *) obj;
+
+ return (cmp_obj->i) ? CMP_MATCH : 0;
}
static int multiple_cb(void *obj, void *arg, int flag)
{
int *i = (int *) arg;
- struct test_obj *test_obj = (struct test_obj *) obj;
-
- return (test_obj->i <= *i) ? CMP_MATCH : 0;
+ struct test_obj *cmp_obj = (struct test_obj *) obj;
+
+ return (cmp_obj->i < *i) ? CMP_MATCH : 0;
}
static int test_cmp_cb(void *obj, void *arg, int flags)
{
struct test_obj *cmp_obj = (struct test_obj *) obj;
-
- if (!arg) {
- return 0;
- }
if (flags & OBJ_KEY) {
int *i = (int *) arg;
- return (cmp_obj->i == *i) ? CMP_MATCH | CMP_STOP : 0;
+
+ return (cmp_obj->i == *i) ? CMP_MATCH : 0;
+ } else if (flags & OBJ_PARTIAL_KEY) {
+ int *i = (int *) arg;
+
+ return (*i - partial_key_match_range <= cmp_obj->i
+ && cmp_obj->i <= *i + partial_key_match_range) ? CMP_MATCH : 0;
} else {
- struct test_obj *test_obj = (struct test_obj *) arg;
- return (cmp_obj->i == test_obj->i) ? CMP_MATCH | CMP_STOP : 0;
+ struct test_obj *arg_obj = (struct test_obj *) arg;
+
+ if (!arg_obj) {
+ /* Never match on the special iax2 OBJ_CONTINUE test. */
+ return 0;
+ }
+
+ return (cmp_obj->i == arg_obj->i) ? CMP_MATCH : 0;
}
}
static int test_hash_cb(const void *obj, const int flags)
{
- if (!obj) {
- return 0;
- }
-
if (flags & OBJ_KEY) {
const int *i = obj;
return *i;
+ } else if (flags & OBJ_PARTIAL_KEY) {
+ /* This is absolutely wrong to be called with this flag value. */
+ abort();
+ /* Just in case abort() doesn't work or something else super silly */
+ *((int *) 0) = 0;
+ return 0;
} else {
- const struct test_obj *test_obj = obj;
-
- return test_obj->i;
- }
-}
-
-static int astobj2_test_helper(int use_hash, int use_cmp, unsigned int lim, struct ast_test *test)
+ const struct test_obj *hash_obj = obj;
+
+ if (!hash_obj) {
+ /*
+ * Use the special_bucket as the bucket for the special iax2
+ * OBJ_CONTINUE test.
+ */
+ return special_bucket;
+ }
+
+ return hash_obj->i;
+ }
+}
+
+static int test_sort_cb(const void *obj_left, const void *obj_right, int flags)
+{
+ const struct test_obj *test_left = obj_left;
+
+ if (flags & OBJ_KEY) {
+ const int *i = obj_right;
+
+ return test_left->i - *i;
+ } else if (flags & OBJ_PARTIAL_KEY) {
+ int *i = (int *) obj_right;
+
+ if (*i - partial_key_match_range <= test_left->i
+ && test_left->i <= *i + partial_key_match_range) {
+ return 0;
+ }
+
+ return test_left->i - *i;
+ } else {
+ const struct test_obj *test_right = obj_right;
+
+ if (!test_right) {
+ /*
+ * Compare with special_match in the special iax2 OBJ_CONTINUE
+ * test.
+ */
+ return test_left->i - special_match;
+ }
+
+ return test_left->i - test_right->i;
+ }
+}
+
+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;
@@ -115,20 +175,40 @@
struct test_obj *obj;
struct test_obj *obj2;
struct test_obj tmp_obj;
- int bucket_size;
+ int n_buckets;
int increment = 0;
int destructor_count = 0;
+ int count;
int num;
- int res = AST_TEST_PASS;
-
- /* This test needs at least 5 objects */
- if (lim < 5) {
- lim = 5;
- }
-
- bucket_size = (ast_random() % ((lim / 4) + 1)) + 1;
- c1 = ao2_t_container_alloc(bucket_size, use_hash ? test_hash_cb : NULL, use_cmp ? test_cmp_cb : NULL, "test");
- c2 = ao2_t_container_alloc(bucket_size, test_hash_cb, test_cmp_cb, "test");
+ 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;
+ }
+ } 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");
@@ -138,7 +218,7 @@
/* Create objects and link into container */
destructor_count = lim;
- for (num = 1; num <= lim; num++) {
+ 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;
@@ -148,7 +228,7 @@
obj->i = num;
ao2_link(c1, obj);
ao2_t_ref(obj, -1, "test");
- if (ao2_container_count(c1) != num) {
+ if (ao2_container_count(c1) != num + 1) {
ast_test_status_update(test, "container did not link correctly\n");
res = AST_TEST_FAIL;
}
@@ -159,7 +239,7 @@
goto cleanup;
}
- ast_test_status_update(test, "Container created: random bucket size %d: number of items: %d\n", bucket_size, lim);
+ 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);
@@ -182,8 +262,6 @@
/*
* Unlink the matching object from the cloned container to make
* the next search faster. This is a big speed optimization!
- * It reduces the container with 100000 objects test time from
- * 18 seconds to 200 ms.
*/
obj2 = ao2_t_callback(c3, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
"test clone");
@@ -209,9 +287,9 @@
c3 = NULL;
/* Testing ao2_find with no flags */
- num = 100;
- for (; num; num--) {
- int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
+ for (num = 100; num--;) {
+ int i = ast_random() % lim; /* find a random object */
+
tmp_obj.i = i;
if (!(obj = ao2_find(c1, &tmp_obj, 0))) {
res = AST_TEST_FAIL;
@@ -227,9 +305,9 @@
}
/* Testing ao2_find with OBJ_POINTER */
- num = 75;
- for (; num; num--) {
- int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
+ for (num = 75; num--;) {
+ int i = ast_random() % lim; /* find a random object */
+
tmp_obj.i = i;
if (!(obj = ao2_find(c1, &tmp_obj, OBJ_POINTER))) {
res = AST_TEST_FAIL;
@@ -245,12 +323,30 @@
}
/* Testing ao2_find with OBJ_KEY */
- num = 75;
- for (; num; num--) {
- int i = (ast_random() % ((lim / 2)) + 1); /* find a random object */
+ 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;
ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
+ } 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);
+ res = AST_TEST_FAIL;
+ }
+ ao2_t_ref(obj, -1, "test");
+ }
+ }
+
+ /* Testing ao2_find with OBJ_PARTIAL_KEY */
+ 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;
+ ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_PARTIAL_KEY flag failed.\n", i);
} else {
/* a correct match will only take place when the custom cmp function is used */
if (use_cmp && obj->i != i) {
@@ -270,16 +366,50 @@
* 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.
*/
- num = lim < 25 ? lim : 25;
- for (; num; num--) {
+ 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;
+ 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 hash function.\n");
+ 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 hash function.\n");
+ 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);
@@ -330,7 +460,7 @@
res = AST_TEST_FAIL;
}
- /* Test OBJ_MULTIPLE with OBJ_UNLINK*/
+ /* Test OBJ_MULTIPLE with OBJ_UNLINK, add items back afterwards */
num = lim < 25 ? lim : 25;
if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK, multiple_cb, &num, "test multiple"))) {
ast_test_status_update(test, "OBJ_MULTIPLE with OBJ_UNLINK test failed.\n");
@@ -360,8 +490,8 @@
}
}
- /* Test OBJ_MULTIPLE without unlink, add items back afterwards */
- num = lim < 25 ? lim : 25;
+ /* Test OBJ_MULTIPLE without unlink and iterate the returned container */
+ num = 5;
if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
ast_test_status_update(test, "OBJ_MULTIPLE without OBJ_UNLINK test failed.\n");
res = AST_TEST_FAIL;
@@ -373,7 +503,7 @@
}
/* Test OBJ_MULTIPLE without unlink and no iterating */
- num = lim < 5 ? lim : 5;
+ num = 5;
if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
ast_test_status_update(test, "OBJ_MULTIPLE with no OBJ_UNLINK and no iterating failed.\n");
res = AST_TEST_FAIL;
@@ -389,11 +519,11 @@
/* Testing iterator. Unlink a single object and break. do not add item back */
it = ao2_iterator_init(c1, 0);
- num = (lim / 4) + 1;
+ num = ast_random() % lim; /* remove a random object */
while ((obj = ao2_t_iterator_next(&it, "test"))) {
if (obj->i == num) {
+ ao2_t_unlink(c1, obj, "test");
ao2_t_ref(obj, -1, "test");
- ao2_t_unlink(c1, obj, "test");
break;
}
ao2_t_ref(obj, -1, "test");
@@ -454,7 +584,7 @@
case TEST_INIT:
info->name = "astobj2_test1";
info->category = "/main/astobj2/";
- info->summary = "astobj2 test using ao2 objects, containers, callbacks, and iterators";
+ info->summary = "Test ao2 objects, containers, callbacks, and iterators";
info->description =
"Builds ao2_containers with various item numbers, bucket sizes, cmp and hash "
"functions. Runs a series of tests to manipulate the container using callbacks "
@@ -464,28 +594,36 @@
break;
}
-
- /* Test 1, 500 items with custom hash and cmp functions */
- ast_test_status_update(test, "Test 1, astobj2 test with 500 items.\n");
- if ((res = astobj2_test_helper(1, 1, 500, test)) == AST_TEST_FAIL) {
+ /* 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) {
return res;
}
- /* Test 2, 1000 items with custom hash and default cmp functions */
- ast_test_status_update(test, "Test 2, astobj2 test with 1000 items.\n");
- if ((res = astobj2_test_helper(1, 0, 1000, test)) == AST_TEST_FAIL) {
+ if ((res = astobj2_test_1_helper(2, 0, 0, 1, 500, test)) == AST_TEST_FAIL) {
return res;
}
- /* Test 3, 10000 items with default hash and custom cmp functions */
- ast_test_status_update(test, "Test 3, astobj2 test with 10000 items.\n");
- if ((res = astobj2_test_helper(0, 1, 10000, test)) == AST_TEST_FAIL) {
+ if ((res = astobj2_test_1_helper(3, 0, 1, 0, 500, test)) == AST_TEST_FAIL) {
return res;
}
- /* Test 4, 100000 items with default hash and cmp functions */
- ast_test_status_update(test, "Test 4, astobj2 test with 100000 items.\n");
- if ((res = astobj2_test_helper(0, 0, 100000, test)) == AST_TEST_FAIL) {
+ 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) {
return res;
}
More information about the asterisk-commits
mailing list