Zephyr Project API  3.2.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);
30#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
31
32static inline bool z_zassert_(bool cond, const char *file, int line)
33{
34 if (cond == false) {
35 PRINT("\n Assertion failed at %s:%d\n", ztest_relative_filename(file), line);
37 return false;
38 }
39
40 return true;
41}
42
43#define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
44
45static inline bool z_zassume_(bool cond, const char *file, int line)
46{
47 if (cond == false) {
48 PRINT("\n Assumption failed at %s:%d\n", ztest_relative_filename(file), line);
50 return false;
51 }
52
53 return true;
54}
55
56#define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
57
58#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
59
60static inline bool z_zassert(bool cond, const char *default_msg, const char *file, int line,
61 const char *func, const char *msg, ...)
62{
63 if (cond == false) {
64 va_list vargs;
65
66 va_start(vargs, msg);
67 PRINT("\n Assertion failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
68 line, func, default_msg);
69 vprintk(msg, vargs);
70 printk("\n");
71 va_end(vargs);
73 return false;
74 }
75#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
76 else {
77 PRINT("\n Assertion succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
78 line, func);
79 }
80#endif
81 return true;
82}
83
84static inline bool z_zassume(bool cond, const char *default_msg, const char *file, int line,
85 const char *func, const char *msg, ...)
86{
87 if (cond == false) {
88 va_list vargs;
89
90 va_start(vargs, msg);
91 PRINT("\n Assumption failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
92 line, func, default_msg);
93 vprintk(msg, vargs);
94 printk("\n");
95 va_end(vargs);
97 return false;
98 }
99#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
100 else {
101 PRINT("\n Assumption succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
102 line, func);
103 }
104#endif
105 return true;
106}
107
108#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
109
133#define _zassert_base(cond, default_msg, msg, ...) \
134 do { \
135 bool _msg = (msg != NULL); \
136 bool _ret = z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__,\
137 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
138 (void)_msg; \
139 if (!_ret) { \
140 /* If kernel but without multithreading return. */ \
141 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
142 ()) \
143 } \
144 } while (0)
145
146#define _zassert_va(cond, default_msg, msg, ...) \
147 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
148
149#define zassert(cond, default_msg, ...) \
150 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
151
170#define _zassume_base(cond, default_msg, msg, ...) \
171 do { \
172 bool _msg = (msg != NULL); \
173 bool _ret = z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__,\
174 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
175 (void)_msg; \
176 if (!_ret) { \
177 /* If kernel but without multithreading return. */ \
178 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
179 ()) \
180 } \
181 } while (0)
182
183#define _zassume_va(cond, default_msg, msg, ...) \
184 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
185
186#define zassume(cond, default_msg, ...) \
187 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
188
193#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
194
200#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
201
207#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
208
214#define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
215
221#define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
222
228#define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
229
239#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
240
250#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
251
261#define zassert_equal_ptr(a, b, ...) \
262 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
263
272#define zassert_within(a, b, d, ...) \
273 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
274 ##__VA_ARGS__)
275
285#define zassert_between_inclusive(a, l, u, ...) \
286 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
287 ##__VA_ARGS__)
288
300#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
301
313#define zassert_mem_equal__(buf, exp, size, ...) \
314 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
315
337#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
338
347#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
348
357#define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
358
367#define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
368
377#define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
378
389#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
390
401#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
402
413#define zassume_equal_ptr(a, b, ...) \
414 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
415
426#define zassume_within(a, b, d, ...) \
427 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
428 ##__VA_ARGS__)
429
441#define zassume_between_inclusive(a, l, u, ...) \
442 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
443 ##__VA_ARGS__)
444
456#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
457
471#define zassume_mem_equal__(buf, exp, size, ...) \
472 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
473
478#ifdef __cplusplus
479}
480#endif
481
482#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_test_fail(void)
void ztest_test_skip(void)