test_linear_ranges.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for the linear_ranges helper.
  4. *
  5. * Copyright (C) 2020, ROHM Semiconductors.
  6. * Author: Matti Vaittinen <matti.vaittien@fi.rohmeurope.com>
  7. */
  8. #include <kunit/test.h>
  9. #include <linux/linear_range.h>
  10. /* First things first. I deeply dislike unit-tests. I have seen all the hell
  11. * breaking loose when people who think the unit tests are "the silver bullet"
  12. * to kill bugs get to decide how a company should implement testing strategy...
  13. *
  14. * Believe me, it may get _really_ ridiculous. It is tempting to think that
  15. * walking through all the possible execution branches will nail down 100% of
  16. * bugs. This may lead to ideas about demands to get certain % of "test
  17. * coverage" - measured as line coverage. And that is one of the worst things
  18. * you can do.
  19. *
  20. * Ask people to provide line coverage and they do. I've seen clever tools
  21. * which generate test cases to test the existing functions - and by default
  22. * these tools expect code to be correct and just generate checks which are
  23. * passing when ran against current code-base. Run this generator and you'll get
  24. * tests that do not test code is correct but just verify nothing changes.
  25. * Problem is that testing working code is pointless. And if it is not
  26. * working, your test must not assume it is working. You won't catch any bugs
  27. * by such tests. What you can do is to generate a huge amount of tests.
  28. * Especially if you were are asked to proivde 100% line-coverage x_x. So what
  29. * does these tests - which are not finding any bugs now - do?
  30. *
  31. * They add inertia to every future development. I think it was Terry Pratchet
  32. * who wrote someone having same impact as thick syrup has to chronometre.
  33. * Excessive amount of unit-tests have this effect to development. If you do
  34. * actually find _any_ bug from code in such environment and try fixing it...
  35. * ...chances are you also need to fix the test cases. In sunny day you fix one
  36. * test. But I've done refactoring which resulted 500+ broken tests (which had
  37. * really zero value other than proving to managers that we do do "quality")...
  38. *
  39. * After this being said - there are situations where UTs can be handy. If you
  40. * have algorithms which take some input and should produce output - then you
  41. * can implement few, carefully selected simple UT-cases which test this. I've
  42. * previously used this for example for netlink and device-tree data parsing
  43. * functions. Feed some data examples to functions and verify the output is as
  44. * expected. I am not covering all the cases but I will see the logic should be
  45. * working.
  46. *
  47. * Here we also do some minor testing. I don't want to go through all branches
  48. * or test more or less obvious things - but I want to see the main logic is
  49. * working. And I definitely don't want to add 500+ test cases that break when
  50. * some simple fix is done x_x. So - let's only add few, well selected tests
  51. * which ensure as much logic is good as possible.
  52. */
  53. /*
  54. * Test Range 1:
  55. * selectors: 2 3 4 5 6
  56. * values (5): 10 20 30 40 50
  57. *
  58. * Test Range 2:
  59. * selectors: 7 8 9 10
  60. * values (4): 100 150 200 250
  61. */
  62. #define RANGE1_MIN 10
  63. #define RANGE1_MIN_SEL 2
  64. #define RANGE1_STEP 10
  65. /* 2, 3, 4, 5, 6 */
  66. static const unsigned int range1_sels[] = { RANGE1_MIN_SEL, RANGE1_MIN_SEL + 1,
  67. RANGE1_MIN_SEL + 2,
  68. RANGE1_MIN_SEL + 3,
  69. RANGE1_MIN_SEL + 4 };
  70. /* 10, 20, 30, 40, 50 */
  71. static const unsigned int range1_vals[] = { RANGE1_MIN, RANGE1_MIN +
  72. RANGE1_STEP,
  73. RANGE1_MIN + RANGE1_STEP * 2,
  74. RANGE1_MIN + RANGE1_STEP * 3,
  75. RANGE1_MIN + RANGE1_STEP * 4 };
  76. #define RANGE2_MIN 100
  77. #define RANGE2_MIN_SEL 7
  78. #define RANGE2_STEP 50
  79. /* 7, 8, 9, 10 */
  80. static const unsigned int range2_sels[] = { RANGE2_MIN_SEL, RANGE2_MIN_SEL + 1,
  81. RANGE2_MIN_SEL + 2,
  82. RANGE2_MIN_SEL + 3 };
  83. /* 100, 150, 200, 250 */
  84. static const unsigned int range2_vals[] = { RANGE2_MIN, RANGE2_MIN +
  85. RANGE2_STEP,
  86. RANGE2_MIN + RANGE2_STEP * 2,
  87. RANGE2_MIN + RANGE2_STEP * 3 };
  88. #define RANGE1_NUM_VALS (ARRAY_SIZE(range1_vals))
  89. #define RANGE2_NUM_VALS (ARRAY_SIZE(range2_vals))
  90. #define RANGE_NUM_VALS (RANGE1_NUM_VALS + RANGE2_NUM_VALS)
  91. #define RANGE1_MAX_SEL (RANGE1_MIN_SEL + RANGE1_NUM_VALS - 1)
  92. #define RANGE1_MAX_VAL (range1_vals[RANGE1_NUM_VALS - 1])
  93. #define RANGE2_MAX_SEL (RANGE2_MIN_SEL + RANGE2_NUM_VALS - 1)
  94. #define RANGE2_MAX_VAL (range2_vals[RANGE2_NUM_VALS - 1])
  95. #define SMALLEST_SEL RANGE1_MIN_SEL
  96. #define SMALLEST_VAL RANGE1_MIN
  97. static struct linear_range testr[] = {
  98. {
  99. .min = RANGE1_MIN,
  100. .min_sel = RANGE1_MIN_SEL,
  101. .max_sel = RANGE1_MAX_SEL,
  102. .step = RANGE1_STEP,
  103. }, {
  104. .min = RANGE2_MIN,
  105. .min_sel = RANGE2_MIN_SEL,
  106. .max_sel = RANGE2_MAX_SEL,
  107. .step = RANGE2_STEP
  108. },
  109. };
  110. static void range_test_get_value(struct kunit *test)
  111. {
  112. int ret, i;
  113. unsigned int sel, val;
  114. for (i = 0; i < RANGE1_NUM_VALS; i++) {
  115. sel = range1_sels[i];
  116. ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
  117. KUNIT_EXPECT_EQ(test, 0, ret);
  118. KUNIT_EXPECT_EQ(test, val, range1_vals[i]);
  119. }
  120. for (i = 0; i < RANGE2_NUM_VALS; i++) {
  121. sel = range2_sels[i];
  122. ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
  123. KUNIT_EXPECT_EQ(test, 0, ret);
  124. KUNIT_EXPECT_EQ(test, val, range2_vals[i]);
  125. }
  126. ret = linear_range_get_value_array(&testr[0], 2, sel + 1, &val);
  127. KUNIT_EXPECT_NE(test, 0, ret);
  128. }
  129. static void range_test_get_selector_high(struct kunit *test)
  130. {
  131. int ret, i;
  132. unsigned int sel;
  133. bool found;
  134. for (i = 0; i < RANGE1_NUM_VALS; i++) {
  135. ret = linear_range_get_selector_high(&testr[0], range1_vals[i],
  136. &sel, &found);
  137. KUNIT_EXPECT_EQ(test, 0, ret);
  138. KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
  139. KUNIT_EXPECT_TRUE(test, found);
  140. }
  141. ret = linear_range_get_selector_high(&testr[0], RANGE1_MAX_VAL + 1,
  142. &sel, &found);
  143. KUNIT_EXPECT_LE(test, ret, 0);
  144. ret = linear_range_get_selector_high(&testr[0], RANGE1_MIN - 1,
  145. &sel, &found);
  146. KUNIT_EXPECT_EQ(test, 0, ret);
  147. KUNIT_EXPECT_FALSE(test, found);
  148. KUNIT_EXPECT_EQ(test, sel, range1_sels[0]);
  149. }
  150. static void range_test_get_value_amount(struct kunit *test)
  151. {
  152. int ret;
  153. ret = linear_range_values_in_range_array(&testr[0], 2);
  154. KUNIT_EXPECT_EQ(test, (int)RANGE_NUM_VALS, ret);
  155. }
  156. static void range_test_get_selector_low(struct kunit *test)
  157. {
  158. int i, ret;
  159. unsigned int sel;
  160. bool found;
  161. for (i = 0; i < RANGE1_NUM_VALS; i++) {
  162. ret = linear_range_get_selector_low_array(&testr[0], 2,
  163. range1_vals[i], &sel,
  164. &found);
  165. KUNIT_EXPECT_EQ(test, 0, ret);
  166. KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
  167. KUNIT_EXPECT_TRUE(test, found);
  168. }
  169. for (i = 0; i < RANGE2_NUM_VALS; i++) {
  170. ret = linear_range_get_selector_low_array(&testr[0], 2,
  171. range2_vals[i], &sel,
  172. &found);
  173. KUNIT_EXPECT_EQ(test, 0, ret);
  174. KUNIT_EXPECT_EQ(test, sel, range2_sels[i]);
  175. KUNIT_EXPECT_TRUE(test, found);
  176. }
  177. /*
  178. * Seek value greater than range max => get_selector_*_low should
  179. * return Ok - but set found to false as value is not in range
  180. */
  181. ret = linear_range_get_selector_low_array(&testr[0], 2,
  182. range2_vals[RANGE2_NUM_VALS - 1] + 1,
  183. &sel, &found);
  184. KUNIT_EXPECT_EQ(test, 0, ret);
  185. KUNIT_EXPECT_EQ(test, sel, range2_sels[RANGE2_NUM_VALS - 1]);
  186. KUNIT_EXPECT_FALSE(test, found);
  187. }
  188. static struct kunit_case range_test_cases[] = {
  189. KUNIT_CASE(range_test_get_value_amount),
  190. KUNIT_CASE(range_test_get_selector_high),
  191. KUNIT_CASE(range_test_get_selector_low),
  192. KUNIT_CASE(range_test_get_value),
  193. {},
  194. };
  195. static struct kunit_suite range_test_module = {
  196. .name = "linear-ranges-test",
  197. .test_cases = range_test_cases,
  198. };
  199. kunit_test_suites(&range_test_module);
  200. MODULE_LICENSE("GPL");