]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/commitdiff
wvtest: Show stringified expressions in WVPASSEQ/NE
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 8 Feb 2011 15:34:33 +0000 (16:34 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 8 Feb 2011 16:08:40 +0000 (17:08 +0100)
Previously, WVPASSEQ(func(), 0) printed only "0 == 0 ok", whereas now,
it prints "func() == 0 == 0 ok". This is useful when the test fails so
that the real value is seen in the output e.g. "func() == 1 == 0 FAILED".

src/wvtest/c/wvtest.c
src/wvtest/c/wvtest.h

index ffeb6e7de6711f85b98d54b5b7859627134eba77..3467b285e1ed1a13863a749e3e3d8e4565810fcf 100644 (file)
@@ -4,6 +4,7 @@
  *       Licensed under the GNU Library General Public License, version 2.
  *       See the included file named LICENSE for license information.
  */
+#define _GNU_SOURCE
 #include "wvtest.h"
 #include <stdio.h>
 #include <string.h>
@@ -365,11 +366,38 @@ void wvtest_check(bool cond, const char *reason)
 
 
 bool wvtest_start_check_eq(const char *file, int line,
-                          int a, int b, bool expect_pass)
+                          int a, int b, bool expect_pass,
+                          const char *a_str, const char *b_str)
+{
+    char *str;
+    char sa[20], sb[20];
+    char *a_op = " == ", *b_op = " == ";
+    sprintf(sa, "%d", a);
+    sprintf(sb, "%d", b);
+    if (strcmp(sa, a_str) == 0)
+           a_str = a_op = "";
+    if (strcmp(sb, b_str) == 0)
+           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);
+    return cond;
+}
+
+bool wvtest_start_check_eq_ptr(const char *file, int line,
+                              void *a, void *b, bool expect_pass)
 {
     size_t len = 11 + 11 + 8 + 1;
     char *str = malloc(len);
-    sprintf(str, "%d %s %d", a, expect_pass ? "==" : "!=", b);
+    sprintf(str, "%p %s %p", a, expect_pass ? "==" : "!=", b);
 
     wvtest_start(file, line, str);
     free(str);
index c324dc6a5dec4f09ac0fd859d8c154d89d3e5bfc..0f0bb8acabef5163783333db609fe158a9025d66 100644 (file)
@@ -32,9 +32,12 @@ 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; }
 bool wvtest_start_check_eq(const char *file, int line,
-                          int a, int b, bool expect_pass);
+                          int a, int b, bool expect_pass,
+                          const char *a_str, const char *b_str);
 bool wvtest_start_check_lt(const char *file, int line,
                           int a, int b);
+bool wvtest_start_check_eq_ptr(const char *file, int line,
+                              void *a, void *b, bool expect_pass);
 bool wvtest_start_check_eq_str(const char *file, int line,
                               const char *a, const char *b, bool expect_pass);
 bool wvtest_start_check_lt_str(const char *file, int line,
@@ -46,9 +49,13 @@ int wvtest_start_check_frsh(const char *file, int line,
 #define WVPASS(cond) \
     wvtest_start_check(__FILE__, __LINE__, #cond, (cond))
 #define WVPASSEQ(a, b) \
-    wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), true)
+       wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), true, #a, #b)
 #define WVPASSLT(a, b) \
     wvtest_start_check_lt(__FILE__, __LINE__, (a), (b))
+#define WVPASSEQPTR(a, b) \
+    wvtest_start_check_eq_ptr(__FILE__, __LINE__, (a), (b), true)
+#define WVFAILEQPTR(a, b) \
+    wvtest_start_check_eq_ptr(__FILE__, __LINE__, (a), (b), false)
 #define WVPASSEQSTR(a, b) \
     wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), true)
 #define WVPASSLTSTR(a, b) \
@@ -56,9 +63,9 @@ int wvtest_start_check_frsh(const char *file, int line,
 #define WVFAIL(cond) \
     wvtest_start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond))
 #define WVFAILEQ(a, b) \
-    wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), false)
+       wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), false, #a, #b)
 #define WVFAILEQSTR(a, b) \
-    wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), false)
+       wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), false)
 #define WVPASSNE(a, b) WVFAILEQ(a, b)
 #define WVPASSNESTR(a, b) WVFAILEQSTR(a, b)
 #define WVFAILNE(a, b) WVPASSEQ(a, b)