Skip to content

Commit d2585f5

Browse files
ihashuahkh
authored andcommitted
lib: kunit: add bitfield test conversion to KUnit
This adds the conversion of the runtime tests of test_bitfield, from `lib/test_bitfield.c` to KUnit tests. Code Style Documentation: [0] Signed-off-by: Vitor Massaru Iha <[email protected]> Link: [0] https://lore.kernel.org/linux-kselftest/[email protected]/T/#u Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent a82763e commit d2585f5

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

lib/Kconfig.debug

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,13 +2037,6 @@ config TEST_BITMAP
20372037

20382038
If unsure, say N.
20392039

2040-
config TEST_BITFIELD
2041-
tristate "Test bitfield functions at runtime"
2042-
help
2043-
Enable this option to test the bitfield functions at boot.
2044-
2045-
If unsure, say N.
2046-
20472040
config TEST_UUID
20482041
tristate "Test functions located in the uuid module at runtime"
20492042

@@ -2193,6 +2186,22 @@ config TEST_SYSCTL
21932186

21942187
If unsure, say N.
21952188

2189+
config BITFIELD_KUNIT
2190+
tristate "KUnit test bitfield functions at runtime"
2191+
depends on KUNIT
2192+
help
2193+
Enable this option to test the bitfield functions at boot.
2194+
2195+
KUnit tests run during boot and output the results to the debug log
2196+
in TAP format (http://testanything.org/). Only useful for kernel devs
2197+
running the KUnit test harness, and not intended for inclusion into a
2198+
production build.
2199+
2200+
For more information on KUnit and unit tests in general please refer
2201+
to the KUnit documentation in Documentation/dev-tools/kunit/.
2202+
2203+
If unsure, say N.
2204+
21962205
config SYSCTL_KUNIT_TEST
21972206
tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS
21982207
depends on KUNIT

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
8080
obj-$(CONFIG_TEST_PRINTF) += test_printf.o
8181
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
8282
obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
83-
obj-$(CONFIG_TEST_BITFIELD) += test_bitfield.o
8483
obj-$(CONFIG_TEST_UUID) += test_uuid.o
8584
obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
8685
obj-$(CONFIG_TEST_PARMAN) += test_parman.o
@@ -340,6 +339,7 @@ obj-$(CONFIG_OBJAGG) += objagg.o
340339
obj-$(CONFIG_PLDMFW) += pldmfw/
341340

342341
# KUnit tests
342+
obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
343343
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
344344
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
345345
obj-$(CONFIG_BITS_TEST) += test_bits.o

lib/test_bitfield.c renamed to lib/bitfield_kunit.c

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@
55

66
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
77

8-
#include <linux/kernel.h>
9-
#include <linux/module.h>
8+
#include <kunit/test.h>
109
#include <linux/bitfield.h>
1110

1211
#define CHECK_ENC_GET_U(tp, v, field, res) do { \
1312
{ \
1413
u##tp _res; \
1514
\
1615
_res = u##tp##_encode_bits(v, field); \
17-
if (_res != res) { \
18-
pr_warn("u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n",\
19-
(u64)_res); \
20-
return -EINVAL; \
21-
} \
22-
if (u##tp##_get_bits(_res, field) != v) \
23-
return -EINVAL; \
16+
KUNIT_ASSERT_FALSE_MSG(context, _res != res, \
17+
"u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n", \
18+
(u64)_res); \
19+
KUNIT_ASSERT_FALSE(context, \
20+
u##tp##_get_bits(_res, field) != v); \
2421
} \
2522
} while (0)
2623

@@ -29,14 +26,13 @@
2926
__le##tp _res; \
3027
\
3128
_res = le##tp##_encode_bits(v, field); \
32-
if (_res != cpu_to_le##tp(res)) { \
33-
pr_warn("le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\
34-
(u64)le##tp##_to_cpu(_res), \
35-
(u64)(res)); \
36-
return -EINVAL; \
37-
} \
38-
if (le##tp##_get_bits(_res, field) != v) \
39-
return -EINVAL; \
29+
KUNIT_ASSERT_FALSE_MSG(context, \
30+
_res != cpu_to_le##tp(res), \
31+
"le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\
32+
(u64)le##tp##_to_cpu(_res), \
33+
(u64)(res)); \
34+
KUNIT_ASSERT_FALSE(context, \
35+
le##tp##_get_bits(_res, field) != v);\
4036
} \
4137
} while (0)
4238

@@ -45,14 +41,13 @@
4541
__be##tp _res; \
4642
\
4743
_res = be##tp##_encode_bits(v, field); \
48-
if (_res != cpu_to_be##tp(res)) { \
49-
pr_warn("be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx\n",\
50-
(u64)be##tp##_to_cpu(_res), \
51-
(u64)(res)); \
52-
return -EINVAL; \
53-
} \
54-
if (be##tp##_get_bits(_res, field) != v) \
55-
return -EINVAL; \
44+
KUNIT_ASSERT_FALSE_MSG(context, \
45+
_res != cpu_to_be##tp(res), \
46+
"be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx", \
47+
(u64)be##tp##_to_cpu(_res), \
48+
(u64)(res)); \
49+
KUNIT_ASSERT_FALSE(context, \
50+
be##tp##_get_bits(_res, field) != v);\
5651
} \
5752
} while (0)
5853

@@ -62,7 +57,7 @@
6257
CHECK_ENC_GET_BE(tp, v, field, res); \
6358
} while (0)
6459

65-
static int test_constants(void)
60+
static void __init test_bitfields_constants(struct kunit *context)
6661
{
6762
/*
6863
* NOTE
@@ -95,19 +90,17 @@ static int test_constants(void)
9590
CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull);
9691
CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
9792
CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
98-
99-
return 0;
10093
}
10194

10295
#define CHECK(tp, mask) do { \
10396
u64 v; \
10497
\
10598
for (v = 0; v < 1 << hweight32(mask); v++) \
106-
if (tp##_encode_bits(v, mask) != v << __ffs64(mask)) \
107-
return -EINVAL; \
99+
KUNIT_ASSERT_FALSE(context, \
100+
tp##_encode_bits(v, mask) != v << __ffs64(mask));\
108101
} while (0)
109102

110-
static int test_variables(void)
103+
static void __init test_bitfields_variables(struct kunit *context)
111104
{
112105
CHECK(u8, 0x0f);
113106
CHECK(u8, 0xf0);
@@ -130,39 +123,34 @@ static int test_variables(void)
130123
CHECK(u64, 0x000000007f000000ull);
131124
CHECK(u64, 0x0000000018000000ull);
132125
CHECK(u64, 0x0000001f8000000ull);
133-
134-
return 0;
135126
}
136127

137-
static int __init test_bitfields(void)
138-
{
139-
int ret = test_constants();
140-
141-
if (ret) {
142-
pr_warn("constant tests failed!\n");
143-
return ret;
144-
}
145-
146-
ret = test_variables();
147-
if (ret) {
148-
pr_warn("variable tests failed!\n");
149-
return ret;
150-
}
151128

152-
#ifdef TEST_BITFIELD_COMPILE
129+
static void __init test_bitfields_compile(struct kunit *context)
130+
{
153131
/* these should fail compilation */
154132
CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
155133
u32_encode_bits(7, 0x06000000);
156134

157135
/* this should at least give a warning */
158136
u16_encode_bits(0, 0x60000);
137+
}
138+
139+
static struct kunit_case __refdata bitfields_test_cases[] = {
140+
KUNIT_CASE(test_bitfields_constants),
141+
KUNIT_CASE(test_bitfields_variables),
142+
#ifdef TEST_BITFIELD_COMPILE
143+
KUNIT_CASE(test_bitfields_compile),
159144
#endif
145+
{}
146+
};
160147

161-
pr_info("tests passed\n");
148+
static struct kunit_suite bitfields_test_suite = {
149+
.name = "bitfields",
150+
.test_cases = bitfields_test_cases,
151+
};
162152

163-
return 0;
164-
}
165-
module_init(test_bitfields)
153+
kunit_test_suites(&bitfields_test_suite);
166154

167155
MODULE_AUTHOR("Johannes Berg <[email protected]>");
168156
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)