Zephyr Project API
3.5.0
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/ztest.h
>
22
23
#ifdef __cplusplus
24
extern
"C"
{
25
#endif
26
27
const
char
*
ztest_relative_filename
(
const
char
*file);
28
void
ztest_test_fail
(
void
);
29
void
ztest_test_skip
(
void
);
30
void
ztest_test_expect_fail
(
void
);
31
void
ztest_skip_failed_assumption
(
void
);
32
#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
33
34
static
inline
bool
z_zassert_(
bool
cond,
const
char
*file,
int
line)
35
{
36
if
(cond ==
false
) {
37
PRINT
(
"\n Assertion failed at %s:%d\n"
,
ztest_relative_filename
(file), line);
38
ztest_test_fail
();
39
return
false
;
40
}
41
42
return
true
;
43
}
44
45
#define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
46
47
static
inline
bool
z_zassume_(
bool
cond,
const
char
*file,
int
line)
48
{
49
if
(cond ==
false
) {
50
PRINT
(
"\n Assumption failed at %s:%d\n"
,
ztest_relative_filename
(file), line);
51
ztest_skip_failed_assumption
();
52
return
false
;
53
}
54
55
return
true
;
56
}
57
58
#define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
59
60
static
inline
bool
z_zexpect_(
bool
cond,
const
char
*file,
int
line)
61
{
62
if
(cond ==
false
) {
63
PRINT
(
"\n Expectation failed at %s:%d\n"
,
ztest_relative_filename
(file), line);
64
ztest_test_expect_fail
();
65
return
false
;
66
}
67
68
return
true
;
69
}
70
71
#define z_zexpect(cond, default_msg, file, line, func, msg, ...) z_zexpect_(cond, file, line)
72
73
#else
/* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
74
75
static
inline
bool
z_zassert(
bool
cond,
const
char
*default_msg,
const
char
*file,
int
line,
76
const
char
*
func
,
const
char
*
msg
, ...)
77
{
78
if
(cond ==
false
) {
79
va_list vargs;
80
81
va_start(vargs,
msg
);
82
PRINT
(
"\n Assertion failed at %s:%d: %s: %s\n"
,
ztest_relative_filename
(file),
83
line,
func
, default_msg);
84
vprintk
(
msg
, vargs);
85
printk
(
"\n"
);
86
va_end(vargs);
87
ztest_test_fail
();
88
return
false
;
89
}
90
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
91
else
{
92
PRINT
(
"\n Assertion succeeded at %s:%d (%s)\n"
,
ztest_relative_filename
(file),
93
line,
func
);
94
}
95
#endif
96
return
true
;
97
}
98
99
static
inline
bool
z_zassume(
bool
cond,
const
char
*default_msg,
const
char
*file,
int
line,
100
const
char
*
func
,
const
char
*
msg
, ...)
101
{
102
if
(cond ==
false
) {
103
va_list vargs;
104
105
va_start(vargs,
msg
);
106
PRINT
(
"\n Assumption failed at %s:%d: %s: %s\n"
,
ztest_relative_filename
(file),
107
line,
func
, default_msg);
108
vprintk
(
msg
, vargs);
109
printk
(
"\n"
);
110
va_end(vargs);
111
ztest_skip_failed_assumption
();
112
return
false
;
113
}
114
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
115
else
{
116
PRINT
(
"\n Assumption succeeded at %s:%d (%s)\n"
,
ztest_relative_filename
(file),
117
line,
func
);
118
}
119
#endif
120
return
true
;
121
}
122
123
static
inline
bool
z_zexpect(
bool
cond,
const
char
*default_msg,
const
char
*file,
int
line,
124
const
char
*
func
,
const
char
*
msg
, ...)
125
{
126
if
(cond ==
false
) {
127
va_list vargs;
128
129
va_start(vargs,
msg
);
130
PRINT
(
"\n Expectation failed at %s:%d: %s: %s\n"
,
ztest_relative_filename
(file),
131
line,
func
, default_msg);
132
vprintk
(
msg
, vargs);
133
printk
(
"\n"
);
134
va_end(vargs);
135
ztest_test_expect_fail
();
136
return
false
;
137
}
138
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
139
else
{
140
PRINT
(
"\n Expectation succeeded at %s:%d (%s)\n"
,
ztest_relative_filename
(file),
141
line,
func
);
142
}
143
#endif
144
return
true
;
145
}
146
147
#endif
/* CONFIG_ZTEST_ASSERT_VERBOSE */
148
172
#define _zassert_base(cond, default_msg, msg, ...) \
173
do { \
174
bool _msg = (msg != NULL); \
175
bool _ret = z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__,\
176
__LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
177
(void)_msg; \
178
if (!_ret) { \
179
/* If kernel but without multithreading return. */
\
180
COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
181
()) \
182
} \
183
} while (0)
184
185
#define _zassert_va(cond, default_msg, msg, ...) \
186
_zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
187
188
#define zassert(cond, default_msg, ...) \
189
_zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
190
209
#define _zassume_base(cond, default_msg, msg, ...) \
210
do { \
211
bool _msg = (msg != NULL); \
212
bool _ret = z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__,\
213
__LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
214
(void)_msg; \
215
if (!_ret) { \
216
/* If kernel but without multithreading return. */
\
217
COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
218
()) \
219
} \
220
} while (0)
221
222
#define _zassume_va(cond, default_msg, msg, ...) \
223
_zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
224
225
#define zassume(cond, default_msg, ...) \
226
_zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
227
238
#define _zexpect_base(cond, default_msg, msg, ...) \
239
do { \
240
bool _msg = (msg != NULL); \
241
bool _ret = \
242
z_zexpect(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
243
__LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
244
(void)_msg; \
245
if (!_ret) { \
246
/* If kernel but without multithreading return. */
\
247
COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
248
()) \
249
} \
250
} while (0)
251
252
#define _zexpect_va(cond, default_msg, msg, ...) \
253
_zexpect_base(cond, default_msg, msg, ##__VA_ARGS__)
254
255
#define zexpect(cond, default_msg, ...) \
256
_zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
257
262
#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
263
269
#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
270
276
#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
277
283
#define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
284
290
#define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
291
297
#define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
298
308
#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
309
319
#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
320
330
#define zassert_equal_ptr(a, b, ...) \
331
zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
332
341
#define zassert_within(a, b, d, ...) \
342
zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
343
##__VA_ARGS__)
344
354
#define zassert_between_inclusive(a, l, u, ...) \
355
zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
356
##__VA_ARGS__)
357
369
#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
370
382
#define zassert_mem_equal__(buf, exp, size, ...) \
383
zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
384
406
#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
407
416
#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
417
426
#define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
427
436
#define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
437
446
#define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
447
458
#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
459
470
#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
471
482
#define zassume_equal_ptr(a, b, ...) \
483
zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
484
495
#define zassume_within(a, b, d, ...) \
496
zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
497
##__VA_ARGS__)
498
510
#define zassume_between_inclusive(a, l, u, ...) \
511
zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
512
##__VA_ARGS__)
513
525
#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
526
540
#define zassume_mem_equal__(buf, exp, size, ...) \
541
zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
542
562
#define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
563
570
#define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
571
579
#define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__)
580
587
#define zexpect_is_null(ptr, ...) zexpect((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
588
595
#define zexpect_not_null(ptr, ...) zexpect((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
596
605
#define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
606
617
#define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
618
628
#define zexpect_equal_ptr(a, b, ...) \
629
zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
630
640
#define zexpect_within(a, b, delta, ...) \
641
zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
642
#a " not within " #b " +/- " #delta, ##__VA_ARGS__)
643
653
#define zexpect_between_inclusive(a, lower, upper, ...) \
654
zexpect(((a) >= (lower)) && ((a) <= (upper)), \
655
#a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
656
666
#define zexpect_mem_equal(buf, exp, size, ...) \
667
zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
668
673
#ifdef __cplusplus
674
}
675
#endif
676
677
#endif
/* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
vprintk
static void vprintk(const char *fmt, va_list ap)
Definition
printk.h:56
printk
static void printk(const char *fmt,...)
Print kernel debugging message.
Definition
printk.h:51
stdbool.h
stdio.h
string.h
func
static void func(void *arg1, void *arg2, void *arg3)
Definition
main.c:22
msg
static void msg(uint64_t c64)
Definition
main.c:17
ztest.h
Zephyr Testsuite.
PRINT
#define PRINT
Definition
ztest.h:39
ztest_relative_filename
const char * ztest_relative_filename(const char *file)
ztest_skip_failed_assumption
void ztest_skip_failed_assumption(void)
ztest_test_expect_fail
void ztest_test_expect_fail(void)
ztest_test_fail
void ztest_test_fail(void)
ztest_test_skip
void ztest_test_skip(void)
subsys
testsuite
ztest
include
zephyr
ztest_assert.h
Generated on Tue Mar 5 2024 08:42:07 for Zephyr Project API by
1.9.8