From nobody Tue Jun 30 21:22:29 2026 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 30572C433FE for ; Sat, 8 Jan 2022 01:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232141AbiAHBX2 (ORCPT ); Fri, 7 Jan 2022 20:23:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232109AbiAHBX1 (ORCPT ); Fri, 7 Jan 2022 20:23:27 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC815C06173F for ; Fri, 7 Jan 2022 17:23:26 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id e137-20020a25378f000000b0060c1f2f4939so15406859yba.3 for ; Fri, 07 Jan 2022 17:23:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=HX9u/gX+wulPpUgYABGAfaI/MF3prDzvD4A5ElqjQQA=; b=pCJbpOK3Hlj59gurktD7xaUarJdHC4kIFDalfvbWNp7W/OUFUh5EwnS6yznGwOhSiM 3RYsuBZcBVqbwyplSu5S6uX3rFkzBkc2WXrZP9BnzgSEbeQGqiYaOqsB/nKJIyUnzAsA crbr29iYX2niJ2W0cviu+FleBtMHb7EddrxiVrfbuPBDP34Jwnhd/FKaEvtCZYpt9uBz gHNVChBqgpycGUnrcF8OP7Iqoo3tiayGmTVPYzJlQ+FxpbcUDw1p8ic3zewDz22KsBRR ChLNjlZ+D6HbvxVnO9xbuD6VBW4gNlnR82Q37RNprPkTpC0RkGes3wJ+A/2x6Z3DybOA 4xQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=HX9u/gX+wulPpUgYABGAfaI/MF3prDzvD4A5ElqjQQA=; b=wZ+ZwzdusaNnWF7cdwBAiUhK9sk8Qvg2zJHeSRZ0kCd0wbL1lE3CVGukH4qP5dKO+l v7UiYit2u1bWUuuJ201Pm5B7FyWkwKMABwRu4g4upu4RknafXZoR2xf4o7dayY1scu09 arjiTx1536zMnCsMH+GrLBQEBSHcBir9lct+Uj9DURQhMmoilU0GhNxriB41a13Rm11+ 9v+VztVk00/DmsHdQGA44oY2JdhBBA6oCCFWWJeHFspyROs7XRWqk4UpvKyXN39eKtKv xyCDvcO31FssCL1ki8YGG6A7fnT1ABBOS9CEvgaWm/xST5Vua5PXILIHstB9hIam0gyf Cpuw== X-Gm-Message-State: AOAM531lg6E02nwpzjagoMTyBNlO8Gl9us5y/VY26qZc0t6kdRxAz63W SkKRvLdxy4Wdw6kkqDxUfGXs2/RnBufVIQ== X-Google-Smtp-Source: ABdhPJypEWFmO1LWVEMbx/BDCFVdQsH015D7EuGHNt3chl8VFnUk2wlx0Kf9w/VeGd2S/JoGM4XejJ46yxNarQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:bf8a:7e90:eb70:9b2b]) (user=dlatypov job=sendgmr) by 2002:a25:d105:: with SMTP id i5mr79661754ybg.382.1641605006051; Fri, 07 Jan 2022 17:23:26 -0800 (PST) Date: Fri, 7 Jan 2022 17:22:59 -0800 In-Reply-To: <20220108012304.1049587-1-dlatypov@google.com> Message-Id: <20220108012304.1049587-2-dlatypov@google.com> Mime-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH 1/6] kunit: add example test case showing off all the expect macros From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, torvalds@linux-foundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, these macros are only really documented near the bottom of https://www.kernel.org/doc/html/latest/dev-tools/kunit/api/test.html#c.KUNI= T_FAIL. E.g. it's likely someone might just not realize that KUNIT_EXPECT_STREQ() exists and instead use KUNIT_EXPECT_FALSE(strcmp()) or similar. This can also serve as a basic smoketest that the KUnit assert machinery still works for all the macros. Signed-off-by: Daniel Latypov Reviewed-by: Brendan Higgins Reviewed-by: David Gow --- lib/kunit/kunit-example-test.c | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 51099b0ca29c..182a64c12541 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -69,6 +69,51 @@ static void example_mark_skipped_test(struct kunit *test) /* This line should run */ kunit_info(test, "You should see this line."); } + +/* + * This test shows off all the KUNIT_EXPECT macros. + */ +static void example_all_expect_macros_test(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, true); + KUNIT_EXPECT_FALSE(test, false); + + KUNIT_EXPECT_EQ(test, 1, 1); + KUNIT_EXPECT_GE(test, 1, 1); + KUNIT_EXPECT_LE(test, 1, 1); + KUNIT_EXPECT_NE(test, 1, 0); + KUNIT_EXPECT_GT(test, 1, 0); + KUNIT_EXPECT_LT(test, 0, 1); + + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test); + KUNIT_EXPECT_PTR_EQ(test, NULL, NULL); + KUNIT_EXPECT_PTR_NE(test, test, NULL); + + KUNIT_EXPECT_STREQ(test, "hi", "hi"); + KUNIT_EXPECT_STRNEQ(test, "hi", "bye"); + + /* + * There are also _MSG variants of all of the above that let you include + * additional text on failure. + */ + KUNIT_EXPECT_TRUE_MSG(test, true, "msg"); + KUNIT_EXPECT_FALSE_MSG(test, false, "msg"); + + KUNIT_EXPECT_EQ_MSG(test, 1, 1, "msg"); + KUNIT_EXPECT_GE_MSG(test, 1, 1, "msg"); + KUNIT_EXPECT_LE_MSG(test, 1, 1, "msg"); + KUNIT_EXPECT_NE_MSG(test, 1, 0, "msg"); + KUNIT_EXPECT_GT_MSG(test, 1, 0, "msg"); + KUNIT_EXPECT_LT_MSG(test, 0, 1, "msg"); + + KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, test, "msg"); + KUNIT_EXPECT_PTR_EQ_MSG(test, NULL, NULL, "msg"); + KUNIT_EXPECT_PTR_NE_MSG(test, test, NULL, "msg"); + + KUNIT_EXPECT_STREQ_MSG(test, "hi", "hi", "msg"); + KUNIT_EXPECT_STRNEQ_MSG(test, "hi", "bye", "msg"); +} + /* * Here we make a list of all the test cases we want to add to the test su= ite * below. @@ -83,6 +128,7 @@ static struct kunit_case example_test_cases[] =3D { KUNIT_CASE(example_simple_test), KUNIT_CASE(example_skip_test), KUNIT_CASE(example_mark_skipped_test), + KUNIT_CASE(example_all_expect_macros_test), {} }; =20 --=20 2.34.1.575.g55b058a8bb-goog From nobody Tue Jun 30 21:22:29 2026 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 64688C433FE for ; Sat, 8 Jan 2022 01:23:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232262AbiAHBXd (ORCPT ); Fri, 7 Jan 2022 20:23:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232172AbiAHBX3 (ORCPT ); Fri, 7 Jan 2022 20:23:29 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 542B8C061574 for ; Fri, 7 Jan 2022 17:23:29 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id c132-20020a251c8a000000b0060c39ab7d36so15488152ybc.12 for ; Fri, 07 Jan 2022 17:23:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=xQZ4UQZIi2wKhWeNCsnm8irdkx9el1/PCTUWisxw+OQ=; b=JNKHAd9yjJBMtJUQ4LClV4CcQhS2izIKEFjaYRH6X+VvJ1d3rzgOwzhakZtbVTscGK y7Z0IcQ1jG7N5HKrXetukC+fh4uluyNwKI7YeOTV5ESukk+qGXxOBjKk5v9OoekzNNNy lAi9C+wv52tQ6TSC8VKnwFVFAX0ICAYFv8UzBME7602kc9FZUJbK8r7IAYYN0lDDEIjn jrh91XnS68u8s/u37NjOKkUmjGIhI4bxs7/Ga97ZA1DYRxfHwYDif5+ucZYLUY+U8ZfZ XuPG8ale7xrBBudlPkeay3+NQQtXnAi0DQG45OPo9maoPvcfgdRcYfxvIHsC1Kz/11VJ O4Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=xQZ4UQZIi2wKhWeNCsnm8irdkx9el1/PCTUWisxw+OQ=; b=zf4cb1KDwaaU4vvLZz8RHuOcV5wkFBQ0nxgHyP86iCfLGQYTvYcJ5eLonYz8613lcl Xdzv6PH4PMDxS5Yc6+0z4rfFzee7ubWAVT7lvXI8lMqT3eigZR1eSxq41ynkmhn0cJUA QYclMrG7lAPgSbByj/K57wwhp5pSsA2JTkpAGgvuiuPvUxdfDF+KNRo7fIQbyYpvFq7f eS0/uqpUW+0GaUxn5QjlAvanMDOcJU5P32INFW3HjkUafEnHB94KYkl8fAwpNHTD8xh7 ZRqlK4KQZAW9ckIL+ogYlluD55rxSUnQRCfnj7AJMaDsLdb2wfvm4eZ4j2iB840iV28C cAYA== X-Gm-Message-State: AOAM530L03z+TDlzwBdxmCHcjVjv41RgWbcLy29IoGzk3NTkwQ6oaJJB qu3AvqF5ZWImVywxaPdLJFNF6S9WGfPieQ== X-Google-Smtp-Source: ABdhPJxMFgpNQJ28897IdObu3t1ppk49x/Uqzz+z4ZzUqGeH02YVRlaDgV9OrKEB/I0UL8dvVVvanAudrF781A== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:bf8a:7e90:eb70:9b2b]) (user=dlatypov job=sendgmr) by 2002:a25:2e4c:: with SMTP id b12mr70062196ybn.569.1641605008613; Fri, 07 Jan 2022 17:23:28 -0800 (PST) Date: Fri, 7 Jan 2022 17:23:00 -0800 In-Reply-To: <20220108012304.1049587-1-dlatypov@google.com> Message-Id: <20220108012304.1049587-3-dlatypov@google.com> Mime-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH 2/6] kunit: move check if assertion passed into the macros From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, torvalds@linux-foundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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) =20 -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, ...); =20 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) d= o { \ - struct assert_class __assertion =3D INITIALIZER; \ - kunit_do_assertion(test, \ - &__assertion.assert, \ - pass, \ - fmt, \ - ##__VA_ARGS__); \ + if (!(pass)) { \ + struct assert_class __assertion =3D INITIALIZER; \ + kunit_failed_assertion(test, \ + &__assertion.assert, \ + fmt, \ + ##__VA_ARGS__); \ + } \ } while (0) =20 =20 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"); } =20 -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); =20 assert->message.fmt =3D fmt; @@ -297,7 +292,7 @@ void kunit_do_assertion(struct kunit *test, if (assert->type =3D=3D KUNIT_ASSERTION) kunit_abort(test); } -EXPORT_SYMBOL_GPL(kunit_do_assertion); +EXPORT_SYMBOL_GPL(kunit_failed_assertion); =20 void kunit_init_test(struct kunit *test, const char *name, char *log) { --=20 2.34.1.575.g55b058a8bb-goog From nobody Tue Jun 30 21:22:29 2026 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 2F14AC433F5 for ; Sat, 8 Jan 2022 01:23:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232307AbiAHBXg (ORCPT ); Fri, 7 Jan 2022 20:23:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232180AbiAHBXb (ORCPT ); Fri, 7 Jan 2022 20:23:31 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FDC0C06173F for ; Fri, 7 Jan 2022 17:23:31 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id c5-20020a25f305000000b0060bbdf755f7so15394551ybs.14 for ; Fri, 07 Jan 2022 17:23:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=pVN2Fx9XGwqMqyXiMDiR9EhkLf7LsMfEMRy4//JUQzk=; b=gVllR1aATpJPFgR32dYfQ3v/CWR84qq2bE6jixkdRt7rxKre/8PoombLazVkLHM3zQ 6zWOOe9vUA8aoFqNksNFDvIT22bSpBruZvOBajuRN7f2KLEI0Z4mNvbF1k/Z+btb1d7M MbPvooD5YJS3WDh5sPSa5H3MbkNnHnLJEZT0PdGxJdT84QSji+iNr665zw2ZPwxwmvEA UG7ifioPP5E8/a2TEvy7tr9IdG1U5SSk71oHz1eHzBUm5n+VFcj5FKU620KXYuqClIsi 66hZ0J8FebxX5OuWzq/wLyqsekhhzyLMLPuBS7g1i7GxCP2qSU1PWPR0cs7CwHKEO/J8 eOJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=pVN2Fx9XGwqMqyXiMDiR9EhkLf7LsMfEMRy4//JUQzk=; b=4WgjaDVN4zFRIpfZianPmErhSiF/ut00nUcWYeLlPjw3+U+XI1F7Bv8Pd0GIY+Yq2R q1Kby39CcGjYRLHWahQN93alPP23Llzg5WdzXtjwTFLmkw4ofifbpYzBJWtY2D+fR7bg P0wqlB8A9gGMuPPJKSJMPulvjkPqp6gB03baWI/UkvC2FnK9BWj1xP7/QBTPHd68oUKi poaj5jWxDJOtaJjJlTf6a2+iQZbAz5H5c+2QczqnC8zRNEktPmPDY5epuVFD2WWbHns9 R1nfTYO+si5s5i3hMFPy9/O1o/BJn6tP3ikeoIHPMS8pM3ERgwnf823t9XQM5x8Qo2nB 3+WQ== X-Gm-Message-State: AOAM533zckHaeCWbmgjpXvA1vLIVq706zQ46auRUWdouI2KXIYJhtUv2 +w/dfHWwCQZUbUph2G1CFXzObyoXuHjsmw== X-Google-Smtp-Source: ABdhPJzJ+Ye8ZlMTssCeQKX+wrtnd+Dpg1Twtowc5dvnIKUKPQfEM0sQjzHz6LB7RAX/qyoumdMNOX9Vab4qkg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:bf8a:7e90:eb70:9b2b]) (user=dlatypov job=sendgmr) by 2002:a25:d142:: with SMTP id i63mr75437812ybg.253.1641605010903; Fri, 07 Jan 2022 17:23:30 -0800 (PST) Date: Fri, 7 Jan 2022 17:23:01 -0800 In-Reply-To: <20220108012304.1049587-1-dlatypov@google.com> Message-Id: <20220108012304.1049587-4-dlatypov@google.com> Mime-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH 3/6] kunit: drop unused kunit* field in kunit_assert From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, torvalds@linux-foundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The `struct kunit* test` field in kunit_assert is unused. Note: we have access to `test` where we need it via the string_stream object. I assume `test` in `kunit_assert` predates this and was leftover after some refactoring. This patch removes the field and cleans up the macros to avoid needlessly passing around `test`. Signed-off-by: Daniel Latypov --- include/kunit/assert.h | 45 ++++++++++++------------------------------ include/kunit/test.h | 14 +++++-------- 2 files changed, 18 insertions(+), 41 deletions(-) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index ad889b539ab3..3da6c792496c 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -30,7 +30,6 @@ enum kunit_assert_type { =20 /** * struct kunit_assert - Data for printing a failed assertion or expectati= on. - * @test: the test case this expectation/assertion is associated with. * @type: the type (either an expectation or an assertion) of this kunit_a= ssert. * @line: the source code line number that the expectation/assertion is at. * @file: the file path of the source file that the expectation/assertion = is in. @@ -41,7 +40,6 @@ enum kunit_assert_type { * format a string to a user reporting the failure. */ struct kunit_assert { - struct kunit *test; enum kunit_assert_type type; int line; const char *file; @@ -60,14 +58,12 @@ struct kunit_assert { =20 /** * KUNIT_INIT_ASSERT_STRUCT() - Initializer for a &struct kunit_assert. - * @kunit: The test case that this expectation/assertion is associated wit= h. * @assert_type: The type (assertion or expectation) of this kunit_assert. * @fmt: The formatting function which builds a string out of this kunit_a= ssert. * * The base initializer for a &struct kunit_assert. */ -#define KUNIT_INIT_ASSERT_STRUCT(kunit, assert_type, fmt) { \ - .test =3D kunit, \ +#define KUNIT_INIT_ASSERT_STRUCT(assert_type, fmt) { \ .type =3D assert_type, \ .file =3D __FILE__, \ .line =3D __LINE__, \ @@ -96,15 +92,13 @@ void kunit_fail_assert_format(const struct kunit_assert= *assert, =20 /** * KUNIT_INIT_FAIL_ASSERT_STRUCT() - Initializer for &struct kunit_fail_as= sert. - * @test: The test case that this expectation/assertion is associated with. * @type: The type (assertion or expectation) of this kunit_assert. * * Initializes a &struct kunit_fail_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_FAIL_ASSERT_STRUCT(test, type) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(test, \ - type, \ +#define KUNIT_INIT_FAIL_ASSERT_STRUCT(type) { \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ kunit_fail_assert_format) \ } =20 @@ -129,7 +123,6 @@ void kunit_unary_assert_format(const struct kunit_asser= t *assert, =20 /** * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_asse= rt. - * @test: The test case that this expectation/assertion is associated with. * @type: The type (assertion or expectation) of this kunit_assert. * @cond: A string representation of the expression asserted true or false. * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwi= se. @@ -137,9 +130,8 @@ void kunit_unary_assert_format(const struct kunit_asser= t *assert, * Initializes a &struct kunit_unary_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_UNARY_ASSERT_STRUCT(test, type, cond, expect_true) { = \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(test, \ - type, \ +#define KUNIT_INIT_UNARY_ASSERT_STRUCT(type, cond, expect_true) { \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ kunit_unary_assert_format), \ .condition =3D cond, \ .expected_true =3D expect_true \ @@ -167,7 +159,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit= _assert *assert, /** * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a * &struct kunit_ptr_not_err_assert. - * @test: The test case that this expectation/assertion is associated with. * @type: The type (assertion or expectation) of this kunit_assert. * @txt: A string representation of the expression passed to the expectati= on. * @val: The actual evaluated pointer value of the expression. @@ -175,9 +166,8 @@ void kunit_ptr_not_err_assert_format(const struct kunit= _assert *assert, * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, type, txt, val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(test, \ - type, \ +#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(type, txt, val) { \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ kunit_ptr_not_err_assert_format), \ .text =3D txt, \ .value =3D val \ @@ -212,7 +202,6 @@ void kunit_binary_assert_format(const struct kunit_asse= rt *assert, /** * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a * &struct kunit_binary_assert. - * @test: The test case that this expectation/assertion is associated with. * @type: The type (assertion or expectation) of this kunit_assert. * @op_str: A string representation of the comparison operator (e.g. "=3D= =3D"). * @left_str: A string representation of the expression in the left slot. @@ -223,15 +212,13 @@ void kunit_binary_assert_format(const struct kunit_as= sert *assert, * Initializes a &struct kunit_binary_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_BINARY_ASSERT_STRUCT(test, \ - type, \ +#define KUNIT_INIT_BINARY_ASSERT_STRUCT(type, \ op_str, \ left_str, \ left_val, \ right_str, \ right_val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(test, \ - type, \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ kunit_binary_assert_format), \ .operation =3D op_str, \ .left_text =3D left_str, \ @@ -269,7 +256,6 @@ void kunit_binary_ptr_assert_format(const struct kunit_= assert *assert, /** * KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT() - Initializes a * &struct kunit_binary_ptr_assert. - * @test: The test case that this expectation/assertion is associated with. * @type: The type (assertion or expectation) of this kunit_assert. * @op_str: A string representation of the comparison operator (e.g. "=3D= =3D"). * @left_str: A string representation of the expression in the left slot. @@ -280,15 +266,13 @@ void kunit_binary_ptr_assert_format(const struct kuni= t_assert *assert, * Initializes a &struct kunit_binary_ptr_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT(test, \ - type, \ +#define KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT(type, \ op_str, \ left_str, \ left_val, \ right_str, \ right_val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(test, \ - type, \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ kunit_binary_ptr_assert_format), \ .operation =3D op_str, \ .left_text =3D left_str, \ @@ -326,7 +310,6 @@ void kunit_binary_str_assert_format(const struct kunit_= assert *assert, /** * KUNIT_INIT_BINARY_STR_ASSERT_STRUCT() - Initializes a * &struct kunit_binary_str_assert. - * @test: The test case that this expectation/assertion is associated with. * @type: The type (assertion or expectation) of this kunit_assert. * @op_str: A string representation of the comparison operator (e.g. "=3D= =3D"). * @left_str: A string representation of the expression in the left slot. @@ -337,15 +320,13 @@ void kunit_binary_str_assert_format(const struct kuni= t_assert *assert, * Initializes a &struct kunit_binary_str_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \ - type, \ +#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(type, \ op_str, \ left_str, \ left_val, \ right_str, \ right_val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(test, \ - type, \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ kunit_binary_str_assert_format), \ .operation =3D op_str, \ .left_text =3D left_str, \ diff --git a/include/kunit/test.h b/include/kunit/test.h index 690a28dfc795..ebd45593321e 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -789,7 +789,7 @@ void kunit_failed_assertion(struct kunit *test, KUNIT_ASSERTION(test, \ false, \ kunit_fail_assert, \ - KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \ + KUNIT_INIT_FAIL_ASSERT_STRUCT(assert_type), \ fmt, \ ##__VA_ARGS__) =20 @@ -819,8 +819,7 @@ void kunit_failed_assertion(struct kunit *test, KUNIT_ASSERTION(test, \ !!(condition) =3D=3D !!expected_true, \ kunit_unary_assert, \ - KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \ - assert_type, \ + KUNIT_INIT_UNARY_ASSERT_STRUCT(assert_type, \ #condition, \ expected_true), \ fmt, \ @@ -878,8 +877,7 @@ do { \ KUNIT_ASSERTION(test, \ __left op __right, \ assert_class, \ - ASSERT_CLASS_INIT(test, \ - assert_type, \ + ASSERT_CLASS_INIT(assert_type, \ #op, \ #left, \ __left, \ @@ -1233,8 +1231,7 @@ do { \ KUNIT_ASSERTION(test, \ strcmp(__left, __right) op 0, \ kunit_binary_str_assert, \ - KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \ - assert_type, \ + KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(assert_type, \ #op, \ #left, \ __left, \ @@ -1293,8 +1290,7 @@ do { \ KUNIT_ASSERTION(test, \ !IS_ERR_OR_NULL(__ptr), \ kunit_ptr_not_err_assert, \ - KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \ - assert_type, \ + KUNIT_INIT_PTR_NOT_ERR_STRUCT(assert_type, \ #ptr, \ __ptr), \ fmt, \ --=20 2.34.1.575.g55b058a8bb-goog From nobody Tue Jun 30 21:22:29 2026 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 D3F76C433F5 for ; Sat, 8 Jan 2022 01:23:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232291AbiAHBXk (ORCPT ); Fri, 7 Jan 2022 20:23:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232266AbiAHBXe (ORCPT ); Fri, 7 Jan 2022 20:23:34 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B840CC06173E for ; Fri, 7 Jan 2022 17:23:33 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id e137-20020a25378f000000b0060c1f2f4939so15407379yba.3 for ; Fri, 07 Jan 2022 17:23:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=UxodAG8MISvgfTavxEJvx2vxa+QsUah/4xKUKf+ZKLE=; b=cXyd0tcGSWGi3eEvbdkhqYFC+pqvi9eGmVjmvAchBIkbSxdYDtOaT0XDzF1BufPKv7 eMwDSBnKMPhZaXaBncMS1cy2yvxBNIDaeTqXpOkSZvuS1LfydrbShaAD/IBz1OFro0F6 PR4P2kAWCZVOz77uZaO0WHg1XiMMdAqq03ElAvZYs/q8jgEWjmKRG1Mr3gnj/qUlFOeU imLmvR6lO+VXwyYKv5smiwbZANQcQyUPtjdSNmGo2AundnDijCnuTfP6j5fsKAHsqJsg xcHi/LKB4K9uTy8YNifsgBvJ5f/rxQKExp/+AT7VQaViI4cOoEFyAaFhXt+i/pusDzhn /81Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=UxodAG8MISvgfTavxEJvx2vxa+QsUah/4xKUKf+ZKLE=; b=FZjaPT5uzacLJ+83Ljgs2bH/0EQa1CqRFUNJaVqyL24bEM3h/ZfehK4hHCOm7VqRoR 9C3arEP4FvP2ONrICQcQk0RYSUo4mmr2Crfte8gziu2nHUmg7ijH86BR3MuPykc4N5ur RF8sHKRbS4bHhVcqGRsGTl4iMooug7CXLIMecr0bj1Q/D0Br/lQsseu/30Vb9cwRErs/ /aMrgG2lXBmwB6jE8M1+dC8leBuy6l46thyJ97maY512ldOz9pbH7Tg0DmdkAOSL5gd3 Hw3MFUGqL8y4TlwwowlRnM50DLgpaXkVIkgZeeHhNV26cEdxskgdTzqV6b9rr0g20jS8 TReg== X-Gm-Message-State: AOAM530FfCqO4XBX559Jx7RidM6c8SuD7yasQAn5fNZooujER4DIJIp4 6eJlw1FV/gmxNRtvphsMLb2OHDdZbBnZog== X-Google-Smtp-Source: ABdhPJyGBj0i2YNBRxNrlnRCJuJAw7ad2nmp13PefOy5E6/jJafSsI8a5/HgOa5T5LfQrUs04k0oNSkNwBNl4Q== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:bf8a:7e90:eb70:9b2b]) (user=dlatypov job=sendgmr) by 2002:a25:740a:: with SMTP id p10mr76384062ybc.516.1641605013013; Fri, 07 Jan 2022 17:23:33 -0800 (PST) Date: Fri, 7 Jan 2022 17:23:02 -0800 In-Reply-To: <20220108012304.1049587-1-dlatypov@google.com> Message-Id: <20220108012304.1049587-5-dlatypov@google.com> Mime-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH 4/6] kunit: factor out kunit_base_assert_format() call into kunit_fail() From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, torvalds@linux-foundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We call this function first thing for all the assertion `format()` functions. This is the part that prints the file and line number and assertion type (EXPECTATION, ASSERTION). Having it as part of the format functions lets us have the flexibility to not print that information (or print it differently) for new assertion types, but I think this we don't need that. And in the future, we'd like to consider factoring that data (file, line#, type) out of the kunit_assert struct and into a `static` variable, as Linus suggested [1], so we'd need to extract it anyways. [1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ Signed-off-by: Daniel Latypov --- lib/kunit/assert.c | 6 ------ lib/kunit/test.c | 1 + 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index b972bda61c0c..4d9a1295efc7 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -40,7 +40,6 @@ EXPORT_SYMBOL_GPL(kunit_assert_print_msg); void kunit_fail_assert_format(const struct kunit_assert *assert, struct string_stream *stream) { - kunit_base_assert_format(assert, stream); string_stream_add(stream, "%pV", &assert->message); } EXPORT_SYMBOL_GPL(kunit_fail_assert_format); @@ -52,7 +51,6 @@ void kunit_unary_assert_format(const struct kunit_assert = *assert, =20 unary_assert =3D container_of(assert, struct kunit_unary_assert, assert); =20 - kunit_base_assert_format(assert, stream); if (unary_assert->expected_true) string_stream_add(stream, KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n", @@ -73,7 +71,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit_a= ssert *assert, ptr_assert =3D container_of(assert, struct kunit_ptr_not_err_assert, assert); =20 - kunit_base_assert_format(assert, stream); if (!ptr_assert->value) { string_stream_add(stream, KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n", @@ -119,7 +116,6 @@ void kunit_binary_assert_format(const struct kunit_asse= rt *assert, binary_assert =3D container_of(assert, struct kunit_binary_assert, assert); =20 - kunit_base_assert_format(assert, stream); string_stream_add(stream, KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", binary_assert->left_text, @@ -147,7 +143,6 @@ void kunit_binary_ptr_assert_format(const struct kunit_= assert *assert, binary_assert =3D container_of(assert, struct kunit_binary_ptr_assert, assert); =20 - kunit_base_assert_format(assert, stream); string_stream_add(stream, KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", binary_assert->left_text, @@ -187,7 +182,6 @@ void kunit_binary_str_assert_format(const struct kunit_= assert *assert, binary_assert =3D container_of(assert, struct kunit_binary_str_assert, assert); =20 - kunit_base_assert_format(assert, stream); string_stream_add(stream, KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", binary_assert->left_text, diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 5ad671745483..735c1b67d843 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -255,6 +255,7 @@ static void kunit_fail(struct kunit *test, struct kunit= _assert *assert) return; } =20 + kunit_base_assert_format(assert, stream); assert->format(assert, stream); =20 kunit_print_string_stream(test, stream); --=20 2.34.1.575.g55b058a8bb-goog From nobody Tue Jun 30 21:22:29 2026 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 BEB11C433EF for ; Sat, 8 Jan 2022 01:23:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232345AbiAHBXm (ORCPT ); Fri, 7 Jan 2022 20:23:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232306AbiAHBXg (ORCPT ); Fri, 7 Jan 2022 20:23:36 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3C4AC061574 for ; Fri, 7 Jan 2022 17:23:35 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id w35-20020a25ac23000000b006106b0711f2so7712506ybi.23 for ; Fri, 07 Jan 2022 17:23:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Zl3tYUGZQ69Xabi7Vl+AmAQkNx7RzOfesWcbmkVMLC0=; b=ir0flGO7cqrT+By+EKhPJd20fm00kXlllVFU+soUpFIn6IqY/4bDgJ9NmeoyOFyGMX KkIPjNCKZcQKtV06ZLBdhJXaGZEEK6jMZW/f20fXsgDbyydiog5QBRK+CRfO5FMXB52E QWNO3oi+FWiUFA6AdZWiMPzBsmDmxwQHGO0nSNF2B8WGgNOoIXCHTj3XjNWH/rS+EV0c HVjH7OC4Huv4BQOjGezbxecIqHHMye9Yocd/8MTpQ+TDAWLkbZkR5Zc4+hck4al90Q4p yHL7xrInFqX3AN+H1l4sxBb6JyPiyM8bbvE5EBE8SB4vJdGQ2rqKBjVEs1tgKzuZEcgm smrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Zl3tYUGZQ69Xabi7Vl+AmAQkNx7RzOfesWcbmkVMLC0=; b=uVXCxAoV1FCKsB3wAGxAxcDpx6oTP8q/UNufx7MvmTfddzIbzXl7Q8bXnhDuS6scFF nxnqUT5AZL8oUH6Gw51rhamaDvzdnEaXR0+NDNsZqVK/t63RtdZH44fKZBJfysqhGeu7 Q3Ghjs+YmtYXzbSq+1mLEmGk5cvpcsLAp87Bpm/KbUOV08dc5cXv1v0tvGs6ahggCu4C yf99v+HEtDEYg/NhoU67QP659wgvoJKj0lnEprfINUnmYGlv5FQg9tBdinYAiuq8XxND 69QlECCkvYsoasScQkaNQzF5CiT75Pm6TP41Qm7dUdPFGVhkldNvqmDoaAG9V1IfbjKn K7Yw== X-Gm-Message-State: AOAM533cN/R8XWjZ2QQ9yy9V5wx75Ik9Qz7NlYqymbG2N2g3iKjmgbHU TKQPnodbwB4gwPt/gttDn5P5XgV0Pk01GA== X-Google-Smtp-Source: ABdhPJxhgH/z/ZwRJwL4bRP4xb8fEBdVqhdaTg9EVoCmk/1ZcrubjfB3aXrl8dmxTnklc+7BnKr0gWOqzEyUDQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:bf8a:7e90:eb70:9b2b]) (user=dlatypov job=sendgmr) by 2002:a25:403:: with SMTP id 3mr68628764ybe.696.1641605015212; Fri, 07 Jan 2022 17:23:35 -0800 (PST) Date: Fri, 7 Jan 2022 17:23:03 -0800 In-Reply-To: <20220108012304.1049587-1-dlatypov@google.com> Message-Id: <20220108012304.1049587-6-dlatypov@google.com> Mime-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH 5/6] kunit: split out part of kunit_assert into a static const From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, torvalds@linux-foundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This is per Linus's suggestion in [1]. The issue there is that every KUNIT_EXPECT/KUNIT_ASSERT puts a kunit_assert object onto the stack. Normally we rely on compilers to elide this, but when that doesn't work out, this blows up the stack usage of kunit test functions. We can move some data off the stack by making it static. This change introduces a new `struct kunit_loc` to hold the file and line number and then just passing assert_type (EXPECT or ASSERT) as an argument. In [1], it was suggested to also move out the format string as well, but users could theoretically craft a format string at runtime, so we can't. This change leaves a copy of `assert_type` in kunit_assert for now because cleaning up all the macros to not pass it around is a bit more involved. Here's an example of the expanded code for KUNIT_FAIL(): if (!(false)) { static const struct kunit_loc loc =3D { .file =3D ... }; struct kunit_unary_assert __assertion =3D { .assert =3D { .type ... }; kunit_failed_assertion(test, &loc, &__assertion.assert, ((void *)0)); }; [1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ Signed-off-by: Daniel Latypov Suggested-by: Linus Torvalds --- include/kunit/assert.h | 25 ++++++++++++++++--------- include/kunit/test.h | 12 +++++++++++- lib/kunit/assert.c | 9 +++++---- lib/kunit/test.c | 15 +++++++++------ 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index 3da6c792496c..4f91dbdb886a 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -28,11 +28,21 @@ enum kunit_assert_type { KUNIT_EXPECTATION, }; =20 +/** + * struct kunit_loc - Identifies the source location of a line of code. + * @line: the line number in the file. + * @file: the file name. + */ +struct kunit_loc { + int line; + const char *file; +}; + +#define KUNIT_CURRENT_LOC { .file =3D __FILE__, .line =3D __LINE__ } + /** * struct kunit_assert - Data for printing a failed assertion or expectati= on. * @type: the type (either an expectation or an assertion) of this kunit_a= ssert. - * @line: the source code line number that the expectation/assertion is at. - * @file: the file path of the source file that the expectation/assertion = is in. * @message: an optional message to provide additional context. * @format: a function which formats the data in this kunit_assert to a st= ring. * @@ -40,9 +50,7 @@ enum kunit_assert_type { * format a string to a user reporting the failure. */ struct kunit_assert { - enum kunit_assert_type type; - int line; - const char *file; + enum kunit_assert_type type; // TODO(dlatypov@google.com): delete this struct va_format message; void (*format)(const struct kunit_assert *assert, struct string_stream *stream); @@ -65,14 +73,13 @@ struct kunit_assert { */ #define KUNIT_INIT_ASSERT_STRUCT(assert_type, fmt) { \ .type =3D assert_type, \ - .file =3D __FILE__, \ - .line =3D __LINE__, \ .message =3D KUNIT_INIT_VA_FMT_NULL, \ .format =3D fmt \ } =20 -void kunit_base_assert_format(const struct kunit_assert *assert, - struct string_stream *stream); +void kunit_assert_prologue(const struct kunit_loc *loc, + enum kunit_assert_type type, + struct string_stream *stream); =20 void kunit_assert_print_msg(const struct kunit_assert *assert, struct string_stream *stream); diff --git a/include/kunit/test.h b/include/kunit/test.h index ebd45593321e..6e201b45ada6 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -771,13 +771,18 @@ void __printf(2, 3) kunit_log_append(char *log, const= char *fmt, ...); #define KUNIT_SUCCEED(test) do {} while (0) =20 void kunit_failed_assertion(struct kunit *test, + const struct kunit_loc *loc, + enum kunit_assert_type type, struct kunit_assert *assert, const char *fmt, ...); =20 -#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) d= o { \ +#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER= , fmt, ...) do { \ if (!(pass)) { \ + static const struct kunit_loc loc =3D KUNIT_CURRENT_LOC; \ struct assert_class __assertion =3D INITIALIZER; \ kunit_failed_assertion(test, \ + &loc, \ + assert_type, \ &__assertion.assert, \ fmt, \ ##__VA_ARGS__); \ @@ -787,6 +792,7 @@ void kunit_failed_assertion(struct kunit *test, =20 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ KUNIT_ASSERTION(test, \ + assert_type, \ false, \ kunit_fail_assert, \ KUNIT_INIT_FAIL_ASSERT_STRUCT(assert_type), \ @@ -817,6 +823,7 @@ void kunit_failed_assertion(struct kunit *test, fmt, \ ...) \ KUNIT_ASSERTION(test, \ + assert_type, \ !!(condition) =3D=3D !!expected_true, \ kunit_unary_assert, \ KUNIT_INIT_UNARY_ASSERT_STRUCT(assert_type, \ @@ -875,6 +882,7 @@ do { \ typeof(right) __right =3D (right); \ \ KUNIT_ASSERTION(test, \ + assert_type, \ __left op __right, \ assert_class, \ ASSERT_CLASS_INIT(assert_type, \ @@ -1229,6 +1237,7 @@ do { \ const char *__right =3D (right); \ \ KUNIT_ASSERTION(test, \ + assert_type, \ strcmp(__left, __right) op 0, \ kunit_binary_str_assert, \ KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(assert_type, \ @@ -1288,6 +1297,7 @@ do { \ typeof(ptr) __ptr =3D (ptr); \ \ KUNIT_ASSERTION(test, \ + assert_type, \ !IS_ERR_OR_NULL(__ptr), \ kunit_ptr_not_err_assert, \ KUNIT_INIT_PTR_NOT_ERR_STRUCT(assert_type, \ diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 4d9a1295efc7..9f4492a8e24e 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -10,12 +10,13 @@ =20 #include "string-stream.h" =20 -void kunit_base_assert_format(const struct kunit_assert *assert, +void kunit_assert_prologue(const struct kunit_loc *loc, + enum kunit_assert_type type, struct string_stream *stream) { const char *expect_or_assert =3D NULL; =20 - switch (assert->type) { + switch (type) { case KUNIT_EXPECTATION: expect_or_assert =3D "EXPECTATION"; break; @@ -25,9 +26,9 @@ void kunit_base_assert_format(const struct kunit_assert *= assert, } =20 string_stream_add(stream, "%s FAILED at %s:%d\n", - expect_or_assert, assert->file, assert->line); + expect_or_assert, loc->file, loc->line); } -EXPORT_SYMBOL_GPL(kunit_base_assert_format); +EXPORT_SYMBOL_GPL(kunit_assert_prologue); =20 void kunit_assert_print_msg(const struct kunit_assert *assert, struct string_stream *stream) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 735c1b67d843..3108ed0575d4 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -240,7 +240,8 @@ static void kunit_print_string_stream(struct kunit *tes= t, } } =20 -static void kunit_fail(struct kunit *test, struct kunit_assert *assert) +static void kunit_fail(struct kunit *test, const struct kunit_loc *loc, + enum kunit_assert_type type, struct kunit_assert *assert) { struct string_stream *stream; =20 @@ -250,12 +251,12 @@ static void kunit_fail(struct kunit *test, struct kun= it_assert *assert) if (!stream) { WARN(true, "Could not allocate stream to print failed assertion in %s:%d\n", - assert->file, - assert->line); + loc->file, + loc->line); return; } =20 - kunit_base_assert_format(assert, stream); + kunit_assert_prologue(loc, type, stream); assert->format(assert, stream); =20 kunit_print_string_stream(test, stream); @@ -277,6 +278,8 @@ static void __noreturn kunit_abort(struct kunit *test) } =20 void kunit_failed_assertion(struct kunit *test, + const struct kunit_loc *loc, + enum kunit_assert_type type, struct kunit_assert *assert, const char *fmt, ...) { @@ -286,11 +289,11 @@ void kunit_failed_assertion(struct kunit *test, assert->message.fmt =3D fmt; assert->message.va =3D &args; =20 - kunit_fail(test, assert); + kunit_fail(test, loc, type, assert); =20 va_end(args); =20 - if (assert->type =3D=3D KUNIT_ASSERTION) + if (type =3D=3D KUNIT_ASSERTION) kunit_abort(test); } EXPORT_SYMBOL_GPL(kunit_failed_assertion); --=20 2.34.1.575.g55b058a8bb-goog From nobody Tue Jun 30 21:22:29 2026 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 66968C433EF for ; Sat, 8 Jan 2022 01:23:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232403AbiAHBXq (ORCPT ); Fri, 7 Jan 2022 20:23:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232274AbiAHBXi (ORCPT ); Fri, 7 Jan 2022 20:23:38 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69F18C06173F for ; Fri, 7 Jan 2022 17:23:38 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id g130-20020a255288000000b0060ba07af29eso15508818ybb.2 for ; Fri, 07 Jan 2022 17:23:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Dc+808Dilm2Mq6arqiuNuO0CRm0J7aU0w6eBUzu4ges=; b=sI+l1fnPclAmmEhwO1yvzZqul3KVslKQ0Jcuryy5qJsrKDn1g7uxmx9aCYS+0MJuTj idE+6fFMEb9F4rP63fiDrgKbVwm1Sbmw0C44QtruV9mFVLpfePlv7mn8xXQR4iKMqUWH n8HbBGgLx0xVZoEJJPvdjUYgJZXmI/33IWwYikm7cODz4xp+0/nWAAFcwQnQSmdYYb/G Se0I26UjhDarjjJaXPcAQ/vDYI4kGAb7XgueJDJzQC2yk93ummZvIvdGI2FJajzsOo5B zE5ZY3XVOkHS961yvthaRYsKmMkgOD3SZvbwnRymr9NG1fyTnmqO2QcOv7x+Xzauuoa3 sHuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Dc+808Dilm2Mq6arqiuNuO0CRm0J7aU0w6eBUzu4ges=; b=6sEpw8mDtevK42cbSbSkEdnDoFKXr/e87qcsr5a2ec2GxuRL3WHh7Zfr42fGDUvyNs KDJoPrgHmURRxw1ndIg5MfYd5YTK7UKa1baevEt1IUkGxg18w2rIjGU5We721q8mllrs o+HQ77tpAgAl08suEXkQ7v3qnOXgqoOXjezXz10N7BgqBACJk1a37WlbtP3rsRfDI/ag JqDM3r1pyZaUb2/lEJKkG2azY3CDmLqk9g1AKhb67zdKVJT81tJOqXnw3jmQNztoH9h7 Aq62v9CMOTUAg6vAWIWvxgp39CJkolouBjhEU+8VinTNAtbLCNKUIr6G6utlzJygcSz/ YBEg== X-Gm-Message-State: AOAM531wIrCMDjP9KJneqUYmBddigK0PldId5p7MylQrOODw9gSoR/K3 EvsB/25oBcVwGZ0kgLM/hCnRc/DHYrOghg== X-Google-Smtp-Source: ABdhPJzoaKV2SUa04VWD6vMFFBi/4s0olIqwbDbaMpVxWqySMaH3oxjOaNK6rOVRMtWxLf6Bf122u/FU7xNQNw== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:bf8a:7e90:eb70:9b2b]) (user=dlatypov job=sendgmr) by 2002:a25:854f:: with SMTP id f15mr74982229ybn.121.1641605017676; Fri, 07 Jan 2022 17:23:37 -0800 (PST) Date: Fri, 7 Jan 2022 17:23:04 -0800 In-Reply-To: <20220108012304.1049587-1-dlatypov@google.com> Message-Id: <20220108012304.1049587-7-dlatypov@google.com> Mime-Version: 1.0 References: <20220108012304.1049587-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH 6/6] kunit: drop unused assert_type from kunit_assert and clean up macros From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, torvalds@linux-foundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This field has been split out from kunit_assert to make the struct less heavy along with the filename and line number. This change drops the assert_type field and cleans up all the macros that were plumbing assert_type into kunit_assert. Signed-off-by: Daniel Latypov --- include/kunit/assert.h | 46 +++++++++++++----------------------------- include/kunit/test.h | 14 +++++-------- 2 files changed, 19 insertions(+), 41 deletions(-) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index 4f91dbdb886a..21299232c120 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -42,7 +42,6 @@ struct kunit_loc { =20 /** * struct kunit_assert - Data for printing a failed assertion or expectati= on. - * @type: the type (either an expectation or an assertion) of this kunit_a= ssert. * @message: an optional message to provide additional context. * @format: a function which formats the data in this kunit_assert to a st= ring. * @@ -50,7 +49,6 @@ struct kunit_loc { * format a string to a user reporting the failure. */ struct kunit_assert { - enum kunit_assert_type type; // TODO(dlatypov@google.com): delete this struct va_format message; void (*format)(const struct kunit_assert *assert, struct string_stream *stream); @@ -66,13 +64,11 @@ struct kunit_assert { =20 /** * KUNIT_INIT_ASSERT_STRUCT() - Initializer for a &struct kunit_assert. - * @assert_type: The type (assertion or expectation) of this kunit_assert. * @fmt: The formatting function which builds a string out of this kunit_a= ssert. * * The base initializer for a &struct kunit_assert. */ -#define KUNIT_INIT_ASSERT_STRUCT(assert_type, fmt) { \ - .type =3D assert_type, \ +#define KUNIT_INIT_ASSERT_STRUCT(fmt) { \ .message =3D KUNIT_INIT_VA_FMT_NULL, \ .format =3D fmt \ } @@ -98,15 +94,13 @@ void kunit_fail_assert_format(const struct kunit_assert= *assert, struct string_stream *stream); =20 /** - * KUNIT_INIT_FAIL_ASSERT_STRUCT() - Initializer for &struct kunit_fail_as= sert. - * @type: The type (assertion or expectation) of this kunit_assert. + * KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_asse= rt. * * Initializes a &struct kunit_fail_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_FAIL_ASSERT_STRUCT(type) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ - kunit_fail_assert_format) \ +#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(kunit_fail_assert_format) \ } =20 /** @@ -130,16 +124,14 @@ void kunit_unary_assert_format(const struct kunit_ass= ert *assert, =20 /** * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_asse= rt. - * @type: The type (assertion or expectation) of this kunit_assert. * @cond: A string representation of the expression asserted true or false. * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwi= se. * * Initializes a &struct kunit_unary_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_UNARY_ASSERT_STRUCT(type, cond, expect_true) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ - kunit_unary_assert_format), \ +#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(kunit_unary_assert_format), \ .condition =3D cond, \ .expected_true =3D expect_true \ } @@ -166,16 +158,14 @@ void kunit_ptr_not_err_assert_format(const struct kun= it_assert *assert, /** * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a * &struct kunit_ptr_not_err_assert. - * @type: The type (assertion or expectation) of this kunit_assert. * @txt: A string representation of the expression passed to the expectati= on. * @val: The actual evaluated pointer value of the expression. * * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(type, txt, val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ - kunit_ptr_not_err_assert_format), \ +#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(kunit_ptr_not_err_assert_format), \ .text =3D txt, \ .value =3D val \ } @@ -209,7 +199,6 @@ void kunit_binary_assert_format(const struct kunit_asse= rt *assert, /** * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a * &struct kunit_binary_assert. - * @type: The type (assertion or expectation) of this kunit_assert. * @op_str: A string representation of the comparison operator (e.g. "=3D= =3D"). * @left_str: A string representation of the expression in the left slot. * @left_val: The actual evaluated value of the expression in the left slo= t. @@ -219,14 +208,12 @@ void kunit_binary_assert_format(const struct kunit_as= sert *assert, * Initializes a &struct kunit_binary_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_BINARY_ASSERT_STRUCT(type, \ - op_str, \ +#define KUNIT_INIT_BINARY_ASSERT_STRUCT(op_str, \ left_str, \ left_val, \ right_str, \ right_val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ - kunit_binary_assert_format), \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(kunit_binary_assert_format), \ .operation =3D op_str, \ .left_text =3D left_str, \ .left_value =3D left_val, \ @@ -273,14 +260,12 @@ void kunit_binary_ptr_assert_format(const struct kuni= t_assert *assert, * Initializes a &struct kunit_binary_ptr_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT(type, \ - op_str, \ +#define KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT(op_str, \ left_str, \ left_val, \ right_str, \ right_val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ - kunit_binary_ptr_assert_format), \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(kunit_binary_ptr_assert_format), \ .operation =3D op_str, \ .left_text =3D left_str, \ .left_value =3D left_val, \ @@ -317,7 +302,6 @@ void kunit_binary_str_assert_format(const struct kunit_= assert *assert, /** * KUNIT_INIT_BINARY_STR_ASSERT_STRUCT() - Initializes a * &struct kunit_binary_str_assert. - * @type: The type (assertion or expectation) of this kunit_assert. * @op_str: A string representation of the comparison operator (e.g. "=3D= =3D"). * @left_str: A string representation of the expression in the left slot. * @left_val: The actual evaluated value of the expression in the left slo= t. @@ -327,14 +311,12 @@ void kunit_binary_str_assert_format(const struct kuni= t_assert *assert, * Initializes a &struct kunit_binary_str_assert. Intended to be used in * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ -#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(type, \ - op_str, \ +#define KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(op_str, \ left_str, \ left_val, \ right_str, \ right_val) { \ - .assert =3D KUNIT_INIT_ASSERT_STRUCT(type, \ - kunit_binary_str_assert_format), \ + .assert =3D KUNIT_INIT_ASSERT_STRUCT(kunit_binary_str_assert_format), \ .operation =3D op_str, \ .left_text =3D left_str, \ .left_value =3D left_val, \ diff --git a/include/kunit/test.h b/include/kunit/test.h index 6e201b45ada6..6f9074ec1995 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -795,7 +795,7 @@ void kunit_failed_assertion(struct kunit *test, assert_type, \ false, \ kunit_fail_assert, \ - KUNIT_INIT_FAIL_ASSERT_STRUCT(assert_type), \ + KUNIT_INIT_FAIL_ASSERT_STRUCT, \ fmt, \ ##__VA_ARGS__) =20 @@ -826,8 +826,7 @@ void kunit_failed_assertion(struct kunit *test, assert_type, \ !!(condition) =3D=3D !!expected_true, \ kunit_unary_assert, \ - KUNIT_INIT_UNARY_ASSERT_STRUCT(assert_type, \ - #condition, \ + KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ expected_true), \ fmt, \ ##__VA_ARGS__) @@ -885,8 +884,7 @@ do { \ assert_type, \ __left op __right, \ assert_class, \ - ASSERT_CLASS_INIT(assert_type, \ - #op, \ + ASSERT_CLASS_INIT(#op, \ #left, \ __left, \ #right, \ @@ -1240,8 +1238,7 @@ do { \ assert_type, \ strcmp(__left, __right) op 0, \ kunit_binary_str_assert, \ - KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(assert_type, \ - #op, \ + KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(#op, \ #left, \ __left, \ #right, \ @@ -1300,8 +1297,7 @@ do { \ assert_type, \ !IS_ERR_OR_NULL(__ptr), \ kunit_ptr_not_err_assert, \ - KUNIT_INIT_PTR_NOT_ERR_STRUCT(assert_type, \ - #ptr, \ + KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \ __ptr), \ fmt, \ ##__VA_ARGS__); \ --=20 2.34.1.575.g55b058a8bb-goog