cmocka: Add assert_int_in_range()

This commit is contained in:
Andreas Schneider 2022-05-18 08:15:36 +02:00
parent ad315da8b0
commit 237d748050
2 changed files with 60 additions and 0 deletions

View File

@ -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);

View File

@ -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,