Zephyr API Documentation  2.7.0-rc2
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 <ztest.h>
17#include <stdarg.h>
18#include <stdio.h>
19#include <string.h>
20#include <stdbool.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26const char *ztest_relative_filename(const char *file);
27void ztest_test_fail(void);
28#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
29
30static inline bool z_zassert_(bool cond, const char *file, int line)
31{
32 if (cond == false) {
33 PRINT("\n Assertion failed at %s:%d\n",
34 ztest_relative_filename(file), line);
36 return false;
37 }
38
39 return true;
40}
41
42#define z_zassert(cond, default_msg, file, line, func, msg, ...) \
43 z_zassert_(cond, file, line)
44
45#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
46
47static inline bool z_zassert(bool cond,
48 const char *default_msg,
49 const char *file,
50 int line, const char *func,
51 const char *msg, ...)
52{
53 if (cond == false) {
54 va_list vargs;
55
56 va_start(vargs, msg);
57 PRINT("\n Assertion failed at %s:%d: %s: %s\n",
58 ztest_relative_filename(file), line, func, default_msg);
59 vprintk(msg, vargs);
60 printk("\n");
61 va_end(vargs);
63 return false;
64 }
65#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
66 else {
67 PRINT("\n Assertion succeeded at %s:%d (%s)\n",
68 ztest_relative_filename(file), line, func);
69 }
70#endif
71 return true;
72}
73
74#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
75
76
100#define zassert(cond, default_msg, msg, ...) do { \
101 bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
102 __FILE__, __LINE__, __func__, \
103 msg ? msg : "", ##__VA_ARGS__); \
104 if (!_ret) { \
105 /* If kernel but without multithreading return. */ \
106 COND_CODE_1(KERNEL, \
107 (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
108 ()) \
109 } \
110} while (0)
111
116#define zassert_unreachable(msg, ...) zassert(0, "Reached unreachable code", \
117 msg, ##__VA_ARGS__)
118
124#define zassert_true(cond, msg, ...) zassert(cond, #cond " is false", \
125 msg, ##__VA_ARGS__)
126
132#define zassert_false(cond, msg, ...) zassert(!(cond), #cond " is true", \
133 msg, ##__VA_ARGS__)
134
140#define zassert_ok(cond, msg, ...) zassert(!(cond), #cond " is non-zero", \
141 msg, ##__VA_ARGS__)
142
148#define zassert_is_null(ptr, msg, ...) zassert((ptr) == NULL, \
149 #ptr " is not NULL", \
150 msg, ##__VA_ARGS__)
151
157#define zassert_not_null(ptr, msg, ...) zassert((ptr) != NULL, \
158 #ptr " is NULL", msg, \
159 ##__VA_ARGS__)
160
170#define zassert_equal(a, b, msg, ...) zassert((a) == (b), \
171 #a " not equal to " #b, \
172 msg, ##__VA_ARGS__)
173
183#define zassert_not_equal(a, b, msg, ...) zassert((a) != (b), \
184 #a " equal to " #b, \
185 msg, ##__VA_ARGS__)
186
196#define zassert_equal_ptr(a, b, msg, ...) \
197 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, \
198 msg, ##__VA_ARGS__)
199
208#define zassert_within(a, b, d, msg, ...) \
209 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), \
210 #a " not within " #b " +/- " #d, \
211 msg, ##__VA_ARGS__)
212
224#define zassert_mem_equal(...) \
225 zassert_mem_equal__(__VA_ARGS__)
226
238#define zassert_mem_equal__(buf, exp, size, msg, ...) \
239 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, \
240 msg, ##__VA_ARGS__)
241
246#ifdef __cplusplus
247}
248#endif
249
250#endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
static void vprintk(const char *fmt, va_list ap)
Definition: printk.h:71
static void printk(const char *fmt,...)
Print kernel debugging message.
Definition: printk.h:66
Zephyr Testsuite.
#define PRINT
Definition: ztest.h:56
const char * ztest_relative_filename(const char *file)
void ztest_test_fail(void)