mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] lib/tests: extend KUnit test for bitops with bitrev APIs
@ 2026-12-06  2:33 Masaharu Noguchi
  0 siblings, 0 replies; only message in thread
From: Masaharu Noguchi @ 2026-12-06  2:33 UTC (permalink / raw)
  To: Andrew Morton, Yury Norov, Rasmus Villemoes
  Cc: Yury Norov, kunit-dev, linux-kselftest, linux-kernel, Masaharu Noguchi

Extend the bitops KUnit test suite (lib/tests/bitops_kunit.c) to cover
the bit reversal APIs in include/linux/bitrev.h, and have BITOPS_KUNIT
select BITREVERSE:

 - bitrev8() (exhaustive over all 256 u8 values plus involution check)
 - bitrev16(), bitrev32(), and bitrev8x4() (boundary patterns,
   involution checks, and single-bit / inverted single-bit sweeps)
 - compile-time constant evaluation via BUILD_BUG_ON() and runtime
   verification of constant expressions

Runtime tests use OPTIMIZER_HIDE_VAR() to ensure the runtime
architecture/generic table implementation is exercised rather than
folded into the compile-time constant path.

Tested under ARCH=um with:
  ./tools/testing/kunit/kunit.py run \
    --kconfig_add CONFIG_BITOPS_KUNIT=y 'bitops'

Sample KUnit output:

    [PASSED] test_bitrev8
    [PASSED] test_bitrev16
    [PASSED] test_bitrev32
    [PASSED] test_bitrev8x4
    [PASSED] test_bitrev_const
    ===================== [PASSED] bitops ======================
    Testing complete. Ran 39 tests: passed: 39

Also verified W=1 compilation of lib/tests/bitops_kunit.o with no
warnings.

Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
---
Note: An LLM (Gemini) was used to help draft the initial test cases and
commit message; all changes were reviewed, verified, and signed off by 
the submitter.
---
Changes in v2:
- Merge the bitrev tests into lib/tests/bitops_kunit.c and have
  BITOPS_KUNIT select BITREVERSE instead of adding a separate
  bitrev_kunit.c module (suggested by Yury Norov).
- Link to v1: https://lore.kernel.org/r/20261205-bitrev-kunit-v1-1-f8b712691e04@gmail.com
---
 lib/Kconfig.debug        |   1 +
 lib/tests/bitops_kunit.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2d22638ec13a..d9888fb8852c 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2726,6 +2726,7 @@ config TEST_SYSCTL
 config BITOPS_KUNIT
 	tristate "KUnit test for bitops" if !KUNIT_ALL_TESTS
 	depends on KUNIT
+	select BITREVERSE
 	default KUNIT_ALL_TESTS
 	help
 	  This option enables the KUnit test for the bitops library
diff --git a/lib/tests/bitops_kunit.c b/lib/tests/bitops_kunit.c
index 7fd9d697f131..95cdbb1575da 100644
--- a/lib/tests/bitops_kunit.c
+++ b/lib/tests/bitops_kunit.c
@@ -5,6 +5,9 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/bitrev.h>
+#include <linux/build_bug.h>
+#include <linux/limits.h>
 #include <linux/module.h>
 #include <kunit/test.h>
 
@@ -179,6 +182,187 @@ static void test_get_count_order_long(struct kunit *test)
 }
 #endif
 
