Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
ztest_assert.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
14#define ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
15
16#include <stdarg.h>
17#include <stdbool.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <zephyr/sys/util.h>
22#include <zephyr/tc_util.h>
23#include <zephyr/ztest.h>
24
25#if defined(__cplusplus) && (__cplusplus >= 201103L)
26/* Raw pointers; arrays and functions decay during template deduction. */
27template < typename T >
28static inline bool z_ztest_is_null(T *ptr)
29{
30 return ptr == nullptr;
31}
32/* Anything else comparable against nullptr, such as smart pointers. */
33template < typename T >
34static inline auto z_ztest_is_null(const T &ptr) -> decltype(ptr == nullptr)
35{
36 return ptr == nullptr;
37}
38static inline bool z_ztest_is_null(decltype(nullptr))
39{
40 return true;
41}
42#define Z_ZTEST_IS_NULL(ptr) z_ztest_is_null(ptr)
43#else
44#define Z_ZTEST_IS_NULL(ptr) is_null_no_warn((void *)(ptr))
45#endif
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51const char *ztest_relative_filename(const char *file);
52void ztest_test_fail(void);
53void ztest_test_skip(void);
56#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
57
58static inline bool z_zassert_(bool cond, const char *file, int line)
59{
60 if (cond == false) {
61 PRINT_DATA("\n Assertion failed at %s:%d\n", ztest_relative_filename(file),
62 line);
64 return false;
65 }
66
67 return true;
68}
69
70#define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
71
72static inline bool z_zassume_(bool cond, const char *file, int line)
73{
74 if (cond == false) {
75 PRINT_DATA("\n Assumption failed at %s:%d\n", ztest_relative_filename(file),
76 line);
78 return false;
79 }
80
81 return true;
82}
83
84#define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
85
86static inline bool z_zexpect_(bool cond, const char *file, int line)
87{
88 if (cond == false) {
89 PRINT_DATA("\n Expectation failed at %s:%d\n", ztest_relative_filename(file),
90 line);
92 return false;
93 }
94
95 return true;
96}
97
98#define z_zexpect(cond, default_msg, file, line, func, msg, ...) z_zexpect_(cond, file, line)
99
100#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
101
102static inline __printf_like(6, 7) bool z_zassert(bool cond, const char *default_msg,
103 const char *file, int line, const char *func,
104 const char *msg, ...)
105{
106 if (cond == false) {
107 va_list vargs;
108
109 va_start(vargs, msg);
110 PRINT_DATA("\n Assertion failed at %s:%d: %s: %s\n",
111 ztest_relative_filename(file), line, func, default_msg);
112 vprintk(msg, vargs);
113 printk("\n");
114 va_end(vargs);
116 return false;
117 }
118#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
119 else {
120 PRINT_DATA("\n Assertion succeeded at %s:%d (%s)\n",
121 ztest_relative_filename(file), line, func);
122 }
123#endif
124 return true;
125}
126
127static inline __printf_like(6, 7) bool z_zassume(bool cond, const char *default_msg,
128 const char *file, int line, const char *func,
129 const char *msg, ...)
130{
131 if (cond == false) {
132 va_list vargs;
133
134 va_start(vargs, msg);
135 PRINT_DATA("\n Assumption failed at %s:%d: %s: %s\n",
136 ztest_relative_filename(file), line, func, default_msg);
137 vprintk(msg, vargs);
138 printk("\n");
139 va_end(vargs);
141 return false;
142 }
143#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
144 else {
145 PRINT_DATA("\n Assumption succeeded at %s:%d (%s)\n",
146 ztest_relative_filename(file), line, func);
147 }
148#endif
149 return true;
150}
151
152static inline __printf_like(6, 7) bool z_zexpect(bool cond, const char *default_msg,
153 const char *file, int line, const char *func,
154 const char *msg, ...)
155{
156 if (cond == false) {
157 va_list vargs;
158
159 va_start(vargs, msg);
160 PRINT_DATA("\n Expectation failed at %s:%d: %s: %s\n",
161 ztest_relative_filename(file), line, func, default_msg);
162 vprintk(msg, vargs);
163 printk("\n");
164 va_end(vargs);
166 return false;
167 }
168#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
169 else {
170 PRINT_DATA("\n Expectation succeeded at %s:%d (%s)\n",
171 ztest_relative_filename(file), line, func);
172 }
173#endif
174 return true;
175}
176
177#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
178
187
202#define _zassert_base(cond, default_msg, msg, ...) \
203 do { \
204 bool _msg = (msg != NULL); \
205 bool _ret = \
206 z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
207 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
208 (void)_msg; \
209 if (!_ret) { \
210 /* If not multithreading, return from the test function. */ \
211 COND_CODE_1(CONFIG_MULTITHREADING, (), (return;)) \
212 } \
213 } while (0)
214
215#define _zassert_va(cond, default_msg, msg, ...) \
216 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
217
218#define zassert(cond, default_msg, ...) \
219 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
220
239#define _zassume_base(cond, default_msg, msg, ...) \
240 do { \
241 bool _msg = (msg != NULL); \
242 bool _ret = \
243 z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
244 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
245 (void)_msg; \
246 if (!_ret) { \
247 /* If not multithreading, return from the test function. */ \
248 COND_CODE_1(CONFIG_MULTITHREADING, (), (return;)) \
249 } \
250 } while (0)
251
252#define _zassume_va(cond, default_msg, msg, ...) \
253 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
254
255#define zassume(cond, default_msg, ...) \
256 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
257
268#define _zexpect_base(cond, default_msg, msg, ...) \
269 do { \
270 bool _msg = (msg != NULL); \
271 bool _ret = \
272 z_zexpect(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
273 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
274 (void)_msg; \
275 if (!_ret) { \
276 /* If not multithreading, return from the test function. */ \
277 COND_CODE_1(CONFIG_MULTITHREADING, (), (return;)) \
278 } \
279 } while (0)
280
281#define _zexpect_va(cond, default_msg, msg, ...) \
282 _zexpect_base(cond, default_msg, msg, ##__VA_ARGS__)
283
284#define zexpect(cond, default_msg, ...) \
285 _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
286
291#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
292
298#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
299
305#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
306
312#define zassert_ok(cond, ...) zassert((cond) == 0, #cond " is non-zero", ##__VA_ARGS__)
313
319#define zassert_not_ok(cond, ...) zassert((cond) != 0, #cond " is zero", ##__VA_ARGS__)
320
326#define zassert_is_null(ptr, ...) \
327 zassert(Z_ZTEST_IS_NULL(ptr), #ptr " is not NULL", ##__VA_ARGS__)
328
334#define zassert_not_null(ptr, ...) \
335 zassert(!Z_ZTEST_IS_NULL(ptr), #ptr " is NULL", ##__VA_ARGS__)
336
346#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
347
357#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
358
368#define zassert_equal_ptr(a, b, ...) \
369 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
370
379#define zassert_within(a, b, d, ...) \
380 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
381 ##__VA_ARGS__)
382
392#define zassert_between_inclusive(a, l, u, ...) \
393 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
394 ##__VA_ARGS__)
395
407#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
408
420#define zassert_mem_equal__(buf, exp, size, ...) \
421 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
422
430#define zassert_str_equal(s1, s2, ...) \
431 zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
432
436
445
454#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
455
464#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
465
474#define zassume_ok(cond, ...) zassume((cond) == 0, #cond " is non-zero", ##__VA_ARGS__)
475
484#define zassume_not_ok(cond, ...) zassume((cond) != 0, #cond " is zero", ##__VA_ARGS__)
485
494#define zassume_is_null(ptr, ...) \
495 zassume(Z_ZTEST_IS_NULL(ptr), #ptr " is not NULL", ##__VA_ARGS__)
496
505#define zassume_not_null(ptr, ...) \
506 zassume(!Z_ZTEST_IS_NULL(ptr), #ptr " is NULL", ##__VA_ARGS__)
507
518#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
519
530#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
531
542#define zassume_equal_ptr(a, b, ...) \
543 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
544
555#define zassume_within(a, b, d, ...) \
556 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
557 ##__VA_ARGS__)
558
570#define zassume_between_inclusive(a, l, u, ...) \
571 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
572 ##__VA_ARGS__)
573
585#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
586
600#define zassume_mem_equal__(buf, exp, size, ...) \
601 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
602
610#define zassume_str_equal(s1, s2, ...) \
611 zassume(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
612
616
625
632#define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
633
640#define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
641
649#define zexpect_ok(cond, ...) zexpect((cond) == 0, #cond " is non-zero", ##__VA_ARGS__)
650
658#define zexpect_not_ok(cond, ...) zexpect((cond) != 0, #cond " is zero", ##__VA_ARGS__)
659
666#define zexpect_is_null(ptr, ...) \
667 zexpect(Z_ZTEST_IS_NULL(ptr), #ptr " is not NULL", ##__VA_ARGS__)
668
675#define zexpect_not_null(ptr, ...) \
676 zexpect(!Z_ZTEST_IS_NULL(ptr), #ptr " is NULL", ##__VA_ARGS__)
677
685#define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
686
697#define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
698
708#define zexpect_equal_ptr(a, b, ...) \
709 zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
710
720#define zexpect_within(a, b, delta, ...) \
721 zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
722 #a " not within " #b " +/- " #delta, ##__VA_ARGS__)
723
733#define zexpect_between_inclusive(a, lower, upper, ...) \
734 zexpect(((a) >= (lower)) && ((a) <= (upper)), \
735 #a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
736
746#define zexpect_mem_equal(buf, exp, size, ...) \
747 zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
748
757#define zexpect_str_equal(s1, s2, ...) \
758 zexpect(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
759
763
764#ifdef __cplusplus
765}
766#endif
767
768#endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
void printk(const char *fmt,...)
Print kernel debugging message.
void vprintk(const char *fmt, va_list ap)
Print kernel debugging message, va_list version.
#define bool
Definition stdbool.h:13
Misc utilities.
#define PRINT_DATA(fmt,...)
Definition tc_util.h:25
Zephyr Testsuite.
const char * ztest_relative_filename(const char *file)
void ztest_skip_failed_assumption(void)
void ztest_test_expect_fail(void)
void ztest_test_fail(void)
void ztest_test_skip(void)