diff --git a/components/utilities/utest/TC_uassert.c b/components/utilities/utest/TC_uassert.c index 1490c06f502..50d45270add 100644 --- a/components/utilities/utest/TC_uassert.c +++ b/components/utilities/utest/TC_uassert.c @@ -24,12 +24,12 @@ static void TC_uassert_int_op(void) uassert_int_equal(a, a); uassert_int_not_equal(a, b); - uassert_int_less(a, b); - uassert_int_less_equal(a, b); - uassert_int_less_equal(a, a); - uassert_int_greater(b, a); - uassert_int_greater_equal(b, a); - uassert_int_greater_equal(b, b); + uassert_value_less(a, b); + uassert_value_less_equal(a, b); + uassert_value_less_equal(a, a); + uassert_value_greater(b, a); + uassert_value_greater_equal(b, a); + uassert_value_greater_equal(b, b); } static void TC_uassert_ptr_op(void) diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index db4eebdfcb7..21329f64b0b 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -172,11 +172,13 @@ utest_t utest_handle_get(void); * @return None * */ -#define UTEST_UNIT_RUN(test_unit_func) \ +#define _UTEST_UNIT_RUN(test_unit_func) \ do { \ utest_unit_run(test_unit_func, #test_unit_func); \ } while (0) +#define UTEST_UNIT_RUN(test_unit_func) _UTEST_UNIT_RUN(test_unit_func) + #ifdef __cplusplus } #endif diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h index e46350b3a3f..6d19660993d 100644 --- a/components/utilities/utest/utest_assert.h +++ b/components/utilities/utest/utest_assert.h @@ -27,6 +27,7 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa /* No need for the user to use this macro directly */ #define __utest_assert(value, msg) utest_assert(value, __FILE__, __LINE__, __func__, msg) +#define __uassert_value_op(a, b, op) __utest_assert((a) op (b), "(" #a ") not " #op " (" #b ")") /** * uassert_x macros @@ -54,13 +55,19 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa #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_int_op(a, b, op) __utest_assert((a) op (b), "(" #a ") not " #op " (" #b ")") -#define uassert_int_equal(a, b) __uassert_int_op(a, b, ==) -#define uassert_int_not_equal(a, b) __uassert_int_op(a, b, !=) -#define uassert_int_less(a, b) __uassert_int_op(a, b, <) -#define uassert_int_less_equal(a, b) __uassert_int_op(a, b, <=) -#define uassert_int_greater(a, b) __uassert_int_op(a, b, >) -#define uassert_int_greater_equal(a, b) __uassert_int_op(a, b, >=) +#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_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_int_equal(a, b) __uassert_value_op(a, b, ==) +#define uassert_int_not_equal(a, b) __uassert_value_op(a, b, !=) + +#define uassert_value_less(a, b) __uassert_value_op(a, b, <) +#define uassert_value_less_equal(a, b) __uassert_value_op(a, b, <=) +#define uassert_value_greater(a, b) __uassert_value_op(a, b, >) +#define uassert_value_greater_equal(a, b) __uassert_value_op(a, b, >=) #define uassert_ptr_equal(a, b) __utest_assert((const void*)(a) == (const void*)(b), "(" #a ") not equal to (" #b ")") #define uassert_ptr_not_equal(a, b) __utest_assert((const void*)(a) != (const void*)(b), "(" #a ") equal to (" #b ")") @@ -71,9 +78,6 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa #define uassert_buf_equal(a, b, sz) utest_assert_buf((const char*)(a), (const char*)(b), (sz), RT_TRUE, __FILE__, __LINE__, __func__, "buf not equal") #define uassert_buf_not_equal(a, b, sz) utest_assert_buf((const char*)(a), (const char*)(b), (sz), RT_FALSE, __FILE__, __LINE__, __func__, "buf equal") -#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")") - #ifdef __cplusplus } #endif diff --git a/src/klibc/utest/TC_rt_memcmp.c b/src/klibc/utest/TC_rt_memcmp.c index c51cf65d653..e8baf556ace 100644 --- a/src/klibc/utest/TC_rt_memcmp.c +++ b/src/klibc/utest/TC_rt_memcmp.c @@ -22,8 +22,8 @@ static void TC_rt_memcmp_str(void) /* The following tests intentionally use a length > 3 */ /* To test what rt_memcmp does in such a situation */ - uassert_int_greater(rt_memcmp(s, "abc", 6), 0); - uassert_int_less(rt_memcmp("abc", s, 6), 0); + uassert_value_greater(rt_memcmp(s, "abc", 6), 0); + uassert_value_less(rt_memcmp("abc", s, 6), 0); } static void TC_rt_memcmp_int_array(void) @@ -33,8 +33,8 @@ static void TC_rt_memcmp_int_array(void) int arr3[] = {1, 2, 3, 4, 6}; uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0); - uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); - uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); + uassert_value_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); + uassert_value_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); } static void TC_rt_memcmp_float_array(void) @@ -44,8 +44,8 @@ static void TC_rt_memcmp_float_array(void) float arr3[] = {1.0f, 2.0f, 3.1f}; uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0); - uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); - uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); + uassert_value_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); + uassert_value_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); } typedef struct { @@ -60,8 +60,8 @@ static void TC_rt_memcmp_struct_array(void) Item arr3[] = {{1, 1.0f}, {2, 2.1f}}; uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0); - uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); - uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); + uassert_value_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); + uassert_value_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); } typedef struct { @@ -77,8 +77,8 @@ static void TC_rt_memcmp_mixed_array(void) MixedItem arr3[] = {{1, 1.0f, "item1"}, {2, 2.1f, "item2"}}; uassert_int_equal(rt_memcmp(arr1, arr2, sizeof(arr1)), 0); - uassert_int_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); - uassert_int_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); + uassert_value_less(rt_memcmp(arr1, arr3, sizeof(arr1)), 0); + uassert_value_greater(rt_memcmp(arr3, arr1, sizeof(arr1)), 0); } typedef struct { @@ -139,8 +139,8 @@ static void TC_rt_memcmp_large_array(void) uassert_int_equal(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0); arr2[LARGE_ARRAY_SIZE - 1] = LARGE_ARRAY_SIZE; - uassert_int_less(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0); - uassert_int_greater(rt_memcmp(arr2, arr1, LARGE_ARRAY_SIZE * sizeof(int)), 0); + uassert_value_less(rt_memcmp(arr1, arr2, LARGE_ARRAY_SIZE * sizeof(int)), 0); + uassert_value_greater(rt_memcmp(arr2, arr1, LARGE_ARRAY_SIZE * sizeof(int)), 0); rt_free(arr1); rt_free(arr2); diff --git a/src/klibc/utest/TC_rt_sscanf.c b/src/klibc/utest/TC_rt_sscanf.c index c0453004e09..b82be049729 100644 --- a/src/klibc/utest/TC_rt_sscanf.c +++ b/src/klibc/utest/TC_rt_sscanf.c @@ -37,7 +37,7 @@ static void TC_rt_sscanf_basic_float(void) float value; int result = rt_sscanf(str, "%f", &value); uassert_int_equal(result, 1); - uassert_in_range(value, 123.445, 123.455); /* Floating point comparison with tolerance */ + uassert_float_equal(value, 123.45); } static void TC_rt_sscanf_basic_string(void)