162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * KUnit test for the linear_ranges helper.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2020, ROHM Semiconductors.
662306a36Sopenharmony_ci * Author: Matti Vaittinen <matti.vaittien@fi.rohmeurope.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <kunit/test.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/linear_range.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* First things first. I deeply dislike unit-tests. I have seen all the hell
1362306a36Sopenharmony_ci * breaking loose when people who think the unit tests are "the silver bullet"
1462306a36Sopenharmony_ci * to kill bugs get to decide how a company should implement testing strategy...
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * Believe me, it may get _really_ ridiculous. It is tempting to think that
1762306a36Sopenharmony_ci * walking through all the possible execution branches will nail down 100% of
1862306a36Sopenharmony_ci * bugs. This may lead to ideas about demands to get certain % of "test
1962306a36Sopenharmony_ci * coverage" - measured as line coverage. And that is one of the worst things
2062306a36Sopenharmony_ci * you can do.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Ask people to provide line coverage and they do. I've seen clever tools
2362306a36Sopenharmony_ci * which generate test cases to test the existing functions - and by default
2462306a36Sopenharmony_ci * these tools expect code to be correct and just generate checks which are
2562306a36Sopenharmony_ci * passing when ran against current code-base. Run this generator and you'll get
2662306a36Sopenharmony_ci * tests that do not test code is correct but just verify nothing changes.
2762306a36Sopenharmony_ci * Problem is that testing working code is pointless. And if it is not
2862306a36Sopenharmony_ci * working, your test must not assume it is working. You won't catch any bugs
2962306a36Sopenharmony_ci * by such tests. What you can do is to generate a huge amount of tests.
3062306a36Sopenharmony_ci * Especially if you were are asked to proivde 100% line-coverage x_x. So what
3162306a36Sopenharmony_ci * does these tests - which are not finding any bugs now - do?
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * They add inertia to every future development. I think it was Terry Pratchet
3462306a36Sopenharmony_ci * who wrote someone having same impact as thick syrup has to chronometre.
3562306a36Sopenharmony_ci * Excessive amount of unit-tests have this effect to development. If you do
3662306a36Sopenharmony_ci * actually find _any_ bug from code in such environment and try fixing it...
3762306a36Sopenharmony_ci * ...chances are you also need to fix the test cases. In sunny day you fix one
3862306a36Sopenharmony_ci * test. But I've done refactoring which resulted 500+ broken tests (which had
3962306a36Sopenharmony_ci * really zero value other than proving to managers that we do do "quality")...
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * After this being said - there are situations where UTs can be handy. If you
4262306a36Sopenharmony_ci * have algorithms which take some input and should produce output - then you
4362306a36Sopenharmony_ci * can implement few, carefully selected simple UT-cases which test this. I've
4462306a36Sopenharmony_ci * previously used this for example for netlink and device-tree data parsing
4562306a36Sopenharmony_ci * functions. Feed some data examples to functions and verify the output is as
4662306a36Sopenharmony_ci * expected. I am not covering all the cases but I will see the logic should be
4762306a36Sopenharmony_ci * working.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * Here we also do some minor testing. I don't want to go through all branches
5062306a36Sopenharmony_ci * or test more or less obvious things - but I want to see the main logic is
5162306a36Sopenharmony_ci * working. And I definitely don't want to add 500+ test cases that break when
5262306a36Sopenharmony_ci * some simple fix is done x_x. So - let's only add few, well selected tests
5362306a36Sopenharmony_ci * which ensure as much logic is good as possible.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * Test Range 1:
5862306a36Sopenharmony_ci * selectors:	2	3	4	5	6
5962306a36Sopenharmony_ci * values (5):	10	20	30	40	50
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * Test Range 2:
6262306a36Sopenharmony_ci * selectors:	7	8	9	10
6362306a36Sopenharmony_ci * values (4):	100	150	200	250
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define RANGE1_MIN 10
6762306a36Sopenharmony_ci#define RANGE1_MIN_SEL 2
6862306a36Sopenharmony_ci#define RANGE1_STEP 10
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/* 2, 3, 4, 5, 6 */
7162306a36Sopenharmony_cistatic const unsigned int range1_sels[] = { RANGE1_MIN_SEL, RANGE1_MIN_SEL + 1,
7262306a36Sopenharmony_ci					    RANGE1_MIN_SEL + 2,
7362306a36Sopenharmony_ci					    RANGE1_MIN_SEL + 3,
7462306a36Sopenharmony_ci					    RANGE1_MIN_SEL + 4 };
7562306a36Sopenharmony_ci/* 10, 20, 30, 40, 50 */
7662306a36Sopenharmony_cistatic const unsigned int range1_vals[] = { RANGE1_MIN, RANGE1_MIN +
7762306a36Sopenharmony_ci					    RANGE1_STEP,
7862306a36Sopenharmony_ci					    RANGE1_MIN + RANGE1_STEP * 2,
7962306a36Sopenharmony_ci					    RANGE1_MIN + RANGE1_STEP * 3,
8062306a36Sopenharmony_ci					    RANGE1_MIN + RANGE1_STEP * 4 };
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define RANGE2_MIN 100
8362306a36Sopenharmony_ci#define RANGE2_MIN_SEL 7
8462306a36Sopenharmony_ci#define RANGE2_STEP 50
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci/*  7, 8, 9, 10 */
8762306a36Sopenharmony_cistatic const unsigned int range2_sels[] = { RANGE2_MIN_SEL, RANGE2_MIN_SEL + 1,
8862306a36Sopenharmony_ci					    RANGE2_MIN_SEL + 2,
8962306a36Sopenharmony_ci					    RANGE2_MIN_SEL + 3 };
9062306a36Sopenharmony_ci/* 100, 150, 200, 250 */
9162306a36Sopenharmony_cistatic const unsigned int range2_vals[] = { RANGE2_MIN, RANGE2_MIN +
9262306a36Sopenharmony_ci					    RANGE2_STEP,
9362306a36Sopenharmony_ci					    RANGE2_MIN + RANGE2_STEP * 2,
9462306a36Sopenharmony_ci					    RANGE2_MIN + RANGE2_STEP * 3 };
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci#define RANGE1_NUM_VALS (ARRAY_SIZE(range1_vals))
9762306a36Sopenharmony_ci#define RANGE2_NUM_VALS (ARRAY_SIZE(range2_vals))
9862306a36Sopenharmony_ci#define RANGE_NUM_VALS (RANGE1_NUM_VALS + RANGE2_NUM_VALS)
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#define RANGE1_MAX_SEL (RANGE1_MIN_SEL + RANGE1_NUM_VALS - 1)
10162306a36Sopenharmony_ci#define RANGE1_MAX_VAL (range1_vals[RANGE1_NUM_VALS - 1])
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci#define RANGE2_MAX_SEL (RANGE2_MIN_SEL + RANGE2_NUM_VALS - 1)
10462306a36Sopenharmony_ci#define RANGE2_MAX_VAL (range2_vals[RANGE2_NUM_VALS - 1])
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci#define SMALLEST_SEL RANGE1_MIN_SEL
10762306a36Sopenharmony_ci#define SMALLEST_VAL RANGE1_MIN
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_cistatic struct linear_range testr[] = {
11062306a36Sopenharmony_ci	LINEAR_RANGE(RANGE1_MIN, RANGE1_MIN_SEL, RANGE1_MAX_SEL, RANGE1_STEP),
11162306a36Sopenharmony_ci	LINEAR_RANGE(RANGE2_MIN, RANGE2_MIN_SEL, RANGE2_MAX_SEL, RANGE2_STEP),
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic void range_test_get_value(struct kunit *test)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	int ret, i;
11762306a36Sopenharmony_ci	unsigned int sel, val;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	for (i = 0; i < RANGE1_NUM_VALS; i++) {
12062306a36Sopenharmony_ci		sel = range1_sels[i];
12162306a36Sopenharmony_ci		ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
12262306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, 0, ret);
12362306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, val, range1_vals[i]);
12462306a36Sopenharmony_ci	}
12562306a36Sopenharmony_ci	for (i = 0; i < RANGE2_NUM_VALS; i++) {
12662306a36Sopenharmony_ci		sel = range2_sels[i];
12762306a36Sopenharmony_ci		ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
12862306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, 0, ret);
12962306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, val, range2_vals[i]);
13062306a36Sopenharmony_ci	}
13162306a36Sopenharmony_ci	ret = linear_range_get_value_array(&testr[0], 2, sel + 1, &val);
13262306a36Sopenharmony_ci	KUNIT_EXPECT_NE(test, 0, ret);
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic void range_test_get_selector_high(struct kunit *test)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	int ret, i;
13862306a36Sopenharmony_ci	unsigned int sel;
13962306a36Sopenharmony_ci	bool found;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	for (i = 0; i < RANGE1_NUM_VALS; i++) {
14262306a36Sopenharmony_ci		ret = linear_range_get_selector_high(&testr[0], range1_vals[i],
14362306a36Sopenharmony_ci						     &sel, &found);
14462306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, 0, ret);
14562306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
14662306a36Sopenharmony_ci		KUNIT_EXPECT_TRUE(test, found);
14762306a36Sopenharmony_ci	}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	ret = linear_range_get_selector_high(&testr[0], RANGE1_MAX_VAL + 1,
15062306a36Sopenharmony_ci					     &sel, &found);
15162306a36Sopenharmony_ci	KUNIT_EXPECT_LE(test, ret, 0);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	ret = linear_range_get_selector_high(&testr[0], RANGE1_MIN - 1,
15462306a36Sopenharmony_ci					     &sel, &found);
15562306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, ret);
15662306a36Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, found);
15762306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, sel, range1_sels[0]);
15862306a36Sopenharmony_ci}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic void range_test_get_value_amount(struct kunit *test)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	int ret;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	ret = linear_range_values_in_range_array(&testr[0], 2);
16562306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, (int)RANGE_NUM_VALS, ret);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic void range_test_get_selector_low(struct kunit *test)
16962306a36Sopenharmony_ci{
17062306a36Sopenharmony_ci	int i, ret;
17162306a36Sopenharmony_ci	unsigned int sel;
17262306a36Sopenharmony_ci	bool found;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	for (i = 0; i < RANGE1_NUM_VALS; i++) {
17562306a36Sopenharmony_ci		ret = linear_range_get_selector_low_array(&testr[0], 2,
17662306a36Sopenharmony_ci							  range1_vals[i], &sel,
17762306a36Sopenharmony_ci							  &found);
17862306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, 0, ret);
17962306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
18062306a36Sopenharmony_ci		KUNIT_EXPECT_TRUE(test, found);
18162306a36Sopenharmony_ci	}
18262306a36Sopenharmony_ci	for (i = 0; i < RANGE2_NUM_VALS; i++) {
18362306a36Sopenharmony_ci		ret = linear_range_get_selector_low_array(&testr[0], 2,
18462306a36Sopenharmony_ci							  range2_vals[i], &sel,
18562306a36Sopenharmony_ci							  &found);
18662306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, 0, ret);
18762306a36Sopenharmony_ci		KUNIT_EXPECT_EQ(test, sel, range2_sels[i]);
18862306a36Sopenharmony_ci		KUNIT_EXPECT_TRUE(test, found);
18962306a36Sopenharmony_ci	}
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	/*
19262306a36Sopenharmony_ci	 * Seek value greater than range max => get_selector_*_low should
19362306a36Sopenharmony_ci	 * return Ok - but set found to false as value is not in range
19462306a36Sopenharmony_ci	 */
19562306a36Sopenharmony_ci	ret = linear_range_get_selector_low_array(&testr[0], 2,
19662306a36Sopenharmony_ci					range2_vals[RANGE2_NUM_VALS - 1] + 1,
19762306a36Sopenharmony_ci					&sel, &found);
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, ret);
20062306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, sel, range2_sels[RANGE2_NUM_VALS - 1]);
20162306a36Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, found);
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic struct kunit_case range_test_cases[] = {
20562306a36Sopenharmony_ci	KUNIT_CASE(range_test_get_value_amount),
20662306a36Sopenharmony_ci	KUNIT_CASE(range_test_get_selector_high),
20762306a36Sopenharmony_ci	KUNIT_CASE(range_test_get_selector_low),
20862306a36Sopenharmony_ci	KUNIT_CASE(range_test_get_value),
20962306a36Sopenharmony_ci	{},
21062306a36Sopenharmony_ci};
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic struct kunit_suite range_test_module = {
21362306a36Sopenharmony_ci	.name = "linear-ranges-test",
21462306a36Sopenharmony_ci	.test_cases = range_test_cases,
21562306a36Sopenharmony_ci};
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cikunit_test_suites(&range_test_module);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
220