From 417b92df7023a32c0b2775d59f1816b629e0a9b9 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 23 Dec 2013 15:38:00 +0100 Subject: [PATCH] cmocka: Add assert_return_code(). --- include/cmocka.h | 26 ++++++++++++++++++++++++++ src/cmocka.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/cmocka.h b/include/cmocka.h index 0df2e90..dac50c4 100644 --- a/include/cmocka.h +++ b/include/cmocka.h @@ -988,6 +988,26 @@ void assert_false(scalar expression); #ifdef DOXYGEN /** + * @brief Assert if the return_code is smaller than 0. + * + * The function prints an error message to standard error and terminates the + * test by calling fail() if the return code is smaller than 0. + * + * @param[in] rc The return code to evaluate. + * + * @param[in] errno Pass errno here or 0. + */ +void assert_return_code(int rc, int error); +#else +#define assert_return_code(rc, error) \ + _assert_return_code(cast_to_largest_integral_type(rc), \ + sizeof(rc), \ + cast_to_largest_integral_type(error), \ + #rc, __FILE__, __LINE__) +#endif + +#ifdef DOXYGEN +/** * @brief Assert that the given pointer is non-NULL. * * The function prints an error message to standard error and terminates the @@ -1684,6 +1704,12 @@ void _will_return(const char * const function_name, const char * const file, void _assert_true(const LargestIntegralType result, const char* const expression, const char * const file, const int line); +void _assert_return_code(const LargestIntegralType result, + size_t rlen, + const LargestIntegralType error, + const char * const expression, + const char * const file, + const int line); void _assert_int_equal( const LargestIntegralType a, const LargestIntegralType b, const char * const file, const int line); diff --git a/src/cmocka.c b/src/cmocka.c index 334f8ab..b4de262 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -1297,6 +1297,47 @@ void _assert_true(const LargestIntegralType result, } } +void _assert_return_code(const LargestIntegralType result, + size_t rlen, + const LargestIntegralType error, + const char * const expression, + const char * const file, + const int line) +{ + LargestIntegralType valmax; + + + switch (rlen) { + case 1: + valmax = 255; + break; + case 2: + valmax = 32767; + break; + case 4: + valmax = 2147483647; + break; + case 8: + default: + if (rlen > sizeof(valmax)) { + valmax = 2147483647; + } else { + valmax = 9223372036854775807L; + } + break; + } + + if (result > valmax - 1) { + if (error > 0) { + print_error("%s < 0, errno(%llu): %s\n", + expression, error, strerror(error)); + } else { + print_error("%s < 0\n", expression); + } + _fail(file, line); + } +} + void _assert_int_equal( const LargestIntegralType a, const LargestIntegralType b, const char * const file, const int line) { -- 2.7.4