^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Test cases for bitfield helpers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <kunit/test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/bitfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define CHECK_ENC_GET_U(tp, v, field, res) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) u##tp _res; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) _res = u##tp##_encode_bits(v, field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) KUNIT_ASSERT_FALSE_MSG(context, _res != res, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) "u" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != " #res "\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) (u64)_res); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) KUNIT_ASSERT_FALSE(context, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) u##tp##_get_bits(_res, field) != v); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define CHECK_ENC_GET_LE(tp, v, field, res) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) __le##tp _res; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) _res = le##tp##_encode_bits(v, field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) KUNIT_ASSERT_FALSE_MSG(context, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) _res != cpu_to_le##tp(res), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) "le" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx",\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (u64)le##tp##_to_cpu(_res), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) (u64)(res)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) KUNIT_ASSERT_FALSE(context, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) le##tp##_get_bits(_res, field) != v);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define CHECK_ENC_GET_BE(tp, v, field, res) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __be##tp _res; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) _res = be##tp##_encode_bits(v, field); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) KUNIT_ASSERT_FALSE_MSG(context, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) _res != cpu_to_be##tp(res), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "be" #tp "_encode_bits(" #v ", " #field ") is 0x%llx != 0x%llx", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) (u64)be##tp##_to_cpu(_res), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) (u64)(res)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) KUNIT_ASSERT_FALSE(context, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) be##tp##_get_bits(_res, field) != v);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define CHECK_ENC_GET(tp, v, field, res) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) CHECK_ENC_GET_U(tp, v, field, res); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) CHECK_ENC_GET_LE(tp, v, field, res); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) CHECK_ENC_GET_BE(tp, v, field, res); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void __init test_bitfields_constants(struct kunit *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * NOTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * This whole function compiles (or at least should, if everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * is going according to plan) to nothing after optimisation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) CHECK_ENC_GET(16, 1, 0x000f, 0x0001);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) CHECK_ENC_GET(16, 3, 0x00f0, 0x0030);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) CHECK_ENC_GET(16, 5, 0x0f00, 0x0500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) CHECK_ENC_GET(16, 7, 0xf000, 0x7000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) CHECK_ENC_GET(16, 14, 0x000f, 0x000e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) CHECK_ENC_GET(16, 15, 0x00f0, 0x00f0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) CHECK_ENC_GET_U(8, 1, 0x0f, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) CHECK_ENC_GET_U(8, 3, 0xf0, 0x30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) CHECK_ENC_GET_U(8, 14, 0x0f, 0x0e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) CHECK_ENC_GET_U(8, 15, 0xf0, 0xf0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) CHECK_ENC_GET(32, 1, 0x00000f00, 0x00000100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) CHECK_ENC_GET(32, 3, 0x0000f000, 0x00003000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) CHECK_ENC_GET(32, 5, 0x000f0000, 0x00050000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) CHECK_ENC_GET(32, 7, 0x00f00000, 0x00700000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) CHECK_ENC_GET(32, 14, 0x0f000000, 0x0e000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) CHECK_ENC_GET(32, 15, 0xf0000000, 0xf0000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) CHECK_ENC_GET(64, 1, 0x00000f0000000000ull, 0x0000010000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) CHECK_ENC_GET(64, 3, 0x0000f00000000000ull, 0x0000300000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) CHECK_ENC_GET(64, 5, 0x000f000000000000ull, 0x0005000000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) CHECK_ENC_GET(64, 7, 0x00f0000000000000ull, 0x0070000000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) CHECK_ENC_GET(64, 14, 0x0f00000000000000ull, 0x0e00000000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) CHECK_ENC_GET(64, 15, 0xf000000000000000ull, 0xf000000000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define CHECK(tp, mask) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u64 v; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (v = 0; v < 1 << hweight32(mask); v++) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) KUNIT_ASSERT_FALSE(context, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) tp##_encode_bits(v, mask) != v << __ffs64(mask));\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void __init test_bitfields_variables(struct kunit *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) CHECK(u8, 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) CHECK(u8, 0xf0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) CHECK(u8, 0x38);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) CHECK(u16, 0x0038);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) CHECK(u16, 0x0380);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) CHECK(u16, 0x3800);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) CHECK(u16, 0x8000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) CHECK(u32, 0x80000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) CHECK(u32, 0x7f000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) CHECK(u32, 0x07e00000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) CHECK(u32, 0x00018000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) CHECK(u64, 0x8000000000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) CHECK(u64, 0x7f00000000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) CHECK(u64, 0x0001800000000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) CHECK(u64, 0x0000000080000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) CHECK(u64, 0x000000007f000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) CHECK(u64, 0x0000000018000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) CHECK(u64, 0x0000001f8000000ull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #ifdef TEST_BITFIELD_COMPILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void __init test_bitfields_compile(struct kunit *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* these should fail compilation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) CHECK_ENC_GET(16, 16, 0x0f00, 0x1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u32_encode_bits(7, 0x06000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* this should at least give a warning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) u16_encode_bits(0, 0x60000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static struct kunit_case __refdata bitfields_test_cases[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) KUNIT_CASE(test_bitfields_constants),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) KUNIT_CASE(test_bitfields_variables),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static struct kunit_suite bitfields_test_suite = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .name = "bitfields",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .test_cases = bitfields_test_cases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) kunit_test_suites(&bitfields_test_suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_LICENSE("GPL");