Skip to content

Commit 4101b3b

Browse files
ryota-sakamotoYury Norov
authored andcommitted
lib/tests: add KUnit test for bitops
Add a KUnit test suite for the bitops API. The existing 'lib/test_bitops.c' is preserved as-is because it contains ad-hoc micro-benchmarks 'test_fns' and is intended to ensure no compiler warnings from C=1 sparse checker or -Wextra compilations. Introduce 'lib/tests/bitops_kunit.c' for functional regression testing. It ports the test logic and data patterns from 'lib/test_bitops.c' to KUnit, verifying correct behavior across various input patterns and architecture-specific edge cases using isolated stack-allocated bitmaps. The following test logic has been ported from test_bitops_startup() in lib/test_bitops.c: - set_bit() / clear_bit() / find_first_bit() validation -> test_set_bit_clear_bit() - get_count_order() validation -> test_get_count_order() - get_count_order_long() validation -> test_get_count_order_long() Also improve the find_first_bit() test to check the full bitmap length (BITOPS_LENGTH) instead of omitting the last bit, ensuring the bitmap is completely empty after cleanup. Verified on x86_64, i386, and arm64 architectures. Sample KUnit output: KTAP version 1 # Subtest: bitops # module: bitops_kunit 1..3 KTAP version 1 # Subtest: test_set_bit_clear_bit ok 1 BITOPS_4 ok 2 BITOPS_7 ok 3 BITOPS_11 ok 4 BITOPS_31 ok 5 BITOPS_88 # test_set_bit_clear_bit: pass:5 fail:0 skip:0 total:5 ok 1 test_set_bit_clear_bit KTAP version 1 # Subtest: test_get_count_order ok 1 0x00000003 ok 2 0x00000004 ok 3 0x00001fff ok 4 0x00002000 ok 5 0x50000000 ok 6 0x80000000 ok 7 0x80003000 # test_get_count_order: pass:7 fail:0 skip:0 total:7 ok 2 test_get_count_order KTAP version 1 # Subtest: test_get_count_order_long ok 1 0x0000000300000000 ok 2 0x0000000400000000 ok 3 0x00001fff00000000 ok 4 0x0000200000000000 ok 5 0x5000000000000000 ok 6 0x8000000000000000 ok 7 0x8000300000000000 # test_get_count_order_long: pass:7 fail:0 skip:0 total:7 ok 3 test_get_count_order_long [Yury: trim Kconfig help message] CC: Jesse Brandeburg <jesse.brandeburg@intel.com> CC: Wei Yang <richard.weiyang@gmail.com> Signed-off-by: Ryota Sakamoto <sakamo.ryota@gmail.com> Signed-off-by: Yury Norov <ynorov@nvidia.com>
1 parent b4f1ffd commit 4101b3b

4 files changed

