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