From 1d8a81eb4ea8e8c1fc82e839cb9f2a65da5346ad Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 19 Sep 2024 11:38:47 +0200 Subject: Add ASSERT_OK_ZERO_ERRNO() and ASSERT_OK_EQ_ERRNO() --- src/shared/tests.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/shared') diff --git a/src/shared/tests.h b/src/shared/tests.h index 0c1b8cfa78..33180e7b37 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -281,6 +281,44 @@ static inline int run_test_table(void) { } \ }) +#define ASSERT_OK_ZERO_ERRNO(expr) \ + ({ \ + typeof(expr) _result = (expr); \ + if (_result < 0) { \ + log_error_errno(errno, "%s:%i: Assertion failed: expected \"%s\" to succeed but got the following error: %m", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + if (_result != 0) { \ + char _sexpr[DECIMAL_STR_MAX(typeof(expr))]; \ + xsprintf(_sexpr, DECIMAL_STR_FMT(_result), _result); \ + log_error("%s:%i: Assertion failed: expected \"%s\" to be zero, but it is %s.", \ + PROJECT_FILE, __LINE__, #expr, _sexpr); \ + abort(); \ + } \ + }) + +#define ASSERT_OK_EQ_ERRNO(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 < 0) { \ + log_error_errno(errno, "%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_FAIL(expr) \ ({ \ typeof(expr) _result = (expr); \ -- cgit v1.2.3