Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 */
12enum 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
21struct bitops_test_case {
22 const char *str;
23 const long nr;
24};
25
26static 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
49KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
50
51static 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
69static 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
87static 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
111static 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
129struct order_test_case {
130 const char *str;
131 const unsigned int count;
132 const int expected;
133};
134
135static struct order_test_case order_test_cases[] = {
136 {"0x00000003", 0x00000003, 2},
137 {"0x00000004", 0x00000004, 2},
138 {"0x00001fff", 0x00001fff, 13},
139 {"0x00002000", 0x00002000, 13},
140 {"0x50000000", 0x50000000, 31},
141 {"0x80000000", 0x80000000, 31},
142 {"0x80003000", 0x80003000, 32},
143};
144
145KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
146
147static void test_get_count_order(struct kunit *test)
148{
149 const struct order_test_case *params = test->param_value;
150
151 KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
152 KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
153}
154
155#ifdef CONFIG_64BIT
156struct order_long_test_case {
157 const char *str;
158 const unsigned long count;
159 const int expected;
160};
161
162static struct order_long_test_case order_long_test_cases[] = {
163 {"0x0000000300000000", 0x0000000300000000, 34},
164 {"0x0000000400000000", 0x0000000400000000, 34},
165 {"0x00001fff00000000", 0x00001fff00000000, 45},
166 {"0x0000200000000000", 0x0000200000000000, 45},
167 {"0x5000000000000000", 0x5000000000000000, 63},
168 {"0x8000000000000000", 0x8000000000000000, 63},
169 {"0x8000300000000000", 0x8000300000000000, 64},
170};
171
172KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
173
174static void test_get_count_order_long(struct kunit *test)
175{
176 const struct order_long_test_case *params = test->param_value;
177
178 KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
179}
180#endif
181
182static struct kunit_case bitops_test_cases[] = {
183 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),
187 KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
188#ifdef CONFIG_64BIT
189 KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
190#endif
191 {},
192};
193
194static struct kunit_suite bitops_test_suite = {
195 .name = "bitops",
196 .test_cases = bitops_test_cases,
197};
198
199kunit_test_suite(bitops_test_suite);
200
201MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
202MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");
203MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
204MODULE_LICENSE("GPL");
205MODULE_DESCRIPTION("Bit testing module");