linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Latypov <dlatypov@google.com>
To: Brendan Higgins <brendanhiggins@google.com>
Cc: davidgow@google.com, linux-kernel@vger.kernel.org,
	kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org,
	skhan@linuxfoundation.org, torvalds@linux-foundation.org
Subject: Re: [PATCH 2/6] kunit: move check if assertion passed into the macros
Date: Mon, 10 Jan 2022 14:32:49 -0800	[thread overview]
Message-ID: <CAGS_qxqhhu=Jy+vwWZGZLRG4qW5=ZJ8t7QbjtL54onc4-NFnrw@mail.gmail.com> (raw)
In-Reply-To: <CAFd5g45HcdzB_CTNRRpH8BFbBvG0nDS4_6VUj3Tqx8XOuVTNOQ@mail.gmail.com>

On Mon, Jan 10, 2022 at 2:21 PM Brendan Higgins
<brendanhiggins@google.com> wrote:
>
> On Fri, Jan 7, 2022 at 8:23 PM Daniel Latypov <dlatypov@google.com> wrote:
> >
> > 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_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.
> >
> > 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: Daniel Latypov <dlatypov@google.com>
>
> Tiny nit, see below. Otherwise:
>
> Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
>
> > ---
> >  include/kunit/test.h | 20 ++++++++++----------
> >  lib/kunit/test.c     | 13 ++++---------
> >  2 files changed, 14 insertions(+), 19 deletions(-)
> >
> > diff --git a/include/kunit/test.h b/include/kunit/test.h
> > index b26400731c02..690a28dfc795 100644
> > --- a/include/kunit/test.h
> > +++ b/include/kunit/test.h
> > @@ -770,18 +770,18 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
> >   */
> >  #define KUNIT_SUCCEED(test) do {} while (0)
> >
> > -void kunit_do_assertion(struct kunit *test,
> > -                       struct kunit_assert *assert,
> > -                       bool pass,
> > -                       const char *fmt, ...);
> > +void kunit_failed_assertion(struct kunit *test,
> > +                           struct kunit_assert *assert,
> > +                           const char *fmt, ...);
>
> Tiny nit: I think this should be kunit_fail_assertion. I think
> functions should be in the active tense, imperative mood since when
> you call a function you are telling it to do something.
>
> Also, do we need to worry about this getting confused with KUNIT_FAIL,
> or KUNIT_FAIL_ASSERTION:

So do we want to try and pick a different name from
kunit_fail_assertion() to avoid confusion with the macro?
That's partly why I went with past tense.
Perhaps: "kunit_do_assertion() => kunit_do_failed_assertion()" instead?

Tangent: we have some similar confusing names, e.g. KUNIT_ASSERTION is
both the name of a macro and an enum (kunit_assert_type), and those
have the exact same case.

>
> https://elixir.bootlin.com/linux/v5.16/source/include/kunit/test.h#L788
>
> ?
>
> >  #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do {  \
> > -       struct assert_class __assertion = INITIALIZER;                         \
> > -       kunit_do_assertion(test,                                               \
> > -                          &__assertion.assert,                                \
> > -                          pass,                                               \
> > -                          fmt,                                                \
> > -                          ##__VA_ARGS__);                                     \
> > +       if (!(pass)) {                                                         \
> > +               struct assert_class __assertion = INITIALIZER;                 \
> > +               kunit_failed_assertion(test,                                   \
> > +                                      &__assertion.assert,                    \
> > +                                      fmt,                                    \
> > +                                      ##__VA_ARGS__);                         \
> > +       }                                                                      \
> >  } while (0)
> >
> >
> > diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> > index c7ed4aabec04..5ad671745483 100644
> > --- a/lib/kunit/test.c
> > +++ b/lib/kunit/test.c
> > @@ -275,16 +275,11 @@ static void __noreturn kunit_abort(struct kunit *test)
> >         WARN_ONCE(true, "Throw could not abort from test!\n");
> >  }
> >
> > -void kunit_do_assertion(struct kunit *test,
> > -                       struct kunit_assert *assert,
> > -                       bool pass,
> > -                       const char *fmt, ...)
> > +void kunit_failed_assertion(struct kunit *test,
> > +                           struct kunit_assert *assert,
> > +                           const char *fmt, ...)
> >  {
> >         va_list args;
> > -
> > -       if (pass)
> > -               return;
> > -
> >         va_start(args, fmt);
> >
> >         assert->message.fmt = fmt;
> > @@ -297,7 +292,7 @@ void kunit_do_assertion(struct kunit *test,
> >         if (assert->type == KUNIT_ASSERTION)
> >                 kunit_abort(test);
> >  }
> > -EXPORT_SYMBOL_GPL(kunit_do_assertion);
> > +EXPORT_SYMBOL_GPL(kunit_failed_assertion);
> >
> >  void kunit_init_test(struct kunit *test, const char *name, char *log)
> >  {
> > --
> > 2.34.1.575.g55b058a8bb-goog
> >

  reply	other threads:[~2022-01-10 22:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-08  1:22 [PATCH 0/6] kunit: refactor assertions to use less stack Daniel Latypov
2022-01-08  1:22 ` [PATCH 1/6] kunit: add example test case showing off all the expect macros Daniel Latypov
2022-01-10 22:13   ` Brendan Higgins
2022-01-10 22:25     ` Daniel Latypov
2022-01-11  6:50   ` David Gow
2022-01-11 17:27     ` Daniel Latypov
2022-01-08  1:23 ` [PATCH 2/6] kunit: move check if assertion passed into the macros Daniel Latypov
2022-01-10 22:21   ` Brendan Higgins
2022-01-10 22:32     ` Daniel Latypov [this message]
2022-01-11  6:51       ` David Gow
2022-01-11 18:42         ` Daniel Latypov
2022-01-08  1:23 ` [PATCH 3/6] kunit: drop unused kunit* field in kunit_assert Daniel Latypov
2022-01-10 22:24   ` Brendan Higgins
2022-01-11  6:51   ` David Gow
2022-01-11 18:34     ` Daniel Latypov
2022-01-08  1:23 ` [PATCH 4/6] kunit: factor out kunit_base_assert_format() call into kunit_fail() Daniel Latypov
2022-01-10 22:31   ` Brendan Higgins
2022-01-10 22:35     ` Daniel Latypov
2022-01-11  6:51   ` David Gow
2022-01-08  1:23 ` [PATCH 5/6] kunit: split out part of kunit_assert into a static const Daniel Latypov
2022-01-11  6:57   ` David Gow
2022-01-11 17:07     ` Daniel Latypov
2022-01-08  1:23 ` [PATCH 6/6] kunit: drop unused assert_type from kunit_assert and clean up macros Daniel Latypov
2022-01-11  6:57   ` David Gow
2022-01-11 19:21     ` Daniel Latypov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGS_qxqhhu=Jy+vwWZGZLRG4qW5=ZJ8t7QbjtL54onc4-NFnrw@mail.gmail.com' \
    --to=dlatypov@google.com \
    --cc=brendanhiggins@google.com \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).