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

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
Loading
Loading
Loading
Loading
+65 −58
Original line number Diff line number Diff line
@@ -476,8 +476,7 @@ void kunit_do_failed_assertion(struct kunit *test,
			       assert_format_t assert_format,
			       const char *fmt, ...);

#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
	if (unlikely(!(pass))) {					       \
#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
	static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;	       \
	struct assert_class __assertion = INITIALIZER;			       \
	kunit_do_failed_assertion(test,					       \
@@ -487,14 +486,12 @@ void kunit_do_failed_assertion(struct kunit *test,
				  assert_format,			       \
				  fmt,					       \
				  ##__VA_ARGS__);			       \
	}								       \
} while (0)


#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
	KUNIT_ASSERTION(test,						       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			false,						       \
		      kunit_fail_assert,				       \
		      kunit_fail_assert_format,				       \
		      {},						       \
@@ -524,15 +521,19 @@ void kunit_do_failed_assertion(struct kunit *test,
			      expected_true,				       \
			      fmt,					       \
			      ...)					       \
	KUNIT_ASSERTION(test,						       \
do {									       \
	if (likely(!!(condition) == !!expected_true))			       \
		break;							       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			!!(condition) == !!expected_true,		       \
		      kunit_unary_assert,				       \
		      kunit_unary_assert_format,			       \
		      KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition,	       \
						     expected_true),	       \
		      fmt,						       \
			##__VA_ARGS__)
		      ##__VA_ARGS__);					       \
} while (0)

#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
	KUNIT_UNARY_ASSERTION(test,					       \
@@ -582,9 +583,11 @@ do { \
		.right_text = #right,					       \
	};								       \
									       \
	KUNIT_ASSERTION(test,						       \
	if (likely(__left op __right))					       \
		break;							       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			__left op __right,				       \
		      assert_class,					       \
		      format_func,					       \
		      KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text,		       \
@@ -640,9 +643,12 @@ do { \
		.right_text = #right,					       \
	};								       \
									       \
	KUNIT_ASSERTION(test,						       \
	if (likely(strcmp(__left, __right) op 0))			       \
		break;							       \
									       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			strcmp(__left, __right) op 0,			       \
		      kunit_binary_str_assert,				       \
		      kunit_binary_str_assert_format,			       \
		      KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text,		       \
@@ -660,13 +666,14 @@ do { \
do {									       \
	const typeof(ptr) __ptr = (ptr);				       \
									       \
	KUNIT_ASSERTION(test,						       \
	if (!IS_ERR_OR_NULL(__ptr))					       \
		break;							       \
									       \
	_KUNIT_FAILED(test,						       \
		      assert_type,					       \
			!IS_ERR_OR_NULL(__ptr),				       \
		      kunit_ptr_not_err_assert,				       \
		      kunit_ptr_not_err_assert_format,			       \
			KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr,		       \
						      __ptr),		       \
		      KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr),	       \
		      fmt,						       \
		      ##__VA_ARGS__);					       \
} while (0)