Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB67AC433F5 for ; Mon, 10 Jan 2022 22:33:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242232AbiAJWdE (ORCPT ); Mon, 10 Jan 2022 17:33:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241251AbiAJWdD (ORCPT ); Mon, 10 Jan 2022 17:33:03 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A659CC06173F for ; Mon, 10 Jan 2022 14:33:02 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id 30so58024085edv.3 for ; Mon, 10 Jan 2022 14:33:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3yUyA3kF4TyoDY7fJI1rswctkI7DoFPRquevsmMCbKw=; b=asBhcryaNp3vUkTNKu2AepHZ486eJ5AUxw/RmSZRaCj9lWLi8ZKC/IEeCpL/8BWI2K U87Yvz8Sl5Zy+BaG4ACcTPLA97xMoE2Hvu5AFrV7CFcEsx0+MNiBa+tBmN0jc+FI1XLW aqT8IPVAalg273G0/+1AChrRpLEjZQFuUW81M5KXGrmDJwsZX1wMvqVsEhPis4l4c47C ADLbZtJlxcQ+6BI2GUqZ57Lzb5EzamLOIkblt+xr42uQrOUyJdEAzJjS9OYzjYsagebU ZAgw+SGdFDbZHxPtcAd+TIxqkQ2DBBIfyVPuYBJrmbm/rtIpD4wmSUws9Jf8dMTiXJ3J FXwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3yUyA3kF4TyoDY7fJI1rswctkI7DoFPRquevsmMCbKw=; b=LAzB/dwrxpOS8pMIPe4ZhqiSL9n/XjuNnWjLGOeAFxgHCweGJ75m+LW/C1HR4pj8BW zus4jDTwF0LZfdJEVeup3lEgTE3OhDGcO5ei0KjxtPv6U9rC606rU50bpYjNsAlVPYlp vhn3oJcnOQei4KI2BfSG+O7jGzuGilMd3q3NtVXZrIt+mk/lu6Avbc6RZyM4tla0UIni 0k0UcEcnkwJeJ3Kaa9mKLOrtJqQbH/4QJTyUROdnUq/DIJ2jHhoaTvLHIBR8oOTC1e7X fGbCtSOtnxiWjW8SKyMXqqH+ArbhIEM9vfYGPWE4q0fKO6kyZdxwzQGIaxLhAD2wSIdK yFmA== X-Gm-Message-State: AOAM530LvjLYHFdNfxo2pjwz8gmndJxFqRajQT1FauJ/acIqgoWQD7ye omGZBlRMQLBmNpDZAm7GEDzugKmFzsfmm5Y/BZc2Tw== X-Google-Smtp-Source: ABdhPJypV8q6W6FO7jt10mVZSmvL3v/XVjry60WwpfutYkFPixIHHzzb9maC66YA2apCHfda9GzFKQ//AwpoJrQ9hxA= X-Received: by 2002:a17:907:3f0c:: with SMTP id hq12mr1372451ejc.358.1641853981056; Mon, 10 Jan 2022 14:33:01 -0800 (PST) MIME-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> <20220108012304.1049587-3-dlatypov@google.com> In-Reply-To: From: Daniel Latypov Date: Mon, 10 Jan 2022 14:32:49 -0800 Message-ID: Subject: Re: [PATCH 2/6] kunit: move check if assertion passed into the macros To: Brendan Higgins 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 10, 2022 at 2:21 PM Brendan Higgins wrote: > > On Fri, Jan 7, 2022 at 8:23 PM Daniel Latypov 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 > > Tiny nit, see below. Otherwise: > > Reviewed-by: Brendan Higgins > > > --- > > 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 > >