Skip to content

Commit da6c62c

Browse files
mysterywolfRbb666
authored andcommitted
[utest] fix twice operation of uassert
1 parent 4e37047 commit da6c62c

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

components/utilities/utest/TC_uassert.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ static void TC_uassert_int_op(void)
3232
uassert_value_greater_equal(b, b);
3333
}
3434

35+
static void TC_uassert_float_op(void)
36+
{
37+
float a = 5.0;
38+
float b = 5.0;
39+
40+
uassert_float_equal(a, b);
41+
uassert_float_not_equal(a, b + 0.0002);
42+
}
43+
3544
static void TC_uassert_ptr_op(void)
3645
{
3746
int a = 5;
@@ -65,6 +74,7 @@ static void utest_do_tc(void)
6574
UTEST_UNIT_RUN(TC_uassert_true_false);
6675
UTEST_UNIT_RUN(TC_uassert_null_not_null);
6776
UTEST_UNIT_RUN(TC_uassert_int_op);
77+
UTEST_UNIT_RUN(TC_uassert_float_op);
6878
UTEST_UNIT_RUN(TC_uassert_ptr_op);
6979
UTEST_UNIT_RUN(TC_uassert_str_op);
7080
UTEST_UNIT_RUN(TC_uassert_in_range);

components/utilities/utest/utest_assert.h

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,50 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa
4747
* @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test.
4848
* @macro uassert_in_range if @value is in range of min and max, not assert, means passing.
4949
* @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing.
50-
*
51-
*/
50+
* @macro uassert_float_equal if @a equal to @b, not assert, means passing. Float type test.
51+
* @macro uassert_float_not_equal if @a not equal to @b, not assert, means passing. Float type test.
52+
* @macro uassert_value_less if @a less than @b, not assert, means passing.
53+
* @macro uassert_value_less_equal if @a less than or equal to @b, not assert, means passing.
54+
* @macro uassert_value_greater if @a greater than @b, not assert, means passing.
55+
* @macro uassert_value_greater_equal if @a greater than or equal to @b, not assert, means passing.
56+
* @macro uassert_ptr_equal if @a equal to @b, not assert, means passing. Pointer type test.
57+
* @macro uassert_ptr_not_equal if @a not equal to @b, not assert, means passing. Pointer type test.
58+
*/
5259
#define uassert_true(value) __utest_assert(value, "(" #value ") is false")
5360
#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")
5461

5562
#define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null")
5663
#define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null")
5764

58-
#define uassert_in_range(value, min, max) __utest_assert(((value >= min) && (value <= max)), "(" #value ") not in range("#min","#max")")
59-
#define uassert_not_in_range(value, min, max) __utest_assert(!((value >= min) && (value <= max)), "(" #value ") in range("#min","#max")")
60-
61-
#define uassert_float_equal(a, b) uassert_in_range(a, ((double)b - 0.0001), ((double)b + 0.0001))
62-
#define uassert_float_not_equal(a, b) uassert_not_in_range(a, ((double)b - 0.0001), ((double)b + 0.0001))
65+
#define uassert_in_range(value, min, max) \
66+
do { \
67+
double _value = (value); \
68+
double _min = (min); \
69+
double _max = (max); \
70+
__utest_assert((_value >= _min) && (_value <= _max), "(" #value ") not in range("#min","#max")"); \
71+
} while(0)
72+
73+
#define uassert_not_in_range(value, min, max) \
74+
do { \
75+
double _value = (value); \
76+
double _min = (min); \
77+
double _max = (max); \
78+
__utest_assert((_value < _min) || (_value > _max), "(" #value ") in range("#min","#max")"); \
79+
} while(0)
80+
81+
#define uassert_float_equal(a, b) \
82+
do { \
83+
double _a = (a); \
84+
double _b = (b); \
85+
uassert_in_range(_a, ((double)_b - 0.0001), ((double)_b + 0.0001)); \
86+
} while(0)
87+
88+
#define uassert_float_not_equal(a, b) \
89+
do { \
90+
double _a = (a); \
91+
double _b = (b); \
92+
uassert_not_in_range(_a, ((double)_b - 0.0001), ((double)_b + 0.0001)); \
93+
} while(0)
6394

6495
#define uassert_int_equal(a, b) __uassert_value_op(a, b, ==)
6596
#define uassert_int_not_equal(a, b) __uassert_value_op(a, b, !=)

0 commit comments

Comments
 (0)