]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/commitdiff
wvtest: Simplify the code by combining start() and check() to a single function
authorMichal Sojka <sojka@os.inf.tu-dresden.de>
Thu, 8 Mar 2012 23:39:28 +0000 (00:39 +0100)
committerMichal Sojka <sojka@os.inf.tu-dresden.de>
Thu, 8 Mar 2012 23:39:28 +0000 (00:39 +0100)
src/wvtest/c/wvtest.c
src/wvtest/c/wvtest.h
src/wvtest/c/wvtestfrsh.c

index 055b71fbf3de92bd2742ede99d07d965df9fd2d3..fc6014478ecfbdb9ac0aca144b0fa23fc32d54ed 100644 (file)
@@ -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;
 }
index 36d3d7690ac6768514049b000ee923aa517c1d91..0d4de12d86f8a89809f8bc060e6bc3f2aae7e531 100644 (file)
@@ -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);
index cc1f2769bdc2407e3860b673a9660e2a4ade3c9c..c1c7746c0a2f10e2ff1ad478871ea189e606bfe0 100644 (file)
@@ -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;
 }