test_linear_ranges.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. LINEAR_RANGE(RANGE1_MIN, RANGE1_MIN_SEL, RANGE1_MAX_SEL, RANGE1_STEP),
  99. LINEAR_RANGE(RANGE2_MIN, RANGE2_MIN_SEL, RANGE2_MAX_SEL, RANGE2_STEP),
  100. };
  101. static void range_test_get_value(struct kunit *test)
  102. {
  103. int ret, i;
  104. unsigned int sel, val;
  105. for (i = 0; i < RANGE1_NUM_VALS; i++) {
  106. sel = range1_sels[i];
  107. ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
  108. KUNIT_EXPECT_EQ(test, 0, ret);
  109. KUNIT_EXPECT_EQ(test, val, range1_vals[i]);
  110. }
  111. for (i = 0; i < RANGE2_NUM_VALS; i++) {
  112. sel = range2_sels[i];
  113. ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
  114. KUNIT_EXPECT_EQ(test, 0, ret);
  115. KUNIT_EXPECT_EQ(test, val, range2_vals[i]);
  116. }
  117. ret = linear_range_get_value_array(&testr[0], 2, sel + 1, &val);
  118. KUNIT_EXPECT_NE(test, 0, ret);
  119. }
  120. static void range_test_get_selector_high(struct kunit *test)
  121. {
  122. int ret, i;
  123. unsigned int sel;
  124. bool found;
  125. for (i = 0; i < RANGE1_NUM_VALS; i++) {
  126. ret = linear_range_get_selector_high(&testr[0], range1_vals[i],
  127. &sel, &found);
  128. KUNIT_EXPECT_EQ(test, 0, ret);
  129. KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
  130. KUNIT_EXPECT_TRUE(test, found);
  131. }
  132. ret = linear_range_get_selector_high(&testr[0], RANGE1_MAX_VAL + 1,
  133. &sel, &found);
  134. KUNIT_EXPECT_LE(test, ret, 0);
  135. ret = linear_range_get_selector_high(&testr[0], RANGE1_MIN - 1,
  136. &sel, &found);
  137. KUNIT_EXPECT_EQ(test, 0, ret);
  138. KUNIT_EXPECT_FALSE(test, found);
  139. KUNIT_EXPECT_EQ(test, sel, range1_sels[0]);
  140. }
  141. static void range_test_get_value_amount(struct kunit *test)
  142. {
  143. int ret;
  144. ret = linear_range_values_in_range_array(&testr[0], 2);
  145. KUNIT_EXPECT_EQ(test, (int)RANGE_NUM_VALS, ret);
  146. }
  147. static void range_test_get_selector_low(struct kunit *test)
  148. {
  149. int i, ret;
  150. unsigned int sel;
  151. bool found;
  152. for (i = 0; i < RANGE1_NUM_VALS; i++) {
  153. ret = linear_range_get_selector_low_array(&testr[0], 2,
  154. range1_vals[i], &sel,
  155. &found);
  156. KUNIT_EXPECT_EQ(test, 0, ret);
  157. KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
  158. KUNIT_EXPECT_TRUE(test, found);
  159. }
  160. for (i = 0; i < RANGE2_NUM_VALS; i++) {
  161. ret = linear_range_get_selector_low_array(&testr[0], 2,
  162. range2_vals[i], &sel,
  163. &found);
  164. KUNIT_EXPECT_EQ(test, 0, ret);
  165. KUNIT_EXPECT_EQ(test, sel, range2_sels[i]);
  166. KUNIT_EXPECT_TRUE(test, found);
  167. }
  168. /*
  169. * Seek value greater than range max => get_selector_*_low should
  170. * return Ok - but set found to false as value is not in range
  171. */
  172. ret = linear_range_get_selector_low_array(&testr[0], 2,
  173. range2_vals[RANGE2_NUM_VALS - 1] + 1,
  174. &sel, &found);
  175. KUNIT_EXPECT_EQ(test, 0, ret);
  176. KUNIT_EXPECT_EQ(test, sel, range2_sels[RANGE2_NUM_VALS - 1]);
  177. KUNIT_EXPECT_FALSE(test, found);
  178. }
  179. static struct kunit_case range_test_cases[] = {
  180. KUNIT_CASE(range_test_get_value_amount),
  181. KUNIT_CASE(range_test_get_selector_high),
  182. KUNIT_CASE(range_test_get_selector_low),
  183. KUNIT_CASE(range_test_get_value),
  184. {},
  185. };
  186. static struct kunit_suite range_test_module = {
  187. .name = "linear-ranges-test",
  188. .test_cases = range_test_cases,
  189. };
  190. kunit_test_suites(&range_test_module);
  191. MODULE_DESCRIPTION("KUnit test for the linear_ranges helper");
  192. MODULE_LICENSE("GPL");