mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ryota Sakamoto <sakamo.ryota@gmail.com>
To: Yury Norov <yury.norov@gmail.com>,
	 Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Ryota Sakamoto <sakamo.ryota@gmail.com>
Subject: [PATCH] lib/tests: extend KUnit test for bitops with more cases
Date: Fri, 23 Jan 2026 00:40:41 +0900	[thread overview]
Message-ID: <20260123-kunit-bitops-new-tests-v1-1-47e516054738@gmail.com> (raw)

Extend a KUnit test suite for the bitops API to cover more APIs from
include/asm-generic/bitops/instrumented-atomic.h.

- change_bit()
- test_and_set_bit()
- test_and_clear_bit()
- test_and_change_bit()

Verified on x86_64, i386, and arm64 architectures.

Sample KUnit output:

        KTAP version 1
        # Subtest: test_change_bit
        ok 1 BITOPS_4
        ok 2 BITOPS_7
        ok 3 BITOPS_11
        ok 4 BITOPS_31
        ok 5 BITOPS_88
    # test_change_bit: pass:5 fail:0 skip:0 total:5
    ok 2 test_change_bit
        KTAP version 1
        # Subtest: test_test_and_set_bit_test_and_clear_bit
        ok 1 BITOPS_4
        ok 2 BITOPS_7
        ok 3 BITOPS_11
        ok 4 BITOPS_31
        ok 5 BITOPS_88
    # test_test_and_set_bit_test_and_clear_bit: pass:5 fail:0 skip:0 total:5
    ok 3 test_test_and_set_bit_test_and_clear_bit
        KTAP version 1
        # Subtest: test_test_and_change_bit
        ok 1 BITOPS_4
        ok 2 BITOPS_7
        ok 3 BITOPS_11
        ok 4 BITOPS_31
        ok 5 BITOPS_88
    # test_test_and_change_bit: pass:5 fail:0 skip:0 total:5
    ok 4 test_test_and_change_bit

Signed-off-by: Ryota Sakamoto <sakamo.ryota@gmail.com>
---
Note that this patch is built on top of bitmap-for-next.
---
 lib/Kconfig.debug        |  4 +--
 lib/tests/bitops_kunit.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index e4f8ba2f9f322168a3e8b44e5e46322dd2bd62ae..c59b12cf633a70e702ea365348ec20b106e75950 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2660,8 +2660,8 @@ config BITOPS_KUNIT
 	  This option enables the KUnit test for the bitops library
 	  which provides functions for bit operations.
 
-	  Note that this is a partial copy of the original test_bitops module.
-	  For the full coverage, enable TEST_BITOPS.
+	  Note that this is derived from the original test_bitops module.
+	  For micro-benchmarks and compiler warning checks, enable TEST_BITOPS.
 
 	  If unsure, say N.
 
diff --git a/lib/tests/bitops_kunit.c b/lib/tests/bitops_kunit.c
index 5c47a12760611a0445feb37c252d00f3bf73f6a1..7fd9d697f131dfa38b5f261dc6255450dbdac156 100644
--- a/lib/tests/bitops_kunit.c
+++ b/lib/tests/bitops_kunit.c
@@ -66,6 +66,66 @@ static void test_set_bit_clear_bit(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
 }
 
+static void test_change_bit(struct kunit *test)
+{
+	const struct bitops_test_case *params = test->param_value;
+	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
+	int bit_set;
+
+	bitmap_zero(bitmap, BITOPS_LENGTH);
+
+	change_bit(params->nr, bitmap);
+	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
+
+	change_bit(params->nr, bitmap);
+	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
+
+	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
+	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
+}
+
+static void test_test_and_set_bit_test_and_clear_bit(struct kunit *test)
+{
+	const struct bitops_test_case *params = test->param_value;
+	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
+	int bit_set;
+
+	bitmap_zero(bitmap, BITOPS_LENGTH);
+
+	KUNIT_EXPECT_FALSE(test, test_and_set_bit(params->nr, bitmap));
+	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
+
+	KUNIT_EXPECT_TRUE(test, test_and_set_bit(params->nr, bitmap));
+	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
+
+	KUNIT_EXPECT_TRUE(test, test_and_clear_bit(params->nr, bitmap));
+	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
+
+	KUNIT_EXPECT_FALSE(test, test_and_clear_bit(params->nr, bitmap));
+	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
+
+	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
+	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
+}
+
+static void test_test_and_change_bit(struct kunit *test)
+{
+	const struct bitops_test_case *params = test->param_value;
+	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
+	int bit_set;
+
+	bitmap_zero(bitmap, BITOPS_LENGTH);
+
+	KUNIT_EXPECT_FALSE(test, test_and_change_bit(params->nr, bitmap));
+	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
+
+	KUNIT_EXPECT_TRUE(test, test_and_change_bit(params->nr, bitmap));
+	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
+
+	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
+	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
+}
+
 struct order_test_case {
 	const char *str;
 	const unsigned int count;
@@ -121,6 +181,9 @@ static void test_get_count_order_long(struct kunit *test)
 
 static struct kunit_case bitops_test_cases[] = {
 	KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
+	KUNIT_CASE_PARAM(test_change_bit, bitops_gen_params),
+	KUNIT_CASE_PARAM(test_test_and_set_bit_test_and_clear_bit, bitops_gen_params),
+	KUNIT_CASE_PARAM(test_test_and_change_bit, bitops_gen_params),
 	KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
 #ifdef CONFIG_64BIT
 	KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),

---
base-commit: c39f47e6c43954822a36952f47c0da027aca9c2b
change-id: 20260122-kunit-bitops-new-tests-bede7e41bac5

Best regards,
-- 
Ryota Sakamoto <sakamo.ryota@gmail.com>


             reply	other threads:[~2026-01-22 15:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 15:40 Ryota Sakamoto [this message]
2026-01-27 13:26 ` Yury Norov

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20260123-kunit-bitops-new-tests-v1-1-47e516054738@gmail.com \
    --to=sakamo.ryota@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

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

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

all inboxes | Powered by JetHome®