bitops_kunit.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Intel Corporation
  4. * Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/module.h>
  8. #include <kunit/test.h>
  9. /* use an enum because that's the most common BITMAP usage */
  10. enum bitops_fun {
  11. BITOPS_4 = 4,
  12. BITOPS_7 = 7,
  13. BITOPS_11 = 11,
  14. BITOPS_31 = 31,
  15. BITOPS_88 = 88,
  16. BITOPS_LENGTH = 256
  17. };
  18. struct bitops_test_case {
  19. const char *str;
  20. const long nr;
  21. };
  22. static struct bitops_test_case bitops_cases[] = {
  23. {
  24. .str = "BITOPS_4",
  25. .nr = BITOPS_4,
  26. },
  27. {
  28. .str = "BITOPS_7",
  29. .nr = BITOPS_7,
  30. },
  31. {
  32. .str = "BITOPS_11",
  33. .nr = BITOPS_11,
  34. },
  35. {
  36. .str = "BITOPS_31",
  37. .nr = BITOPS_31,
  38. },
  39. {
  40. .str = "BITOPS_88",
  41. .nr = BITOPS_88,
  42. },
  43. };
  44. KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
  45. static void test_set_bit_clear_bit(struct kunit *test)
  46. {
  47. const struct bitops_test_case *params = test->param_value;
  48. DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
  49. int bit_set;
  50. bitmap_zero(bitmap, BITOPS_LENGTH);
  51. set_bit(params->nr, bitmap);
  52. KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
  53. clear_bit(params->nr, bitmap);
  54. KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
  55. bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
  56. KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
  57. }
  58. static void test_change_bit(struct kunit *test)
  59. {
  60. const struct bitops_test_case *params = test->param_value;
  61. DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
  62. int bit_set;
  63. bitmap_zero(bitmap, BITOPS_LENGTH);
  64. change_bit(params->nr, bitmap);
  65. KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
  66. change_bit(params->nr, bitmap);
  67. KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
  68. bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
  69. KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
  70. }
  71. static void test_test_and_set_bit_test_and_clear_bit(struct kunit *test)
  72. {
  73. const struct bitops_test_case *params = test->param_value;
  74. DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
  75. int bit_set;
  76. bitmap_zero(bitmap, BITOPS_LENGTH);
  77. KUNIT_EXPECT_FALSE(test, test_and_set_bit(params->nr, bitmap));
  78. KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
  79. KUNIT_EXPECT_TRUE(test, test_and_set_bit(params->nr, bitmap));
  80. KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
  81. KUNIT_EXPECT_TRUE(test, test_and_clear_bit(params->nr, bitmap));
  82. KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
  83. KUNIT_EXPECT_FALSE(test, test_and_clear_bit(params->nr, bitmap));
  84. KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
  85. bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
  86. KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
  87. }
  88. static void test_test_and_change_bit(struct kunit *test)
  89. {
  90. const struct bitops_test_case *params = test->param_value;
  91. DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
  92. int bit_set;
  93. bitmap_zero(bitmap, BITOPS_LENGTH);
  94. KUNIT_EXPECT_FALSE(test, test_and_change_bit(params->nr, bitmap));
  95. KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
  96. KUNIT_EXPECT_TRUE(test, test_and_change_bit(params->nr, bitmap));
  97. KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
  98. bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
  99. KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
  100. }
  101. struct order_test_case {
  102. const char *str;
  103. const unsigned int count;
  104. const int expected;
  105. };
  106. static struct order_test_case order_test_cases[] = {
  107. {"0x00000003", 0x00000003, 2},
  108. {"0x00000004", 0x00000004, 2},
  109. {"0x00001fff", 0x00001fff, 13},
  110. {"0x00002000", 0x00002000, 13},
  111. {"0x50000000", 0x50000000, 31},
  112. {"0x80000000", 0x80000000, 31},
  113. {"0x80003000", 0x80003000, 32},
  114. };
  115. KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
  116. static void test_get_count_order(struct kunit *test)
  117. {
  118. const struct order_test_case *params = test->param_value;
  119. KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
  120. KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
  121. }
  122. #ifdef CONFIG_64BIT
  123. struct order_long_test_case {
  124. const char *str;
  125. const unsigned long count;
  126. const int expected;
  127. };
  128. static struct order_long_test_case order_long_test_cases[] = {
  129. {"0x0000000300000000", 0x0000000300000000, 34},
  130. {"0x0000000400000000", 0x0000000400000000, 34},
  131. {"0x00001fff00000000", 0x00001fff00000000, 45},
  132. {"0x0000200000000000", 0x0000200000000000, 45},
  133. {"0x5000000000000000", 0x5000000000000000, 63},
  134. {"0x8000000000000000", 0x8000000000000000, 63},
  135. {"0x8000300000000000", 0x8000300000000000, 64},
  136. };
  137. KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
  138. static void test_get_count_order_long(struct kunit *test)
  139. {
  140. const struct order_long_test_case *params = test->param_value;
  141. KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
  142. }
  143. #endif
  144. static struct kunit_case bitops_test_cases[] = {
  145. KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
  146. KUNIT_CASE_PARAM(test_change_bit, bitops_gen_params),
  147. KUNIT_CASE_PARAM(test_test_and_set_bit_test_and_clear_bit, bitops_gen_params),
  148. KUNIT_CASE_PARAM(test_test_and_change_bit, bitops_gen_params),
  149. KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
  150. #ifdef CONFIG_64BIT
  151. KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
  152. #endif
  153. {},
  154. };
  155. static struct kunit_suite bitops_test_suite = {
  156. .name = "bitops",
  157. .test_cases = bitops_test_cases,
  158. };
  159. kunit_test_suite(bitops_test_suite);
  160. MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
  161. MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");
  162. MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
  163. MODULE_LICENSE("GPL");
  164. MODULE_DESCRIPTION("Bit testing module");