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:
committed by
Luca Boccassi
parent
6644445586
commit
e19186359a
@@ -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(); \
|
||||
} \
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user