4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-01-18 17:03:30 +08:00

[utest] fix twice operation of uassert

This commit is contained in:
Meco Man 2025-01-13 20:41:55 -05:00 committed by Rbb666
parent 4e370473c5
commit da6c62c293
2 changed files with 47 additions and 6 deletions

View File

@ -32,6 +32,15 @@ static void TC_uassert_int_op(void)
uassert_value_greater_equal(b, b); uassert_value_greater_equal(b, b);
} }
static void TC_uassert_float_op(void)
{
float a = 5.0;
float b = 5.0;
uassert_float_equal(a, b);
uassert_float_not_equal(a, b + 0.0002);
}
static void TC_uassert_ptr_op(void) static void TC_uassert_ptr_op(void)
{ {
int a = 5; int a = 5;
@ -65,6 +74,7 @@ static void utest_do_tc(void)
UTEST_UNIT_RUN(TC_uassert_true_false); UTEST_UNIT_RUN(TC_uassert_true_false);
UTEST_UNIT_RUN(TC_uassert_null_not_null); UTEST_UNIT_RUN(TC_uassert_null_not_null);
UTEST_UNIT_RUN(TC_uassert_int_op); UTEST_UNIT_RUN(TC_uassert_int_op);
UTEST_UNIT_RUN(TC_uassert_float_op);
UTEST_UNIT_RUN(TC_uassert_ptr_op); UTEST_UNIT_RUN(TC_uassert_ptr_op);
UTEST_UNIT_RUN(TC_uassert_str_op); UTEST_UNIT_RUN(TC_uassert_str_op);
UTEST_UNIT_RUN(TC_uassert_in_range); UTEST_UNIT_RUN(TC_uassert_in_range);

View File

@ -47,19 +47,50 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa
* @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test. * @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test.
* @macro uassert_in_range if @value is in range of min and max, not assert, means passing. * @macro uassert_in_range if @value is in range of min and max, not assert, means passing.
* @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing. * @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing.
* * @macro uassert_float_equal if @a equal to @b, not assert, means passing. Float type test.
*/ * @macro uassert_float_not_equal if @a not equal to @b, not assert, means passing. Float type test.
* @macro uassert_value_less if @a less than @b, not assert, means passing.
* @macro uassert_value_less_equal if @a less than or equal to @b, not assert, means passing.
* @macro uassert_value_greater if @a greater than @b, not assert, means passing.
* @macro uassert_value_greater_equal if @a greater than or equal to @b, not assert, means passing.
* @macro uassert_ptr_equal if @a equal to @b, not assert, means passing. Pointer type test.
* @macro uassert_ptr_not_equal if @a not equal to @b, not assert, means passing. Pointer type test.
*/
#define uassert_true(value) __utest_assert(value, "(" #value ") is false") #define uassert_true(value) __utest_assert(value, "(" #value ") is false")
#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true") #define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")
#define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null") #define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null")
#define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null") #define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null")
#define uassert_in_range(value, min, max) __utest_assert(((value >= min) && (value <= max)), "(" #value ") not in range("#min","#max")") #define uassert_in_range(value, min, max) \
#define uassert_not_in_range(value, min, max) __utest_assert(!((value >= min) && (value <= max)), "(" #value ") in range("#min","#max")") do { \
double _value = (value); \
double _min = (min); \
double _max = (max); \
__utest_assert((_value >= _min) && (_value <= _max), "(" #value ") not in range("#min","#max")"); \
} while(0)
#define uassert_float_equal(a, b) uassert_in_range(a, ((double)b - 0.0001), ((double)b + 0.0001)) #define uassert_not_in_range(value, min, max) \
#define uassert_float_not_equal(a, b) uassert_not_in_range(a, ((double)b - 0.0001), ((double)b + 0.0001)) do { \
double _value = (value); \
double _min = (min); \
double _max = (max); \
__utest_assert((_value < _min) || (_value > _max), "(" #value ") in range("#min","#max")"); \
} while(0)
#define uassert_float_equal(a, b) \
do { \
double _a = (a); \
double _b = (b); \
uassert_in_range(_a, ((double)_b - 0.0001), ((double)_b + 0.0001)); \
} while(0)
#define uassert_float_not_equal(a, b) \
do { \
double _a = (a); \
double _b = (b); \
uassert_not_in_range(_a, ((double)_b - 0.0001), ((double)_b + 0.0001)); \
} while(0)
#define uassert_int_equal(a, b) __uassert_value_op(a, b, ==) #define uassert_int_equal(a, b) __uassert_value_op(a, b, ==)
#define uassert_int_not_equal(a, b) __uassert_value_op(a, b, !=) #define uassert_int_not_equal(a, b) __uassert_value_op(a, b, !=)