Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: rename base KUNIT_ASSERTION macro to _KUNIT_FAILED

Context:
Currently this macro's name, KUNIT_ASSERTION conflicts with the name of
an enum whose values are {KUNIT_EXPECTATION, KUNIT_ASSERTION}.

It's hard to think of a better name for the enum, so rename this macro.
It's also a bit strange that the macro might do nothing depending on the
boolean argument `pass`. Why not have callers check themselves?

This patch:
Moves the pass/fail checking into the callers of KUNIT_ASSERTION, so now
we only call it when the check has failed.
Then we rename the macro the _KUNIT_FAILED() to reflect the new
semantics.

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Daniel Latypov and committed by
Shuah Khan
97d453bc a8495ad8

+65 -58
+65 -58
include/kunit/test.h
··· 476 476 assert_format_t assert_format, 477 477 const char *fmt, ...); 478 478 479 - #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ 480 - if (unlikely(!(pass))) { \ 481 - static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ 482 - struct assert_class __assertion = INITIALIZER; \ 483 - kunit_do_failed_assertion(test, \ 484 - &__loc, \ 485 - assert_type, \ 486 - &__assertion.assert, \ 487 - assert_format, \ 488 - fmt, \ 489 - ##__VA_ARGS__); \ 490 - } \ 479 + #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ 480 + static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ 481 + struct assert_class __assertion = INITIALIZER; \ 482 + kunit_do_failed_assertion(test, \ 483 + &__loc, \ 484 + assert_type, \ 485 + &__assertion.assert, \ 486 + assert_format, \ 487 + fmt, \ 488 + ##__VA_ARGS__); \ 491 489 } while (0) 492 490 493 491 494 492 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ 495 - KUNIT_ASSERTION(test, \ 496 - assert_type, \ 497 - false, \ 498 - kunit_fail_assert, \ 499 - kunit_fail_assert_format, \ 500 - {}, \ 501 - fmt, \ 502 - ##__VA_ARGS__) 493 + _KUNIT_FAILED(test, \ 494 + assert_type, \ 495 + kunit_fail_assert, \ 496 + kunit_fail_assert_format, \ 497 + {}, \ 498 + fmt, \ 499 + ##__VA_ARGS__) 503 500 504 501 /** 505 502 * KUNIT_FAIL() - Always causes a test to fail when evaluated. ··· 521 524 expected_true, \ 522 525 fmt, \ 523 526 ...) \ 524 - KUNIT_ASSERTION(test, \ 525 - assert_type, \ 526 - !!(condition) == !!expected_true, \ 527 - kunit_unary_assert, \ 528 - kunit_unary_assert_format, \ 529 - KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ 530 - expected_true), \ 531 - fmt, \ 532 - ##__VA_ARGS__) 527 + do { \ 528 + if (likely(!!(condition) == !!expected_true)) \ 529 + break; \ 530 + \ 531 + _KUNIT_FAILED(test, \ 532 + assert_type, \ 533 + kunit_unary_assert, \ 534 + kunit_unary_assert_format, \ 535 + KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ 536 + expected_true), \ 537 + fmt, \ 538 + ##__VA_ARGS__); \ 539 + } while (0) 533 540 534 541 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 535 542 KUNIT_UNARY_ASSERTION(test, \ ··· 583 582 .right_text = #right, \ 584 583 }; \ 585 584 \ 586 - KUNIT_ASSERTION(test, \ 587 - assert_type, \ 588 - __left op __right, \ 589 - assert_class, \ 590 - format_func, \ 591 - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ 592 - __left, \ 593 - __right), \ 594 - fmt, \ 595 - ##__VA_ARGS__); \ 585 + if (likely(__left op __right)) \ 586 + break; \ 587 + \ 588 + _KUNIT_FAILED(test, \ 589 + assert_type, \ 590 + assert_class, \ 591 + format_func, \ 592 + KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ 593 + __left, \ 594 + __right), \ 595 + fmt, \ 596 + ##__VA_ARGS__); \ 596 597 } while (0) 597 598 598 599 #define KUNIT_BINARY_INT_ASSERTION(test, \ ··· 643 640 .right_text = #right, \ 644 641 }; \ 645 642 \ 646 - KUNIT_ASSERTION(test, \ 647 - assert_type, \ 648 - strcmp(__left, __right) op 0, \ 649 - kunit_binary_str_assert, \ 650 - kunit_binary_str_assert_format, \ 651 - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ 652 - __left, \ 653 - __right), \ 654 - fmt, \ 655 - ##__VA_ARGS__); \ 643 + if (likely(strcmp(__left, __right) op 0)) \ 644 + break; \ 645 + \ 646 + \ 647 + _KUNIT_FAILED(test, \ 648 + assert_type, \ 649 + kunit_binary_str_assert, \ 650 + kunit_binary_str_assert_format, \ 651 + KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ 652 + __left, \ 653 + __right), \ 654 + fmt, \ 655 + ##__VA_ARGS__); \ 656 656 } while (0) 657 657 658 658 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ ··· 666 660 do { \ 667 661 const typeof(ptr) __ptr = (ptr); \ 668 662 \ 669 - KUNIT_ASSERTION(test, \ 670 - assert_type, \ 671 - !IS_ERR_OR_NULL(__ptr), \ 672 - kunit_ptr_not_err_assert, \ 673 - kunit_ptr_not_err_assert_format, \ 674 - KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \ 675 - __ptr), \ 676 - fmt, \ 677 - ##__VA_ARGS__); \ 663 + if (!IS_ERR_OR_NULL(__ptr)) \ 664 + break; \ 665 + \ 666 + _KUNIT_FAILED(test, \ 667 + assert_type, \ 668 + kunit_ptr_not_err_assert, \ 669 + kunit_ptr_not_err_assert_format, \ 670 + KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \ 671 + fmt, \ 672 + ##__VA_ARGS__); \ 678 673 } while (0) 679 674 680 675 /**