1*b6c75c4aSTrent Piepho // SPDX-License-Identifier: GPL-2.0
2*b6c75c4aSTrent Piepho
3*b6c75c4aSTrent Piepho #include <kunit/test.h>
4*b6c75c4aSTrent Piepho
5*b6c75c4aSTrent Piepho #include <linux/rational.h>
6*b6c75c4aSTrent Piepho
7*b6c75c4aSTrent Piepho struct rational_test_param {
8*b6c75c4aSTrent Piepho unsigned long num, den;
9*b6c75c4aSTrent Piepho unsigned long max_num, max_den;
10*b6c75c4aSTrent Piepho unsigned long exp_num, exp_den;
11*b6c75c4aSTrent Piepho
12*b6c75c4aSTrent Piepho const char *name;
13*b6c75c4aSTrent Piepho };
14*b6c75c4aSTrent Piepho
15*b6c75c4aSTrent Piepho static const struct rational_test_param test_parameters[] = {
16*b6c75c4aSTrent Piepho { 1230, 10, 100, 20, 100, 1, "Exceeds bounds, semi-convergent term > 1/2 last term" },
17*b6c75c4aSTrent Piepho { 34567,100, 120, 20, 120, 1, "Exceeds bounds, semi-convergent term < 1/2 last term" },
18*b6c75c4aSTrent Piepho { 1, 30, 100, 10, 0, 1, "Closest to zero" },
19*b6c75c4aSTrent Piepho { 1, 19, 100, 10, 1, 10, "Closest to smallest non-zero" },
20*b6c75c4aSTrent Piepho { 27,32, 16, 16, 11, 13, "Use convergent" },
21*b6c75c4aSTrent Piepho { 1155, 7735, 255, 255, 33, 221, "Exact answer" },
22*b6c75c4aSTrent Piepho { 87, 32, 70, 32, 68, 25, "Semiconvergent, numerator limit" },
23*b6c75c4aSTrent Piepho { 14533, 4626, 15000, 2400, 7433, 2366, "Semiconvergent, denominator limit" },
24*b6c75c4aSTrent Piepho };
25*b6c75c4aSTrent Piepho
get_desc(const struct rational_test_param * param,char * desc)26*b6c75c4aSTrent Piepho static void get_desc(const struct rational_test_param *param, char *desc)
27*b6c75c4aSTrent Piepho {
28*b6c75c4aSTrent Piepho strscpy(desc, param->name, KUNIT_PARAM_DESC_SIZE);
29*b6c75c4aSTrent Piepho }
30*b6c75c4aSTrent Piepho
31*b6c75c4aSTrent Piepho /* Creates function rational_gen_params */
32*b6c75c4aSTrent Piepho KUNIT_ARRAY_PARAM(rational, test_parameters, get_desc);
33*b6c75c4aSTrent Piepho
rational_test(struct kunit * test)34*b6c75c4aSTrent Piepho static void rational_test(struct kunit *test)
35*b6c75c4aSTrent Piepho {
36*b6c75c4aSTrent Piepho const struct rational_test_param *param = (const struct rational_test_param *)test->param_value;
37*b6c75c4aSTrent Piepho unsigned long n = 0, d = 0;
38*b6c75c4aSTrent Piepho
39*b6c75c4aSTrent Piepho rational_best_approximation(param->num, param->den, param->max_num, param->max_den, &n, &d);
40*b6c75c4aSTrent Piepho KUNIT_EXPECT_EQ(test, n, param->exp_num);
41*b6c75c4aSTrent Piepho KUNIT_EXPECT_EQ(test, d, param->exp_den);
42*b6c75c4aSTrent Piepho }
43*b6c75c4aSTrent Piepho
44*b6c75c4aSTrent Piepho static struct kunit_case rational_test_cases[] = {
45*b6c75c4aSTrent Piepho KUNIT_CASE_PARAM(rational_test, rational_gen_params),
46*b6c75c4aSTrent Piepho {}
47*b6c75c4aSTrent Piepho };
48*b6c75c4aSTrent Piepho
49*b6c75c4aSTrent Piepho static struct kunit_suite rational_test_suite = {
50*b6c75c4aSTrent Piepho .name = "rational",
51*b6c75c4aSTrent Piepho .test_cases = rational_test_cases,
52*b6c75c4aSTrent Piepho };
53*b6c75c4aSTrent Piepho
54*b6c75c4aSTrent Piepho kunit_test_suites(&rational_test_suite);
55*b6c75c4aSTrent Piepho
56*b6c75c4aSTrent Piepho MODULE_LICENSE("GPL v2");
57