From 237d7480506c0748a01e07d7557860fb2ea8b17c Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Wed, 18 May 2022 08:15:36 +0200 Subject: [PATCH] cmocka: Add assert_int_in_range() --- include/cmocka.h | 28 ++++++++++++++++++++++++++++ src/cmocka.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/cmocka.h b/include/cmocka.h index c7537ac..bc59ef7 100644 --- a/include/cmocka.h +++ b/include/cmocka.h @@ -1417,6 +1417,29 @@ void assert_memory_not_equal(const void *a, const void *b, size_t size); __FILE__, __LINE__) #endif +#ifdef DOXYGEN +/** + * @brief Assert that the specified integer value is not smaller than the + * minimum and and not greater than the maximum. + * + * The function prints an error message to standard error and terminates the + * test by calling fail() if value is not in range. + * + * @param[in] value The value to check. + * + * @param[in] minimum The minimum value allowed. + * + * @param[in] maximum The maximum value allowed. + */ +void assert_int_in_range(intmax_t value, intmax_t minimum, intmax_t maximum); +#else +#define assert_int_in_range(value, minimum, maximum) \ + _assert_int_in_range( \ + cast_to_intmax_type(value), \ + cast_to_intmax_type(minimum), \ + cast_to_intmax_type(maximum), __FILE__, __LINE__) +#endif + #ifdef DOXYGEN /** * @brief Assert that the specified value is not smaller than the minimum @@ -2355,6 +2378,11 @@ void _assert_memory_equal(const void * const a, const void * const b, void _assert_memory_not_equal(const void * const a, const void * const b, const size_t size, const char* const file, const int line); +void _assert_int_in_range(const intmax_t value, + const intmax_t minimum, + const intmax_t maximum, + const char* const file, + const int line); void _assert_in_range( const uintmax_t value, const uintmax_t minimum, const uintmax_t maximum, const char* const file, const int line); diff --git a/src/cmocka.c b/src/cmocka.c index c10a81c..35652e7 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -1356,6 +1356,26 @@ static bool uint_in_range_display_error(const uintmax_t value, } +/* + * Determine whether a value is within the specified range. + */ +static bool int_in_range_display_error(const intmax_t value, + const intmax_t range_min, + const intmax_t range_max) +{ + if (value >= range_min && value <= range_max) { + return true; + } + + cmocka_print_error("%jd is not within the range [%jd, %jd]\n", + value, + range_min, + range_max); + + return false; +} + + /* * Determine whether a value is within the specified range. If the value * is not within the range 1 is returned. If the value is within the @@ -1960,6 +1980,18 @@ void _assert_memory_not_equal(const void * const a, const void * const b, } +void _assert_int_in_range(const intmax_t value, + const intmax_t minimum, + const intmax_t maximum, + const char* const file, + const int line) +{ + if (!int_in_range_display_error(value, minimum, maximum)) { + _fail(file, line); + } +} + + void _assert_in_range( const uintmax_t value, const uintmax_t minimum, const uintmax_t maximum, const char* const file,