xref: /nrf52832-nimble/rt-thread/components/utilities/utest/utest_assert.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, 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 /* No need for the user to use this function directly */
18 void utest_assert(int value, const char *file, int line, const char *func, const char *msg);
19 
20 /* No need for the user to use this function directly */
21 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);
22 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);
23 
24 /* No need for the user to use this macro directly */
25 #define __utest_assert(value, msg) utest_assert(value, __FILE__, __LINE__, __func__, msg)
26 
27 /**
28  * uassert_x macros
29  *
30  * @brief Get the utest data structure handle.
31  *        No need for the user to call this function directly.
32  *
33  * @macro uassert_true          if @value is true,     not assert, means passing.
34  * @macro uassert_false         if @value is false,    not assert, means passing.
35  * @macro uassert_null          if @value is null,     not assert, means passing.
36  * @macro uassert_not_null      if @value is not null, not assert, means passing.
37  * @macro uassert_int_equal     if @a equal to @b,     not assert, means passing. Integer type test.
38  * @macro uassert_int_not_equal if @a not equal to @b, not assert, means passing. Integer type test.
39  * @macro uassert_str_equal     if @a equal to @b,     not assert, means passing. String type test.
40  * @macro uassert_str_not_equal if @a not equal to @b, not assert, means passing. String type test.
41  * @macro uassert_buf_equal     if @a equal to @b,     not assert, means passing. buf type test.
42  * @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test.
43  * @macro uassert_in_range      if @value is in range of min and max,     not assert, means passing.
44  * @macro uassert_not_in_range  if @value is not in range of min and max, not assert, means passing.
45  *
46 */
47 #define uassert_true(value)      __utest_assert(value, "(" #value ") is false")
48 #define uassert_false(value)     __utest_assert(!(value), "(" #value ") is true")
49 #define uassert_null(value)      __utest_assert((const char *)(value) == NULL, "(" #value ") is not null")
50 #define uassert_not_null(value)  __utest_assert((const char *)(value) != NULL, "(" #value ") is null")
51 
52 #define uassert_int_equal(a, b)      __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")")
53 #define uassert_int_not_equal(a, b)  __utest_assert((a) != (b), "(" #a ") equal to (" #b ")")
54 
55 #define uassert_str_equal(a, b)      utest_assert_string((const char*)(a), (const char*)(b), RT_TRUE, __FILE__, __LINE__, __func__, "string not equal")
56 #define uassert_str_not_equal(a, b)  utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal")
57 
58 #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")
59 #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")
60 
61 #define uassert_in_range(value, min, max)     __utest_assert(((value >= min) && (value <= max)), "(" #value ") not in range("#min","#max")")
62 #define uassert_not_in_range(value, min, max) __utest_assert(!((value >= min) && (value <= max)), "(" #value ") in range("#min","#max")")
63 
64 #endif /* __UTEST_ASSERT_H__ */
65