Skip to content

Commit 6711069

Browse files
ryota-sakamotoYury Norov
authored andcommitted
lib/tests: extend KUnit test for bitops with more cases
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> Signed-off-by: Yury Norov <ynorov@nvidia.com>
1 parent 9d6f676 commit 6711069

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

lib/Kconfig.debug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,8 +2660,8 @@ config BITOPS_KUNIT
26602660
This option enables the KUnit test for the bitops library
26612661
which provides functions for bit operations.
26622662

2663-
Note that this is a partial copy of the original test_bitops module.
2664-
For the full coverage, enable TEST_BITOPS.
2663+
Note that this is derived from the original test_bitops module.
2664+
For micro-benchmarks and compiler warning checks, enable TEST_BITOPS.
26652665

26662666
If unsure, say N.
26672667

lib/tests/bitops_kunit.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,66 @@ static void test_set_bit_clear_bit(struct kunit *test)
6666
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
6767
}
6868

69+
static void test_change_bit(struct kunit *test)
70+
{
71+
const struct bitops_test_case *params = test->param_value;
72+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
73+
int bit_set;
74+
75+
bitmap_zero(bitmap, BITOPS_LENGTH);
76+
77+
change_bit(params->nr, bitmap);
78+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
79+
80+
change_bit(params->nr, bitmap);
81+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
82+
83+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
84+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
85+
}
86+
87+
static void test_test_and_set_bit_test_and_clear_bit(struct kunit *test)
88+
{
89+
const struct bitops_test_case *params = test->param_value;
90+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
91+
int bit_set;
92+
93+
bitmap_zero(bitmap, BITOPS_LENGTH);
94+
95+
KUNIT_EXPECT_FALSE(test, test_and_set_bit(params->nr, bitmap));
96+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
97+
98+
KUNIT_EXPECT_TRUE(test, test_and_set_bit(params->nr, bitmap));
99+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
100+
101+
KUNIT_EXPECT_TRUE(test, test_and_clear_bit(params->nr, bitmap));
102+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
103+
104+
KUNIT_EXPECT_FALSE(test, test_and_clear_bit(params->nr, bitmap));
105+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
106+
107+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
108+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
109+
}
110+
111+
static void test_test_and_change_bit(struct kunit *test)
112+
{
113+
const struct bitops_test_case *params = test->param_value;
114+
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
115+
int bit_set;
116+
117+
bitmap_zero(bitmap, BITOPS_LENGTH);
118+
119+
KUNIT_EXPECT_FALSE(test, test_and_change_bit(params->nr, bitmap));
120+
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
121+
122+
KUNIT_EXPECT_TRUE(test, test_and_change_bit(params->nr, bitmap));
123+
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
124+
125+
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
126+
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
127+
}
128+
69129
struct order_test_case {
70130
const char *str;
71131
const unsigned int count;
@@ -121,6 +181,9 @@ static void test_get_count_order_long(struct kunit *test)
121181

122182
static struct kunit_case bitops_test_cases[] = {
123183
KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
184+
KUNIT_CASE_PARAM(test_change_bit, bitops_gen_params),
185+
KUNIT_CASE_PARAM(test_test_and_set_bit_test_and_clear_bit, bitops_gen_params),
186+
KUNIT_CASE_PARAM(test_test_and_change_bit, bitops_gen_params),
124187
KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
125188
#ifdef CONFIG_64BIT
126189
KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),

0 commit comments

Comments
 (0)