Add a set of assertion macros to tests.h

(ASSERT_OK(), ASSERT_EQ(), ASSERT_GE(), ASSERT_LE()) that log the failed condition before crashing and convert test-gpt.c test file to use them
This commit is contained in:
Unique-Usman
2024-03-19 11:37:45 +00:00
committed by Luca Boccassi
parent 6644445586
commit e19186359a
2 changed files with 58 additions and 19 deletions
+39
View File
@@ -200,3 +200,42 @@ static inline int run_test_table(void) {
DEFINE_TEST_MAIN_FULL(log_level, intro, NULL)
#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_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) \
({ \
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(); \
} \
})