+static u32 reference_bitrev(u32 val, unsigned int bits)
+{
+	u32 res = 0;
+	unsigned int i;
+
+	for (i = 0; i < bits; i++) {
+		res = (res << 1) | (val & 1U);
+		val >>= 1;
+	}
+
+	return res;
+}
+
+static void test_bitrev8(struct kunit *test)
+{
+	unsigned int i;
+
+	for (i = 0; i <= U8_MAX; i++) {
+		u8 val = i;
+		u8 rev;
+
+		OPTIMIZER_HIDE_VAR(val);
+		rev = bitrev8(val);
+		KUNIT_EXPECT_EQ(test, rev, (u8)reference_bitrev(i, 8));
+
+		OPTIMIZER_HIDE_VAR(rev);
+		KUNIT_EXPECT_EQ(test, bitrev8(rev), (u8)i);
+	}
+}
+
+static void test_bitrev16(struct kunit *test)
+{
+	static const struct {
+		u16 val;
+		u16 expected;
+	} cases[] = {
+		{ 0x0000, 0x0000 },
+		{ 0x0001, 0x8000 },
+		{ 0x8000, 0x0001 },
+		{ 0x00ff, 0xff00 },
+		{ 0xff00, 0x00ff },
+		{ 0x5555, 0xaaaa },
+		{ 0xaaaa, 0x5555 },
+		{ 0x0f0f, 0xf0f0 },
+		{ 0x1234, 0x2c48 },
+		{ U16_MAX, U16_MAX },
+	};
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		u16 val = cases[i].val;
+		u16 rev;
+
+		OPTIMIZER_HIDE_VAR(val);
+		rev = bitrev16(val);
+		KUNIT_EXPECT_EQ(test, rev, cases[i].expected);
+
+		OPTIMIZER_HIDE_VAR(rev);
+		KUNIT_EXPECT_EQ(test, bitrev16(rev), cases[i].val);
+	}
+
+	for (i = 0; i < 16; i++) {
+		u16 single = BIT(i);
+		u16 expected = BIT(15 - i);
+		u16 inverted = ~single;
+
+		OPTIMIZER_HIDE_VAR(single);
+		OPTIMIZER_HIDE_VAR(inverted);
+		KUNIT_EXPECT_EQ(test, bitrev16(single), expected);
+		KUNIT_EXPECT_EQ(test, bitrev16(inverted), (u16)~expected);
+	}
+}
+
+static void test_bitrev32(struct kunit *test)
+{
+	static const struct {
+		u32 val;
+		u32 expected;
+	} cases[] = {
+		{ 0x00000000U, 0x00000000U },
+		{ 0x00000001U, 0x80000000U },
+		{ 0x80000000U, 0x00000001U },
+		{ 0x0000ffffU, 0xffff0000U },
+		{ 0xffff0000U, 0x0000ffffU },
+		{ 0x55555555U, 0xaaaaaaaaU },
+		{ 0xaaaaaaaaU, 0x55555555U },
+		{ 0x12345678U, 0x1e6a2c48U },
+		{ U32_MAX, U32_MAX },
+	};
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		u32 val = cases[i].val;
+		u32 rev;
+
+		OPTIMIZER_HIDE_VAR(val);
+		rev = bitrev32(val);
+		KUNIT_EXPECT_EQ(test, rev, cases[i].expected);
+
+		OPTIMIZER_HIDE_VAR(rev);
+		KUNIT_EXPECT_EQ(test, bitrev32(rev), cases[i].val);
+	}
+
+	for (i = 0; i < 32; i++) {
+		u32 single = BIT(i);
+		u32 expected = BIT(31 - i);
+		u32 inverted = ~single;
+
+		OPTIMIZER_HIDE_VAR(single);
+		OPTIMIZER_HIDE_VAR(inverted);
+		KUNIT_EXPECT_EQ(test, bitrev32(single), expected);
+		KUNIT_EXPECT_EQ(test, bitrev32(inverted), ~expected);
+	}
+}
+
+static void test_bitrev8x4(struct kunit *test)
+{
+	static const struct {
+		u32 val;
+		u32 expected;
+	} cases[] = {
+		{ 0x00000000U, 0x00000000U },
+		{ 0x01020408U, 0x80402010U },
+		{ 0x80402010U, 0x01020408U },
+		{ 0x55555555U, 0xaaaaaaaaU },
+		{ 0xaaaaaaaaU, 0x55555555U },
+		{ 0x0f0f0f0fU, 0xf0f0f0f0U },
+		{ 0x12345678U, 0x482c6a1eU },
+		{ U32_MAX, U32_MAX },
+	};
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(cases); i++) {
+		u32 val = cases[i].val;
+		u32 rev;
+
+		OPTIMIZER_HIDE_VAR(val);
+		rev = bitrev8x4(val);
+		KUNIT_EXPECT_EQ(test, rev, cases[i].expected);
+
+		OPTIMIZER_HIDE_VAR(rev);
+		KUNIT_EXPECT_EQ(test, bitrev8x4(rev), cases[i].val);
+	}
+
+	for (i = 0; i < 32; i++) {
+		unsigned int byte_shift = (i / 8) * 8;
+		unsigned int bit_in_byte = i % 8;
+		u32 single = BIT(i);
+		u32 expected = BIT(byte_shift + (7 - bit_in_byte));
+
+		OPTIMIZER_HIDE_VAR(single);
+		KUNIT_EXPECT_EQ(test, bitrev8x4(single), expected);
+	}
+}
+
+static void test_bitrev_const(struct kunit *test)
+{
+	BUILD_BUG_ON(bitrev8(0x00) != 0x00);
+	BUILD_BUG_ON(bitrev8(0x01) != 0x80);
+	BUILD_BUG_ON(bitrev8(0xa5) != 0xa5);
+	BUILD_BUG_ON(bitrev8(0x12) != 0x48);
+	BUILD_BUG_ON(bitrev8(0xff) != 0xff);
+
+	BUILD_BUG_ON(bitrev16(0x0000) != 0x0000);
+	BUILD_BUG_ON(bitrev16(0x1234) != 0x2c48);
+	BUILD_BUG_ON(bitrev16(0xffff) != 0xffff);
+
+	BUILD_BUG_ON(bitrev32(0x00000000U) != 0x00000000U);
+	BUILD_BUG_ON(bitrev32(0x12345678U) != 0x1e6a2c48U);
+	BUILD_BUG_ON(bitrev32(0xffffffffU) != 0xffffffffU);
+
+	BUILD_BUG_ON(bitrev8x4(0x00000000U) != 0x00000000U);
+	BUILD_BUG_ON(bitrev8x4(0x12345678U) != 0x482c6a1eU);
+	BUILD_BUG_ON(bitrev8x4(0xffffffffU) != 0xffffffffU);
+
+	KUNIT_EXPECT_EQ(test, bitrev8(0x12), (u8)0x48);
+	KUNIT_EXPECT_EQ(test, bitrev16(0x1234), (u16)0x2c48);
+	KUNIT_EXPECT_EQ(test, bitrev32(0x12345678U), 0x1e6a2c48U);
+	KUNIT_EXPECT_EQ(test, bitrev8x4(0x12345678U), 0x482c6a1eU);
+}
+
 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),
@@ -188,6 +372,11 @@ static struct kunit_case bitops_test_cases[] = {
 #ifdef CONFIG_64BIT
 	KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
 #endif
+	KUNIT_CASE(test_bitrev8),
+	KUNIT_CASE(test_bitrev16),
+	KUNIT_CASE(test_bitrev32),
+	KUNIT_CASE(test_bitrev8x4),
+	KUNIT_CASE(test_bitrev_const),
 	{},
 };
 

---
base-commit: 0810bd27d7bbccb4f579f1fbb1afc98687c4c7c3
change-id: 20261205-bitrev-kunit-0aa52a7ed8b2

Best regards,
-- 
Masaharu Noguchi <nogunix@gmail.com>


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06  2:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-06  2:33 [PATCH v2] lib/tests: extend KUnit test for bitops with bitrev APIs Masaharu Noguchi

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®