From ad567f8d85e41c41e61b8a4d039837eb640e3c22 Mon Sep 17 00:00:00 2001 From: Eshan Kelkar Date: Mon, 18 Dec 2023 11:40:26 +0530 Subject: Make assert_true(), assert_false() more verbose Both assert_true(expression) and assert_false(expression) print the expression when the assertion fails. Its not very clear on seeing the expression that what exactly is the error, whether its the expression being true or it being false. This commit changes the assert_true() and assert_false() such that on failure of assertion: - assert_true(expression) prints : expression is not true - assert_false(expression) prints : expression is not false Signed-off-by: Eshan Kelkar Reviewed-by: Andreas Schneider --- include/cmocka.h | 7 +++++-- src/cmocka.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/cmocka.h b/include/cmocka.h index 70dd603..307165f 100644 --- a/include/cmocka.h +++ b/include/cmocka.h @@ -1482,8 +1482,8 @@ void assert_true(scalar expression); */ void assert_false(scalar expression); #else -#define assert_false(c) _assert_true(!(cast_to_uintmax_type(c)), #c, \ - __FILE__, __LINE__) +#define assert_false(c) _assert_false(cast_to_uintmax_type(c), #c, \ + __FILE__, __LINE__) #endif #ifdef DOXYGEN @@ -2912,6 +2912,9 @@ void _will_return(const char *const function_name, void _assert_true(const uintmax_t result, const char* const expression, const char * const file, const int line); +void _assert_false(const uintmax_t result, + const char * const expression, + const char * const file, const int line); void _assert_return_code(const intmax_t result, const int32_t error, const char * const expression, diff --git a/src/cmocka.c b/src/cmocka.c index 43652d6..b1963d3 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -2040,7 +2040,17 @@ void _assert_true(const uintmax_t result, const char * const expression, const char * const file, const int line) { if (!result) { - cmocka_print_error("%s\n", expression); + cmocka_print_error("%s is not true\n", expression); + _fail(file, line); + } +} + +void _assert_false(const uintmax_t result, + const char * const expression, + const char * const file, const int line) +{ + if (result) { + cmocka_print_error("%s is not false\n", expression); _fail(file, line); } } -- cgit v1.1