1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <kunit/test.h>
4 #include <linux/math.h>
5
6 struct test_case_params {
7 u64 base;
8 unsigned int exponent;
9 u64 expected_result;
10 const char *name;
11 };
12
13 static const struct test_case_params params[] = {
14 { 64, 0, 1, "Power of zero" },
15 { 64, 1, 64, "Power of one"},
16 { 0, 5, 0, "Base zero" },
17 { 1, 64, 1, "Base one" },
18 { 2, 2, 4, "Two squared"},
19 { 2, 3, 8, "Two cubed"},
20 { 5, 5, 3125, "Five raised to the fifth power" },
21 { U64_MAX, 1, U64_MAX, "Max base" },
22 { 2, 63, 9223372036854775808ULL, "Large result"},
23 };
24
get_desc(const struct test_case_params * tc,char * desc)25 static void get_desc(const struct test_case_params *tc, char *desc)
26 {
27 strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
28 }
29
30 KUNIT_ARRAY_PARAM(int_pow, params, get_desc);
31
int_pow_test(struct kunit * test)32 static void int_pow_test(struct kunit *test)
33 {
34 const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
35
36 KUNIT_EXPECT_EQ(test, tc->expected_result, int_pow(tc->base, tc->exponent));
37 }
38
39 static struct kunit_case math_int_pow_test_cases[] = {
40 KUNIT_CASE_PARAM(int_pow_test, int_pow_gen_params),
41 {}
42 };
43
44 static struct kunit_suite int_pow_test_suite = {
45 .name = "math-int_pow",
46 .test_cases = math_int_pow_test_cases,
47 };
48
49 kunit_test_suites(&int_pow_test_suite);
50
51 MODULE_DESCRIPTION("math.int_pow KUnit test suite");
52 MODULE_LICENSE("GPL");
53