13#ifndef ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
14#define ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
25#if defined(__cplusplus) && (__cplusplus >= 201103L)
27template <
typename T >
28static inline bool z_ztest_is_null(T *ptr)
30 return ptr ==
nullptr;
33template <
typename T >
34static inline auto z_ztest_is_null(
const T &ptr) ->
decltype(ptr ==
nullptr)
36 return ptr ==
nullptr;
38static inline bool z_ztest_is_null(
decltype(
nullptr))
42#define Z_ZTEST_IS_NULL(ptr) z_ztest_is_null(ptr)
44#define Z_ZTEST_IS_NULL(ptr) is_null_no_warn((void *)(ptr))
56#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
58static inline bool z_zassert_(
bool cond,
const char *file,
int line)
70#define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
72static inline bool z_zassume_(
bool cond,
const char *file,
int line)
84#define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
86static inline bool z_zexpect_(
bool cond,
const char *file,
int line)
98#define z_zexpect(cond, default_msg, file, line, func, msg, ...) z_zexpect_(cond, file, line)
102static inline __printf_like(6, 7)
bool z_zassert(
bool cond, const
char *default_msg,
103 const
char *file,
int line, const
char *func,
104 const
char *msg, ...)
109 va_start(vargs, msg);
110 PRINT_DATA(
"\n Assertion failed at %s:%d: %s: %s\n",
118#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
120 PRINT_DATA(
"\n Assertion succeeded at %s:%d (%s)\n",
127static inline __printf_like(6, 7)
bool z_zassume(
bool cond, const
char *default_msg,
128 const
char *file,
int line, const
char *func,
129 const
char *msg, ...)
134 va_start(vargs, msg);
135 PRINT_DATA(
"\n Assumption failed at %s:%d: %s: %s\n",
143#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
145 PRINT_DATA(
"\n Assumption succeeded at %s:%d (%s)\n",
152static inline __printf_like(6, 7)
bool z_zexpect(
bool cond, const
char *default_msg,
153 const
char *file,
int line, const
char *func,
154 const
char *msg, ...)
159 va_start(vargs, msg);
160 PRINT_DATA(
"\n Expectation failed at %s:%d: %s: %s\n",
168#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
170 PRINT_DATA(
"\n Expectation succeeded at %s:%d (%s)\n",
202#define _zassert_base(cond, default_msg, msg, ...) \
204 bool _msg = (msg != NULL); \
206 z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
207 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
211 COND_CODE_1(CONFIG_MULTITHREADING, (), (return;)) \
215#define _zassert_va(cond, default_msg, msg, ...) \
216 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
218#define zassert(cond, default_msg, ...) \
219 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
239#define _zassume_base(cond, default_msg, msg, ...) \
241 bool _msg = (msg != NULL); \
243 z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
244 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
248 COND_CODE_1(CONFIG_MULTITHREADING, (), (return;)) \
252#define _zassume_va(cond, default_msg, msg, ...) \
253 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
255#define zassume(cond, default_msg, ...) \
256 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
268#define _zexpect_base(cond, default_msg, msg, ...) \
270 bool _msg = (msg != NULL); \
272 z_zexpect(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
273 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
277 COND_CODE_1(CONFIG_MULTITHREADING, (), (return;)) \
281#define _zexpect_va(cond, default_msg, msg, ...) \
282 _zexpect_base(cond, default_msg, msg, ##__VA_ARGS__)
284#define zexpect(cond, default_msg, ...) \
285 _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
291#define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
298#define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
305#define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
312#define zassert_ok(cond, ...) zassert((cond) == 0, #cond " is non-zero", ##__VA_ARGS__)
319#define zassert_not_ok(cond, ...) zassert((cond) != 0, #cond " is zero", ##__VA_ARGS__)
326#define zassert_is_null(ptr, ...) \
327 zassert(Z_ZTEST_IS_NULL(ptr), #ptr " is not NULL", ##__VA_ARGS__)
334#define zassert_not_null(ptr, ...) \
335 zassert(!Z_ZTEST_IS_NULL(ptr), #ptr " is NULL", ##__VA_ARGS__)
346#define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
357#define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
368#define zassert_equal_ptr(a, b, ...) \
369 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
379#define zassert_within(a, b, d, ...) \
380 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
392#define zassert_between_inclusive(a, l, u, ...) \
393 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
407#define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
420#define zassert_mem_equal__(buf, exp, size, ...) \
421 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
430#define zassert_str_equal(s1, s2, ...) \
431 zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
454#define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
464#define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
474#define zassume_ok(cond, ...) zassume((cond) == 0, #cond " is non-zero", ##__VA_ARGS__)
484#define zassume_not_ok(cond, ...) zassume((cond) != 0, #cond " is zero", ##__VA_ARGS__)
494#define zassume_is_null(ptr, ...) \
495 zassume(Z_ZTEST_IS_NULL(ptr), #ptr " is not NULL", ##__VA_ARGS__)
505#define zassume_not_null(ptr, ...) \
506 zassume(!Z_ZTEST_IS_NULL(ptr), #ptr " is NULL", ##__VA_ARGS__)
518#define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
530#define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
542#define zassume_equal_ptr(a, b, ...) \
543 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
555#define zassume_within(a, b, d, ...) \
556 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
570#define zassume_between_inclusive(a, l, u, ...) \
571 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
585#define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
600#define zassume_mem_equal__(buf, exp, size, ...) \
601 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
610#define zassume_str_equal(s1, s2, ...) \
611 zassume(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
632#define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
640#define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
649#define zexpect_ok(cond, ...) zexpect((cond) == 0, #cond " is non-zero", ##__VA_ARGS__)
658#define zexpect_not_ok(cond, ...) zexpect((cond) != 0, #cond " is zero", ##__VA_ARGS__)
666#define zexpect_is_null(ptr, ...) \
667 zexpect(Z_ZTEST_IS_NULL(ptr), #ptr " is not NULL", ##__VA_ARGS__)
675#define zexpect_not_null(ptr, ...) \
676 zexpect(!Z_ZTEST_IS_NULL(ptr), #ptr " is NULL", ##__VA_ARGS__)
685#define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
697#define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
708#define zexpect_equal_ptr(a, b, ...) \
709 zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
720#define zexpect_within(a, b, delta, ...) \
721 zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
722 #a " not within " #b " +/- " #delta, ##__VA_ARGS__)
733#define zexpect_between_inclusive(a, lower, upper, ...) \
734 zexpect(((a) >= (lower)) && ((a) <= (upper)), \
735 #a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
746#define zexpect_mem_equal(buf, exp, size, ...) \
747 zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
757#define zexpect_str_equal(s1, s2, ...) \
758 zexpect(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
void printk(const char *fmt,...)
Print kernel debugging message.
void vprintk(const char *fmt, va_list ap)
Print kernel debugging message, va_list version.
#define bool
Definition stdbool.h:13
#define PRINT_DATA(fmt,...)
Definition tc_util.h:25
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)