From mboxrd@z Thu Jan 1 00:00:00 1970 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 74253C433F5 for ; Mon, 10 Jan 2022 22:21:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345361AbiAJWVy (ORCPT ); Mon, 10 Jan 2022 17:21:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242105AbiAJWVx (ORCPT ); Mon, 10 Jan 2022 17:21:53 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6BAEC06173F for ; Mon, 10 Jan 2022 14:21:53 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id pj2so14186436pjb.2 for ; Mon, 10 Jan 2022 14:21:53 -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=dpEgaA7tQQxRTXpViippoUOAKxED1oc92U+eN95LNGs=; b=Hn++l73OqhxLfme2ErG2NqRu9ncAa+tUVLdIKUfHhZxaidp4c7iue3e8A5v7PyIFIG QmO+PYhfi5xJoqAWLdAvi3zyIXYfDeLbcd2v8rWPgHzM0Gkk5OjEkoKCiCJCUiCorRmJ c9CCKtbz4SERrhgz1qeXtCFRuQVC9YbVKmqNu1SnfNWoLFSo4BTJGzP99+iNa1NJfvTn 3g7MwaTneEG1gIRuUaF+CbS8VTj0/L2LkNgVSy6ILJhHv7EPCX8ITbeOh3BWo+nuIXmP zEhsFaVwFTD0zo2zi39CbHch1gVGB47ATJVfKbdItybJZnCPl2hB6hK55QIIM76/OEWf A1tA== 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=dpEgaA7tQQxRTXpViippoUOAKxED1oc92U+eN95LNGs=; b=ZH2RPHQph2Mc2LznXlHOk/N93w/xXbjBu962mqAdd11+aiqXJQkqMvBRSpzMDT3ps6 qaCtEq42ywHk/5zl8s3rS0qdW1bQx7F06pOg5r5HIGHgntlIn3pvnZvrCb0Rcjat+gIc WhKGgkxrwIcF4QRvObG13TG3oQvepv11RwYLE4FcUEEZoEYA/fhyjm1q4l6AFZ1tuUa4 SgnP9WUEdlTxmso47LHgbbkDEGm6bPDNjBtLC55Jor6EWgl7ZZTaCElDoX/dQQdVaC+J 21Pk0dySnW13LwEoa5gO7Lo6YCC2oIuGM4DwMQyd0LhqiiFDepGh3IqUyPZMtxz0ZckI T//w== X-Gm-Message-State: AOAM532j3TFqfH3vD1GkfjjSB/YyZy4/+umtWCfJOiOJICV2NDxj7WRW XdfXQABgmWDyBaQAu4axUa+JOOu2x7mEjoAwN/vptQI65IK73flb X-Google-Smtp-Source: ABdhPJzU68WaUrCxAJr0XWxbBe2huF8ZD7GWtX8Nh04kLOHZS3nCqLkZsig9aqAB5u3s7goLMXyUd0Ng5bYukv2mPdM= X-Received: by 2002:a62:80c2:0:b0:4bb:47a:6983 with SMTP id j185-20020a6280c2000000b004bb047a6983mr1823271pfd.24.1641853312838; Mon, 10 Jan 2022 14:21:52 -0800 (PST) MIME-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> <20220108012304.1049587-3-dlatypov@google.com> In-Reply-To: <20220108012304.1049587-3-dlatypov@google.com> From: Brendan Higgins Date: Mon, 10 Jan 2022 17:21:41 -0500 Message-ID: Subject: Re: [PATCH 2/6] kunit: move check if assertion passed into the macros To: Daniel Latypov 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 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: 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 >