Skip to content

Commit 7fcc9b5

Browse files
lf-hernandezshuahkh
authored andcommitted
lib/math: Add int_pow test suite
Adds test suite for integer based power function which performs integer exponentiation. The test suite is designed to verify that the implementation of int_pow correctly computes the power of a given base raised to a given exponent. The tests check various scenarios and edge cases to ensure the accuracy and reliability of the exponentiation function. Updated commit with test information at commit time: Shuah Khan Signed-off-by: Luis Felipe Hernandez <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent e4835f1 commit 7fcc9b5

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

lib/Kconfig.debug

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,3 +3051,19 @@ config RUST_KERNEL_DOCTESTS
30513051
endmenu # "Rust"
30523052

30533053
endmenu # Kernel hacking
3054+
3055+
config INT_POW_TEST
3056+
tristate "Integer exponentiation (int_pow) test" if !KUNIT_ALL_TESTS
3057+
depends on KUNIT
3058+
default KUNIT_ALL_TESTS
3059+
help
3060+
This option enables the KUnit test suite for the int_pow function,
3061+
which performs integer exponentiation. The test suite is designed to
3062+
verify that the implementation of int_pow correctly computes the power
3063+
of a given base raised to a given exponent.
3064+
3065+
Enabling this option will include tests that check various scenarios
3066+
and edge cases to ensure the accuracy and reliability of the exponentiation
3067+
function.
3068+
3069+
If unsure, say N

lib/math/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ obj-$(CONFIG_CORDIC) += cordic.o
55
obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
66
obj-$(CONFIG_RATIONAL) += rational.o
77

8+
obj-$(CONFIG_INT_POW_TEST) += tests/int_pow_kunit.o
89
obj-$(CONFIG_TEST_DIV64) += test_div64.o
910
obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o

lib/math/tests/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o

lib/math/tests/int_pow_kunit.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
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+
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");

0 commit comments

Comments
 (0)