0001
0002
0003
0004
0005
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007
0008 #include <kunit/test.h>
0009 #include <linux/bitfield.h>
0010
0011 #define CHECK_ENC_GET_U(tp, v, field, res) do { \
0012 { \
0013 u##tp _res; \
0014 \
0015 _res = u##tp##_encode_bits(v, field); \
0016 KUNIT_ASSERT_FALSE_MSG(context, _res != res, \
0017 "u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n", \
0018 (u64)_res); \
0019 KUNIT_ASSERT_FALSE(context, \
0020 u##tp##_get_bits(_res, field) != v); \
0021 } \
0022 } while (0)
0023
0024 #define CHECK_ENC_GET_LE(tp, v, field, res) do { \
0025 { \
0026 __le##tp _res; \
0027 \
0028 _res = le##tp##_encode_bits(v, field); \
0029 KUNIT_ASSERT_FALSE_MSG(context, \
0030 _res != cpu_to_le##tp(res), \
0031 "le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\
0032 (u64)le##tp##_to_cpu(_res), \
0033 (u64)(res)); \
0034 KUNIT_ASSERT_FALSE(context, \
0035 le##tp##_get_bits(_res, field) != v);\
0036 } \
0037 } while (0)
0038
0039 #define CHECK_ENC_GET_BE(tp, v, field, res) do { \
0040 { \
0041 __be##tp _res; \
0042 \
0043 _res = be##tp##_encode_bits(v, field); \
0044 KUNIT_ASSERT_FALSE_MSG(context, \
0045 _res != cpu_to_be##tp(res), \
0046 "be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx", \
0047 (u64)be##tp##_to_cpu(_res), \
0048 (u64)(res)); \
0049 KUNIT_ASSERT_FALSE(context, \
0050 be##tp##_get_bits(_res, field) != v);\
0051 } \
0052 } while (0)
0053
0054 #define CHECK_ENC_GET(tp, v, field, res) do { \
0055 CHECK_ENC_GET_U(tp, v, field, res); \
0056 CHECK_ENC_GET_LE(tp, v, field, res); \
0057 CHECK_ENC_GET_BE(tp, v, field, res); \
0058 } while (0)
0059
0060 static void __init test_bitfields_constants(struct kunit *context)
0061 {
0062
0063
0064
0065
0066
0067
0068 CHECK_ENC_GET(16, 1, 0x000f, 0x0001);
0069 CHECK_ENC_GET(16, 3, 0x00f0, 0x0030);
0070 CHECK_ENC_GET(16, 5, 0x0f00, 0x0500);
0071 CHECK_ENC_GET(16, 7, 0xf000, 0x7000);
0072 CHECK_ENC_GET(16, 14, 0x000f, 0x000e);
0073 CHECK_ENC_GET(16, 15, 0x00f0, 0x00f0);
0074
0075 CHECK_ENC_GET_U(8, 1, 0x0f, 0x01);
0076 CHECK_ENC_GET_U(8, 3, 0xf0, 0x30);
0077 CHECK_ENC_GET_U(8, 14, 0x0f, 0x0e);
0078 CHECK_ENC_GET_U(8, 15, 0xf0, 0xf0);
0079
0080 CHECK_ENC_GET(32, 1, 0x00000f00, 0x00000100);
0081 CHECK_ENC_GET(32, 3, 0x0000f000, 0x00003000);
0082 CHECK_ENC_GET(32, 5, 0x000f0000, 0x00050000);
0083 CHECK_ENC_GET(32, 7, 0x00f00000, 0x00700000);
0084 CHECK_ENC_GET(32, 14, 0x0f000000, 0x0e000000);
0085 CHECK_ENC_GET(32, 15, 0xf0000000, 0xf0000000);
0086
0087 CHECK_ENC_GET(64, 1, 0x00000f0000000000ull, 0x0000010000000000ull);
0088 CHECK_ENC_GET(64, 3, 0x0000f00000000000ull, 0x0000300000000000ull);
0089 CHECK_ENC_GET(64, 5, 0x000f000000000000ull, 0x0005000000000000ull);
0090 CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull);
0091 CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
0092 CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
0093 }
0094
0095 #define CHECK(tp, mask) do { \
0096 u64 v; \
0097 \
0098 for (v = 0; v < 1 << hweight32(mask); v++) \
0099 KUNIT_ASSERT_FALSE(context, \
0100 tp##_encode_bits(v, mask) != v << __ffs64(mask));\
0101 } while (0)
0102
0103 static void __init test_bitfields_variables(struct kunit *context)
0104 {
0105 CHECK(u8, 0x0f);
0106 CHECK(u8, 0xf0);
0107 CHECK(u8, 0x38);
0108
0109 CHECK(u16, 0x0038);
0110 CHECK(u16, 0x0380);
0111 CHECK(u16, 0x3800);
0112 CHECK(u16, 0x8000);
0113
0114 CHECK(u32, 0x80000000);
0115 CHECK(u32, 0x7f000000);
0116 CHECK(u32, 0x07e00000);
0117 CHECK(u32, 0x00018000);
0118
0119 CHECK(u64, 0x8000000000000000ull);
0120 CHECK(u64, 0x7f00000000000000ull);
0121 CHECK(u64, 0x0001800000000000ull);
0122 CHECK(u64, 0x0000000080000000ull);
0123 CHECK(u64, 0x000000007f000000ull);
0124 CHECK(u64, 0x0000000018000000ull);
0125 CHECK(u64, 0x0000001f8000000ull);
0126 }
0127
0128 #ifdef TEST_BITFIELD_COMPILE
0129 static void __init test_bitfields_compile(struct kunit *context)
0130 {
0131
0132 CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
0133 u32_encode_bits(7, 0x06000000);
0134
0135
0136 u16_encode_bits(0, 0x60000);
0137 }
0138 #endif
0139
0140 static struct kunit_case __refdata bitfields_test_cases[] = {
0141 KUNIT_CASE(test_bitfields_constants),
0142 KUNIT_CASE(test_bitfields_variables),
0143 {}
0144 };
0145
0146 static struct kunit_suite bitfields_test_suite = {
0147 .name = "bitfields",
0148 .test_cases = bitfields_test_cases,
0149 };
0150
0151 kunit_test_suites(&bitfields_test_suite);
0152
0153 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
0154 MODULE_LICENSE("GPL");