Commit 97d453bc authored by Daniel Latypov's avatar Daniel Latypov Committed by Shuah Khan

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: default avatarDaniel Latypov <dlatypov@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent a8495ad8
...@@ -476,30 +476,27 @@ void kunit_do_failed_assertion(struct kunit *test, ...@@ -476,30 +476,27 @@ void kunit_do_failed_assertion(struct kunit *test,
assert_format_t assert_format, assert_format_t assert_format,
const char *fmt, ...); const char *fmt, ...);
#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
if (unlikely(!(pass))) { \ static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ struct assert_class __assertion = INITIALIZER; \
struct assert_class __assertion = INITIALIZER; \ kunit_do_failed_assertion(test, \
kunit_do_failed_assertion(test, \ &__loc, \
&__loc, \ assert_type, \
assert_type, \ &__assertion.assert, \
&__assertion.assert, \ assert_format, \
assert_format, \ fmt, \
fmt, \ ##__VA_ARGS__); \
##__VA_ARGS__); \
} \
} while (0) } while (0)
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
KUNIT_ASSERTION(test, \ _KUNIT_FAILED(test, \
assert_type, \ assert_type, \
false, \ kunit_fail_assert, \
kunit_fail_assert, \ kunit_fail_assert_format, \
kunit_fail_assert_format, \ {}, \
{}, \ fmt, \
fmt, \ ##__VA_ARGS__)
##__VA_ARGS__)
/** /**
* KUNIT_FAIL() - Always causes a test to fail when evaluated. * KUNIT_FAIL() - Always causes a test to fail when evaluated.
...@@ -524,15 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test, ...@@ -524,15 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test,
expected_true, \ expected_true, \
fmt, \ fmt, \
...) \ ...) \
KUNIT_ASSERTION(test, \ do { \
assert_type, \ if (likely(!!(condition) == !!expected_true)) \
!!(condition) == !!expected_true, \ break; \
kunit_unary_assert, \ \
kunit_unary_assert_format, \ _KUNIT_FAILED(test, \
KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ assert_type, \
expected_true), \ kunit_unary_assert, \
fmt, \ kunit_unary_assert_format, \
##__VA_ARGS__) KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
expected_true), \
fmt, \
##__VA_ARGS__); \
} while (0)
#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
KUNIT_UNARY_ASSERTION(test, \ KUNIT_UNARY_ASSERTION(test, \
...@@ -582,16 +583,18 @@ do { \ ...@@ -582,16 +583,18 @@ do { \
.right_text = #right, \ .right_text = #right, \
}; \ }; \
\ \
KUNIT_ASSERTION(test, \ if (likely(__left op __right)) \
assert_type, \ break; \
__left op __right, \ \
assert_class, \ _KUNIT_FAILED(test, \
format_func, \ assert_type, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ assert_class, \
__left, \ format_func, \
__right), \ KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
fmt, \ __left, \
##__VA_ARGS__); \ __right), \
fmt, \
##__VA_ARGS__); \
} while (0) } while (0)
#define KUNIT_BINARY_INT_ASSERTION(test, \ #define KUNIT_BINARY_INT_ASSERTION(test, \
...@@ -640,16 +643,19 @@ do { \ ...@@ -640,16 +643,19 @@ do { \
.right_text = #right, \ .right_text = #right, \
}; \ }; \
\ \
KUNIT_ASSERTION(test, \ if (likely(strcmp(__left, __right) op 0)) \
assert_type, \ break; \
strcmp(__left, __right) op 0, \ \
kunit_binary_str_assert, \ \
kunit_binary_str_assert_format, \ _KUNIT_FAILED(test, \
KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ assert_type, \
__left, \ kunit_binary_str_assert, \
__right), \ kunit_binary_str_assert_format, \
fmt, \ KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
##__VA_ARGS__); \ __left, \
__right), \
fmt, \
##__VA_ARGS__); \
} while (0) } while (0)
#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
...@@ -660,15 +666,16 @@ do { \ ...@@ -660,15 +666,16 @@ do { \
do { \ do { \
const typeof(ptr) __ptr = (ptr); \ const typeof(ptr) __ptr = (ptr); \
\ \
KUNIT_ASSERTION(test, \ if (!IS_ERR_OR_NULL(__ptr)) \
assert_type, \ break; \
!IS_ERR_OR_NULL(__ptr), \ \
kunit_ptr_not_err_assert, \ _KUNIT_FAILED(test, \
kunit_ptr_not_err_assert_format, \ assert_type, \
KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \ kunit_ptr_not_err_assert, \
__ptr), \ kunit_ptr_not_err_assert_format, \
fmt, \ KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
##__VA_ARGS__); \ fmt, \
##__VA_ARGS__); \
} while (0) } while (0)
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment