From 1ee6a08623610f222bed30d4b39af69a8cf634f7 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 27 Aug 2024 12:15:24 +0200 Subject: [PATCH] tests: Add ASSERT_OK_EQ() --- src/shared/tests.h | 20 ++++++++++++++++++++ src/test/test-macro.c | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/src/shared/tests.h b/src/shared/tests.h index eb848b081f4..0c1b8cfa780 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -251,6 +251,26 @@ static inline int run_test_table(void) { } \ }) +#define ASSERT_OK_EQ(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 < 0) { \ + log_error_errno(_expr1, "%s:%i: Assertion failed: expected \"%s\" to succeed but got the following error: %m", \ + PROJECT_FILE, __LINE__, #expr1); \ + abort(); \ + } \ + 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_OK_ERRNO(expr) \ ({ \ typeof(expr) _result = (expr); \ diff --git a/src/test/test-macro.c b/src/test/test-macro.c index b56f5b86e47..69df6bc5674 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -1129,6 +1129,12 @@ TEST(ASSERT) { ASSERT_SIGNAL(ASSERT_OK_ZERO(-1), SIGABRT); ASSERT_SIGNAL(ASSERT_OK_ZERO(-ENOANO), SIGABRT); + ASSERT_OK_EQ(0, 0); + ASSERT_SIGNAL(ASSERT_OK_EQ(1, 0), SIGABRT); + ASSERT_SIGNAL(ASSERT_OK_EQ(255, 5), SIGABRT); + ASSERT_SIGNAL(ASSERT_OK_EQ(-1, 0), SIGABRT); + ASSERT_SIGNAL(ASSERT_OK_EQ(-ENOANO, 0), SIGABRT); + ASSERT_OK_ERRNO(0 >= 0); ASSERT_OK_ERRNO(255 >= 0); ASSERT_OK_ERRNO(printf("Hello world\n")); -- 2.47.3