bitfield_kunit.c 4.4 KB

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