Skip to content

Commit

Permalink
[utest] add float operators
Browse files Browse the repository at this point in the history
uassert_float_equal and uassert_float_not_equal
  • Loading branch information
mysterywolf committed Jan 8, 2025
1 parent c9c103f commit 0a25fcf
Showing 4 changed files with 33 additions and 29 deletions.
12 changes: 6 additions & 6 deletions components/utilities/utest/TC_uassert.c
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 14 additions & 10 deletions components/utilities/utest/utest_assert.h
Original file line number Diff line number Diff line change
@@ -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
24 changes: 12 additions & 12 deletions src/klibc/utest/TC_rt_memcmp.c
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/klibc/utest/TC_rt_sscanf.c
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0a25fcf

Please sign in to comment.