[asterisk-commits] branch russell/codetest - r7707 /team/russell/codetest/codetest.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sun Jan 1 20:31:21 CST 2006


Author: russell
Date: Sun Jan  1 20:31:19 2006
New Revision: 7707

URL: http://svn.digium.com/view/asterisk?rev=7707&view=rev
Log:
- remove duplicated list traversal coe
- add some comments
- fix a bug if no arguments provided to 'codetest run'

Modified:
    team/russell/codetest/codetest.c

Modified: team/russell/codetest/codetest.c
URL: http://svn.digium.com/view/asterisk/team/russell/codetest/codetest.c?rev=7707&r1=7706&r2=7707&view=diff
==============================================================================
--- team/russell/codetest/codetest.c (original)
+++ team/russell/codetest/codetest.c Sun Jan  1 20:31:19 2006
@@ -37,12 +37,30 @@
 
 struct testfile {
 	const char *filename;
-	AST_LIST_HEAD_NOLOCK(codetest_list, ast_codetest) codetests;
+	AST_LIST_HEAD_NOLOCK(, ast_codetest) codetests;
 	AST_LIST_ENTRY(testfile) list;
 };
 
 static AST_LIST_HEAD_STATIC(testfile_list, testfile);
 
+/* Prototypes for non-exported functions */
+struct testfile *find_testfile_by_name(const char *filename);
+static struct testfile *add_testfile(struct ast_codetest *test);
+static int count_tests(struct testfile *tf);
+static int handle_cli_show_files(int fd, int argc, char *argv[]);
+static int handle_cli_show_file(int fd, int argc, char *argv[]);
+static char *complete_testfiles(char *line, char *word, int pos, int state);
+static char *complete_cli_show_file(char *line, char *word, int pos, int state);
+static void check_test_result(int fd, struct ast_codetest *test, int res);
+static int handle_cli_run(int fd, int argc, char *argv[]);
+static char *complete_tests(char *line, char *word, int pos, int state);
+static char *complete_cli_run(char *line, char *word, int pos, int state);
+
+/*!
+  \brief Add a test to the list of tests for a file
+
+  \note This function is to be called with the testfile_list locked
+*/
 static struct testfile *add_testfile(struct ast_codetest *test)
 {
 	struct testfile *tf;
@@ -60,16 +78,30 @@
 	return tf;
 }
 
+/*! 
+  \brief search the list of files that have registered tests
+
+  \note This function is to be called with the testfile_list locked
+*/
+struct testfile *find_testfile_by_name(const char *filename)
+{
+	struct testfile *cur;
+
+	AST_LIST_TRAVERSE(&testfile_list, cur, list) {
+		if (!strcmp(cur->filename, filename))
+			break;
+	}
+
+	return cur;
+}
+
 int ast_register_codetest(struct ast_codetest *test)
 {
 	struct testfile *cur;
 
 	AST_LIST_LOCK(&testfile_list);
-
-	AST_LIST_TRAVERSE(&testfile_list, cur, list) {
-		if (!strcmp(cur->filename, test->filename))
-			break;
-	}
+	
+	cur = find_testfile_by_name(test->filename);
 
 	if (!cur)
 		cur = add_testfile(test);
@@ -91,11 +123,7 @@
 
 	AST_LIST_LOCK(&testfile_list);
 
-	AST_LIST_TRAVERSE(&testfile_list, cur, list) {
-		if (!strcmp(cur->filename, test->filename))
-			break;
-	}
-	if (!cur) {
+	if (!(cur = find_testfile_by_name(test->filename))) {
 		ast_log(LOG_WARNING, "File '%s' not found in list of files with registered tests!\n", test->filename);
 		AST_LIST_UNLOCK(&testfile_list);
 		return -1;
@@ -103,7 +131,7 @@
 	
 	AST_LIST_REMOVE(&cur->codetests, test, list);
 
-	if (!cur->codetests.first) {
+	if (AST_LIST_EMPTY(&cur->codetests)) {
 		AST_LIST_REMOVE(&testfile_list, cur, list);
 		free(cur);
 	}
@@ -113,6 +141,11 @@
 	return 0;
 }
 
+/*!
+  \brief Count how many tests are registered by a file
+
+  \note This function is to be called with the testfile_list locked
+*/
 static int count_tests(struct testfile *tf)
 {
 	struct ast_codetest *cur;
@@ -164,12 +197,7 @@
 
 	AST_LIST_LOCK(&testfile_list);
 
-	AST_LIST_TRAVERSE(&testfile_list, cur, list) {
-		if (!strcmp(cur->filename, argv[3]))
-			break;	
-	}
-
-	if (!cur) {
+	if (!(cur = find_testfile_by_name(argv[3]))) {
 		ast_cli(fd, "File '%s' not found in list of files with registered code tests!\n", argv[3]);
 		AST_LIST_UNLOCK(&testfile_list);
 		return RESULT_SHOWUSAGE;
@@ -260,15 +288,12 @@
 			test_argc = argc - 4;
 			test_argv = argv + 4;	
 		}	
-	}
-
-	AST_LIST_LOCK(&testfile_list);
-
-	AST_LIST_TRAVERSE(&testfile_list, tf, list) {
-		if (!strcmp(tf->filename, argv[2]))
-			break;
-	}
-	if (!tf) {
+	} else
+		return RESULT_SHOWUSAGE;
+
+	AST_LIST_LOCK(&testfile_list);
+
+	if (!(tf = find_testfile_by_name(argv[2]))) {
 		ast_cli(fd, "File '%s' not found in list of files with code tests.\n", argv[2]);
 		AST_LIST_UNLOCK(&testfile_list);
 		return RESULT_SHOWUSAGE;
@@ -286,8 +311,8 @@
 		} else {
 			ast_cli(fd, "Executing test '%s' for file '%s' ...\n", test->testname, test->filename);
 			res = test->codetest(fd, test_argc, test_argv);
+			check_test_result(fd, test, res);
 			test_executed = 1;
-			check_test_result(fd, test, res);
 		}
 	}
 
@@ -304,6 +329,7 @@
 static char *complete_tests(char *line, char *word, int pos, int state)
 {
 	char *file;
+	char *ret = NULL;
 	struct ast_codetest *test;
 	struct testfile *tf;
 	int match = 0;
@@ -323,29 +349,21 @@
 
 	AST_LIST_LOCK(&testfile_list);
 
-	AST_LIST_TRAVERSE(&testfile_list, tf, list) {
-		if (!strcmp(file, tf->filename))
-			break;
-	}
-	if (!tf) {
+	if (!(tf = find_testfile_by_name(file))) {
 		AST_LIST_UNLOCK(&testfile_list);
 		return NULL;
 	}
 
 	AST_LIST_TRAVERSE(&tf->codetests, test, list) {
-		if (!strncasecmp(word, test->filename, wordlen)) {
-			match++;
-			if (match > state) {
-				file = strdup(test->filename);
-				AST_LIST_UNLOCK(&testfile_list);
-				return file;
-			}
+		if (!strncasecmp(word, test->filename, wordlen) && ++match > state) {
+			ret = strdup(test->filename);
+			break;
 		}
 	}	
 
 	AST_LIST_UNLOCK(&testfile_list);
 
-	return NULL;
+	return ret;
 }
 
 static char *complete_cli_run(char *line, char *word, int pos, int state)



More information about the asterisk-commits mailing list