Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * KUnit test for the linear_ranges helper.
0004  *
0005  * Copyright (C) 2020, ROHM Semiconductors.
0006  * Author: Matti Vaittinen <matti.vaittien@fi.rohmeurope.com>
0007  */
0008 #include <kunit/test.h>
0009 
0010 #include <linux/linear_range.h>
0011 
0012 /* First things first. I deeply dislike unit-tests. I have seen all the hell
0013  * breaking loose when people who think the unit tests are "the silver bullet"
0014  * to kill bugs get to decide how a company should implement testing strategy...
0015  *
0016  * Believe me, it may get _really_ ridiculous. It is tempting to think that
0017  * walking through all the possible execution branches will nail down 100% of
0018  * bugs. This may lead to ideas about demands to get certain % of "test
0019  * coverage" - measured as line coverage. And that is one of the worst things
0020  * you can do.
0021  *
0022  * Ask people to provide line coverage and they do. I've seen clever tools
0023  * which generate test cases to test the existing functions - and by default
0024  * these tools expect code to be correct and just generate checks which are
0025  * passing when ran against current code-base. Run this generator and you'll get
0026  * tests that do not test code is correct but just verify nothing changes.
0027  * Problem is that testing working code is pointless. And if it is not
0028  * working, your test must not assume it is working. You won't catch any bugs
0029  * by such tests. What you can do is to generate a huge amount of tests.
0030  * Especially if you were are asked to proivde 100% line-coverage x_x. So what
0031  * does these tests - which are not finding any bugs now - do?
0032  *
0033  * They add inertia to every future development. I think it was Terry Pratchet
0034  * who wrote someone having same impact as thick syrup has to chronometre.
0035  * Excessive amount of unit-tests have this effect to development. If you do
0036  * actually find _any_ bug from code in such environment and try fixing it...
0037  * ...chances are you also need to fix the test cases. In sunny day you fix one
0038  * test. But I've done refactoring which resulted 500+ broken tests (which had
0039  * really zero value other than proving to managers that we do do "quality")...
0040  *
0041  * After this being said - there are situations where UTs can be handy. If you
0042  * have algorithms which take some input and should produce output - then you
0043  * can implement few, carefully selected simple UT-cases which test this. I've
0044  * previously used this for example for netlink and device-tree data parsing
0045  * functions. Feed some data examples to functions and verify the output is as
0046  * expected. I am not covering all the cases but I will see the logic should be
0047  * working.
0048  *
0049  * Here we also do some minor testing. I don't want to go through all branches
0050  * or test more or less obvious things - but I want to see the main logic is
0051  * working. And I definitely don't want to add 500+ test cases that break when
0052  * some simple fix is done x_x. So - let's only add few, well selected tests
0053  * which ensure as much logic is good as possible.
0054  */
0055 
0056 /*
0057  * Test Range 1:
0058  * selectors:   2   3   4   5   6
0059  * values (5):  10  20  30  40  50
0060  *
0061  * Test Range 2:
0062  * selectors:   7   8   9   10
0063  * values (4):  100 150 200 250
0064  */
0065 
0066 #define RANGE1_MIN 10
0067 #define RANGE1_MIN_SEL 2
0068 #define RANGE1_STEP 10
0069 
0070 /* 2, 3, 4, 5, 6 */
0071 static const unsigned int range1_sels[] = { RANGE1_MIN_SEL, RANGE1_MIN_SEL + 1,
0072                         RANGE1_MIN_SEL + 2,
0073                         RANGE1_MIN_SEL + 3,
0074                         RANGE1_MIN_SEL + 4 };
0075 /* 10, 20, 30, 40, 50 */
0076 static const unsigned int range1_vals[] = { RANGE1_MIN, RANGE1_MIN +
0077                         RANGE1_STEP,
0078                         RANGE1_MIN + RANGE1_STEP * 2,
0079                         RANGE1_MIN + RANGE1_STEP * 3,
0080                         RANGE1_MIN + RANGE1_STEP * 4 };
0081 
0082 #define RANGE2_MIN 100
0083 #define RANGE2_MIN_SEL 7
0084 #define RANGE2_STEP 50
0085 
0086 /*  7, 8, 9, 10 */
0087 static const unsigned int range2_sels[] = { RANGE2_MIN_SEL, RANGE2_MIN_SEL + 1,
0088                         RANGE2_MIN_SEL + 2,
0089                         RANGE2_MIN_SEL + 3 };
0090 /* 100, 150, 200, 250 */
0091 static const unsigned int range2_vals[] = { RANGE2_MIN, RANGE2_MIN +
0092                         RANGE2_STEP,
0093                         RANGE2_MIN + RANGE2_STEP * 2,
0094                         RANGE2_MIN + RANGE2_STEP * 3 };
0095 
0096 #define RANGE1_NUM_VALS (ARRAY_SIZE(range1_vals))
0097 #define RANGE2_NUM_VALS (ARRAY_SIZE(range2_vals))
0098 #define RANGE_NUM_VALS (RANGE1_NUM_VALS + RANGE2_NUM_VALS)
0099 
0100 #define RANGE1_MAX_SEL (RANGE1_MIN_SEL + RANGE1_NUM_VALS - 1)
0101 #define RANGE1_MAX_VAL (range1_vals[RANGE1_NUM_VALS - 1])
0102 
0103 #define RANGE2_MAX_SEL (RANGE2_MIN_SEL + RANGE2_NUM_VALS - 1)
0104 #define RANGE2_MAX_VAL (range2_vals[RANGE2_NUM_VALS - 1])
0105 
0106 #define SMALLEST_SEL RANGE1_MIN_SEL
0107 #define SMALLEST_VAL RANGE1_MIN
0108 
0109 static struct linear_range testr[] = {
0110     {
0111         .min = RANGE1_MIN,
0112         .min_sel = RANGE1_MIN_SEL,
0113         .max_sel = RANGE1_MAX_SEL,
0114         .step = RANGE1_STEP,
0115     }, {
0116         .min = RANGE2_MIN,
0117         .min_sel = RANGE2_MIN_SEL,
0118         .max_sel = RANGE2_MAX_SEL,
0119         .step = RANGE2_STEP
0120     },
0121 };
0122 
0123 static void range_test_get_value(struct kunit *test)
0124 {
0125     int ret, i;
0126     unsigned int sel, val;
0127 
0128     for (i = 0; i < RANGE1_NUM_VALS; i++) {
0129         sel = range1_sels[i];
0130         ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
0131         KUNIT_EXPECT_EQ(test, 0, ret);
0132         KUNIT_EXPECT_EQ(test, val, range1_vals[i]);
0133     }
0134     for (i = 0; i < RANGE2_NUM_VALS; i++) {
0135         sel = range2_sels[i];
0136         ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
0137         KUNIT_EXPECT_EQ(test, 0, ret);
0138         KUNIT_EXPECT_EQ(test, val, range2_vals[i]);
0139     }
0140     ret = linear_range_get_value_array(&testr[0], 2, sel + 1, &val);
0141     KUNIT_EXPECT_NE(test, 0, ret);
0142 }
0143 
0144 static void range_test_get_selector_high(struct kunit *test)
0145 {
0146     int ret, i;
0147     unsigned int sel;
0148     bool found;
0149 
0150     for (i = 0; i < RANGE1_NUM_VALS; i++) {
0151         ret = linear_range_get_selector_high(&testr[0], range1_vals[i],
0152                              &sel, &found);
0153         KUNIT_EXPECT_EQ(test, 0, ret);
0154         KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
0155         KUNIT_EXPECT_TRUE(test, found);
0156     }
0157 
0158     ret = linear_range_get_selector_high(&testr[0], RANGE1_MAX_VAL + 1,
0159                          &sel, &found);
0160     KUNIT_EXPECT_LE(test, ret, 0);
0161 
0162     ret = linear_range_get_selector_high(&testr[0], RANGE1_MIN - 1,
0163                          &sel, &found);
0164     KUNIT_EXPECT_EQ(test, 0, ret);
0165     KUNIT_EXPECT_FALSE(test, found);
0166     KUNIT_EXPECT_EQ(test, sel, range1_sels[0]);
0167 }
0168 
0169 static void range_test_get_value_amount(struct kunit *test)
0170 {
0171     int ret;
0172 
0173     ret = linear_range_values_in_range_array(&testr[0], 2);
0174     KUNIT_EXPECT_EQ(test, (int)RANGE_NUM_VALS, ret);
0175 }
0176 
0177 static void range_test_get_selector_low(struct kunit *test)
0178 {
0179     int i, ret;
0180     unsigned int sel;
0181     bool found;
0182 
0183     for (i = 0; i < RANGE1_NUM_VALS; i++) {
0184         ret = linear_range_get_selector_low_array(&testr[0], 2,
0185                               range1_vals[i], &sel,
0186                               &found);
0187         KUNIT_EXPECT_EQ(test, 0, ret);
0188         KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
0189         KUNIT_EXPECT_TRUE(test, found);
0190     }
0191     for (i = 0; i < RANGE2_NUM_VALS; i++) {
0192         ret = linear_range_get_selector_low_array(&testr[0], 2,
0193                               range2_vals[i], &sel,
0194                               &found);
0195         KUNIT_EXPECT_EQ(test, 0, ret);
0196         KUNIT_EXPECT_EQ(test, sel, range2_sels[i]);
0197         KUNIT_EXPECT_TRUE(test, found);
0198     }
0199 
0200     /*
0201      * Seek value greater than range max => get_selector_*_low should
0202      * return Ok - but set found to false as value is not in range
0203      */
0204     ret = linear_range_get_selector_low_array(&testr[0], 2,
0205                     range2_vals[RANGE2_NUM_VALS - 1] + 1,
0206                     &sel, &found);
0207 
0208     KUNIT_EXPECT_EQ(test, 0, ret);
0209     KUNIT_EXPECT_EQ(test, sel, range2_sels[RANGE2_NUM_VALS - 1]);
0210     KUNIT_EXPECT_FALSE(test, found);
0211 }
0212 
0213 static struct kunit_case range_test_cases[] = {
0214     KUNIT_CASE(range_test_get_value_amount),
0215     KUNIT_CASE(range_test_get_selector_high),
0216     KUNIT_CASE(range_test_get_selector_low),
0217     KUNIT_CASE(range_test_get_value),
0218     {},
0219 };
0220 
0221 static struct kunit_suite range_test_module = {
0222     .name = "linear-ranges-test",
0223     .test_cases = range_test_cases,
0224 };
0225 
0226 kunit_test_suites(&range_test_module);
0227 
0228 MODULE_LICENSE("GPL");