From da6c62c2933988587c20bf87866b05a743172c96 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Mon, 13 Jan 2025 20:41:55 -0500 Subject: [PATCH] [utest] fix twice operation of uassert --- components/utilities/utest/TC_uassert.c | 10 ++++++ components/utilities/utest/utest_assert.h | 43 +++++++++++++++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/components/utilities/utest/TC_uassert.c b/components/utilities/utest/TC_uassert.c index 50d45270ad..bb212c14d1 100644 --- a/components/utilities/utest/TC_uassert.c +++ b/components/utilities/utest/TC_uassert.c @@ -32,6 +32,15 @@ static void TC_uassert_int_op(void) 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) { 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_null_not_null); 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_str_op); UTEST_UNIT_RUN(TC_uassert_in_range); diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h index 6d19660993..86381f57cc 100644 --- a/components/utilities/utest/utest_assert.h +++ b/components/utilities/utest/utest_assert.h @@ -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_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_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_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_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_not_in_range(value, min, max) __utest_assert(!((value >= min) && (value <= max)), "(" #value ") in range("#min","#max")") +#define uassert_in_range(value, 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_float_not_equal(a, b) uassert_not_in_range(a, ((double)b - 0.0001), ((double)b + 0.0001)) +#define uassert_not_in_range(value, min, max) \ + 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_not_equal(a, b) __uassert_value_op(a, b, !=)