1 /* 2 * Copyright (c) 2006-2021, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2018-11-19 MurphyZhao the first version 9 */ 10 11 #ifndef __UTEST_ASSERT_H__ 12 #define __UTEST_ASSERT_H__ 13 14 #include "utest.h" 15 #include <rtthread.h> 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 /* No need for the user to use this function directly */ 22 rt_bool_t utest_assert(int value, const char *file, int line, const char *func, const char *msg); 23 24 /* No need for the user to use this function directly */ 25 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); 26 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); 27 28 /* No need for the user to use this macro directly */ 29 #define __utest_assert(value, msg) utest_assert(value, __FILE__, __LINE__, __func__, msg) 30 #define __uassert_value_op(a, b, op) __utest_assert((a) op (b), "(" #a ") not " #op " (" #b ")") 31 32 /** 33 * uassert_x macros 34 * 35 * @brief Get the utest data structure handle. 36 * No need for the user to call this function directly. 37 * 38 * @macro uassert_true if @value is true, not assert, means passing. 39 * @macro uassert_false if @value is false, not assert, means passing. 40 * @macro uassert_null if @value is null, not assert, means passing. 41 * @macro uassert_not_null if @value is not null, not assert, means passing. 42 * @macro uassert_int_equal if @a equal to @b, not assert, means passing. Integer type test. 43 * @macro uassert_int_not_equal if @a not equal to @b, not assert, means passing. Integer type test. 44 * @macro uassert_str_equal if @a equal to @b, not assert, means passing. String type test. 45 * @macro uassert_str_not_equal if @a not equal to @b, not assert, means passing. String type test. 46 * @macro uassert_buf_equal if @a equal to @b, not assert, means passing. buf type test. 47 * @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test. 48 * @macro uassert_in_range if @value is in range of min and max, not assert, means passing. 49 * @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing. 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 */ 59 #define uassert_true(value) __utest_assert(value, "(" #value ") is false") 60 #define uassert_false(value) __utest_assert(!(value), "(" #value ") is true") 61 62 #define uassert_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null") 63 #define uassert_not_null(value) __utest_assert((const char *)(value) != RT_NULL, "(" #value ") is null") 64 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) 94 95 #define uassert_int_equal(a, b) __uassert_value_op(a, b, ==) 96 #define uassert_int_not_equal(a, b) __uassert_value_op(a, b, !=) 97 98 #define uassert_value_less(a, b) __uassert_value_op(a, b, <) 99 #define uassert_value_less_equal(a, b) __uassert_value_op(a, b, <=) 100 #define uassert_value_greater(a, b) __uassert_value_op(a, b, >) 101 #define uassert_value_greater_equal(a, b) __uassert_value_op(a, b, >=) 102 103 #define uassert_ptr_equal(a, b) __utest_assert((const void*)(a) == (const void*)(b), "(" #a ") not equal to (" #b ")") 104 #define uassert_ptr_not_equal(a, b) __utest_assert((const void*)(a) != (const void*)(b), "(" #a ") equal to (" #b ")") 105 106 #define uassert_str_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_TRUE, __FILE__, __LINE__, __func__, "string not equal") 107 #define uassert_str_not_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal") 108 109 #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") 110 #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") 111 112 #ifdef __cplusplus 113 } 114 #endif 115 116 #endif /* __UTEST_ASSERT_H__ */ 117