From nobody Tue Jun 30 17:47:35 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 AA43AC433FE for ; Tue, 11 Jan 2022 19:42:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237745AbiAKTmo (ORCPT ); Tue, 11 Jan 2022 14:42:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237443AbiAKTml (ORCPT ); Tue, 11 Jan 2022 14:42:41 -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 4CC06C061748 for ; Tue, 11 Jan 2022 11:42:41 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id i129-20020a255487000000b006107b38b495so26878072ybb.16 for ; Tue, 11 Jan 2022 11:42:41 -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=IK/pmS24q6VZVkmGI5V/Gm7usfxnRrjcLFdAJnMZ6jo=; b=kPS3NVMXisQ+S/QiUTo32EZgKyhJGfLV5qbJ/CUrzZeNer1H5zWimCjpTdxSG89Tjk 9dRxuAb/q0Kff2CmnjGBbVOoMGKy7tQMH7gWghOqk8iI76NU34RIk+i+J5AiujsustPr x7DpfQO/TXPs4wNx/3fqlndDE2PdYkaXp4y8sMiZBZ2iMFauWQeFSPaGn7Z4vz8dWh60 3GcqpW7YdE4mNEg+BoWHj84ROx4gpnFC/r3cej87lr/JRDYt47DNJ/2be4N4rqfAW56H nYovJo/oOvJfuKLzjk1ZR5WazDeSlvvcW4ARJAtVilMMhYM+uHhAtXEO1iNwLuim+XsZ WIpQ== 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=IK/pmS24q6VZVkmGI5V/Gm7usfxnRrjcLFdAJnMZ6jo=; b=2HaD4MbLGX6HuSHi2ik7MCZCJyKgpPsFqhzyJx7iOXt0/yMZBXiwGzNXaBBzpTbf+R vPSdjY0l1L9PI3m8wdBhVbd+pKvu9pfF5R2WBy5u22WWJNWldMZfYIC6zr7tJwpI3x5d /6GLWRDk5TebyeJOrn0W4tu7SckvtJJUoxOxO1ZkYlu2Lpbw/Yw/vZxKdN4m7bEThU4P MNBC5E5ZggjqDbbBolUIIsDiXEBVBp8y64VhNq3Du+VZr1tz6k3Tebba5e7lZ8VLo2LR rlOEhiKHmtVIzbNh3IDnf6JM3IoOpmOaITWoilINbnxK8sNdgcjlh5qfHLgfSO9aPTM+ wDEA== X-Gm-Message-State: AOAM531hjGISG0ZN+zQzxU5xQ3bg1ik5xJf5Thscz9BDFTMI9+suc+Tg eC5ytBbDOUyuslGWroHcWPyheBOh40j2Wg== X-Google-Smtp-Source: ABdhPJwX78HJ4lOcr1NG1sNwWGZipmSIxgpMjxDBLYD02itZJXKKzuQQmiZ+DWe0Ps8yNuW9G+qj3USG47dl3A== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:8c76:9de0:1a30:7de8]) (user=dlatypov job=sendgmr) by 2002:a25:360f:: with SMTP id d15mr6124569yba.31.1641930160484; Tue, 11 Jan 2022 11:42:40 -0800 (PST) Date: Tue, 11 Jan 2022 11:42:26 -0800 In-Reply-To: <20220111194231.1797841-1-dlatypov@google.com> Message-Id: <20220111194231.1797841-2-dlatypov@google.com> Mime-Version: 1.0 References: <20220111194231.1797841-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH v2 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 | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 51099b0ca29c..4bbf37c04eba 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -69,6 +69,47 @@ 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 types of KUNIT_EXPECT macros. + */ +static void example_all_expect_macros_test(struct kunit *test) +{ + /* Boolean assertions */ + KUNIT_EXPECT_TRUE(test, true); + KUNIT_EXPECT_FALSE(test, false); + + /* Integer assertions */ + KUNIT_EXPECT_EQ(test, 1, 1); /* check =3D=3D */ + KUNIT_EXPECT_GE(test, 1, 1); /* check >=3D */ + KUNIT_EXPECT_LE(test, 1, 1); /* check <=3D */ + KUNIT_EXPECT_NE(test, 1, 0); /* check !=3D */ + KUNIT_EXPECT_GT(test, 1, 0); /* check > */ + KUNIT_EXPECT_LT(test, 0, 1); /* check < */ + + /* Pointer assertions */ + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test); + KUNIT_EXPECT_PTR_EQ(test, NULL, NULL); + KUNIT_EXPECT_PTR_NE(test, test, NULL); + + /* String assertions */ + KUNIT_EXPECT_STREQ(test, "hi", "hi"); + KUNIT_EXPECT_STRNEQ(test, "hi", "bye"); + + /* + * There are also ASSERT variants of all of the above that abort test + * execution if they fail. Useful for memory allocations, etc. + */ + KUNIT_ASSERT_GT(test, sizeof(char), 0); + + /* + * There are also _MSG variants of all of the above that let you include + * additional text on failure. + */ + KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); + KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); +} + /* * Here we make a list of all the test cases we want to add to the test su= ite * below. @@ -83,6 +124,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 17:47:35 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 8141FC433EF for ; Tue, 11 Jan 2022 19:42:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237868AbiAKTmr (ORCPT ); Tue, 11 Jan 2022 14:42:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237621AbiAKTmn (ORCPT ); Tue, 11 Jan 2022 14:42:43 -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 6A667C06173F for ; Tue, 11 Jan 2022 11:42:43 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 2-20020a251302000000b006118f867dadso2593361ybt.12 for ; Tue, 11 Jan 2022 11:42:43 -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=7m9XYx4AzAS0+JhiVJNvVIbNeaQHgXtmefJJRSWs+Yo=; b=Z8rie0K5w5Y7can5kAE35T/Bx66u7zkzrOaWt9/TqCJJ3Jyc4/cSbGYgwlq61D9X8u kCHXaAxYjSXZ7Lr0fHY7QdaDM13DIT6MwAkYUFxndn474+hpJWHgy1aoprr2W2mw3bXM bHa/FjnIhHIg/efnpDrsRVlZVlCxisG5bDt88BQhHN/KCN7lw0BFdrjCXDYY8aJvr3LW /Wpc39lqrsrsvW4vBC8UM8NMveZcoAT+Jta4OzemN7sVeRoqQ40q0h4OvdhBaWE0kV5F TBZI9cuhUwUCrmYzWDZgI3tNnAPE3pXgRDu0x37AYwqfZPl9yG3Gi6eibKIESDM1UE+u /fXw== 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=7m9XYx4AzAS0+JhiVJNvVIbNeaQHgXtmefJJRSWs+Yo=; b=FcOYGzkRcQxCY80/B8XofbJbBK9EVzr27AknHpJGrM+g3TbRzJ3IxXV7eVi1Py7kqV HCfWg/BUnJ6mAnFz0R5Su88ibpyjH28gBaaDS6wX+jAp13QGc/ydJAxTzV/qmgTavfBQ OaZ9NfJEjBQ1rnh+LcLZVwSVq9Nswnb1mejXdLImTbst417mZYHG+5xwOCRHaBZ5YCTv U9daiNxWSsM/gs/1fZXENZgZqP2uw1A4e2osqgscIWmwJaVMflu19r/ygCWN5qKVbe7N CjfVtUVUvZXMXepneqFmxZ5+aHrEj03/Xt2PTpZ44VJUkawq6smwenfAlOEnQKYbtHRG 9ofg== X-Gm-Message-State: AOAM530+3gtoXNJc8iMzwFV5tTyBBfOVbm9VjlT591zz2kst7OSlie4l 5V952G71O3orFkXrrGTLfVB9XbbFKFrVlg== X-Google-Smtp-Source: ABdhPJy2qjNd99ZaZDSTzdjCMiTY7iugt1d+Mw4DC2/UQM79F+zzLtISUuTSZknSpbZbF4v+JMsruSz5rCsqpA== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:8c76:9de0:1a30:7de8]) (user=dlatypov job=sendgmr) by 2002:a25:d34b:: with SMTP id e72mr8522778ybf.497.1641930162675; Tue, 11 Jan 2022 11:42:42 -0800 (PST) Date: Tue, 11 Jan 2022 11:42:27 -0800 In-Reply-To: <20220111194231.1797841-1-dlatypov@google.com> Message-Id: <20220111194231.1797841-3-dlatypov@google.com> Mime-Version: 1.0 References: <20220111194231.1797841-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH v2 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_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). Signed-off-by: Daniel Latypov Reviewed-by: Brendan Higgins --- include/kunit/test.h | 21 +++++++++++---------- lib/kunit/test.c | 13 ++++--------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index b26400731c02..12cabd15449a 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -12,6 +12,7 @@ #include #include =20 +#include #include #include #include @@ -770,18 +771,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_do_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 (unlikely(!(pass))) { \ + struct assert_class __assertion =3D INITIALIZER; \ + kunit_do_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..3a52c321c280 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_do_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_do_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 17:47:35 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 A2195C4332F for ; Tue, 11 Jan 2022 19:42:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237981AbiAKTmt (ORCPT ); Tue, 11 Jan 2022 14:42:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237785AbiAKTmp (ORCPT ); Tue, 11 Jan 2022 14:42:45 -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 9A362C061756 for ; Tue, 11 Jan 2022 11:42:45 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id h2-20020a5b0a82000000b0061192499188so15792ybq.9 for ; Tue, 11 Jan 2022 11:42:45 -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=Sxvbc7bGwYaMSWzxJGMkvPlmIs+2+EWa68wYWXx+WlQ=; b=kiwFv3ubbdVeoHOyf9A5AC0GiQ3S1Wtf2dWo4fVq8U0qsoqck7oV9f8a860p0sjoQT WdhSKirjVqLL4+z+pzfREj/HF2NOvYXD81oCK7pMTy/HZnR9lxq0pKxKafmasSgsyWAv 9dHsCZtY0PeKcTaQrXGEEtV+xFB+LoUolPH9Q6fUmAnS/MN2i0j19HA2swQWB5NdVJM0 5DhgAOonQ98rGbAPJYOv8m0HGuaK8Lk+59LUzVmgdndsCZTyqs8OGExVRfRN77F28Ubm ep0jNcFl0zdZm9Ky/zgPfEFu5l2y+BHl9R5OpmyFcK3XGMY24XHvhuD4g6a2Wu6hdWyh Z/9Q== 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=Sxvbc7bGwYaMSWzxJGMkvPlmIs+2+EWa68wYWXx+WlQ=; b=PX35pXTWzJM/DCeffqayKATwUKylUfkaSen51CnTUBUy3oKVJXFVz7K0pURVb68vGy GjKUjUD24oVnsMmHcKnw5VcZ33ZgjL2iGvg1UWJPaX70/hEOJzUXTRB7bhG/LDlzeRc0 pIt4hszygmUU3NfRvP88jnhRP5NofSpIqI12l2BMCON8H2zSh1mH8bTH63OIlhf9aiEy +Bd0X3fS7yEG42w3MWZnvI/GAnJIDTO70+9NlwmkqEkQixDiXwuEZoQupg6u+XI47T8Z ttvtQMHsxpPhbpLfzSCkfjfRBraaf5dTIhdhJLUNKpJ6pGiuqDgt5QlAJ+xh93h79Qxy QUtA== X-Gm-Message-State: AOAM530PoWEXGKykrEFBxStS37LdYKePMONMyp9OU9KcxW7NeQjhXNhh 9f32kyPXh+Mcm+Jr5H0J5tJeGR5b+Jcm+w== X-Google-Smtp-Source: ABdhPJwaSjGoUr7Ea82OVwl9y0xe+KO+zhJGwPYVotL9crsYQr0M9kyLKiJ5ci/7Aw+Bns+YkCirZoCtEdY8tw== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:8c76:9de0:1a30:7de8]) (user=dlatypov job=sendgmr) by 2002:a25:8b0e:: with SMTP id i14mr7567465ybl.218.1641930164872; Tue, 11 Jan 2022 11:42:44 -0800 (PST) Date: Tue, 11 Jan 2022 11:42:28 -0800 In-Reply-To: <20220111194231.1797841-1-dlatypov@google.com> Message-Id: <20220111194231.1797841-4-dlatypov@google.com> Mime-Version: 1.0 References: <20220111194231.1797841-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH v2 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: string_stream needs it, but it has its own `test` field. 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 Reviewed-by: Brendan Higgins Reviewed-by: David Gow --- 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 12cabd15449a..25ea3bce6663 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -790,7 +790,7 @@ void kunit_do_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 @@ -820,8 +820,7 @@ void kunit_do_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, \ @@ -879,8 +878,7 @@ do { \ KUNIT_ASSERTION(test, \ __left op __right, \ assert_class, \ - ASSERT_CLASS_INIT(test, \ - assert_type, \ + ASSERT_CLASS_INIT(assert_type, \ #op, \ #left, \ __left, \ @@ -1234,8 +1232,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, \ @@ -1294,8 +1291,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 17:47:35 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 BDD62C433F5 for ; Tue, 11 Jan 2022 19:42:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237443AbiAKTmx (ORCPT ); Tue, 11 Jan 2022 14:42:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237985AbiAKTms (ORCPT ); Tue, 11 Jan 2022 14:42:48 -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 E201DC061751 for ; Tue, 11 Jan 2022 11:42:47 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id m202-20020a25d4d3000000b0061196f2a881so730158ybf.19 for ; Tue, 11 Jan 2022 11:42:47 -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=+6cc3qbI1qJ+oPBhzvsIgEPS0KrWO2CbkJwRjbfjFBc=; b=pqjl57YLDSxGbbFCiFxjeg3/lT1sOabF5iw+ncfjzX/F34inIieRLjiOpJj+7tjw9z fGcSQ3nCMWXyKQEfxqq0Ap50z8yHpYuFbgHGRFPKrX8Hg4qhJiC9UC8vg+y/cWk9QY0i CrdbtwoTHbcwl4rN0b4Ru0qBvEceK85Gb7zs4WmBOxJ63QlQFH7r+wWfaRVmfiTXvGIc MIi/GtgwAoEkuxcNkixvtC/hsP02FiWPqfOXLlEagAmqsdATVsMVVVYFUYP/nmFsbRh1 OPyhasXE+oRbKHGNrnkZa9vAtWEFB5UptIBkF7RuHvMeTVYO7dqmdSAP71dqsV9GwVeE SqBw== 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=+6cc3qbI1qJ+oPBhzvsIgEPS0KrWO2CbkJwRjbfjFBc=; b=3crs1O49h8qjkBYps9DVPoJ06/SBBylneu9cou87bQmsPTLP+GamyQXninHY+BRpeP 1LRBJoRNuzzifDH1e4nxmkHIcu7ziJ8wFo1+OtcEGpoHfxzeyvbpo5xpcKFsqwSSag/Q awlyA9o0aiKemHcM5fUr06VyLckl57uguVQTCCST5bgSzjhwhmxA0HNnPAmak0ea1qP/ inBH7BoSlSv+ZC2B+xxQTtXRrm/0pisf+TsbOurI3oa1MR37i/cW7wfVDaLuvWnw1+Kq qssMmohFZAys3qG/e/VO0OiatHXNdMJ5iNQ2/AJdiZ2oLgpIbY0QbrK+vB8Xq7t0aU9n L+yQ== X-Gm-Message-State: AOAM532xX6fBbC0XshxyheG33+/0a5HrGMmNbElMco10rywag7G+qdm1 MZYVZRSUIOVXgO/T3Dpz3NHDfiAKFwAiwg== X-Google-Smtp-Source: ABdhPJxKEbHl4KymXCxtimtS0gGONswWeOd5+R8wrSSBDMLJRBxLoPr6xWfpZPipmHxQzS9kp7CtI3Jxom6lOQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:8c76:9de0:1a30:7de8]) (user=dlatypov job=sendgmr) by 2002:a5b:281:: with SMTP id x1mr7792024ybl.41.1641930167213; Tue, 11 Jan 2022 11:42:47 -0800 (PST) Date: Tue, 11 Jan 2022 11:42:29 -0800 In-Reply-To: <20220111194231.1797841-1-dlatypov@google.com> Message-Id: <20220111194231.1797841-5-dlatypov@google.com> Mime-Version: 1.0 References: <20220111194231.1797841-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH v2 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 Reviewed-by: David Gow --- 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 3a52c321c280..345a9dd88c27 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 17:47:35 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 5423DC433F5 for ; Tue, 11 Jan 2022 19:42:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238551AbiAKTmz (ORCPT ); Tue, 11 Jan 2022 14:42:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238022AbiAKTmu (ORCPT ); Tue, 11 Jan 2022 14:42:50 -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 36861C061751 for ; Tue, 11 Jan 2022 11:42:50 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id y4-20020a5b0f44000000b00611862e546dso32616ybr.7 for ; Tue, 11 Jan 2022 11:42:50 -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=0ZjUIsLW4acHCjBv+u6cMQmjq5bt8fbJ806Zes8EZIE=; b=sQL03s7vgv1vzShIYJCYEgWgL8UsKXJ/CtD9/dVCEARL96A5mtf6TDO9DH9SJwPIZM XjKg5FXk0TmxaIWA4pbZfrq2ZwgKKdy65x/X+xBYVjs6TZpAWXmH7cSSpSTTAfAUAM3+ tcxjJRRR74sp25hss6iri/Q8XK86hgDpJz9RU4hwk+91wi2Ggh/JscN9K1K5j1IrEftA 6Wl6abam9t63aKn3pLFzeyXFtgmwd1iz3od5cnTw5Y1+DSbRqUzIr0/2qtNvidI6xNqU tjVhJ4rYxf7SsbIyGTeBVdN2FKNDixNySKhcr44DX6nDYV7wtEYnqccuxBLqs41cP2z5 9DTw== 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=0ZjUIsLW4acHCjBv+u6cMQmjq5bt8fbJ806Zes8EZIE=; b=l6OBGMByVEDTian13/SZfjwiSM8DlA2fcXP8s8v8+U66CGr0/TfCWiMNf5oTung5/J PIHmcOdGyr3MX9qQ4c4nbopjR+Kk/rXmqIWN7RodUnhJEKS/tS3SF6q9nH53TzHE+hRL n2ax7P0t8y+2A1TbFuEokffSY5IStM37d+Ohe3eUDicmFpDGmULsSrurho0j72mVKkTZ v1jg14ohmBbkqBGJmRUfHHWSOt9cPU/ZGMD3HO/HKv9uj/0CyApQ0BURDIw07/qz2H5p akvo1JYStMd7cwsXATu9/e9rigFgKVdUZIlruQ3Ky0k66cyXL+fUsqXNFql+4xKFttRv ZIhw== X-Gm-Message-State: AOAM533Z0KDMGugI6tE0XrU6VvuffcqyuEryufA5POThl45FEIZulS0J ERtiyOLai0UV3Lb4LvV7eJsAq6Ex9jppsA== X-Google-Smtp-Source: ABdhPJxEyrBFfq0EYaHGpSsAPYXRIdOZ3Ncs6drlt0Rmh9umYGkQ5WeQEHRGNEmQLAyl+Xx3Qa3D5/15ICrrUw== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:8c76:9de0:1a30:7de8]) (user=dlatypov job=sendgmr) by 2002:a25:cdc3:: with SMTP id d186mr8428161ybf.400.1641930169460; Tue, 11 Jan 2022 11:42:49 -0800 (PST) Date: Tue, 11 Jan 2022 11:42:30 -0800 In-Reply-To: <20220111194231.1797841-1-dlatypov@google.com> Message-Id: <20220111194231.1797841-6-dlatypov@google.com> Mime-Version: 1.0 References: <20220111194231.1797841-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH v2 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 (__builtin_expect(!!(!(false)), 0)) { static const struct kunit_loc loc =3D { .file =3D ... }; struct kunit_fail_assert __assertion =3D { .assert =3D { .type ... }; kunit_do_failed_assertion(test, &loc, KUNIT_EXPECTATION, &__assertion.ass= ert, ...); }; [1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ Signed-off-by: Daniel Latypov Suggested-by: Linus Torvalds Reviewed-by: David Gow --- 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 25ea3bce6663..7b752175e614 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -772,13 +772,18 @@ void __printf(2, 3) kunit_log_append(char *log, const= char *fmt, ...); #define KUNIT_SUCCEED(test) do {} while (0) =20 void kunit_do_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 (unlikely(!(pass))) { \ + static const struct kunit_loc loc =3D KUNIT_CURRENT_LOC; \ struct assert_class __assertion =3D INITIALIZER; \ kunit_do_failed_assertion(test, \ + &loc, \ + assert_type, \ &__assertion.assert, \ fmt, \ ##__VA_ARGS__); \ @@ -788,6 +793,7 @@ void kunit_do_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), \ @@ -818,6 +824,7 @@ void kunit_do_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, \ @@ -876,6 +883,7 @@ do { \ typeof(right) __right =3D (right); \ \ KUNIT_ASSERTION(test, \ + assert_type, \ __left op __right, \ assert_class, \ ASSERT_CLASS_INIT(assert_type, \ @@ -1230,6 +1238,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, \ @@ -1289,6 +1298,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 345a9dd88c27..7dec3248562f 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_do_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_do_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_do_failed_assertion); --=20 2.34.1.575.g55b058a8bb-goog From nobody Tue Jun 30 17:47:35 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 CFDDDC4332F for ; Tue, 11 Jan 2022 19:43:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239037AbiAKTnF (ORCPT ); Tue, 11 Jan 2022 14:43:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238383AbiAKTmw (ORCPT ); Tue, 11 Jan 2022 14:42:52 -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 59B6AC061748 for ; Tue, 11 Jan 2022 11:42:52 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id u185-20020a2560c2000000b0060fd98540f7so150786ybb.0 for ; Tue, 11 Jan 2022 11:42:52 -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=njxZip906y8sbtXtB//R8uMp68/sA5bXENQX80k/Srk=; b=pgaVZovcHu1KrMz6FM69+AY1yG/4j980SRhfQVG6QwMEMtghVw8m2q0kzrLv8OpMwj dYm5c/lbDNUbBmSeRAnwCt99w2PwfOZRWj1cbpNv6n/nB1UXKHCxlw52Gayj/SL4VRnd 1LchSJFvnKWmaQducCuoajMvAvlhnZ0ngcKNxaf085NYFn938gBKm4xTKqXawG1d9ldr TNRm+ZKf/h2PT+lUBAf0EyZpA3tsqd59lkfglYnKvweUUPEFUC8sBIfEGJHtHRZolpzH cI6jQ8V1twj30jy484Y0KekoWZzP3N5v5UQBKt4IsBjXkTE1pxixOH5B9tm77XB1qtft pgWQ== 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=njxZip906y8sbtXtB//R8uMp68/sA5bXENQX80k/Srk=; b=kF7RJhrEmwFe6oLVfP5Wvp3fmEQ+ep9xB40i1BhLbi2x2LmxCzvYcLT67akC/koYk7 cLu3az+RVw4sAW7oajI0S8e0/WveTa/kQXtr4G/+wU3F8Y1zxLQPXBhnSzbj6nA9y/Wo ii1ozg4c74DM9yTrxANF/y6Azx1VhnI/IaN2FMH/hBMpca6ryJxYMg6QHO15Km/8W+k8 zup6da8GhdBWIY3+EE+R/2Qp0Nm+zPQfZgRLysmXDXolAds4JRGTDNDZK6zqALOURpDb Fm63SE30lQyBT9IsUkUHrFdpXoRrXSwp1raWHy7Ju0lF8zVNjyarl1Y7fRDtv+Q7fG2n rlTw== X-Gm-Message-State: AOAM532e4EO8LsWki3vabweT8WQ6ryG9nYCP157gGVswpu0zBEHUxxrm tM2Cxb5zcE3IORTLBJdYkmm91L1qNCPfSA== X-Google-Smtp-Source: ABdhPJzqmLf41D4hrwL52IQzu7lV1Y001ZUixSM5TOCzSUksWiYeMSuwhv/K72vYxYeFplp/THLgBxSReIn6yg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:8c76:9de0:1a30:7de8]) (user=dlatypov job=sendgmr) by 2002:a25:c747:: with SMTP id w68mr4511513ybe.302.1641930171578; Tue, 11 Jan 2022 11:42:51 -0800 (PST) Date: Tue, 11 Jan 2022 11:42:31 -0800 In-Reply-To: <20220111194231.1797841-1-dlatypov@google.com> Message-Id: <20220111194231.1797841-7-dlatypov@google.com> Mime-Version: 1.0 References: <20220111194231.1797841-1-dlatypov@google.com> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog Subject: [PATCH v2 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 Reviewed-by: David Gow --- 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 7b752175e614..5964af750d93 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -796,7 +796,7 @@ void kunit_do_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 @@ -827,8 +827,7 @@ void kunit_do_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__) @@ -886,8 +885,7 @@ do { \ assert_type, \ __left op __right, \ assert_class, \ - ASSERT_CLASS_INIT(assert_type, \ - #op, \ + ASSERT_CLASS_INIT(#op, \ #left, \ __left, \ #right, \ @@ -1241,8 +1239,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, \ @@ -1301,8 +1298,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