tests: Add ASSERT_OK_EQ()

This commit is contained in:
Daan De Meyer
2024-08-27 12:15:24 +02:00
parent 3862a227fb
commit 1ee6a08623
2 changed files with 26 additions and 0 deletions
+20
View File
@@ -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); \
+6
View File
@@ -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"));