aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2010-06-25 18:58:37 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2010-06-25 18:58:37 +0000
commita3a4d3061dc9bbfae8f387fe9295a1cbe94db748 (patch)
treeaf460717bd24b7267eca56910c9dae1e9d8d1bc3 /tests
parentaf204cb6e6ccd0702d6da0ab960f3222d9590091 (diff)
Backport unit test API from trunk.
Also, update existing test modules that were already in this branch but had been converted to the unit test API in trunk. Review: https://reviewboard.asterisk.org/r/748/ git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.2@272531 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'tests')
-rw-r--r--tests/test_heap.c193
-rw-r--r--tests/test_sched.c81
-rw-r--r--tests/test_skel.c44
3 files changed, 172 insertions, 146 deletions
diff --git a/tests/test_heap.c b/tests/test_heap.c
index 3ba9abb48..de48f0ae4 100644
--- a/tests/test_heap.c
+++ b/tests/test_heap.c
@@ -24,7 +24,7 @@
*/
/*** MODULEINFO
- <defaultenabled>no</defaultenabled>
+ <depend>TEST_FRAMEWORK</depend>
***/
#include "asterisk.h"
@@ -32,9 +32,9 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h"
-#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/heap.h"
+#include "asterisk/test.h"
struct node {
long val;
@@ -55,24 +55,30 @@ static int node_cmp(void *_n1, void *_n2)
}
}
-static int test1(int fd)
+AST_TEST_DEFINE(heap_test_1)
{
struct ast_heap *h;
struct node *obj;
struct node nodes[3] = {
- { 1, },
- { 2, },
- { 3, },
+ { 1, } ,
+ { 2, } ,
+ { 3, } ,
};
- if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
- return -1;
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "heap_test_1";
+ info->category = "main/heap/";
+ info->summary = "push and pop elements";
+ info->description = "Push a few elements onto a heap and make sure that they come back off in the right order.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
}
- /* Pushing 1 2 3, and then popping 3 elements */
-
- ast_cli(fd, "Test #1 - Push a few elements onto a heap and make sure that they "
- "come back off in the right order.\n");
+ if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
+ return AST_TEST_FAIL;
+ }
ast_heap_push(h, &nodes[0]);
@@ -82,52 +88,66 @@ static int test1(int fd)
obj = ast_heap_pop(h);
if (obj->val != 3) {
- return -2;
+ ast_test_status_update(test, "expected 3, got %ld\n", obj->val);
+ return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj->val != 2) {
- return -3;
+ ast_test_status_update(test, "expected 2, got %ld\n", obj->val);
+ return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj->val != 1) {
- return -4;
+ ast_test_status_update(test, "expected 1, got %ld\n", obj->val);
+ return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj) {
- return -5;
+ ast_test_status_update(test, "got unexpected object\n");
+ return AST_TEST_FAIL;
}
h = ast_heap_destroy(h);
- ast_cli(fd, "Test #1 successful.\n");
-
- return 0;
+ return AST_TEST_PASS;
}
-static int test2(int fd)
+AST_TEST_DEFINE(heap_test_2)
{
struct ast_heap *h = NULL;
- static const unsigned int one_million = 1000000;
+ static const unsigned int total = 100000;
struct node *nodes = NULL;
struct node *node;
- unsigned int i = one_million;
- long last = LONG_MAX, cur;
- int res = 0;
+ unsigned int i = total;
+ long last = LONG_MAX;
+ long cur;
+ enum ast_test_result_state res = AST_TEST_PASS;
- ast_cli(fd, "Test #2 - Push a million random elements on to a heap, "
- "verify that the heap has been properly constructed, "
- "and then ensure that the elements come back off in the proper order\n");
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "heap_test_2";
+ info->category = "main/heap/";
+ info->summary = "load test";
+ info->description =
+ "Push one hundred thousand random elements on to a heap, "
+ "verify that the heap has been properly constructed, "
+ "and then ensure that the elements are come back off "
+ "in the proper order.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
- if (!(nodes = ast_malloc(one_million * sizeof(*node)))) {
- res = -1;
+ if (!(nodes = ast_malloc(total * sizeof(*node)))) {
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
- res = -2;
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -137,7 +157,7 @@ static int test2(int fd)
}
if (ast_heap_verify(h)) {
- res = -3;
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -145,22 +165,20 @@ static int test2(int fd)
while ((node = ast_heap_pop(h))) {
cur = node->val;
if (cur > last) {
- ast_cli(fd, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
- res = -4;
+ ast_test_status_update(test, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
last = cur;
i++;
}
- if (i != one_million) {
- ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
- res = -5;
+ if (i != total) {
+ ast_test_status_update(test, "Stopped popping off after only getting %u nodes\n", i);
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
- ast_cli(fd, "Test #2 successful.\n");
-
return_cleanup:
if (h) {
h = ast_heap_destroy(h);
@@ -172,7 +190,7 @@ return_cleanup:
return res;
}
-static int test3(int fd)
+AST_TEST_DEFINE(heap_test_3)
{
struct ast_heap *h = NULL;
struct node *nodes = NULL;
@@ -180,20 +198,33 @@ static int test3(int fd)
static const unsigned int test_size = 100000;
unsigned int i = test_size;
long last = LONG_MAX, cur;
- int res = 0;
int random_index;
+ enum ast_test_result_state res = AST_TEST_PASS;
- ast_cli(fd, "Test #3 - Push a hundred thousand random elements on to a heap, "
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "heap_test_3";
+ info->category = "main/heap/";
+ info->summary = "random element removal test";
+ info->description =
+ "Push a hundred thousand random elements on to a heap, "
"verify that the heap has been properly constructed, "
- "randomly remove and re-add 10000 elements and then ensure that the elements come back off in the proper order\n");
+ "randomly remove and re-add 10000 elements, and then "
+ "ensure that the elements come back off in the proper order.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
if (!(nodes = ast_malloc(test_size * sizeof(*node)))) {
- res = -1;
+ ast_test_status_update(test, "memory allocation failure\n");
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
if (!(h = ast_heap_create(20, node_cmp, offsetof(struct node, index)))) {
- res = -2;
+ ast_test_status_update(test, "Failed to allocate heap\n");
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -203,7 +234,8 @@ static int test3(int fd)
}
if (ast_heap_verify(h)) {
- res = -3;
+ ast_test_status_update(test, "Failed to verify heap after populating it\n");
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -212,15 +244,16 @@ static int test3(int fd)
random_index = ast_random() % test_size;
node = ast_heap_remove(h, &nodes[random_index]);
if (nodes[random_index].val != node->val){
- res = -4;
+ ast_test_status_update(test, "Failed to remove what we expected to\n");
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
- ast_heap_push(h, &nodes[random_index]);
-
+ ast_heap_push(h, &nodes[random_index]);
}
if (ast_heap_verify(h)) {
- res = -5;
+ ast_test_status_update(test, "Failed to verify after removals\n");
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -228,8 +261,8 @@ static int test3(int fd)
while ((node = ast_heap_pop(h))) {
cur = node->val;
if (cur > last) {
- ast_cli(fd, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
- res = -6;
+ ast_test_status_update(test, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
last = cur;
@@ -237,13 +270,11 @@ static int test3(int fd)
}
if (i != test_size) {
- ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
- res = -7;
+ ast_test_status_update(test, "Stopped popping off after only getting %u nodes\n", i);
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
- ast_cli(fd, "Test #3 successful.\n");
-
return_cleanup:
if (h) {
h = ast_heap_destroy(h);
@@ -255,57 +286,21 @@ return_cleanup:
return res;
}
-
-static char *handle_cli_heap_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
- int res;
-
- switch (cmd) {
- case CLI_INIT:
- e->command = "heap test";
- e->usage = ""
- "Usage: heap test\n"
- "";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
-
- if (a->argc != e->args) {
- return CLI_SHOWUSAGE;
- }
-
- if ((res = test1(a->fd))) {
- ast_cli(a->fd, "Test 1 failed! (%d)\n", res);
- return CLI_FAILURE;
- }
-
- if ((res = test2(a->fd))) {
- ast_cli(a->fd, "Test 2 failed! (%d)\n", res);
- return CLI_FAILURE;
- }
-
- if ((res = test3(a->fd))) {
- ast_cli(a->fd, "Test 3 failed! (%d)\n", res);
- return CLI_FAILURE;
- }
-
- return CLI_SUCCESS;
-}
-
-static struct ast_cli_entry cli_heap[] = {
- AST_CLI_DEFINE(handle_cli_heap_test, "Test the heap implementation"),
-};
-
static int unload_module(void)
{
- ast_cli_unregister_multiple(cli_heap, ARRAY_LEN(cli_heap));
+ AST_TEST_UNREGISTER(heap_test_1);
+ AST_TEST_UNREGISTER(heap_test_2);
+ AST_TEST_UNREGISTER(heap_test_3);
+
return 0;
}
static int load_module(void)
{
- ast_cli_register_multiple(cli_heap, ARRAY_LEN(cli_heap));
+ AST_TEST_REGISTER(heap_test_1);
+ AST_TEST_REGISTER(heap_test_2);
+ AST_TEST_REGISTER(heap_test_3);
+
return AST_MODULE_LOAD_SUCCESS;
}
diff --git a/tests/test_sched.c b/tests/test_sched.c
index afbb75949..6cb9f61cb 100644
--- a/tests/test_sched.c
+++ b/tests/test_sched.c
@@ -24,7 +24,7 @@
*/
/*** MODULEINFO
- <defaultenabled>no</defaultenabled>
+ <depend>TEST_FRAMEWORK</depend>
***/
#include "asterisk.h"
@@ -34,122 +34,124 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h"
-#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/sched.h"
+#include "asterisk/test.h"
+#include "asterisk/cli.h"
static int sched_cb(const void *data)
{
return 0;
}
-static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+AST_TEST_DEFINE(sched_test_order)
{
struct sched_context *con;
- char *res = CLI_FAILURE;
+ enum ast_test_result_state res = AST_TEST_FAIL;
int id1, id2, id3, wait;
switch (cmd) {
- case CLI_INIT:
- e->command = "sched test";
- e->usage = ""
- "Usage: sched test\n"
- " Test scheduler entry ordering.\n"
- "";
- return NULL;
- case CLI_GENERATE:
- return NULL;
+ case TEST_INIT:
+ info->name = "sched_test_order";
+ info->category = "main/sched/";
+ info->summary = "Test ordering of events in the scheduler API";
+ info->description =
+ "This test ensures that events are properly ordered by the "
+ "time they are scheduled to execute in the scheduler API.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
}
- if (a->argc != e->args) {
- return CLI_SHOWUSAGE;
- }
-
- ast_cli(a->fd, "Testing scheduler entry ordering ...\n");
-
if (!(con = sched_context_create())) {
- ast_cli(a->fd, "Test failed - could not create scheduler context\n");
- return CLI_FAILURE;
+ ast_test_status_update(test,
+ "Test failed - could not create scheduler context\n");
+ return AST_TEST_FAIL;
}
/* Add 3 scheduler entries, and then remove them, ensuring that the result
* of ast_sched_wait() looks appropriate at each step along the way. */
if ((wait = ast_sched_wait(con)) != -1) {
- ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned -1, returned '%d'\n",
wait);
goto return_cleanup;
}
if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) {
- ast_cli(a->fd, "Failed to add scheduler entry\n");
+ ast_test_status_update(test, "Failed to add scheduler entry\n");
goto return_cleanup;
}
if ((wait = ast_sched_wait(con)) > 100000) {
- ast_cli(a->fd, "ast_sched_wait() should have returned <= 100000, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned <= 100000, returned '%d'\n",
wait);
goto return_cleanup;
}
if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) {
- ast_cli(a->fd, "Failed to add scheduler entry\n");
+ ast_test_status_update(test, "Failed to add scheduler entry\n");
goto return_cleanup;
}
if ((wait = ast_sched_wait(con)) > 10000) {
- ast_cli(a->fd, "ast_sched_wait() should have returned <= 10000, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned <= 10000, returned '%d'\n",
wait);
goto return_cleanup;
}
if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) {
- ast_cli(a->fd, "Failed to add scheduler entry\n");
+ ast_test_status_update(test, "Failed to add scheduler entry\n");
goto return_cleanup;
}
if ((wait = ast_sched_wait(con)) > 1000) {
- ast_cli(a->fd, "ast_sched_wait() should have returned <= 1000, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned <= 1000, returned '%d'\n",
wait);
goto return_cleanup;
}
if (ast_sched_del(con, id3) == -1) {
- ast_cli(a->fd, "Failed to remove scheduler entry\n");
+ ast_test_status_update(test, "Failed to remove scheduler entry\n");
goto return_cleanup;
}
if ((wait = ast_sched_wait(con)) <= 1000) {
- ast_cli(a->fd, "ast_sched_wait() should have returned > 1000, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned > 1000, returned '%d'\n",
wait);
goto return_cleanup;
}
if (ast_sched_del(con, id2) == -1) {
- ast_cli(a->fd, "Failed to remove scheduler entry\n");
+ ast_test_status_update(test, "Failed to remove scheduler entry\n");
goto return_cleanup;
}
if ((wait = ast_sched_wait(con)) <= 10000) {
- ast_cli(a->fd, "ast_sched_wait() should have returned > 10000, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned > 10000, returned '%d'\n",
wait);
goto return_cleanup;
}
if (ast_sched_del(con, id1) == -1) {
- ast_cli(a->fd, "Failed to remove scheduler entry\n");
+ ast_test_status_update(test, "Failed to remove scheduler entry\n");
goto return_cleanup;
}
if ((wait = ast_sched_wait(con)) != -1) {
- ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n",
+ ast_test_status_update(test,
+ "ast_sched_wait() should have returned -1, returned '%d'\n",
wait);
goto return_cleanup;
}
- res = CLI_SUCCESS;
-
- ast_cli(a->fd, "Test passed!\n");
+ res = AST_TEST_PASS;
return_cleanup:
sched_context_destroy(con);
@@ -168,7 +170,7 @@ static char *handle_cli_sched_bench(struct ast_cli_entry *e, int cmd, struct ast
case CLI_INIT:
e->command = "sched benchmark";
e->usage = ""
- "Usage: sched test <num>\n"
+ "Usage: sched benchmark <num>\n"
"";
return NULL;
case CLI_GENERATE:
@@ -233,17 +235,18 @@ return_cleanup:
static struct ast_cli_entry cli_sched[] = {
AST_CLI_DEFINE(handle_cli_sched_bench, "Benchmark ast_sched add/del performance"),
- AST_CLI_DEFINE(handle_cli_sched_test, "Test scheduler entry ordering"),
};
static int unload_module(void)
{
+ AST_TEST_UNREGISTER(sched_test_order);
ast_cli_unregister_multiple(cli_sched, ARRAY_LEN(cli_sched));
return 0;
}
static int load_module(void)
{
+ AST_TEST_REGISTER(sched_test_order);
ast_cli_register_multiple(cli_sched, ARRAY_LEN(cli_sched));
return AST_MODULE_LOAD_SUCCESS;
}
diff --git a/tests/test_skel.c b/tests/test_skel.c
index 9f8008658..c1ce5e844 100644
--- a/tests/test_skel.c
+++ b/tests/test_skel.c
@@ -16,8 +16,8 @@
* at the top of the source tree.
*/
-/*! \file
- *
+/*!
+ * \file
* \brief Skeleton Test
*
* \author\verbatim <Your Name Here> <<Your Email Here>> \endverbatim
@@ -27,27 +27,55 @@
*/
/*** MODULEINFO
- <defaultenabled>no</defaultenabled>
+ <depend>TEST_FRAMEWORK</depend>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-#include "asterisk/file.h"
-#include "asterisk/channel.h"
-#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
#include "asterisk/module.h"
-#include "asterisk/lock.h"
-#include "asterisk/app.h"
+#include "asterisk/test.h"
+
+AST_TEST_DEFINE(sample_test)
+{
+ void *ptr;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "sample_test";
+ info->category = "main/sample/";
+ info->summary = "sample unit test";
+ info->description =
+ "This demonstrates what is required to implement "
+ "a unit test.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_status_update(test, "Executing sample test...\n");
+
+ if (!(ptr = ast_malloc(8))) {
+ ast_test_status_update(test, "ast_malloc() failed\n");
+ return AST_TEST_FAIL;
+ }
+
+ ast_free(ptr);
+
+ return AST_TEST_PASS;
+}
static int unload_module(void)
{
+ AST_TEST_UNREGISTER(sample_test);
return 0;
}
static int load_module(void)
{
+ AST_TEST_REGISTER(sample_test);
return AST_MODULE_LOAD_SUCCESS;
}