Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <luis.hernandez093@gmail.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Luis Felipe Hernandez and committed by
Shuah Khan
7fcc9b53 e4835f1d

+72
+16
lib/Kconfig.debug
··· 3051 3051 endmenu # "Rust" 3052 3052 3053 3053 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
+1
lib/math/Makefile
··· 5 5 obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o 6 6 obj-$(CONFIG_RATIONAL) += rational.o 7 7 8 + obj-$(CONFIG_INT_POW_TEST) += tests/int_pow_kunit.o 8 9 obj-$(CONFIG_TEST_DIV64) += test_div64.o 9 10 obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o
+3
lib/math/tests/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + 3 + obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o
+52
lib/math/tests/int_pow_kunit.c
··· 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");