4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-01-19 12:43:32 +08:00

[utest] add float operators

uassert_float_equal and uassert_float_not_equal
This commit is contained in:
Meco Man 2025-01-04 15:41:38 -05:00
parent c9c103f30e
commit 0a25fcffab
4 changed files with 33 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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