[asterisk-commits] dvossel: branch dvossel/test_api r235131 - in /team/dvossel/test_api: include...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Dec 15 12:28:18 CST 2009
Author: dvossel
Date: Tue Dec 15 12:28:17 2009
New Revision: 235131
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=235131
Log:
convert test_heap.c to use test framework
Modified:
team/dvossel/test_api/include/asterisk/test.h
team/dvossel/test_api/main/test.c
team/dvossel/test_api/tests/test_heap.c
Modified: team/dvossel/test_api/include/asterisk/test.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/include/asterisk/test.h?view=diff&rev=235131&r1=235130&r2=235131
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Tue Dec 15 12:28:17 2009
@@ -88,6 +88,12 @@
*/
typedef int (ast_test_cb_t)(struct ast_str **error_str);
+enum ast_test_result_state {
+ AST_TEST_NOT_RUN = 0,
+ AST_TEST_PASS = 1,
+ AST_TEST_FAIL = 2,
+};
+
/*!
* \brief Initializes test framework.
*
Modified: team/dvossel/test_api/main/test.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/main/test.c?view=diff&rev=235131&r1=235130&r2=235131
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Tue Dec 15 12:28:17 2009
@@ -40,12 +40,6 @@
#define DEFAULT_XML_PATH "asterisk_test_result.xml"
#define DEFAULT_TXT_PATH "asterisk_test_result.txt"
-
-enum ast_test_result_state {
- AST_TEST_NOT_RUN = 0,
- AST_TEST_PASS = 1,
- AST_TEST_FAIL = 2,
-};
static const char *test_result2str[] = {
"NOT RUN",
@@ -666,24 +660,11 @@
AST_CLI_DEFINE(test_cli_generate_results, "generate test results"),
};
-/* Sample Test */
-AST_TEST_DEFINE(sample_test1,
-{
- sleep(1);
- ast_str_set(error_str, 0, "FAKE ERRORS\n");
- return AST_TEST_FAIL;
-})
-AST_TEST_DEFINE(sample_test2,{return AST_TEST_PASS;}) //todohere remove
-
int ast_test_init()
{
/* Register cli commands */
ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
- /* Register Sample Tests */
- AST_TEST_REGISTER("sample_test_1", "main/test", "this is sample test 1", "for example purposes", sample_test1);
- AST_TEST_REGISTER("sample_test_2", "main/test", NULL, NULL, sample_test2);
-
return 0;
}
#endif
Modified: team/dvossel/test_api/tests/test_heap.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/tests/test_heap.c?view=diff&rev=235131&r1=235130&r2=235131
==============================================================================
--- team/dvossel/test_api/tests/test_heap.c (original)
+++ team/dvossel/test_api/tests/test_heap.c Tue Dec 15 12:28:17 2009
@@ -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,21 @@
}
}
-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, },
- };
+ struct node nodes[3];
+
+ nodes[0].val = nodes[0].index = 1;
+ nodes[1].val = nodes[1].index = 2;
+ nodes[2].val = nodes[2].index = 3;
if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
return -1;
}
/* 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");
ast_heap_push(h, &nodes[0]);
@@ -82,52 +79,48 @@
obj = ast_heap_pop(h);
if (obj->val != 3) {
- return -2;
+ return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj->val != 2) {
- return -3;
+ return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj->val != 1) {
- return -4;
+ return AST_TEST_FAIL;
}
obj = ast_heap_pop(h);
if (obj) {
- return -5;
+ return AST_TEST_FAIL;
}
h = ast_heap_destroy(h);
- ast_cli(fd, "Test #1 successful.\n");
+ return AST_TEST_PASS;
+}
+)
- return 0;
-}
-
-static int test2(int fd)
+AST_TEST_DEFINE(heap_test_2,
{
struct ast_heap *h = NULL;
static const unsigned int one_million = 1000000;
struct node *nodes = NULL;
struct node *node;
unsigned int i = one_million;
- long last = LONG_MAX, cur;
- int res = 0;
-
- 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 are come back off in the proper order\n");
+ long last = LONG_MAX;
+ long cur;
+ int res = AST_TEST_PASS;
if (!(nodes = ast_malloc(one_million * sizeof(*node)))) {
- res = -1;
+ 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 +130,7 @@
}
if (ast_heap_verify(h)) {
- res = -3;
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -145,8 +138,8 @@
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_str_set(error_str, 0, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
+ res = AST_TEST_FAIL;
goto return_cleanup;
}
last = cur;
@@ -154,12 +147,10 @@
}
if (i != one_million) {
- ast_cli(fd, "Stopped popping off after only getting %u nodes\n", i);
- res = -5;
+ ast_str_set(error_str, 0, "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) {
@@ -170,53 +161,31 @@
}
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;
- }
-
- 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);
return 0;
}
static int load_module(void)
{
- ast_cli_register_multiple(cli_heap, ARRAY_LEN(cli_heap));
+
+ AST_TEST_REGISTER("heap_test_1",
+ "main/heap",
+ "push and pop elements",
+ "Push a few elements onto a heap and make sure that they come back off in the right order.",
+ heap_test_1);
+
+ AST_TEST_REGISTER("heap_test_2",
+ "main/heap",
+ "load test",
+ "Push a million 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",
+ heap_test_2);
+
return AST_MODULE_LOAD_SUCCESS;
}
More information about the asterisk-commits
mailing list