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