From ae261f3caf1630723dd59b4dd6a04c8ed1cb2d72 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 9 Mar 2012 00:39:28 +0100 Subject: [PATCH] wvtest: Simplify the code by combining start() and check() to a single function --- src/wvtest/c/wvtest.c | 97 ++++++++------------------------------- src/wvtest/c/wvtest.h | 5 +- src/wvtest/c/wvtestfrsh.c | 3 +- 3 files changed, 22 insertions(+), 83 deletions(-) diff --git a/src/wvtest/c/wvtest.c b/src/wvtest/c/wvtest.c index 055b71fb..fc601447 100644 --- a/src/wvtest/c/wvtest.c +++ b/src/wvtest/c/wvtest.c @@ -103,16 +103,6 @@ static void alarm_handler(int sig) } -static const char *pathstrip(const char *filename) -{ - const char *cptr; - cptr = strrchr(filename, '/'); - if (cptr) filename = cptr + 1; - cptr = strrchr(filename, '\\'); - if (cptr) filename = cptr + 1; - return filename; -} - static bool prefix_match(const char *s, char * const *prefixes) { char *const *prefix; @@ -244,60 +234,22 @@ int wvtest_run_all(char * const *prefixes) // the test is done and we can output it all at once. // // Yes, this is probably the worst API of all time. -static void print_result_str(bool start, const char *_file, int _line, - const char *_condstr, const char *result) +static void print_result_str(const char *file, int line, + const char *condstr, const char *result) { - static char *file; - static char *condstr; - static int line; - char *cptr; - - if (start) - { - if (file) - free(file); - if (condstr) - free(condstr); - file = strdup(pathstrip(_file)); - condstr = strdup(_condstr); - line = _line; - - for (cptr = condstr; *cptr; cptr++) - { - if (!isprint((unsigned char)*cptr)) - *cptr = '!'; - } - } - - if (!start) - printf(TEST_START_FORMAT "%s\n", file, line, condstr, result); + printf(TEST_START_FORMAT "%s\n", file, line, condstr, result); fflush(stdout); - - if (!start) - { - if (file) - free(file); - if (condstr) - free(condstr); - file = condstr = NULL; - } + } static inline void -print_result(bool start, const char *file, int line, +print_result(const char *file, int line, const char *condstr, bool result) { - print_result_str(start, file, line, condstr, result ? "ok" : "FAILED"); + print_result_str(file, line, condstr, result ? "ok" : "FAILED"); } -void wvtest_start(const char *file, int line, const char *condstr) -{ - // Either print the file, line, and condstr, or save them for later. - print_result(true, file, line, condstr, false); -} - - -void wvtest_check(bool cond, const char *reason) +void wvtest_check(const char *file, int line, const char *condstr, bool cond, const char *reason) { #ifndef _WIN32 alarm(MAX_TEST_TIME); // restart per-test timeout @@ -314,7 +266,7 @@ void wvtest_check(bool cond, const char *reason) runs++; - print_result_str(false, NULL, 0, NULL, cond ? "ok" : (reason ? reason : "FAILED")); + print_result_str(file, line, condstr, cond ? "ok" : (reason ? reason : "FAILED")); if (!cond) { @@ -341,15 +293,13 @@ bool wvtest_start_check_eq(const char *file, int line, b_str = b_op = ""; asprintf(&str, "%s%s%d %s %s%s%d", a_str, a_op, a, expect_pass ? "==" : "!=", b_str, b_op, b); - - wvtest_start(file, line, str); - free(str); - bool cond = (a == b); if (!expect_pass) cond = !cond; - wvtest_check(cond, NULL); + wvtest_check(file, line, str, cond, NULL); + free(str); + return cond; } @@ -360,14 +310,12 @@ bool wvtest_start_check_eq_ptr(const char *file, int line, char *str = malloc(len); sprintf(str, "%p %s %p", a, expect_pass ? "==" : "!=", b); - wvtest_start(file, line, str); - free(str); - bool cond = (a == b); if (!expect_pass) cond = !cond; - wvtest_check(cond, NULL); + wvtest_check(file, line, str, cond, NULL); + free(str); return cond; } @@ -379,11 +327,9 @@ bool wvtest_start_check_lt(const char *file, int line, char *str = malloc(len); sprintf(str, "%d < %d", a, b); - wvtest_start(file, line, str); - free(str); - bool cond = (a < b); - wvtest_check(cond, NULL); + wvtest_check(file, line, str, cond, NULL); + free(str); return cond; } bool wvtest_start_check_eq_str(const char *file, int line, @@ -396,13 +342,11 @@ bool wvtest_start_check_eq_str(const char *file, int line, char *str = malloc(len); sprintf(str, "[%s] %s [%s]", a, expect_pass ? "==" : "!=", b); - wvtest_start(file, line, str); - bool cond = !strcmp(a, b); if (!expect_pass) cond = !cond; - wvtest_check(cond, NULL); + wvtest_check(file, line, str, cond, NULL); return cond; } @@ -417,22 +361,19 @@ bool wvtest_start_check_lt_str(const char *file, int line, char *str = malloc(len); sprintf(str, "[%s] < [%s]", a, b); - wvtest_start(file, line, str); - free(str); - bool cond = strcmp(a, b) < 0; - wvtest_check(cond, NULL); + wvtest_check(file, line, str, cond, NULL); + free(str); return cond; } int wvtest_start_check_errno(const char *file, int line, const char *condstr, int retval) { - wvtest_start(file, line, condstr); if (retval == -1) perror(condstr); - wvtest_check(retval != -1, NULL); + wvtest_check(file, line, condstr, retval != -1, NULL); return retval; } diff --git a/src/wvtest/c/wvtest.h b/src/wvtest/c/wvtest.h index 36d3d769..0d4de12d 100644 --- a/src/wvtest/c/wvtest.h +++ b/src/wvtest/c/wvtest.h @@ -26,11 +26,10 @@ struct WvTest { void wvtest_register(struct WvTest *ptr); int wvtest_run_all(char * const *prefixes); -void wvtest_start(const char *file, int line, const char *condstr); -void wvtest_check(bool cond, const char *reason); +void wvtest_check(const char *file, int line, const char *condstr, bool cond, const char *reason); static inline bool wvtest_start_check(const char *file, int line, const char *condstr, bool cond) -{ wvtest_start(file, line, condstr); wvtest_check(cond, NULL); return cond; } +{ wvtest_check(file, line, condstr, cond, NULL); return cond; } bool wvtest_start_check_eq(const char *file, int line, int a, int b, bool expect_pass, const char *a_str, const char *b_str); diff --git a/src/wvtest/c/wvtestfrsh.c b/src/wvtest/c/wvtestfrsh.c index cc1f2769..c1c7746c 100644 --- a/src/wvtest/c/wvtestfrsh.c +++ b/src/wvtest/c/wvtestfrsh.c @@ -5,11 +5,10 @@ int wvtest_start_check_frsh(const char *file, int line, const char *condstr, int frsh_retval) { char errstr[80] = "ok"; - wvtest_start(file, line, condstr); if (frsh_retval != 0) frsh_strerror (frsh_retval, errstr, sizeof(errstr)); - wvtest_check(frsh_retval == 0, errstr); + wvtest_check(file, line, condstr, frsh_retval == 0, errstr); return frsh_retval; } -- 2.39.2