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