[asterisk-commits] dvossel: branch dvossel/test_api r235379 - in /team/dvossel/test_api: include...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Dec 16 17:42:46 CST 2009
Author: dvossel
Date: Wed Dec 16 17:42:45 2009
New Revision: 235379
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=235379
Log:
fixes first round of reviewboard comments
Modified:
team/dvossel/test_api/include/asterisk/_private.h
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/_private.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/include/asterisk/_private.h?view=diff&rev=235379&r1=235378&r2=235379
==============================================================================
--- team/dvossel/test_api/include/asterisk/_private.h (original)
+++ team/dvossel/test_api/include/asterisk/_private.h Wed Dec 16 17:42:45 2009
@@ -44,7 +44,8 @@
void ast_stun_init(void); /*!< Provided by stun.c */
int ast_cel_engine_init(void); /*!< Provided by cel.c */
int ast_cel_engine_reload(void); /*!< Provided by cel.c */
-int ast_ssl_init(void); /*!< Porvided by ssl.c */
+int ast_ssl_init(void); /*!< Provided by ssl.c */
+int ast_test_init(void); /*!< Provided by test.c */
/*!
* \brief Reload asterisk modules.
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=235379&r1=235378&r2=235379
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Wed Dec 16 17:42:45 2009
@@ -1,7 +1,7 @@
/*
* Asterisk -- An open source telephony toolkit.
*
- * Copyright (C) 2007 - 2009, Digium, Inc.
+ * Copyright (C) 2009, Digium, Inc.
*
* David Vossel <dvossel at digium.com>
*
@@ -23,11 +23,14 @@
*/
#ifdef TEST_FRAMEWORK
+#ifndef _AST_TEST_H_
+#define _AST_TEST_H_
+
#include "asterisk/cli.h"
#include "asterisk/strings.h"
#endif
-/* USING THE TEST FRAMEWORK
+/*! USING THE TEST FRAMEWORK
1. DEFINE TEST: Create a callback function for the test
using the AST_TEST_DEFINE macro.
@@ -39,7 +42,7 @@
.
.
if (fail) { \\ the following is just some example logic
- ast_str_set(error_str, 0 , "an error occured because...");
+ ast_str_set(ast_test_error_str, 0 , "an error occured because...");
res = AST_RESULT_FAIL;
} else {
res = AST_RESULT_PASS
@@ -69,7 +72,7 @@
Tests are unregestered by using the AST_TEST_UNREGISTER macro.
- AST_TEST_UNREGISTER(sample_test_cb); \\ Remove a registered test by callback function
+ AST_TEST_UNREGISTER(sample_test_cb); \\ Remove a registered test by callback function
3. EXECUTE: Execute and generate test results via CLI commands
@@ -83,7 +86,7 @@
/*! Macros used for defining and registering a test */
#ifdef TEST_FRAMEWORK
-#define AST_TEST_DEFINE(hdr, body) enum ast_test_result_state hdr(struct ast_str **error_str); enum ast_test_result_state hdr(struct ast_str **error_str) body
+#define AST_TEST_DEFINE(hdr, body) enum ast_test_result_state hdr(struct ast_str **ast_test_error_str); enum ast_test_result_state hdr(struct ast_str **ast_test_error_str) body
#define AST_TEST_REGISTER(name, cat, sum, des, cb) ast_test_register(name, cat, sum, des, cb)
#define AST_TEST_UNREGISTER(cb) ast_test_unregister(cb)
#else /* else no-op */
@@ -100,7 +103,7 @@
*
* \return AST_TEST_PASS for pass, AST_TEST_FAIL for failure
*/
-typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_str **error_str);
+typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_str **ast_test_error_str);
enum ast_test_result_state {
AST_TEST_NOT_RUN = 0,
@@ -109,18 +112,12 @@
};
/*!
- * \brief Initializes test framework.
- *
- * \return 0 on success, -1 on failure.
- */
-int ast_test_init(void);
-
-/*!
* \brief unregisters a test with the test framework
*
* \param test callback function (required)
*
- * \return 0 for pass, -1 for failure
+ * \retval 0 success
+ * \retval -1 failure
*/
int ast_test_unregister(ast_test_cb_t *cb);
@@ -133,42 +130,10 @@
* \param test description (optional)
* \param test callback function (required)
*
- * \return 0 for pass, -1 for failure
+ * \retval 0 success
+ * \retval -1 failure
*/
int ast_test_register(const char *name, const char *category, const char *summary, const char *description, ast_test_cb_t *cb);
-/*!
- * \brief Executes registered unit tests
- *
- * \param name of test to run (optional)
- * \param test category to run (optional)
- * \param cli arguments for realtime cli test updates (optional)
- *
- * \return number of tests executed.
- *
- * \note This function has three modes of operation
- * 1. When given a name and category, a matching individual test will execute if found.
- * 2. When given only a category all matching tests within that category will execute.
- * 3. If given no name or category all registered tests will execute.
- */
-int ast_test_execute(const char *name, const char *category, struct ast_cli_args *a);
-
-/*!
- * \brief Generate test results.
- *
- * \param name of test result to generate (optional)
- * \param test category to generate (optional)
- * \param path to xml file to generate. (optional)
- * \param path to txt file to generate, (optional)
- *
- * \return 0 if results were generated, -1 if error
- *
- * \note This function has three modes of operation.
- * 1. When given both a name and category, results will be generated for that single test.
- * 2. When given only a category, results for every test within the category will be generated.
- * 3. When given no name or category, results for every registered test will be generated.
- *
- * In order for the results to be generated, an xml and or txt file path must be provided.
- */
-int ast_test_generate_results(const char *name, const char *category, const char *xml_path, const char *txt_path);
-#endif
+#endif /* TEST_FRAMEWORK */
+#endif /* _AST_TEST_H */
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=235379&r1=235378&r2=235379
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Wed Dec 16 17:42:45 2009
@@ -1,7 +1,7 @@
/*
* Asterisk -- An open source telephony toolkit.
*
- * Copyright (C) 2007 - 2009, Digium, Inc.
+ * Copyright (C) 2009, Digium, Inc.
*
* David Vossel <dvossel at digium.com>
*
@@ -42,16 +42,16 @@
/*! This array corrisponds to the values defined in the ast_test_result_state enum */
static const char *test_result2str[] = {
- "NOT RUN",
- "PASS",
- "FAIL",
+ [AST_TEST_NOT_RUN] = "NOT RUN",
+ [AST_TEST_PASS] = "PASS",
+ [AST_TEST_FAIL] = "FAIL",
};
/*! represents all the test result data for a single ast_test object */
struct ast_test_result {
enum ast_test_result_state state; /*! current test result state */
struct ast_str *error; /*! optional error str to describe error result */
- int time; /*! time in ms test took */
+ unsigned int time; /*! time in ms test took */
};
/*! holds all the information pertaining to a single defined test */
@@ -67,14 +67,20 @@
/*! global structure containing both total and last test execution results */
static struct ast_test_execute_results {
- int total_tests; /* total number of tests, reguardless if they have been executed or not */
- int total_passed; /* total number of executed tests passed */
- int total_failed; /* total number of executed tests failed */
- int total_time; /* total time of all executed tests */
- int last_passed; /* number of passed tests during last execution */
- int last_failed; /* number of failed tests during last execution */
- int last_time; /* total time of the last test execution */
-} last_results = { 0 };
+ unsigned int total_tests; /* total number of tests, reguardless if they have been executed or not */
+ unsigned int total_passed; /* total number of executed tests passed */
+ unsigned int total_failed; /* total number of executed tests failed */
+ unsigned int total_time; /* total time of all executed tests */
+ unsigned int last_passed; /* number of passed tests during last execution */
+ unsigned int last_failed; /* number of failed tests during last execution */
+ unsigned int last_time; /* total time of the last test execution */
+} last_results;
+
+enum test_mode {
+ TEST_ALL = 0,
+ TEST_CATEGORY = 1,
+ TEST_NAME_CATEGORY = 2,
+};
/*! List of registered test definitions */
static AST_LIST_HEAD_STATIC(tests, ast_test);
@@ -90,8 +96,10 @@
{
struct ast_test *test;
- /* verify data. Name, Catagory, and cb _MUST_ be present to register a test */
- if (ast_strlen_zero(name) || ast_strlen_zero(category) || !cb) {
+ /* verify data.*/
+ if (ast_strlen_zero(name) || ast_strlen_zero(category) ||
+ ast_strlen_zero(description) || ast_strlen_zero(summary) || !cb) {
+ ast_log(LOG_WARNING, "Attempted to register test without all required information\n");
return -1;
}
@@ -124,13 +132,14 @@
return 0;
}
-/*!
+/*!
+ * \internal
* \brief executes a single test, storing the results in the test->result structure.
*
* \note The last_results structure which contains global statistics about test execution
- * must be updated when using this function. See use in ast_test_execute().
- */
-static void __test_execute(struct ast_test *test)
+ * must be updated when using this function. See use in test_execute_multiple().
+ */
+static void test_execute(struct ast_test *test)
{
struct timeval begin;
@@ -144,7 +153,7 @@
test->result.time = ast_tvdiff_ms(ast_tvnow(), begin);
}
-static void __test_xml_entry(struct ast_test *test, FILE *f)
+static void test_xml_entry(struct ast_test *test, FILE *f)
{
if (!f || !test) {
return;
@@ -155,13 +164,20 @@
fprintf(f, "<category>%s</category>\n", test->category);
fprintf(f, "<summary>%s</summary>\n", test->summary);
fprintf(f, "<description>%s</description>\n", test->description);
- fprintf(f, "<result>%s</result>\n", test_result2str[test->result.state]);
- fprintf(f, "<error>%s</error>\n", S_OR(ast_str_buffer(test->result.error), "NA"));
- fprintf(f, "<time>%d</time>\n", test->result.time);
+
+ fprintf(f, "<result>\n%s\n", test_result2str[test->result.state]);
+ if (test->result.state == AST_TEST_FAIL) {
+ fprintf(f, "\t<error>\n\t\t%s\n\t</error>\n", S_OR(ast_str_buffer(test->result.error), "NA"));
+ }
+ if (test->result.state != AST_TEST_NOT_RUN) {
+ fprintf(f, "\t<time>\n\t\t%d\n\t</time>\n", test->result.time);
+ }
+ fprintf(f, "</result>\n");
+
fprintf(f, "</test>\n");
}
-static void __test_txt_entry(struct ast_test *test, FILE *f)
+static void test_txt_entry(struct ast_test *test, FILE *f)
{
if (!f || !test) {
return;
@@ -172,23 +188,41 @@
fprintf(f, "Summary: %s\n", test->summary);
fprintf(f, "Description: %s\n", test->description);
fprintf(f, "Result: %s\n", test_result2str[test->result.state]);
- fprintf(f, "Error Description: %s\n", S_OR(ast_str_buffer(test->result.error), "NA"));
- fprintf(f, "Time: %d\n", test->result.time);
-}
-
-int ast_test_execute(const char *name, const char *category, struct ast_cli_args *a)
+ if (test->result.state == AST_TEST_FAIL) {
+ fprintf(f, "Error Description: %s\n", S_OR(ast_str_buffer(test->result.error), "NA"));
+ }
+ if (test->result.state != AST_TEST_NOT_RUN) {
+ fprintf(f, "Time: %d\n", test->result.time);
+ }
+}
+
+/*!
+ * \brief Executes registered unit tests
+ *
+ * \param name of test to run (optional)
+ * \param test category to run (optional)
+ * \param fd for realtime cli test updates (optional)
+ *
+ * \return number of tests executed.
+ *
+ * \note This function has three modes of operation
+ * 1. When given a name and category, a matching individual test will execute if found.
+ * 2. When given only a category all matching tests within that category will execute.
+ * 3. If given no name or category all registered tests will execute.
+ */
+static int test_execute_multiple(const char *name, const char *category, const int fd)
{
char result_buf[32] = { 0 };
struct ast_test *test = NULL;
- int mode = 0; /* 3 modes, 0 = run all, 1 = only by category, 2 = only by name and category */
+ enum test_mode mode = TEST_ALL; /* 3 modes, 0 = run all, 1 = only by category, 2 = only by name and category */
int execute = 0;
int res = 0;
if (!ast_strlen_zero(category)) {
if (!ast_strlen_zero(name)) {
- mode = 2;
+ mode = TEST_NAME_CATEGORY;
} else {
- mode = 1;
+ mode = TEST_CATEGORY;
}
}
@@ -198,20 +232,28 @@
AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
execute = 0;
- if ((!mode) ||
- ((mode == 1) && !(test_cat_cmp(test->category, category))) ||
- ((mode == 2) && !(strcmp(test->category, category)) && !(strcmp(test->name, name)))) {
-
+ switch (mode) {
+ case TEST_CATEGORY:
+ if (!test_cat_cmp(test->category, category)) {
+ execute = 1;
+ }
+ break;
+ case TEST_NAME_CATEGORY:
+ if (!(strcmp(test->category, category)) && !(strcmp(test->name, name))) {
+ execute = 1;
+ }
+ break;
+ case TEST_ALL:
execute = 1;
}
if (execute) {
- if (a) {
- ast_cli(a->fd, "START %s/%s \n", test->category, test->name);
+ if (fd) {
+ ast_cli(fd, "START %s/%s \n", test->category, test->name);
}
/* execute the test and save results */
- __test_execute(test);
+ test_execute(test);
/* update execution specific counts here */
last_results.last_time += test->result.time;
@@ -221,13 +263,13 @@
last_results.last_failed++;
}
- if (a) {
+ if (fd) {
term_color(result_buf,
test_result2str[test->result.state],
(test->result.state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN,
0,
sizeof(result_buf));
- ast_cli(a->fd, "END %s/%s Time: %dms Result: %s %s\n",
+ ast_cli(fd, "END %s/%s Time: %dms Result: %s %s\n",
test->category,
test->name,
test->result.time,
@@ -255,9 +297,28 @@
return res;
}
-int ast_test_generate_results(const char *name, const char *category, const char *xml_path, const char *txt_path)
-{
- char mode = 0; /* 0 generate all, 1 generate by category only, 2 generate by name and category */
+/*!
+ * \internal
+ * \brief Generate test results.
+ *
+ * \param name of test result to generate (optional)
+ * \param test category to generate (optional)
+ * \param path to xml file to generate. (optional)
+ * \param path to txt file to generate, (optional)
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * \note This function has three modes of operation.
+ * 1. When given both a name and category, results will be generated for that single test.
+ * 2. When given only a category, results for every test within the category will be generated.
+ * 3. When given no name or category, results for every registered test will be generated.
+ *
+ * In order for the results to be generated, an xml and or txt file path must be provided.
+ */
+static int test_generate_results(const char *name, const char *category, const char *xml_path, const char *txt_path)
+{
+ enum test_mode mode = TEST_ALL; /* 0 generate all, 1 generate by category only, 2 generate by name and category */
FILE *f_xml = NULL, *f_txt = NULL;
int res = 0;
struct ast_test *test = NULL;
@@ -270,9 +331,9 @@
/* define what mode is to be used */
if (!ast_strlen_zero(category)) {
if (!ast_strlen_zero(name)) {
- mode = 2;
+ mode = TEST_NAME_CATEGORY;
} else {
- mode = 1;
+ mode = TEST_CATEGORY;
}
}
@@ -320,8 +381,10 @@
/* export each individual test */
AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
- __test_xml_entry(test, f_xml);
- __test_txt_entry(test, f_txt);
+ /* TODO XXX make this filter by matching category and name
+ * like the function actually says it does */
+ test_xml_entry(test, f_xml);
+ test_txt_entry(test, f_txt);
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&tests);
@@ -338,6 +401,7 @@
}
/*!
+ * \internal
* \brief adds test to container sorted first by category then by name
*
* \return 0 on success, -1 on failure
@@ -383,6 +447,7 @@
}
/*!
+ * \internal
* \brief removes test from container
*
* \return ast_test removed from list on success, or NULL on failure
@@ -406,6 +471,7 @@
/*!
* \brief frees a ast_test object and all it's data members
+ * \internal
*/
static struct ast_test *test_free(struct ast_test *test)
{
@@ -425,6 +491,7 @@
/*!
* \brief compares two test catagories to determine if cat1 resides in cat2
+ * \internal
*
* \return 0 if true
*/
@@ -449,20 +516,21 @@
}
/*!
+ * \internal
* \brief allocates an ast_test object.
*/
static struct ast_test *test_alloc(const char *name, const char *category, const char *summary, const char *description, ast_test_cb_t *cb)
{
struct ast_test *test;
- if (!(test = ast_calloc(sizeof(*test), 1))) {
+ if (!(test = ast_calloc(1, sizeof(*test)))) {
return NULL;
}
if (!(test->name = ast_strdup(name)) ||
!(test->category = ast_strdup(category)) ||
- !(test->summary = ast_strdup(S_OR(summary, "NA"))) ||
- !(test->description = ast_strdup(S_OR(description, "NA"))) ||
+ !(test->summary = ast_strdup(summary)) ||
+ !(test->description = ast_strdup(description)) ||
!(test->result.error = ast_str_create(128))) {
return test_free(test);
@@ -559,13 +627,13 @@
if ((a->argc == 3) && !strcmp(a->argv[2], "all")) { /* run all registered tests */
ast_cli(a->fd, "Running all available tests...\n\n");
- ast_test_execute(NULL, NULL, a);
+ test_execute_multiple(NULL, NULL, a->fd);
} else if (a->argc == 4) { /* run only tests within a category */
ast_cli(a->fd, "Running all available tests matching category %s\n\n", a->argv[3]);
- ast_test_execute(NULL, a->argv[3], a);
+ test_execute_multiple(NULL, a->argv[3], a->fd);
} else if (a->argc == 6) { /* run only a single test matching the category and name */
ast_cli(a->fd, "Running all available tests matching category %s and name %s\n\n", a->argv[5], a->argv[3]);
- ast_test_execute(a->argv[5], a->argv[3], a);
+ test_execute_multiple(a->argv[5], a->argv[3], a->fd);
} else {
return CLI_SHOWUSAGE;
}
@@ -628,21 +696,22 @@
ast_cli(a->fd, FORMAT_RES_ALL, "Result", "", "Name", "Catagory", "Error Description");
AST_LIST_LOCK(&tests);
AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
- if (test->result.state != AST_TEST_NOT_RUN) {
- test->result.state == AST_TEST_FAIL ? failed++ : passed++;
- if (!mode || ((mode == 1) && (test->result.state == AST_TEST_FAIL)) || ((mode == 2) && (test->result.state == AST_TEST_PASS))) {
- /* give our results pretty colors */
- term_color(result_buf, test_result2str[test->result.state],
- (test->result.state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN,
- 0, sizeof(result_buf));
-
- ast_cli(a->fd, FORMAT_RES_ALL,
- result_buf,
- " ",
- test->name,
- test->category,
- (test->result.state == AST_TEST_FAIL) ? S_OR(ast_str_buffer(test->result.error), "Not Avaliable") : "");
- }
+ if (test->result.state == AST_TEST_NOT_RUN) {
+ continue;
+ }
+ test->result.state == AST_TEST_FAIL ? failed++ : passed++;
+ if (!mode || ((mode == 1) && (test->result.state == AST_TEST_FAIL)) || ((mode == 2) && (test->result.state == AST_TEST_PASS))) {
+ /* give our results pretty colors */
+ term_color(result_buf, test_result2str[test->result.state],
+ (test->result.state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN,
+ 0, sizeof(result_buf));
+
+ ast_cli(a->fd, FORMAT_RES_ALL,
+ result_buf,
+ " ",
+ test->name,
+ test->category,
+ (test->result.state == AST_TEST_FAIL) ? S_OR(ast_str_buffer(test->result.error), "Not Avaliable") : "");
}
}
AST_LIST_TRAVERSE_SAFE_END;
@@ -683,10 +752,10 @@
return CLI_SHOWUSAGE;
} else if (!strcmp(a->argv[3], "xml")) {
file = (a->argc == 5) ? a->argv[4] : DEFAULT_XML_PATH;
- res = ast_test_generate_results(NULL, NULL, file, NULL);
+ res = test_generate_results(NULL, NULL, file, NULL);
} else if (!strcmp(a->argv[3], "txt")) {
file = (a->argc == 5) ? a->argv[4] : DEFAULT_TXT_PATH;
- res = ast_test_generate_results(NULL, NULL, NULL, (a->argc == 5) ? a->argv[4] : DEFAULT_TXT_PATH);
+ res = test_generate_results(NULL, NULL, NULL, (a->argc == 5) ? a->argv[4] : DEFAULT_TXT_PATH);
} else {
return CLI_SHOWUSAGE;
}
@@ -709,15 +778,17 @@
AST_CLI_DEFINE(test_cli_show_results, "show last test results"),
AST_CLI_DEFINE(test_cli_generate_results, "generate test results to file"),
};
+#endif /* TEST_FRAMEWORK */
int ast_test_init()
{
+#ifdef TEST_FRAMEWORK
/* Register cli commands */
ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
/* in the future this function could be used to register functions not
* defined within a module */
+#endif
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=235379&r1=235378&r2=235379
==============================================================================
--- team/dvossel/test_api/tests/test_heap.c (original)
+++ team/dvossel/test_api/tests/test_heap.c Wed Dec 16 17:42:45 2009
@@ -140,7 +140,7 @@
while ((node = ast_heap_pop(h))) {
cur = node->val;
if (cur > last) {
- ast_str_set(error_str, 0, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
+ ast_str_set(ast_test_error_str, 0, "i: %u, cur: %ld, last: %ld\n", i, cur, last);
res = AST_TEST_FAIL;
goto return_cleanup;
}
@@ -149,7 +149,7 @@
}
if (i != one_million) {
- ast_str_set(error_str, 0, "Stopped popping off after only getting %u nodes\n", i);
+ ast_str_set(ast_test_error_str, 0, "Stopped popping off after only getting %u nodes\n", i);
res = AST_TEST_FAIL;
goto return_cleanup;
}
More information about the asterisk-commits
mailing list