Zephyr API Documentation 4.2.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
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/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 __printf_like(6, 7) bool z_zassert(bool cond, const char *default_msg,
80 const char *file, int line, const char *func,
81 const char *msg, ...)
82{
83 if (cond == false) {
84 va_list vargs;
85
86 va_start(vargs, msg);
87 PRINT_DATA("\n Assertion failed at %s:%d: %s: %s\n",
88 ztest_relative_filename(file), line, func, default_msg);
89 vprintk(msg, vargs);
90 printk("\n");
91 va_end(vargs);
93 return false;
94 }
95#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
96 else {
97 PRINT_DATA("\n Assertion succeeded at %s:%d (%s)\n",
98 ztest_relative_filename(file), line, func);
99 }
100#endif
101 return true;
102}
103
104static inline __printf_like(6, 7) bool z_zassume(bool cond, const char *default_msg,
105 const char *file, int line, const char *func,
106 const char *msg, ...)
107{
108 if (cond == false) {
109 va_list vargs;
110
111 va_start(vargs, msg);
112 PRINT_DATA("\n Assumption failed at %s:%d: %s: %s\n",
113 ztest_relative_filename(file), line, func, default_msg);
114 vprintk(msg, vargs);
115 printk("\n");
116 va_end(vargs);
118 return false;
119 }
120#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
121 else {
122 PRINT_DATA("\n Assumption succeeded at %s:%d (%s)\n",
123 ztest_relative_filename(file), line, func);
124 }
125#endif
126 return true;
127}
128
129static inline __printf_like(6, 7) bool z_zexpect(bool cond, const char *default_msg,
130 const char *file, int line, const char *func,
131 const char *msg, ...)
132{
133 if (cond == false) {
134 va_list vargs;
135
136 va_start(vargs, msg);
137 PRINT_DATA("\n Expectation failed at %s:%d: %s: %s\n",
138 ztest_relative_filename(file), line, func, default_msg);
139 vprintk(msg, vargs);
140 printk("\n");
141 va_end(vargs);
143 return false;
144 }
145#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
146 else {
147 PRINT_DATA("\n Expectation succeeded at %s:%d (%s)\n",
148 ztest_relative_filename(file), line, func);
149 }
150#endif
151 return true;
152}
153
154#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
155
164
179#define _zassert_base(cond, default_msg, msg, ...) \
180 do { \
181 bool _msg = (msg != NULL); \
182 bool _ret = \
183 z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
184 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
185 (void)_msg; \
186 if (!_ret) { \
187 /* If kernel but without multithreading return. */ \
188 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
189 ()) \
190 } \
191 } while (0)
192
193#define _zassert_va(cond, default_msg, msg, ...) \
194 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
195
196#define zassert(cond, default_msg, ...) \
197 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
198
217#define _zassume_base(cond, default_msg, msg, ...) \
218 do { \
219 bool _msg = (msg != NULL); \
220 bool _ret = \
221 z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
222 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
223 (void)_msg; \
224 if (!_ret) { \
225 /* If kernel but without multithreading return. */ \
226 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
227 ()) \
228 } \
229 } while (0)
230
231#define _zassume_va(cond, default_msg, msg, ...) \
232 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
233
234#define zassume(cond, default_msg, ...) \
235 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
236
247#define _zexpect_base(cond, default_msg, msg, ...) \
248 do { \
249 bool _msg = (msg != NULL); \
250 bool _ret = \
251 z_zexpect(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
252 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
253 (void)_msg; \
254 if (!_ret) { \
255 /* If kernel but without multithreading return. */ \
256 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
257 ()) \
258 } \
259 } while (0)
260
261#define _zexpect_va(cond, default_msg, msg, ...) \
262 _zexpect_base(cond, default_msg, msg, ##__VA_ARGS__)
263
264#define zexpect(cond, default_msg, ...) \
265 _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
266
271#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
272
278#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
279
285#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
286
292#define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
293
299#define zassert_not_ok(cond, ...) zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)
300
306#define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
307
313#define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
314
324#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
325
335#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
336
346#define zassert_equal_ptr(a, b, ...) \
347 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
348
357#define zassert_within(a, b, d, ...) \
358 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
359 ##__VA_ARGS__)
360
370#define zassert_between_inclusive(a, l, u, ...) \
371 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
372 ##__VA_ARGS__)
373
385#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
386
398#define zassert_mem_equal__(buf, exp, size, ...) \
399 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
400
408#define zassert_str_equal(s1, s2, ...) \
409 zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
410
414
423
432#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
433
442#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
443
452#define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
453
462#define zassume_not_ok(cond, ...) zassume(!!(cond), #cond " is zero", ##__VA_ARGS__)
463
472#define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
473
482#define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
483
494#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
495
506#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
507
518#define zassume_equal_ptr(a, b, ...) \
519 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
520
531#define zassume_within(a, b, d, ...) \
532 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
533 ##__VA_ARGS__)
534
546#define zassume_between_inclusive(a, l, u, ...) \
547 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
548 ##__VA_ARGS__)
549
561#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
562
576#define zassume_mem_equal__(buf, exp, size, ...) \
577 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
578
586#define zassume_str_equal(s1, s2, ...) \
587 zassume(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
588
592
601
608#define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
609
616#define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
617
625#define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__)
626
634#define zexpect_not_ok(cond, ...) zexpect(!!(cond), #cond " is zero", ##__VA_ARGS__)
635
642#define zexpect_is_null(ptr, ...) zexpect((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
643
650#define zexpect_not_null(ptr, ...) zexpect((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
651
659#define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
660
671#define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
672
682#define zexpect_equal_ptr(a, b, ...) \
683 zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
684
694#define zexpect_within(a, b, delta, ...) \
695 zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
696 #a " not within " #b " +/- " #delta, ##__VA_ARGS__)
697
707#define zexpect_between_inclusive(a, lower, upper, ...) \
708 zexpect(((a) >= (lower)) && ((a) <= (upper)), \
709 #a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
710
720#define zexpect_mem_equal(buf, exp, size, ...) \
721 zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
722
731#define zexpect_str_equal(s1, s2, ...) \
732 zexpect(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
733
737
738#ifdef __cplusplus
739}
740#endif
741
742#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 bool
Definition stdbool.h:13
#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)