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