123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-11-19 MurphyZhao the first version
- */
- #ifndef __UTEST_ASSERT_H__
- #define __UTEST_ASSERT_H__
- #include "utest.h"
- #include <rtthread.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* No need for the user to use this function directly */
- rt_bool_t utest_assert(int value, const char *file, int line, const char *func, const char *msg);
- /* No need for the user to use this function directly */
- void utest_assert_string(const char *a, const char *b, rt_bool_t equal, const char *file, int line, const char *func, const char *msg);
- void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equal, const char *file, int line, const char *func, const char *msg);
- /* 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
- *
- * @brief Get the utest data structure handle.
- * No need for the user to call this function directly.
- *
- * @macro uassert_true if @value is true, not assert, means passing.
- * @macro uassert_false if @value is false, not assert, means passing.
- * @macro uassert_null if @value is null, not assert, means passing.
- * @macro uassert_not_null if @value is not null, not assert, means passing.
- * @macro uassert_int_equal if @a equal to @b, not assert, means passing. Integer type test.
- * @macro uassert_int_not_equal if @a not equal to @b, not assert, means passing. Integer type test.
- * @macro uassert_str_equal if @a equal to @b, not assert, means passing. String type test.
- * @macro uassert_str_not_equal if @a not equal to @b, not assert, means passing. String type test.
- * @macro uassert_buf_equal if @a equal to @b, not assert, means passing. buf type test.
- * @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test.
- * @macro uassert_in_range if @value is in range of min and max, not assert, means passing.
- * @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing.
- * @macro uassert_float_equal if @a equal to @b, not assert, means passing. Float type test.
- * @macro uassert_float_not_equal if @a not equal to @b, not assert, means passing. Float type test.
- * @macro uassert_value_less if @a less than @b, not assert, means passing.
- * @macro uassert_value_less_equal if @a less than or equal to @b, not assert, means passing.
- * @macro uassert_value_greater if @a greater than @b, not assert, means passing.
- * @macro uassert_value_greater_equal if @a greater than or equal to @b, not assert, means passing.
- * @macro uassert_ptr_equal if @a equal to @b, not assert, means passing. Pointer type test.
- * @macro uassert_ptr_not_equal if @a not equal to @b, not assert, means passing. Pointer type test.
- */
- #define uassert_true(value) __utest_assert(value, "(" #value ") is false")
- #define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")
- #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_in_range(value, min, max) \
- do { \
- double _value = (value); \
- double _min = (min); \
- double _max = (max); \
- __utest_assert((_value >= _min) && (_value <= _max), "(" #value ") not in range("#min","#max")"); \
- } while(0)
- #define uassert_not_in_range(value, min, max) \
- do { \
- double _value = (value); \
- double _min = (min); \
- double _max = (max); \
- __utest_assert((_value < _min) || (_value > _max), "(" #value ") in range("#min","#max")"); \
- } while(0)
- #define uassert_float_equal(a, b) \
- do { \
- double _a = (a); \
- double _b = (b); \
- uassert_in_range(_a, ((double)_b - 0.0001), ((double)_b + 0.0001)); \
- } while(0)
- #define uassert_float_not_equal(a, b) \
- do { \
- double _a = (a); \
- double _b = (b); \
- uassert_not_in_range(_a, ((double)_b - 0.0001), ((double)_b + 0.0001)); \
- } while(0)
- #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 ")")
- #define uassert_str_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_TRUE, __FILE__, __LINE__, __func__, "string not equal")
- #define uassert_str_not_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal")
- #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")
- #ifdef __cplusplus
- }
- #endif
- #endif /* __UTEST_ASSERT_H__ */
|