Zephyr API Documentation 4.0.99
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
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/tc_util.h>
22#include <zephyr/ztest.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28const char *ztest_relative_filename(const char *file);
29void ztest_test_fail(void);
30void ztest_test_skip(void);
33#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
34
35static inline bool z_zassert_(bool cond, const char *file, int line)
36{
37 if (cond == false) {
38 PRINT_DATA("\n Assertion failed at %s:%d\n", ztest_relative_filename(file),
39 line);
41 return false;
42 }
43
44 return true;
45}
46
47#define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
48
49static inline bool z_zassume_(bool cond, const char *file, int line)
50{
51 if (cond == false) {
52 PRINT_DATA("\n Assumption failed at %s:%d\n", ztest_relative_filename(file),
53 line);
55 return false;
56 }
57
58 return true;
59}
60
61#define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
62
63static inline bool z_zexpect_(bool cond, const char *file, int line)
64{
65 if (cond == false) {
66 PRINT_DATA("\n Expectation failed at %s:%d\n", ztest_relative_filename(file),
67 line);
69 return false;
70 }
71
72 return true;
73}
74
75#define z_zexpect(cond, default_msg, file, line, func, msg, ...) z_zexpect_(cond, file, line)
76
77#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
78
79static inline bool z_zassert(bool cond, const char *default_msg, const char *file, int line,
80 const char *func, const char *msg, ...)
81{
82 if (cond == false) {
83 va_list vargs;
84
85 va_start(vargs, msg);
86 PRINT_DATA("\n Assertion failed at %s:%d: %s: %s\n",
87 ztest_relative_filename(file), line, func, default_msg);
88 vprintk(msg, vargs);
89 printk("\n");
90 va_end(vargs);
92 return false;
93 }
94#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
95 else {
96 PRINT_DATA("\n Assertion succeeded at %s:%d (%s)\n",
97 ztest_relative_filename(file), line, func);
98 }
99#endif
100 return true;
101}
102
103static inline bool z_zassume(bool cond, const char *default_msg, const char *file, int line,
104 const char *func, const char *msg, ...)
105{
106 if (cond == false) {
107 va_list vargs;
108
109 va_start(vargs, msg);
110 PRINT_DATA("\n Assumption 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 Assumption succeeded at %s:%d (%s)\n",
121 ztest_relative_filename(file), line, func);
122 }
123#endif
124 return true;
125}
126
127static inline bool z_zexpect(bool cond, const char *default_msg, const char *file, int line,
128 const char *func, const char *msg, ...)
129{
130 if (cond == false) {
131 va_list vargs;
132
133 va_start(vargs, msg);
134 PRINT_DATA("\n Expectation failed at %s:%d: %s: %s\n",
135 ztest_relative_filename(file), line, func, default_msg);
136 vprintk(msg, vargs);
137 printk("\n");
138 va_end(vargs);
140 return false;
141 }
142#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
143 else {
144 PRINT_DATA("\n Expectation succeeded at %s:%d (%s)\n",
145 ztest_relative_filename(file), line, func);
146 }
147#endif
148 return true;
149}
150
151#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
152
176#define _zassert_base(cond, default_msg, msg, ...) \
177 do { \
178 bool _msg = (msg != NULL); \
179 bool _ret = \
180 z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
181 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
182 (void)_msg; \
183 if (!_ret) { \
184 /* If kernel but without multithreading return. */ \
185 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
186 ()) \
187 } \
188 } while (0)
189
190#define _zassert_va(cond, default_msg, msg, ...) \
191 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
192
193#define zassert(cond, default_msg, ...) \
194 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
195
214#define _zassume_base(cond, default_msg, msg, ...) \
215 do { \
216 bool _msg = (msg != NULL); \
217 bool _ret = \
218 z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
219 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
220 (void)_msg; \
221 if (!_ret) { \
222 /* If kernel but without multithreading return. */ \
223 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
224 ()) \
225 } \
226 } while (0)
227
228#define _zassume_va(cond, default_msg, msg, ...) \
229 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
230
231#define zassume(cond, default_msg, ...) \
232 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
233
244#define _zexpect_base(cond, default_msg, msg, ...) \
245 do { \
246 bool _msg = (msg != NULL); \
247 bool _ret = \
248 z_zexpect(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
249 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
250 (void)_msg; \
251 if (!_ret) { \
252 /* If kernel but without multithreading return. */ \
253 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
254 ()) \
255 } \
256 } while (0)
257
258#define _zexpect_va(cond, default_msg, msg, ...) \
259 _zexpect_base(cond, default_msg, msg, ##__VA_ARGS__)
260
261#define zexpect(cond, default_msg, ...) \
262 _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
263
268#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
269
275#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
276
282#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
283
289#define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
290
296#define zassert_not_ok(cond, ...) zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)
297
303#define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
304
310#define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
311
321#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
322
332#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
333
343#define zassert_equal_ptr(a, b, ...) \
344 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
345
354#define zassert_within(a, b, d, ...) \
355 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
356 ##__VA_ARGS__)
357
367#define zassert_between_inclusive(a, l, u, ...) \
368 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
369 ##__VA_ARGS__)
370
382#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
383
395#define zassert_mem_equal__(buf, exp, size, ...) \
396 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
397
405#define zassert_str_equal(s1, s2, ...) \
406 zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
407
429#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
430
439#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
440
449#define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
450
459#define zassume_not_ok(cond, ...) zassume(!!(cond), #cond " is zero", ##__VA_ARGS__)
460
469#define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
470
479#define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
480
491#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
492
503#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
504
515#define zassume_equal_ptr(a, b, ...) \
516 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
517
528#define zassume_within(a, b, d, ...) \
529 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
530 ##__VA_ARGS__)
531
543#define zassume_between_inclusive(a, l, u, ...) \
544 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
545 ##__VA_ARGS__)
546
558#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
559
573#define zassume_mem_equal__(buf, exp, size, ...) \
574 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
575
583#define zassume_str_equal(s1, s2, ...) \
584 zassume(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
585
605#define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
606
613#define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
614
622#define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__)
623
631#define zexpect_not_ok(cond, ...) zexpect(!!(cond), #cond " is zero", ##__VA_ARGS__)
632
639#define zexpect_is_null(ptr, ...) zexpect((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
640
647#define zexpect_not_null(ptr, ...) zexpect((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
648
656#define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
657
668#define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
669
679#define zexpect_equal_ptr(a, b, ...) \
680 zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
681
691#define zexpect_within(a, b, delta, ...) \
692 zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
693 #a " not within " #b " +/- " #delta, ##__VA_ARGS__)
694
704#define zexpect_between_inclusive(a, lower, upper, ...) \
705 zexpect(((a) >= (lower)) && ((a) <= (upper)), \
706 #a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
707
717#define zexpect_mem_equal(buf, exp, size, ...) \
718 zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
719
728#define zexpect_str_equal(s1, s2, ...) \
729 zexpect(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
730
735#ifdef __cplusplus
736}
737#endif
738
739#endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
static void vprintk(const char *fmt, va_list ap)
Definition printk.h:56
static void printk(const char *fmt,...)
Print kernel debugging message.
Definition printk.h:51
#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)