Zephyr Project API  3.3.0
A Scalable Open Source RTOS
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/ztest.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27const char *ztest_relative_filename(const char *file);
28void ztest_test_fail(void);
29void ztest_test_skip(void);
31#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
32
33static inline bool z_zassert_(bool cond, const char *file, int line)
34{
35 if (cond == false) {
36 PRINT("\n Assertion failed at %s:%d\n", ztest_relative_filename(file), line);
38 return false;
39 }
40
41 return true;
42}
43
44#define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
45
46static inline bool z_zassume_(bool cond, const char *file, int line)
47{
48 if (cond == false) {
49 PRINT("\n Assumption failed at %s:%d\n", ztest_relative_filename(file), line);
51 return false;
52 }
53
54 return true;
55}
56
57#define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
58
59#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
60
61static inline bool z_zassert(bool cond, const char *default_msg, const char *file, int line,
62 const char *func, const char *msg, ...)
63{
64 if (cond == false) {
65 va_list vargs;
66
67 va_start(vargs, msg);
68 PRINT("\n Assertion failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
69 line, func, default_msg);
70 vprintk(msg, vargs);
71 printk("\n");
72 va_end(vargs);
74 return false;
75 }
76#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
77 else {
78 PRINT("\n Assertion succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
79 line, func);
80 }
81#endif
82 return true;
83}
84
85static inline bool z_zassume(bool cond, const char *default_msg, const char *file, int line,
86 const char *func, const char *msg, ...)
87{
88 if (cond == false) {
89 va_list vargs;
90
91 va_start(vargs, msg);
92 PRINT("\n Assumption failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
93 line, func, default_msg);
94 vprintk(msg, vargs);
95 printk("\n");
96 va_end(vargs);
98 return false;
99 }
100#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
101 else {
102 PRINT("\n Assumption succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
103 line, func);
104 }
105#endif
106 return true;
107}
108
109#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
110
134#define _zassert_base(cond, default_msg, msg, ...) \
135 do { \
136 bool _msg = (msg != NULL); \
137 bool _ret = z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__,\
138 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
139 (void)_msg; \
140 if (!_ret) { \
141 /* If kernel but without multithreading return. */ \
142 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
143 ()) \
144 } \
145 } while (0)
146
147#define _zassert_va(cond, default_msg, msg, ...) \
148 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
149
150#define zassert(cond, default_msg, ...) \
151 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
152
171#define _zassume_base(cond, default_msg, msg, ...) \
172 do { \
173 bool _msg = (msg != NULL); \
174 bool _ret = z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__,\
175 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
176 (void)_msg; \
177 if (!_ret) { \
178 /* If kernel but without multithreading return. */ \
179 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
180 ()) \
181 } \
182 } while (0)
183
184#define _zassume_va(cond, default_msg, msg, ...) \
185 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
186
187#define zassume(cond, default_msg, ...) \
188 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
189
194#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
195
201#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
202
208#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
209
215#define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
216
222#define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
223
229#define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
230
240#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
241
251#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
252
262#define zassert_equal_ptr(a, b, ...) \
263 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
264
273#define zassert_within(a, b, d, ...) \
274 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
275 ##__VA_ARGS__)
276
286#define zassert_between_inclusive(a, l, u, ...) \
287 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
288 ##__VA_ARGS__)
289
301#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
302
314#define zassert_mem_equal__(buf, exp, size, ...) \
315 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
316
338#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
339
348#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
349
358#define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
359
368#define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
369
378#define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
379
390#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
391
402#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
403
414#define zassume_equal_ptr(a, b, ...) \
415 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
416
427#define zassume_within(a, b, d, ...) \
428 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
429 ##__VA_ARGS__)
430
442#define zassume_between_inclusive(a, l, u, ...) \
443 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
444 ##__VA_ARGS__)
445
457#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
458
472#define zassume_mem_equal__(buf, exp, size, ...) \
473 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
474
479#ifdef __cplusplus
480}
481#endif
482
483#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
static void msg(uint64_t c64)
Definition: main.c:17
Zephyr Testsuite.
#define PRINT
Definition: ztest.h:45
const char * ztest_relative_filename(const char *file)
void ztest_skip_failed_assumption(void)
void ztest_test_fail(void)
void ztest_test_skip(void)