Commit 4fdacef8 authored by Daniel Latypov's avatar Daniel Latypov Committed by Shuah Khan
Browse files

kunit: move check if assertion passed into the macros



Currently the code always calls kunit_do_assertion() even though it does
nothing when `pass` is true.

This change moves the `if(!(pass))` check into the macro instead
and renames the function to kunit_do_failed_assertion().
I feel this a bit easier to read and understand.

This has the potential upside of avoiding a function call that does
nothing most of the time (assuming your tests are passing) but comes
with the downside of generating a bit more code and branches. We try to
mitigate the branches by tagging them with `unlikely()`.

This also means we don't have to initialize structs that we don't need,
which will become a tiny bit more expensive if we switch over to using
static variables to try and reduce stack usage. (There's runtime code
to check if the variable has been initialized yet or not).

Signed-off-by: default avatarDaniel Latypov <dlatypov@google.com>
Reviewed-by: default avatarBrendan Higgins <brendanhiggins@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 7b339105
Loading
Loading
Loading
Loading
+11 −10
Original line number Original line Diff line number Diff line
@@ -12,6 +12,7 @@
#include <kunit/assert.h>
#include <kunit/assert.h>
#include <kunit/try-catch.h>
#include <kunit/try-catch.h>


#include <linux/compiler.h>
#include <linux/container_of.h>
#include <linux/container_of.h>
#include <linux/err.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/init.h>
@@ -770,18 +771,18 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
 */
 */
#define KUNIT_SUCCEED(test) do {} while (0)
#define KUNIT_SUCCEED(test) do {} while (0)


void kunit_do_assertion(struct kunit *test,
void kunit_do_failed_assertion(struct kunit *test,
			       struct kunit_assert *assert,
			       struct kunit_assert *assert,
			bool pass,
			       const char *fmt, ...);
			       const char *fmt, ...);


#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do {  \
#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do {  \
	if (unlikely(!(pass))) {					       \
		struct assert_class __assertion = INITIALIZER;		       \
		struct assert_class __assertion = INITIALIZER;		       \
	kunit_do_assertion(test,					       \
		kunit_do_failed_assertion(test,				       \
					  &__assertion.assert,		       \
					  &__assertion.assert,		       \
			   pass,					       \
					  fmt,				       \
					  fmt,				       \
					  ##__VA_ARGS__);		       \
					  ##__VA_ARGS__);		       \
	}								       \
} while (0)
} while (0)




+4 −9
Original line number Original line Diff line number Diff line
@@ -275,16 +275,11 @@ static void __noreturn kunit_abort(struct kunit *test)
	WARN_ONCE(true, "Throw could not abort from test!\n");
	WARN_ONCE(true, "Throw could not abort from test!\n");
}
}


void kunit_do_assertion(struct kunit *test,
void kunit_do_failed_assertion(struct kunit *test,
			       struct kunit_assert *assert,
			       struct kunit_assert *assert,
			bool pass,
			       const char *fmt, ...)
			       const char *fmt, ...)
{
{
	va_list args;
	va_list args;

	if (pass)
		return;

	va_start(args, fmt);
	va_start(args, fmt);


	assert->message.fmt = fmt;
	assert->message.fmt = fmt;
@@ -297,7 +292,7 @@ void kunit_do_assertion(struct kunit *test,
	if (assert->type == KUNIT_ASSERTION)
	if (assert->type == KUNIT_ASSERTION)
		kunit_abort(test);
		kunit_abort(test);
}
}
EXPORT_SYMBOL_GPL(kunit_do_assertion);
EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);


void kunit_init_test(struct kunit *test, const char *name, char *log)
void kunit_init_test(struct kunit *test, const char *name, char *log)
{
{