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/tc_util.h>
22 #include <zephyr/ztest.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 const char *ztest_relative_filename(const char *file);
29 void ztest_test_fail(void);
30 void ztest_test_skip(void);
31 void ztest_test_expect_fail(void);
32 void ztest_skip_failed_assumption(void);
33 #if CONFIG_ZTEST_ASSERT_VERBOSE == 0
34
z_zassert_(bool cond,const char * file,int line)35 static inline bool z_zassert_(bool cond, const char *file, int line)
36 {
37 if (cond == false) {
38 PRINT_DATA("\n Assertion failed at %s:%d\n", ztest_relative_filename(file),
39 line);
40 ztest_test_fail();
41 return false;
42 }
43
44 return true;
45 }
46
47 #define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
48
z_zassume_(bool cond,const char * file,int line)49 static inline bool z_zassume_(bool cond, const char *file, int line)
50 {
51 if (cond == false) {
52 PRINT_DATA("\n Assumption failed at %s:%d\n", ztest_relative_filename(file),
53 line);
54 ztest_skip_failed_assumption();
55 return false;
56 }
57
58 return true;
59 }
60
61 #define z_zassume(cond, default_msg, file, line, func, msg, ...) z_zassume_(cond, file, line)
62
z_zexpect_(bool cond,const char * file,int line)63 static inline bool z_zexpect_(bool cond, const char *file, int line)
64 {
65 if (cond == false) {
66 PRINT_DATA("\n Expectation failed at %s:%d\n", ztest_relative_filename(file),
67 line);
68 ztest_test_expect_fail();
69 return false;
70 }
71
72 return true;
73 }
74
75 #define z_zexpect(cond, default_msg, file, line, func, msg, ...) z_zexpect_(cond, file, line)
76
77 #else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
78
z_zassert(bool cond,const char * default_msg,const char * file,int line,const char * func,const char * msg,...)79 static inline bool z_zassert(bool cond, const char *default_msg, const char *file, int line,
80 const char *func, const char *msg, ...)
81 {
82 if (cond == false) {
83 va_list vargs;
84
85 va_start(vargs, msg);
86 PRINT_DATA("\n Assertion failed at %s:%d: %s: %s\n",
87 ztest_relative_filename(file), line, func, default_msg);
88 vprintk(msg, vargs);
89 printk("\n");
90 va_end(vargs);
91 ztest_test_fail();
92 return false;
93 }
94 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
95 else {
96 PRINT_DATA("\n Assertion succeeded at %s:%d (%s)\n",
97 ztest_relative_filename(file), line, func);
98 }
99 #endif
100 return true;
101 }
102
z_zassume(bool cond,const char * default_msg,const char * file,int line,const char * func,const char * msg,...)103 static inline bool z_zassume(bool cond, const char *default_msg, const char *file, int line,
104 const char *func, const char *msg, ...)
105 {
106 if (cond == false) {
107 va_list vargs;
108
109 va_start(vargs, msg);
110 PRINT_DATA("\n Assumption failed at %s:%d: %s: %s\n",
111 ztest_relative_filename(file), line, func, default_msg);
112 vprintk(msg, vargs);
113 printk("\n");
114 va_end(vargs);
115 ztest_skip_failed_assumption();
116 return false;
117 }
118 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
119 else {
120 PRINT_DATA("\n Assumption succeeded at %s:%d (%s)\n",
121 ztest_relative_filename(file), line, func);
122 }
123 #endif
124 return true;
125 }
126
z_zexpect(bool cond,const char * default_msg,const char * file,int line,const char * func,const char * msg,...)127 static inline bool z_zexpect(bool cond, const char *default_msg, const char *file, int line,
128 const char *func, const char *msg, ...)
129 {
130 if (cond == false) {
131 va_list vargs;
132
133 va_start(vargs, msg);
134 PRINT_DATA("\n Expectation failed at %s:%d: %s: %s\n",
135 ztest_relative_filename(file), line, func, default_msg);
136 vprintk(msg, vargs);
137 printk("\n");
138 va_end(vargs);
139 ztest_test_expect_fail();
140 return false;
141 }
142 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
143 else {
144 PRINT_DATA("\n Expectation succeeded at %s:%d (%s)\n",
145 ztest_relative_filename(file), line, func);
146 }
147 #endif
148 return true;
149 }
150
151 #endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
152
153 /**
154 * @defgroup ztest_assert Ztest assertion macros
155 * @ingroup ztest
156 *
157 * This module provides assertions when using Ztest.
158 *
159 * @{
160 */
161
162 /**
163 * @brief Fail the test, if @a cond is false
164 *
165 * You probably don't need to call this macro directly. You should
166 * instead use zassert_{condition} macros below.
167 *
168 * Note that when CONFIG_MULTITHREADING=n macro returns from the function. It is
169 * then expected that in that case ztest asserts will be used only in the
170 * context of the test function.
171 *
172 * @param cond Condition to check
173 * @param default_msg Message to print if @a cond is false
174 * @param msg Optional, can be NULL. Message to print if @a cond is false.
175 */
176 #define _zassert_base(cond, default_msg, msg, ...) \
177 do { \
178 bool _msg = (msg != NULL); \
179 bool _ret = \
180 z_zassert(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
181 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
182 (void)_msg; \
183 if (!_ret) { \
184 /* If kernel but without multithreading return. */ \
185 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
186 ()) \
187 } \
188 } while (0)
189
190 #define _zassert_va(cond, default_msg, msg, ...) \
191 _zassert_base(cond, default_msg, msg, ##__VA_ARGS__)
192
193 #define zassert(cond, default_msg, ...) \
194 _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
195
196 /**
197 * @brief Skip the test, if @a cond is false
198 *
199 * You probably don't need to call this macro directly. You should
200 * instead use zassume_{condition} macros below.
201 *
202 * Note that when CONFIG_MULTITHREADING=n macro returns from the function. It's then expected that
203 * in that case ztest assumes will be used only in the context of the test function.
204 *
205 * NOTE: zassume should not be used to replace zassert, the goal of zassume is to skip tests that
206 * would otherwise fail due to a zassert on some other dependent behavior that is *not* under test,
207 * thus reducing what could be tens to hundreds of assertion failures to investigate down to a few
208 * failures only.
209 *
210 * @param cond Condition to check
211 * @param default_msg Message to print if @a cond is false
212 * @param msg Optional, can be NULL. Message to print if @a cond is false.
213 */
214 #define _zassume_base(cond, default_msg, msg, ...) \
215 do { \
216 bool _msg = (msg != NULL); \
217 bool _ret = \
218 z_zassume(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
219 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
220 (void)_msg; \
221 if (!_ret) { \
222 /* If kernel but without multithreading return. */ \
223 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
224 ()) \
225 } \
226 } while (0)
227
228 #define _zassume_va(cond, default_msg, msg, ...) \
229 _zassume_base(cond, default_msg, msg, ##__VA_ARGS__)
230
231 #define zassume(cond, default_msg, ...) \
232 _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
233
234 /**
235 * @brief If @a cond is false, fail the test but continue its execution.
236 *
237 * You probably don't need to call this macro directly. You should
238 * instead use zexpect_{condition} macros below.
239 *
240 * @param cond Condition to check
241 * @param default_msg Message to print if @a cond is false
242 * @param msg Optional, can be NULL. Message to print if @a cond is false.
243 */
244 #define _zexpect_base(cond, default_msg, msg, ...) \
245 do { \
246 bool _msg = (msg != NULL); \
247 bool _ret = \
248 z_zexpect(cond, _msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
249 __LINE__, __func__, _msg ? msg : "", ##__VA_ARGS__); \
250 (void)_msg; \
251 if (!_ret) { \
252 /* If kernel but without multithreading return. */ \
253 COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
254 ()) \
255 } \
256 } while (0)
257
258 #define _zexpect_va(cond, default_msg, msg, ...) \
259 _zexpect_base(cond, default_msg, msg, ##__VA_ARGS__)
260
261 #define zexpect(cond, default_msg, ...) \
262 _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
263
264 /**
265 * @brief Assert that this function call won't be reached
266 * @param ... Optional message and variables to print if the assertion fails
267 */
268 #define zassert_unreachable(...) zassert(0, "Reached unreachable code", ##__VA_ARGS__)
269
270 /**
271 * @brief Assert that @a cond is true
272 * @param cond Condition to check
273 * @param ... Optional message and variables to print if the assertion fails
274 */
275 #define zassert_true(cond, ...) zassert(cond, #cond " is false", ##__VA_ARGS__)
276
277 /**
278 * @brief Assert that @a cond is false
279 * @param cond Condition to check
280 * @param ... Optional message and variables to print if the assertion fails
281 */
282 #define zassert_false(cond, ...) zassert(!(cond), #cond " is true", ##__VA_ARGS__)
283
284 /**
285 * @brief Assert that @a cond is 0 (success)
286 * @param cond Condition to check
287 * @param ... Optional message and variables to print if the assertion fails
288 */
289 #define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
290
291 /**
292 * @brief Assert that @a cond is not 0 (failure)
293 * @param cond Condition to check
294 * @param ... Optional message and variables to print if the assertion fails
295 */
296 #define zassert_not_ok(cond, ...) zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)
297
298 /**
299 * @brief Assert that @a ptr is NULL
300 * @param ptr Pointer to compare
301 * @param ... Optional message and variables to print if the assertion fails
302 */
303 #define zassert_is_null(ptr, ...) zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
304
305 /**
306 * @brief Assert that @a ptr is not NULL
307 * @param ptr Pointer to compare
308 * @param ... Optional message and variables to print if the assertion fails
309 */
310 #define zassert_not_null(ptr, ...) zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
311
312 /**
313 * @brief Assert that @a a equals @a b
314 *
315 * @a a and @a b won't be converted and will be compared directly.
316 *
317 * @param a Value to compare
318 * @param b Value to compare
319 * @param ... Optional message and variables to print if the assertion fails
320 */
321 #define zassert_equal(a, b, ...) zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
322
323 /**
324 * @brief Assert that @a a does not equal @a b
325 *
326 * @a a and @a b won't be converted and will be compared directly.
327 *
328 * @param a Value to compare
329 * @param b Value to compare
330 * @param ... Optional message and variables to print if the assertion fails
331 */
332 #define zassert_not_equal(a, b, ...) zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
333
334 /**
335 * @brief Assert that @a a equals @a b
336 *
337 * @a a and @a b will be converted to `void *` before comparing.
338 *
339 * @param a Value to compare
340 * @param b Value to compare
341 * @param ... Optional message and variables to print if the assertion fails
342 */
343 #define zassert_equal_ptr(a, b, ...) \
344 zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
345
346 /**
347 * @brief Assert that @a a is within @a b with delta @a d
348 *
349 * @param a Value to compare
350 * @param b Value to compare
351 * @param d Delta
352 * @param ... Optional message and variables to print if the assertion fails
353 */
354 #define zassert_within(a, b, d, ...) \
355 zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
356 ##__VA_ARGS__)
357
358 /**
359 * @brief Assert that @a a is greater than or equal to @a l and less
360 * than or equal to @a u
361 *
362 * @param a Value to compare
363 * @param l Lower limit
364 * @param u Upper limit
365 * @param ... Optional message and variables to print if the assertion fails
366 */
367 #define zassert_between_inclusive(a, l, u, ...) \
368 zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
369 ##__VA_ARGS__)
370
371 /**
372 * @brief Assert that 2 memory buffers have the same contents
373 *
374 * This macro calls the final memory comparison assertion macro.
375 * Using double expansion allows providing some arguments by macros that
376 * would expand to more than one values (ANSI-C99 defines that all the macro
377 * arguments have to be expanded before macro call).
378 *
379 * @param ... Arguments, see @ref zassert_mem_equal__
380 * for real arguments accepted.
381 */
382 #define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
383
384 /**
385 * @brief Internal assert that 2 memory buffers have the same contents
386 *
387 * @note This is internal macro, to be used as a second expansion.
388 * See @ref zassert_mem_equal.
389 *
390 * @param buf Buffer to compare
391 * @param exp Buffer with expected contents
392 * @param size Size of buffers
393 * @param ... Optional message and variables to print if the assertion fails
394 */
395 #define zassert_mem_equal__(buf, exp, size, ...) \
396 zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
397
398 /**
399 * @brief Assert that 2 strings have the same contents
400 *
401 * @param s1 The first string
402 * @param s2 The second string
403 * @param ... Optional message and variables to print if the expectation fails
404 */
405 #define zassert_str_equal(s1, s2, ...) \
406 zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
407
408 /**
409 * @}
410 */
411
412 /**
413 * @defgroup ztest_assume Ztest assumption macros
414 * @ingroup ztest
415 *
416 * This module provides assumptions when using Ztest.
417 *
418 * @{
419 */
420
421 /**
422 * @brief Assume that @a cond is true
423 *
424 * If the assumption fails, the test will be marked as "skipped".
425 *
426 * @param cond Condition to check
427 * @param ... Optional message and variables to print if the assumption fails
428 */
429 #define zassume_true(cond, ...) zassume(cond, #cond " is false", ##__VA_ARGS__)
430
431 /**
432 * @brief Assume that @a cond is false
433 *
434 * If the assumption fails, the test will be marked as "skipped".
435 *
436 * @param cond Condition to check
437 * @param ... Optional message and variables to print if the assumption fails
438 */
439 #define zassume_false(cond, ...) zassume(!(cond), #cond " is true", ##__VA_ARGS__)
440
441 /**
442 * @brief Assume that @a cond is 0 (success)
443 *
444 * If the assumption fails, the test will be marked as "skipped".
445 *
446 * @param cond Condition to check
447 * @param ... Optional message and variables to print if the assumption fails
448 */
449 #define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
450
451 /**
452 * @brief Assume that @a cond is not 0 (failure)
453 *
454 * If the assumption fails, the test will be marked as "skipped".
455 *
456 * @param cond Condition to check
457 * @param ... Optional message and variables to print if the assumption fails
458 */
459 #define zassume_not_ok(cond, ...) zassume(!!(cond), #cond " is zero", ##__VA_ARGS__)
460
461 /**
462 * @brief Assume that @a ptr is NULL
463 *
464 * If the assumption fails, the test will be marked as "skipped".
465 *
466 * @param ptr Pointer to compare
467 * @param ... Optional message and variables to print if the assumption fails
468 */
469 #define zassume_is_null(ptr, ...) zassume((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
470
471 /**
472 * @brief Assume that @a ptr is not NULL
473 *
474 * If the assumption fails, the test will be marked as "skipped".
475 *
476 * @param ptr Pointer to compare
477 * @param ... Optional message and variables to print if the assumption fails
478 */
479 #define zassume_not_null(ptr, ...) zassume((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
480
481 /**
482 * @brief Assume that @a a equals @a b
483 *
484 * @a a and @a b won't be converted and will be compared directly. If the
485 * assumption fails, the test will be marked as "skipped".
486 *
487 * @param a Value to compare
488 * @param b Value to compare
489 * @param ... Optional message and variables to print if the assumption fails
490 */
491 #define zassume_equal(a, b, ...) zassume((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
492
493 /**
494 * @brief Assume that @a a does not equal @a b
495 *
496 * @a a and @a b won't be converted and will be compared directly. If the
497 * assumption fails, the test will be marked as "skipped".
498 *
499 * @param a Value to compare
500 * @param b Value to compare
501 * @param ... Optional message and variables to print if the assumption fails
502 */
503 #define zassume_not_equal(a, b, ...) zassume((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
504
505 /**
506 * @brief Assume that @a a equals @a b
507 *
508 * @a a and @a b will be converted to `void *` before comparing. If the
509 * assumption fails, the test will be marked as "skipped".
510 *
511 * @param a Value to compare
512 * @param b Value to compare
513 * @param ... Optional message and variables to print if the assumption fails
514 */
515 #define zassume_equal_ptr(a, b, ...) \
516 zassume((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
517
518 /**
519 * @brief Assume that @a a is within @a b with delta @a d
520 *
521 * If the assumption fails, the test will be marked as "skipped".
522 *
523 * @param a Value to compare
524 * @param b Value to compare
525 * @param d Delta
526 * @param ... Optional message and variables to print if the assumption fails
527 */
528 #define zassume_within(a, b, d, ...) \
529 zassume(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
530 ##__VA_ARGS__)
531
532 /**
533 * @brief Assume that @a a is greater than or equal to @a l and less
534 * than or equal to @a u
535 *
536 * If the assumption fails, the test will be marked as "skipped".
537 *
538 * @param a Value to compare
539 * @param l Lower limit
540 * @param u Upper limit
541 * @param ... Optional message and variables to print if the assumption fails
542 */
543 #define zassume_between_inclusive(a, l, u, ...) \
544 zassume(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
545 ##__VA_ARGS__)
546
547 /**
548 * @brief Assume that 2 memory buffers have the same contents
549 *
550 * This macro calls the final memory comparison assumption macro.
551 * Using double expansion allows providing some arguments by macros that
552 * would expand to more than one values (ANSI-C99 defines that all the macro
553 * arguments have to be expanded before macro call).
554 *
555 * @param ... Arguments, see @ref zassume_mem_equal__
556 * for real arguments accepted.
557 */
558 #define zassume_mem_equal(...) zassume_mem_equal__(__VA_ARGS__)
559
560 /**
561 * @brief Internal assume that 2 memory buffers have the same contents
562 *
563 * If the assumption fails, the test will be marked as "skipped".
564 *
565 * @note This is internal macro, to be used as a second expansion.
566 * See @ref zassume_mem_equal.
567 *
568 * @param buf Buffer to compare
569 * @param exp Buffer with expected contents
570 * @param size Size of buffers
571 * @param ... Optional message and variables to print if the assumption fails
572 */
573 #define zassume_mem_equal__(buf, exp, size, ...) \
574 zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
575
576 /**
577 * @brief Assumes that 2 strings have the same contents
578 *
579 * @param s1 The first string
580 * @param s2 The second string
581 * @param ... Optional message and variables to print if the expectation fails
582 */
583 #define zassume_str_equal(s1, s2, ...) \
584 zassume(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
585
586 /**
587 * @}
588 */
589
590 /**
591 * @defgroup ztest_expect Ztest expectation macros
592 * @ingroup ztest
593 *
594 * This module provides expectations when using Ztest.
595 *
596 * @{
597 */
598
599 /**
600 * @brief Expect that @a cond is true, otherwise mark test as failed but continue its execution.
601 *
602 * @param cond Condition to check
603 * @param ... Optional message and variables to print if the expectation fails
604 */
605 #define zexpect_true(cond, ...) zexpect(cond, #cond " is false", ##__VA_ARGS__)
606
607 /**
608 * @brief Expect that @a cond is false, otherwise mark test as failed but continue its execution.
609 *
610 * @param cond Condition to check
611 * @param ... Optional message and variables to print if the expectation fails
612 */
613 #define zexpect_false(cond, ...) zexpect(!(cond), #cond " is true", ##__VA_ARGS__)
614
615 /**
616 * @brief Expect that @a cond is 0 (success), otherwise mark test as failed but continue its
617 * execution.
618 *
619 * @param cond Condition to check
620 * @param ... Optional message and variables to print if the expectation fails
621 */
622 #define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__)
623
624 /**
625 * @brief Expect that @a cond is not 0 (failure), otherwise mark test as failed but continue its
626 * execution.
627 *
628 * @param cond Condition to check
629 * @param ... Optional message and variables to print if the expectation fails
630 */
631 #define zexpect_not_ok(cond, ...) zexpect(!!(cond), #cond " is zero", ##__VA_ARGS__)
632
633 /**
634 * @brief Expect that @a ptr is NULL, otherwise mark test as failed but continue its execution.
635 *
636 * @param ptr Pointer to compare
637 * @param ... Optional message and variables to print if the expectation fails
638 */
639 #define zexpect_is_null(ptr, ...) zexpect((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
640
641 /**
642 * @brief Expect that @a ptr is not NULL, otherwise mark test as failed but continue its execution.
643 *
644 * @param ptr Pointer to compare
645 * @param ... Optional message and variables to print if the expectation fails
646 */
647 #define zexpect_not_null(ptr, ...) zexpect((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
648
649 /**
650 * @brief Expect that @a a equals @a b, otherwise mark test as failed but continue its execution.
651 *
652 * @param a Value to compare
653 * @param b Value to compare
654 * @param ... Optional message and variables to print if the expectation fails
655 */
656 #define zexpect_equal(a, b, ...) zexpect((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
657
658 /**
659 * @brief Expect that @a a does not equal @a b, otherwise mark test as failed but continue its
660 * execution.
661 *
662 * @a a and @a b won't be converted and will be compared directly.
663 *
664 * @param a Value to compare
665 * @param b Value to compare
666 * @param ... Optional message and variables to print if the expectation fails
667 */
668 #define zexpect_not_equal(a, b, ...) zexpect((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
669
670 /**
671 * @brief Expect that @a a equals @a b, otherwise mark test as failed but continue its execution.
672 *
673 * @a a and @a b will be converted to `void *` before comparing.
674 *
675 * @param a Value to compare
676 * @param b Value to compare
677 * @param ... Optional message and variables to print if the expectation fails
678 */
679 #define zexpect_equal_ptr(a, b, ...) \
680 zexpect((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
681
682 /**
683 * @brief Expect that @a a is within @a b with delta @a d, otherwise mark test as failed but
684 * continue its execution.
685 *
686 * @param a Value to compare
687 * @param b Value to compare
688 * @param delta Difference between a and b
689 * @param ... Optional message and variables to print if the expectation fails
690 */
691 #define zexpect_within(a, b, delta, ...) \
692 zexpect(((a) >= ((b) - (delta))) && ((a) <= ((b) + (delta))), \
693 #a " not within " #b " +/- " #delta, ##__VA_ARGS__)
694
695 /**
696 * @brief Expect that @a a is greater than or equal to @a l and less
697 * than or equal to @a u, otherwise mark test as failed but continue its execution.
698 *
699 * @param a Value to compare
700 * @param lower Lower limit
701 * @param upper Upper limit
702 * @param ... Optional message and variables to print if the expectation fails
703 */
704 #define zexpect_between_inclusive(a, lower, upper, ...) \
705 zexpect(((a) >= (lower)) && ((a) <= (upper)), \
706 #a " not between " #lower " and " #upper " inclusive", ##__VA_ARGS__)
707
708 /**
709 * @brief Expect that 2 memory buffers have the same contents, otherwise mark test as failed but
710 * continue its execution.
711 *
712 * @param buf Buffer to compare
713 * @param exp Buffer with expected contents
714 * @param size Size of buffers
715 * @param ... Optional message and variables to print if the expectation fails
716 */
717 #define zexpect_mem_equal(buf, exp, size, ...) \
718 zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
719
720 /**
721 * @brief Expect that 2 strings have the same contents, otherwise mark test as failed but
722 * continue its execution.
723 *
724 * @param s1 The first string
725 * @param s2 The second string
726 * @param ... Optional message and variables to print if the expectation fails
727 */
728 #define zexpect_str_equal(s1, s2, ...) \
729 zexpect(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
730
731 /**
732 * @}
733 */
734
735 #ifdef __cplusplus
736 }
737 #endif
738
739 #endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
740