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