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
1212 #include <kunit/assert.h>
1313 #include <kunit/try-catch.h>
1414
15+#include <linux/compiler.h>
1516 #include <linux/container_of.h>
1617 #include <linux/err.h>
1718 #include <linux/init.h>
@@ -770,18 +771,18 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); · 752 unmodified lines
770771 */
771772 #define KUNIT_SUCCEED(test) do {} while (0)
772773
773−void kunit_do_assertion(struct kunit *test,
774− struct kunit_assert *assert,
775− bool pass,
776− const char *fmt, ...);
774+void kunit_do_failed_assertion(struct kunit *test,
775+ struct kunit_assert *assert,
776+ const char *fmt, ...);
777777
778778 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
779− struct assert_class __assertion = INITIALIZER; \
780− kunit_do_assertion(test, \
781− &__assertion.assert, \
782− pass, \
783− fmt, \
784− ##__VA_ARGS__); \
779+ if (unlikely(!(pass))) { \
780+ struct assert_class __assertion = INITIALIZER; \
781+ kunit_do_failed_assertion(test, \
782+ &__assertion.assert, \
783+ fmt, \
784+ ##__VA_ARGS__); \
785+ } \
785786 } while (0)
786787
787788
lib/kunit/test.c+4−9
@@ -275,16 +275,11 @@ static void __noreturn kunit_abort(struct kunit *test) · 274 unmodified lines
275275 WARN_ONCE(true, "Throw could not abort from test!\n");
276276 }
277277
278−void kunit_do_assertion(struct kunit *test,
279− struct kunit_assert *assert,
280− bool pass,
281− const char *fmt, ...)
278+void kunit_do_failed_assertion(struct kunit *test,
279+ struct kunit_assert *assert,
280+ const char *fmt, ...)
282281 {
283282 va_list args;
284−
285− if (pass)
286− return;
287−
288283 va_start(args, fmt);
289284
290285 assert->message.fmt = fmt;
@@ -297,7 +292,7 @@ void kunit_do_assertion(struct kunit *test, · 6 unmodified lines
297292 if (assert->type == KUNIT_ASSERTION)
298293 kunit_abort(test);
299294 }
300−EXPORT_SYMBOL_GPL(kunit_do_assertion);
295+EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
301296
302297 void kunit_init_test(struct kunit *test, const char *name, char *log)
303298 {

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