Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[utest] add float operators #9868

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions components/utilities/utest/TC_uassert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion components/utilities/utest/utest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions components/utilities/utest/utest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ")")
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions src/klibc/utest/TC_rt_memcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/klibc/utest/TC_rt_sscanf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading