From 2a7cf67913c8c20edb445e145685d4eded6b798b Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Tue, 30 May 2017 19:21:14 +0300 Subject: [PATCH] Add generic `check_bool`, extend `check` to be more informative (libtest) --- libtransmission/libtransmission-test.c | 16 +++++++++++++--- libtransmission/libtransmission-test.h | 25 ++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/libtransmission/libtransmission-test.c b/libtransmission/libtransmission-test.c index 4c9267a9b..302e37330 100644 --- a/libtransmission/libtransmission-test.c +++ b/libtransmission/libtransmission-test.c @@ -50,13 +50,23 @@ bool should_print(bool pass) #endif } -bool check_condition_impl(char const* file, int line, bool condition) +bool libtest_check(char const* file, int line, bool pass, bool condition, char const* condition_str) { - bool const pass = condition; + if (should_print(pass)) + { + fprintf(stderr, "%s %s:%d: %s (%s)\n", pass ? "PASS" : "FAIL", file, line, condition_str, condition ? "true": "false"); + } + return pass; +} + +bool libtest_check_bool(char const* file, int line, bool pass, bool lhs, bool rhs, char const* lhs_str, char const* op_str, + char const* rhs_str) +{ if (should_print(pass)) { - fprintf(stderr, "%s %s:%d\n", pass ? "PASS" : "FAIL", file, line); + fprintf(stderr, "%s %s:%d: %s %s %s (%s %s %s)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str, + lhs ? "true" : "false", op_str, rhs ? "true" : "false"); } return pass; diff --git a/libtransmission/libtransmission-test.h b/libtransmission/libtransmission-test.h index 4ff2e4a51..c6c41197a 100644 --- a/libtransmission/libtransmission-test.h +++ b/libtransmission/libtransmission-test.h @@ -22,8 +22,9 @@ extern bool verbose; bool should_print(bool pass); -bool check_condition_impl(char const* file, int line, bool condition); - +bool libtest_check(char const* file, int line, bool pass, bool condition, char const* condition_str); +bool libtest_check_bool(char const* file, int line, bool pass, bool lhs, bool rhs, char const* lhs_str, char const* op_str, + char const* rhs_str); bool libtest_check_str(char const* file, int line, bool pass, char const* lhs, char const* rhs, char const* lhs_str, char const* op_str, char const* rhs_str); bool libtest_check_int(char const* file, int line, bool pass, intmax_t lhs, intmax_t rhs, char const* lhs_str, @@ -42,7 +43,25 @@ bool libtest_check_ptr(char const* file, int line, bool pass, void const* lhs, v { \ ++current_test; \ \ - if (!check_condition_impl(__FILE__, __LINE__, (condition))) \ + bool const check_result = (condition); \ + \ + if (!libtest_check(__FILE__, __LINE__, check_result, check_result, #condition)) \ + { \ + return current_test; \ + } \ + } \ + while (0) + +#define check_bool(lhs, op, rhs) \ + do \ + { \ + ++current_test; \ + \ + bool const check_bool_lhs = (lhs); \ + bool const check_bool_rhs = (rhs); \ + \ + if (!libtest_check_bool(__FILE__, __LINE__, check_bool_lhs op check_bool_rhs, check_bool_lhs, check_bool_rhs, #lhs, \ + #op, #rhs)) \ { \ return current_test; \ } \ -- 2.40.0