Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016 Intel Corporation
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * @file
9 : : *
10 : : * @brief Zephyr testing framework assertion macros
11 : : */
12 : :
13 : : #ifndef ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
14 : : #define ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
15 : :
16 : : #include <ztest.h>
17 : : #include <stdarg.h>
18 : : #include <stdio.h>
19 : : #include <string.h>
20 : : #include <stdbool.h>
21 : :
22 : : #ifdef __cplusplus
23 : : extern "C" {
24 : : #endif
25 : :
26 : : const char *ztest_relative_filename(const char *file);
27 : : void ztest_test_fail(void);
28 : : #if CONFIG_ZTEST_ASSERT_VERBOSE == 0
29 : :
30 : : static inline bool z_zassert_(bool cond, const char *file, int line)
31 : : {
32 : : if (cond == false) {
33 : : PRINT("\n Assertion failed at %s:%d\n",
34 : : ztest_relative_filename(file), line);
35 : : ztest_test_fail();
36 : : return false;
37 : : }
38 : :
39 : : return true;
40 : : }
41 : :
42 : : #define z_zassert(cond, default_msg, file, line, func, msg, ...) \
43 : : z_zassert_(cond, file, line)
44 : :
45 : : #else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
46 : :
47 : 1 : static inline bool z_zassert(bool cond,
48 : : const char *default_msg,
49 : : const char *file,
50 : : int line, const char *func,
51 : : const char *msg, ...)
52 : : {
53 [ - + ]: 1 : if (cond == false) {
54 : : va_list vargs;
55 : :
56 : 0 : va_start(vargs, msg);
57 : 0 : PRINT("\n Assertion failed at %s:%d: %s: %s\n",
58 : : ztest_relative_filename(file), line, func, default_msg);
59 : 0 : vprintk(msg, vargs);
60 : 0 : printk("\n");
61 : 0 : va_end(vargs);
62 : 0 : ztest_test_fail();
63 : 0 : return false;
64 : : }
65 : : #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
66 : : else {
67 : : PRINT("\n Assertion succeeded at %s:%d (%s)\n",
68 : : ztest_relative_filename(file), line, func);
69 : : }
70 : : #endif
71 : 1 : return true;
72 : : }
73 : :
74 : : #endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
75 : :
76 : :
77 : : /**
78 : : * @defgroup ztest_assert Ztest assertion macros
79 : : * @ingroup ztest
80 : : *
81 : : * This module provides assertions when using Ztest.
82 : : *
83 : : * @{
84 : : */
85 : :
86 : : /**
87 : : * @brief Fail the test, if @a cond is false
88 : : *
89 : : * You probably don't need to call this macro directly. You should
90 : : * instead use zassert_{condition} macros below.
91 : : *
92 : : * Note that when CONFIG_MULTITHREADING=n macro returns from the function. It is
93 : : * then expected that in that case ztest asserts will be used only in the
94 : : * context of the test function.
95 : : *
96 : : * @param cond Condition to check
97 : : * @param msg Optional, can be NULL. Message to print if @a cond is false.
98 : : * @param default_msg Message to print if @a cond is false
99 : : */
100 : : #define zassert(cond, default_msg, msg, ...) do { \
101 : : bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
102 : : __FILE__, __LINE__, __func__, \
103 : : msg ? msg : "", ##__VA_ARGS__); \
104 : : if (!_ret) { \
105 : : /* If kernel but without multithreading return. */ \
106 : : COND_CODE_1(KERNEL, \
107 : : (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
108 : : ()) \
109 : : } \
110 : : } while (0)
111 : :
112 : : /**
113 : : * @brief Assert that this function call won't be reached
114 : : * @param msg Optional message to print if the assertion fails
115 : : */
116 : : #define zassert_unreachable(msg, ...) zassert(0, "Reached unreachable code", \
117 : : msg, ##__VA_ARGS__)
118 : :
119 : : /**
120 : : * @brief Assert that @a cond is true
121 : : * @param cond Condition to check
122 : : * @param msg Optional message to print if the assertion fails
123 : : */
124 : : #define zassert_true(cond, msg, ...) zassert(cond, #cond " is false", \
125 : : msg, ##__VA_ARGS__)
126 : :
127 : : /**
128 : : * @brief Assert that @a cond is false
129 : : * @param cond Condition to check
130 : : * @param msg Optional message to print if the assertion fails
131 : : */
132 : : #define zassert_false(cond, msg, ...) zassert(!(cond), #cond " is true", \
133 : : msg, ##__VA_ARGS__)
134 : :
135 : : /**
136 : : * @brief Assert that @a cond is 0 (success)
137 : : * @param cond Condition to check
138 : : * @param msg Optional message to print if the assertion fails
139 : : */
140 : : #define zassert_ok(cond, msg, ...) zassert(!(cond), #cond " is non-zero", \
141 : : msg, ##__VA_ARGS__)
142 : :
143 : : /**
144 : : * @brief Assert that @a ptr is NULL
145 : : * @param ptr Pointer to compare
146 : : * @param msg Optional message to print if the assertion fails
147 : : */
148 : : #define zassert_is_null(ptr, msg, ...) zassert((ptr) == NULL, \
149 : : #ptr " is not NULL", \
150 : : msg, ##__VA_ARGS__)
151 : :
152 : : /**
153 : : * @brief Assert that @a ptr is not NULL
154 : : * @param ptr Pointer to compare
155 : : * @param msg Optional message to print if the assertion fails
156 : : */
157 : : #define zassert_not_null(ptr, msg, ...) zassert((ptr) != NULL, \
158 : : #ptr " is NULL", msg, \
159 : : ##__VA_ARGS__)
160 : :
161 : : /**
162 : : * @brief Assert that @a a equals @a b
163 : : *
164 : : * @a a and @a b won't be converted and will be compared directly.
165 : : *
166 : : * @param a Value to compare
167 : : * @param b Value to compare
168 : : * @param msg Optional message to print if the assertion fails
169 : : */
170 : : #define zassert_equal(a, b, msg, ...) zassert((a) == (b), \
171 : : #a " not equal to " #b, \
172 : : msg, ##__VA_ARGS__)
173 : :
174 : : /**
175 : : * @brief Assert that @a a does not equal @a b
176 : : *
177 : : * @a a and @a b won't be converted and will be compared directly.
178 : : *
179 : : * @param a Value to compare
180 : : * @param b Value to compare
181 : : * @param msg Optional message to print if the assertion fails
182 : : */
183 : : #define zassert_not_equal(a, b, msg, ...) zassert((a) != (b), \
184 : : #a " equal to " #b, \
185 : : msg, ##__VA_ARGS__)
186 : :
187 : : /**
188 : : * @brief Assert that @a a equals @a b
189 : : *
190 : : * @a a and @a b will be converted to `void *` before comparing.
191 : : *
192 : : * @param a Value to compare
193 : : * @param b Value to compare
194 : : * @param msg Optional message to print if the assertion fails
195 : : */
196 : : #define zassert_equal_ptr(a, b, msg, ...) \
197 : : zassert((void *)(a) == (void *)(b), #a " not equal to " #b, \
198 : : msg, ##__VA_ARGS__)
199 : :
200 : : /**
201 : : * @brief Assert that @a a is within @a b with delta @a d
202 : : *
203 : : * @param a Value to compare
204 : : * @param b Value to compare
205 : : * @param d Delta
206 : : * @param msg Optional message to print if the assertion fails
207 : : */
208 : : #define zassert_within(a, b, d, msg, ...) \
209 : : zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), \
210 : : #a " not within " #b " +/- " #d, \
211 : : msg, ##__VA_ARGS__)
212 : :
213 : : /**
214 : : * @brief Assert that 2 memory buffers have the same contents
215 : : *
216 : : * This macro calls the final memory comparison assertion macro.
217 : : * Using double expansion allows providing some arguments by macros that
218 : : * would expand to more than one values (ANSI-C99 defines that all the macro
219 : : * arguments have to be expanded before macro call).
220 : : *
221 : : * @param ... Arguments, see @ref zassert_mem_equal__
222 : : * for real arguments accepted.
223 : : */
224 : : #define zassert_mem_equal(...) \
225 : : zassert_mem_equal__(__VA_ARGS__)
226 : :
227 : : /**
228 : : * @brief Internal assert that 2 memory buffers have the same contents
229 : : *
230 : : * @note This is internal macro, to be used as a second expansion.
231 : : * See @ref zassert_mem_equal.
232 : : *
233 : : * @param buf Buffer to compare
234 : : * @param exp Buffer with expected contents
235 : : * @param size Size of buffers
236 : : * @param msg Optional message to print if the assertion fails
237 : : */
238 : : #define zassert_mem_equal__(buf, exp, size, msg, ...) \
239 : : zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, \
240 : : msg, ##__VA_ARGS__)
241 : :
242 : : /**
243 : : * @}
244 : : */
245 : :
246 : : #ifdef __cplusplus
247 : : }
248 : : #endif
249 : :
250 : : #endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
|