1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 *
10 * @brief Zephyr testing framework assertion macros
11 */
12
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
z_zassert_(bool cond,const char * file,int line)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
z_zassume_(bool cond,const char * file,int line)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
z_zexpect_(bool cond,const char * file,int line)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
z_zassert(bool cond,const char * default_msg,const char * file,int line,const char * func,const char * msg,...)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
z_zassume(bool cond,const char * default_msg,const char * file,int line,const char * func,const char * msg,...)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
z_zexpect(bool cond,const char * default_msg,const char * file,int line,const char * func,const char * msg,...)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
149 /**
150 * @defgroup ztest_assert Ztest assertion macros
151 * @ingroup ztest
152 *
153 * This module provides assertions when using Ztest.
154 *
155 * @{
156 */
157
158 /**
159 * @brief Fail the test, if @a cond is false
160 *
161 * You probably don't need to call this macro directly. You should
162 * instead use zassert_{condition} macros below.
163 *
164 * Note that when CONFIG_MULTITHREADING=n macro returns from the function. It is
165 * then expected that in that case ztest asserts will be used only in the
166 * context of the test function.
167 *
168 * @param cond Condition to check
169 * @param default_msg Message to print if @a cond is false
170 * @param msg Optional, can be NULL. Message to print if @a cond is false.
171 */
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
191 /**
192 * @brief Skip the test, if @a cond is false
193 *
194 * You probably don't need to call this macro directly. You should
195 * instead use zassume_{condition} macros below.
196 *
197 * Note that when CONFIG_MULTITHREADING=n macro returns from the function. It's then expected that
198 * in that case ztest assumes will be used only in the context of the test function.
199 *
200 * NOTE: zassume should not be used to replace zassert, the goal of zassume is to skip tests that
201 * would otherwise fail due to a zassert on some other dependent behavior that is *not* under test,
202 * thus reducing what could be tens to hundreds of assertion failures to investigate down to a few
203 * failures only.
204 *
205 * @param cond Condition to check
206 * @param default_msg Message to print if @a cond is false
207 * @param msg Optional, can be NULL. Message to print if @a cond is false.
208 */
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
228 /**
229 * @brief If @a cond is false, fail the test but continue its execution.
230 *
231 * You probably don't need to call this macro directly. You should
232 * instead use zexpect_{condition} macros below.
233 *
234 * @param cond Condition to check
235 * @param default_msg Message to print if @a cond is false
236 * @param msg Optional, can be NULL. Message to print if @a cond is false.
237 */
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
258 /**
259 * @brief Assert that this function call won't be reached
260 * @param ... Optional message and variables to print if the assertion fails
261 */
262 #define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
263
264 /**
265 * @brief Assert that @a cond is true
266 * @param cond Condition to check
267 * @param ... Optional message and variables to print if the assertion fails
268 */
269 #define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
270
271 /**
272 * @brief Assert that @a cond is false
273 * @param cond Condition to check
274 * @param ... Optional message and variables to print if the assertion fails
275 */
276 #define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
277
278 /**
279 * @brief Assert that @a cond is 0 (success)
280 * @param cond Condition to check
281 * @param ... Optional message and variables to print if the assertion fails
282 */
283 #define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
284
285 /**
286 * @brief Assert that @a cond is not 0 (failure)
287 * @param cond Condition to check
288 * @param ... Optional message and variables to print if the assertion fails
289 */
290 #define zassert_not_ok(cond, ...) zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)
291
292 /**
293 * @brief Assert that @a ptr is NULL
294 * @param ptr Pointer to compare
295 * @param ... Optional message and variables to print if the assertion fails
296 */
297 #define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
298
299 /**
300 * @brief Assert that @a ptr is not NULL
301 * @param ptr Pointer to compare
302 * @param ... Optional message and variables to print if the assertion fails
303 */
304 #define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
305
306 /**
307 * @brief Assert that @a a equals @a b
308 *
309 * @a a and @a b won't be converted and will be compared directly.
310 *
311 * @param a Value to compare
312 * @param b Value to compare
313 * @param ... Optional message and variables to print if the assertion fails
314 */
315 #define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
316
317 /**
318 * @brief Assert that @a a does not equal @a b
319 *
320 * @a a and @a b won't be converted and will be compared directly.
321 *
322 * @param a Value to compare
323 * @param b Value to compare
324 * @param ... Optional message and variables to print if the assertion fails
325 */
326 #define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
327
328 /**
329 * @brief Assert that @a a equals @a b
330 *
331 * @a a and @a b will be converted to `void *` before comparing.
332 *
333 * @param a Value to compare
334 * @param b Value to compare
335 * @param ... Optional message and variables to print if the assertion fails
336 */
337 #define zassert_equal_ptr(a, b, ...) \
338 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
339
340 /**
341 * @brief Assert that @a a is within @a b with delta @a d
342 *
343 * @param a Value to compare
344 * @param b Value to compare
345 * @param d Delta
346 * @param ... Optional message and variables to print if the assertion fails
347 */
348 #define zassert_within(a, b, d, ...) \
349 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
350 ##__VA_ARGS__)
351
352 /**
353 * @brief Assert that @a a is greater than or equal to @a l and less
354 * than or equal to @a u
355 *
356 * @param a Value to compare
357 * @param l Lower limit
358 * @param u Upper limit
359 * @param ... Optional message and variables to print if the assertion fails
360 */
361 #define zassert_between_inclusive(a, l, u, ...) \
362 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
363 ##__VA_ARGS__)
364
365 /**
366 * @brief Assert that 2 memory buffers have the same contents
367 *
368 * This macro calls the final memory comparison assertion macro.
369 * Using double expansion allows providing some arguments by macros that
370 * would expand to more than one values (ANSI-C99 defines that all the macro
371 * arguments have to be expanded before macro call).
372 *
373 * @param ... Arguments, see @ref zassert_mem_equal__
374 * for real arguments accepted.
375 */
376 #define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
377
378 /**
379 * @brief Internal assert that 2 memory buffers have the same contents
380 *
381 * @note This is internal macro, to be used as a second expansion.
382 * See @ref zassert_mem_equal.
383 *
384 * @param buf Buffer to compare
385 * @param exp Buffer with expected contents
386 * @param size Size of buffers
387 * @param ... Optional message and variables to print if the assertion fails
388 */
389 #define zassert_mem_equal__(buf, exp, size, ...) \
390 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
391
392 /**
393 * @}
394 */
395
396 /**
397 * @defgroup ztest_assume Ztest assumption macros
398 * @ingroup ztest
399 *
400 * This module provides assumptions when using Ztest.
401 *
402 * @{
403 */
404
405 /**
406 * @brief Assume that @a cond is true
407 *
408 * If the assumption fails, the test will be marked as "skipped".
409 *
410 * @param cond Condition to check
411 * @param ... Optional message and variables to print if the assumption fails
412 */
413 #define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
414
415 /**
416 * @brief Assume that @a cond is false
417 *
418 * If the assumption fails, the test will be marked as "skipped".
419 *
420 * @param cond Condition to check
421 * @param ... Optional message and variables to print if the assumption fails
422 */
423 #define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
424
425 /**
426 * @brief Assume that @a cond is 0 (success)
427 *
428 * If the assumption fails, the test will be marked as "skipped".
429 *
430 * @param cond Condition to check
431 * @param ... Optional message and variables to print if the assumption fails
432 */
433 #define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
434
435 /**
436 * @brief Assume that @a cond is not 0 (failure)
437 *
438 * If the assumption fails, the test will be marked as "skipped".
439 *
440 * @param cond Condition to check
441 * @param ... Optional message and variables to print if the assumption fails
442 */
443 #define zassume_not_ok(cond, ...) zassume(!!(cond), #cond " is zero", ##__VA_ARGS__)
444
445 /**
446 * @brief Assume that @a ptr is NULL
447 *
448 * If the assumption fails, the test will be marked as "skipped".
449 *
450 * @param ptr Pointer to compare
451 * @param ... Optional message and variables to print if the assumption fails
452 */
453 #define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
454
455 /**
456 * @brief Assume that @a ptr is not NULL
457 *
458 * If the assumption fails, the test will be marked as "skipped".
459 *
460 * @param ptr Pointer to compare
461 * @param ... Optional message and variables to print if the assumption fails
462 */
463 #define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
464
465 /**
466 * @brief Assume that @a a equals @a b
467 *
468 * @a a and @a b won't be converted and will be compared directly. If the
469 * assumption fails, the test will be marked as "skipped".
470 *
471 * @param a Value to compare
472 * @param b Value to compare
473 * @param ... Optional message and variables to print if the assumption fails
474 */
475 #define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
476
477 /**
478 * @brief Assume that @a a does not equal @a b
479 *
480 * @a a and @a b won't be converted and will be compared directly. If the
481 * assumption fails, the test will be marked as "skipped".
482 *
483 * @param a Value to compare
484 * @param b Value to compare
485 * @param ... Optional message and variables to print if the assumption fails
486 */
487 #define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
488
489 /**
490 * @brief Assume that @a a equals @a b
491 *
492 * @a a and @a b will be converted to `void *` before comparing. If the
493 * assumption fails, the test will be marked as "skipped".
494 *
495 * @param a Value to compare
496 * @param b Value to compare
497 * @param ... Optional message and variables to print if the assumption fails
498 */
499 #define zassume_equal_ptr(a, b, ...) \
500 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
501
502 /**
503 * @brief Assume that @a a is within @a b with delta @a d
504 *
505 * If the assumption fails, the test will be marked as "skipped".
506 *
507 * @param a Value to compare
508 * @param b Value to compare
509 * @param d Delta
510 * @param ... Optional message and variables to print if the assumption fails
511 */
512 #define zassume_within(a, b, d, ...) \
513 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
514 ##__VA_ARGS__)
515
516 /**
517 * @brief Assume that @a a is greater than or equal to @a l and less
518 * than or equal to @a u
519 *
520 * If the assumption fails, the test will be marked as "skipped".
521 *
522 * @param a Value to compare
523 * @param l Lower limit
524 * @param u Upper limit
525 * @param ... Optional message and variables to print if the assumption fails
526 */
527 #define zassume_between_inclusive(a, l, u, ...) \
528 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
529 ##__VA_ARGS__)
530
531 /**
532 * @brief Assume that 2 memory buffers have the same contents
533 *
534 * This macro calls the final memory comparison assumption macro.
535 * Using double expansion allows providing some arguments by macros that
536 * would expand to more than one values (ANSI-C99 defines that all the macro
537 * arguments have to be expanded before macro call).
538 *
539 * @param ... Arguments, see @ref zassume_mem_equal__
540 * for real arguments accepted.
541 */
542 #define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
543
544 /**
545 * @brief Internal assume that 2 memory buffers have the same contents
546 *
547 * If the assumption fails, the test will be marked as "skipped".
548 *
549 * @note This is internal macro, to be used as a second expansion.
550 * See @ref zassume_mem_equal.
551 *
552 * @param buf Buffer to compare
553 * @param exp Buffer with expected contents
554 * @param size Size of buffers
555 * @param ... Optional message and variables to print if the assumption fails
556 */
557 #define zassume_mem_equal__(buf, exp, size, ...) \
558 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
559
560 /**
561 * @}
562 */
563
564 /**
565 * @defgroup ztest_expect Ztest expectation macros
566 * @ingroup ztest
567 *
568 * This module provides expectations when using Ztest.
569 *
570 * @{
571 */
572
573 /**
574 * @brief Expect that @a cond is true, otherwise mark test as failed but continue its execution.
575 *
576 * @param cond Condition to check
577 * @param ... Optional message and variables to print if the expectation fails
578 */
579 #define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
580
581 /**
582 * @brief Expect that @a cond is false, otherwise mark test as failed but continue its execution.
583 *
584 * @param cond Condition to check
585 * @param ... Optional message and variables to print if the expectation fails
586 */
587 #define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
588
589 /**
590 * @brief Expect that @a cond is 0 (success), otherwise mark test as failed but continue its
591 * execution.
592 *
593 * @param cond Condition to check
594 * @param ... Optional message and variables to print if the expectation fails
595 */
596 #define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__)
597
598 /**
599 * @brief Expect that @a cond is not 0 (failure), otherwise mark test as failed but continue its
600 * execution.
601 *
602 * @param cond Condition to check
603 * @param ... Optional message and variables to print if the expectation fails
604 */
605 #define zexpect_not_ok(cond, ...) zexpect(!!(cond), #cond " is zero", ##__VA_ARGS__)
606
607 /**
608 * @brief Expect that @a ptr is NULL, otherwise mark test as failed but continue its execution.
609 *
610 * @param ptr Pointer to compare
611 * @param ... Optional message and variables to print if the expectation fails
612 */
613 #define zexpect_is_null(ptr, ...) zexpect((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
614
615 /**
616 * @brief Expect that @a ptr is not NULL, otherwise mark test as failed but continue its execution.
617 *
618 * @param ptr Pointer to compare
619 * @param ... Optional message and variables to print if the expectation fails
620 */
621 #define zexpect_not_null(ptr, ...) zexpect((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
622
623 /**
624 * @brief Expect that @a a equals @a b, otherwise mark test as failed but continue its execution.
625 * expectation fails, the test will be marked as "skipped".
626 *
627 * @param a Value to compare
628 * @param b Value to compare
629 * @param ... Optional message and variables to print if the expectation fails
630 */
631 #define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
632
633 /**
634 * @brief Expect that @a a does not equal @a b, otherwise mark test as failed but continue its
635 * execution.
636 *
637 * @a a and @a b won't be converted and will be compared directly.
638 *
639 * @param a Value to compare
640 * @param b Value to compare
641 * @param ... Optional message and variables to print if the expectation fails
642 */
643 #define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
644
645 /**
646 * @brief Expect that @a a equals @a b, otherwise mark test as failed but continue its execution.
647 *
648 * @a a and @a b will be converted to `void *` before comparing.
649 *
650 * @param a Value to compare
651 * @param b Value to compare
652 * @param ... Optional message and variables to print if the expectation fails
653 */
654 #define zexpect_equal_ptr(a, b, ...) \
655 zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
656
657 /**
658 * @brief Expect that @a a is within @a b with delta @a d, otherwise mark test as failed but
659 * continue its execution.
660 *
661 * @param a Value to compare
662 * @param b Value to compare
663 * @param delta Difference between a and b
664 * @param ... Optional message and variables to print if the expectation fails
665 */
666 #define zexpect_within(a, b, delta, ...) \
667 zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
668 #a " not within " #b " +/- " #delta, ##__VA_ARGS__)
669
670 /**
671 * @brief Expect that @a a is greater than or equal to @a l and less
672 * than or equal to @a u, otherwise mark test as failed but continue its execution.
673 *
674 * @param a Value to compare
675 * @param lower Lower limit
676 * @param upper Upper limit
677 * @param ... Optional message and variables to print if the expectation fails
678 */
679 #define zexpect_between_inclusive(a, lower, upper, ...) \
680 zexpect(((a) >= (lower)) && ((a) <= (upper)), \
681 #a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
682
683 /**
684 * @brief Expect that 2 memory buffers have the same contents, otherwise mark test as failed but
685 * continue its execution.
686 *
687 * @param buf Buffer to compare
688 * @param exp Buffer with expected contents
689 * @param size Size of buffers
690 * @param ... Optional message and variables to print if the expectation fails
691 */
692 #define zexpect_mem_equal(buf, exp, size, ...) \
693 zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
694
695 /**
696 * @}
697 */
698
699 #ifdef __cplusplus
700 }
701 #endif
702
703 #endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
704