Lines changed: 157 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4468,6 +4468,7 @@ F: include/asm-generic/bitops.h
44684468
F: include/linux/bitops.h
44694469
F: lib/hweight.c
44704470
F: lib/test_bitops.c
4471+
F: lib/tests/bitops_kunit.c
44714472
F: tools/*/bitops*
44724473

44734474
BITOPS API BINDINGS [RUST]

lib/Kconfig.debug

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,19 @@ config TEST_SYSCTL
26522652

26532653
If unsure, say N.
26542654

2655+
config BITOPS_KUNIT
2656+
tristate "KUnit test for bitops" if !KUNIT_ALL_TESTS
2657+
depends on KUNIT
2658+
default KUNIT_ALL_TESTS
2659+
help
2660+
This option enables the KUnit test for the bitops library
2661+
which provides functions for bit operations.
2662+
2663+
Note that this is a partial copy of the original test_bitops module.
2664+
For the full coverage, enable TEST_BITOPS.
2665+
2666+
If unsure, say N.
2667+
26552668
config BITFIELD_KUNIT
26562669
tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS
26572670
depends on KUNIT

lib/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# KUnit tests
66
CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
77
obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
8+
obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
89
obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
910
obj-$(CONFIG_BITS_TEST) += test_bits.o
1011
obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o

lib/tests/bitops_kunit.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2020 Intel Corporation
4+
* Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com>
5+
*/
6+
7+
#include <linux/bitops.h>
8+
#include <linux/module.h>
9+
#include <kunit/test.h>
10+
11+
/* use an enum because that's the most common BITMAP usage */
12+
enum bitops_fun {
13+
BITOPS_4 = 4,
14+
BITOPS_7 = 7,
15+
BITOPS_11 = 11,
16+
BITOPS_31 = 31,
17+
BITOPS_88 = 88,
18+
BITOPS_LENGTH = 256
19+
};
20+
21+
struct bitops_test_case {
22+
const char *str;
23+
const long nr;
24+
};
25+
26+
static struct bitops_test_case bitops_cases[] = {
27+
{
28+
.str = "BITOPS_4",
29+
.nr = BITOPS_4,
30+
},
31+
{
32+
.str = "BITOPS_7",
33+
.nr = BITOPS_7,
34+
},
35+
{
36+
.str = "BITOPS_11",
37+
.nr = BITOPS_11,
38+
},
39+
{
40+
.str = "BITOPS_31",
41+
.nr = BITOPS_31,
42+
},
43+
{
44+
.str = "BITOPS_88",
45+
.nr = BITOPS_88,
46+
},
47+
};
48+
49+
KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
50+
51+
static void test_set_bit_clear_bit(struct kunit *test)
52+
{
53+
const struct bitops_test_case *params = test->param_value;
54+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
55+
int bit_set;
56+
57+
bitmap_zero(bitmap, BITOPS_LENGTH);
58+
59+
set_bit(params->nr, bitmap);
60+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
61+
62+
clear_bit(params->nr, bitmap);
63+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
64+
65+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
66+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
67+
}
68+
69+
struct order_test_case {
70+
const char *str;
71+
const unsigned int count;
72+
const int expected;
73+
};
74+
75+
static struct order_test_case order_test_cases[] = {
76+
{"0x00000003", 0x00000003, 2},
77+
{"0x00000004", 0x00000004, 2},
78+
{"0x00001fff", 0x00001fff, 13},
79+
{"0x00002000", 0x00002000, 13},
80+
{"0x50000000", 0x50000000, 31},
81+
{"0x80000000", 0x80000000, 31},
82+
{"0x80003000", 0x80003000, 32},
83+
};
84+
85+
KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
86+
87+
static void test_get_count_order(struct kunit *test)
88+
{
89+
const struct order_test_case *params = test->param_value;
90+
91+
KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
92+
KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
93+
}
94+
95+
#ifdef CONFIG_64BIT
96+
struct order_long_test_case {
97+
const char *str;
98+
const unsigned long count;
99+
const int expected;
100+
};
101+
102+
static struct order_long_test_case order_long_test_cases[] = {
103+
{"0x0000000300000000", 0x0000000300000000, 34},
104+
{"0x0000000400000000", 0x0000000400000000, 34},
105+
{"0x00001fff00000000", 0x00001fff00000000, 45},
106+
{"0x0000200000000000", 0x0000200000000000, 45},
107+
{"0x5000000000000000", 0x5000000000000000, 63},
108+
{"0x8000000000000000", 0x8000000000000000, 63},
109+
{"0x8000300000000000", 0x8000300000000000, 64},
110+
};
111+
112+
KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
113+
114+
static void test_get_count_order_long(struct kunit *test)
115+
{
116+
const struct order_long_test_case *params = test->param_value;
117+
118+
KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
119+
}
120+
#endif
121+
122+
static struct kunit_case bitops_test_cases[] = {
123+
KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
124+
KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
125+
#ifdef CONFIG_64BIT
126+
KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
127+
#endif
128+
{},
129+
};
130+
131+
static struct kunit_suite bitops_test_suite = {
132+
.name = "bitops",
133+
.test_cases = bitops_test_cases,
134+
};
135+
136+
kunit_test_suite(bitops_test_suite);
137+
138+
MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
139+
MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");
140+
MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
141+
MODULE_LICENSE("GPL");
142+
MODULE_DESCRIPTION("Bit testing module");

0 commit comments

Comments
 (0)