From ee061f5077f7baa3ec337ca59a0f587de3231ee4 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Tue, 8 Feb 2011 16:34:33 +0100 Subject: [PATCH] wvtest: Show stringified expressions in WVPASSEQ/NE 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 | 32 ++++++++++++++++++++++++++++++-- src/wvtest/c/wvtest.h | 15 +++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/wvtest/c/wvtest.c b/src/wvtest/c/wvtest.c index ffeb6e7d..3467b285 100644 --- a/src/wvtest/c/wvtest.c +++ b/src/wvtest/c/wvtest.c @@ -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 #include @@ -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); diff --git a/src/wvtest/c/wvtest.h b/src/wvtest/c/wvtest.h index c324dc6a..0f0bb8ac 100644 --- a/src/wvtest/c/wvtest.h +++ b/src/wvtest/c/wvtest.h @@ -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) -- 2.39.2