From c0cd99eee69fd9c0a66e7167784d01a49f93b13f Mon Sep 17 00:00:00 2001 From: Unique-Usman Date: Tue, 19 Mar 2024 18:20:29 +0530 Subject: [PATCH] Follow up with the PR #31819 --- src/basic/macro.h | 13 ++++++ src/shared/tests.h | 85 ++++++++++++++++++++++++---------------- src/test/test-compress.c | 4 +- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/basic/macro.h b/src/basic/macro.h index fe78363b861..d43c3cdd5dd 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -383,4 +383,17 @@ assert_cc(sizeof(dummy_t) == 0); ((long)(_current_ - _entries_) < (long)(ELEMENTSOF(_entries_) - 1)) && ({ entry = *_current_; true; }); \ _current_++) +#define DECIMAL_STR_FMT(x) _Generic((x), \ + char: "%c", \ + bool: "%d", \ + unsigned char: "%d", \ + short: "%hd", \ + unsigned short: "%hu", \ + int: "%d", \ + unsigned: "%u", \ + long: "%ld", \ + unsigned long: "%lu", \ + long long: "%lld", \ + unsigned long long: "%llu") + #include "log.h" diff --git a/src/shared/tests.h b/src/shared/tests.h index ee8a575b2c8..644417f7c59 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -201,41 +201,60 @@ static inline int run_test_table(void) { #define DEFINE_TEST_MAIN(log_level) \ DEFINE_TEST_MAIN_FULL(log_level, NULL, NULL) -#define ASSERT_OK(expr) \ - ({ \ - int _result = (expr); \ - if (_result < 0) { \ - log_error_errno("Assertion failed: %s (result: %d, error: %m)", #expr, _result); \ - abort(); \ - } \ +#define ASSERT_OK(expr) \ + ({ \ + typeof(expr) _result = (expr); \ + if (_result < 0) { \ + log_error_errno(_result, "%s:%i: Assertion failed: %s: %m", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + }) + +/* DECIMAL_STR_FMT() uses _Generic which cannot be used in string concatenation so we have to format the + * input into strings first and then format those into the final assertion message. */ + +#define ASSERT_EQ(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 != _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s == %s\", but \"%s != %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ }) -#define ASSERT_EQ(expr1, expr2) \ - ({ \ - int _expr1 = (expr1); \ - int _expr2 = (expr2); \ - if (_expr1 != _expr2) { \ - log_error("Assertion failed: expected %s == %s, but %d != %d", #expr1, #expr2, _expr1, _expr2); \ - abort(); \ - } \ +#define ASSERT_GE(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 < _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s >= %s\", but \"%s < %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ }) -#define ASSERT_GE(expr1, expr2) \ - ({ \ - int _expr1 = (expr1); \ - int _expr2 = (expr2); \ - if (_expr1 < _expr2) { \ - log_error("Assertion failed: expected %s >= %s, but %d < %d", #expr1, #expr2, _expr1, _expr2); \ - abort(); \ - } \ - }) - -#define ASSERT_LE(expr1, expr2) \ - ({ \ - int _expr1 = (expr1); \ - int _expr2 = (expr2); \ - if (_expr1 > _expr2) { \ - log_error("Assertion failed: expected %s <= %s, but %d > %d", #expr1, #expr2, _expr1, _expr2); \ - abort(); \ - } \ +#define ASSERT_LE(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 > _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s <= %s\", but \"%s > %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ }) diff --git a/src/test/test-compress.c b/src/test/test-compress.c index 57cd77d4e50..868b862dc7a 100644 --- a/src/test/test-compress.c +++ b/src/test/test-compress.c @@ -188,13 +188,13 @@ _unused_ static void test_compress_stream(const char *compression, log_debug("/* create source from %s */", srcfile); - assert_se((src = open(srcfile, O_RDONLY|O_CLOEXEC)) >= 0); + ASSERT_OK((src = open(srcfile, O_RDONLY|O_CLOEXEC))); log_debug("/* test compression */"); assert_se((dst = mkostemp_safe(pattern)) >= 0); - assert_se(compress(src, dst, -1, &uncompressed_size) >= 0); + ASSERT_OK(compress(src, dst, -1, &uncompressed_size)); if (cat) { assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0); -- 2.47.3