History

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).

Daniel Latypov committed 4 years ago

Commit 4fdacef1 parent 7b339102 files · +15 −19Browse files

Unified · Split

2 files
  1. include/kunit/test.h +11 −10
  2. lib/kunit/test.c +4 −9
include/kunit/test.h+11−10
@@ -12,6 +12,7 @@ · 11 unmodified lines
12#include <kunit/assert.h>12#include <kunit/assert.h>
13#include <kunit/try-catch.h>13#include <kunit/try-catch.h>
14 14
15#include <linux/compiler.h>
15#include <linux/container_of.h>16#include <linux/container_of.h>
16#include <linux/err.h>17#include <linux/err.h>
17#include <linux/init.h>18#include <linux/init.h>
@@ -770,18 +771,18 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); · 752 unmodified lines
770 */771 */
771#define KUNIT_SUCCEED(test) do {} while (0)772#define KUNIT_SUCCEED(test) do {} while (0)
772 773
773void kunit_do_assertion(struct kunit *test,774void kunit_do_failed_assertion(struct kunit *test,
774 struct kunit_assert *assert,775 struct kunit_assert *assert,
775 bool pass,776 const char *fmt, ...);
776 const char *fmt, ...);
777 777
778#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \778#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
779 struct assert_class __assertion = INITIALIZER; \779 if (unlikely(!(pass))) { \
780 kunit_do_assertion(test, \780 struct assert_class __assertion = INITIALIZER; \
781 &__assertion.assert, \781 kunit_do_failed_assertion(test, \
782 pass, \782 &__assertion.assert, \
783 fmt, \783 fmt, \
784 ##__VA_ARGS__); \784 ##__VA_ARGS__); \
785 } \
785} while (0)786} while (0)
786 787
787 788
lib/kunit/test.c+4−9
@@ -275,16 +275,11 @@ static void __noreturn kunit_abort(struct kunit *test) · 274 unmodified lines
275 WARN_ONCE(true, "Throw could not abort from test!\n");275 WARN_ONCE(true, "Throw could not abort from test!\n");
276}276}
277 277
278void kunit_do_assertion(struct kunit *test,278void kunit_do_failed_assertion(struct kunit *test,
279 struct kunit_assert *assert,279 struct kunit_assert *assert,
280 bool pass,280 const char *fmt, ...)
281 const char *fmt, ...)
282{281{
283 va_list args;282 va_list args;
284
285 if (pass)
286 return;
287
288 va_start(args, fmt);283 va_start(args, fmt);
289 284
290 assert->message.fmt = fmt;285 assert->message.fmt = fmt;
@@ -297,7 +292,7 @@ void kunit_do_assertion(struct kunit *test, · 6 unmodified lines
297 if (assert->type == KUNIT_ASSERTION)292 if (assert->type == KUNIT_ASSERTION)
298 kunit_abort(test);293 kunit_abort(test);
299}294}
300EXPORT_SYMBOL_GPL(kunit_do_assertion);295EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
301 296
302void kunit_init_test(struct kunit *test, const char *name, char *log)297void kunit_init_test(struct kunit *test, const char *name, char *log)
303{298{

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t