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
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
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
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
167155MODULE_AUTHOR ("Johannes Berg <johannes@sipsolutions.net>" );
168156MODULE_LICENSE ("GPL" );
0 commit comments