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

lib/math: Add Kunit test suite for gcd()

Add a KUnit test suite for the gcd() function.
This test suite verifies the correctness of gcd() across various
scenarios, including edge cases.

Signed-off-by: Yu-Chun Lin <eleanor15x@gmail.com>
Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Link: https://lore.kernel.org/r/20250203075400.3431330-1-eleanor15x@gmail.com
Signed-off-by: Kees Cook <kees@kernel.org>

authored by

Yu-Chun Lin and committed by
Kees Cook
9ab61886 2be6ce9d

+70
+13
lib/Kconfig.debug
··· 3222 3222 3223 3223 If unsure, say N 3224 3224 3225 + config GCD_KUNIT_TEST 3226 + tristate "Greatest common divisor test" if !KUNIT_ALL_TESTS 3227 + depends on KUNIT 3228 + default KUNIT_ALL_TESTS 3229 + help 3230 + This option enables the KUnit test suite for the gcd() function, 3231 + which computes the greatest common divisor of two numbers. 3232 + 3233 + This test suite verifies the correctness of gcd() across various 3234 + scenarios, including edge cases. 3235 + 3236 + If unsure, say N 3237 + 3225 3238 endif # RUNTIME_TESTING_MENU 3226 3239 3227 3240 config ARCH_USE_MEMTEST
+1
lib/math/tests/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 3 + obj-$(CONFIG_GCD_KUNIT_TEST) += gcd_kunit.o 3 4 obj-$(CONFIG_INT_LOG_KUNIT_TEST) += int_log_kunit.o 4 5 obj-$(CONFIG_INT_POW_KUNIT_TEST) += int_pow_kunit.o 5 6 obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
+56
lib/math/tests/gcd_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + #include <kunit/test.h> 4 + #include <linux/gcd.h> 5 + #include <linux/limits.h> 6 + 7 + struct test_case_params { 8 + unsigned long val1; 9 + unsigned long val2; 10 + unsigned long expected_result; 11 + const char *name; 12 + }; 13 + 14 + static const struct test_case_params params[] = { 15 + { 48, 18, 6, "GCD of 48 and 18" }, 16 + { 18, 48, 6, "GCD of 18 and 48" }, 17 + { 56, 98, 14, "GCD of 56 and 98" }, 18 + { 17, 13, 1, "Coprime numbers" }, 19 + { 101, 103, 1, "Coprime numbers" }, 20 + { 270, 192, 6, "GCD of 270 and 192" }, 21 + { 0, 5, 5, "GCD with zero" }, 22 + { 7, 0, 7, "GCD with zero reversed" }, 23 + { 36, 36, 36, "GCD of identical numbers" }, 24 + { ULONG_MAX, 1, 1, "GCD of max ulong and 1" }, 25 + { ULONG_MAX, ULONG_MAX, ULONG_MAX, "GCD of max ulong values" }, 26 + }; 27 + 28 + static void get_desc(const struct test_case_params *tc, char *desc) 29 + { 30 + strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE); 31 + } 32 + 33 + KUNIT_ARRAY_PARAM(gcd, params, get_desc); 34 + 35 + static void gcd_test(struct kunit *test) 36 + { 37 + const struct test_case_params *tc = (const struct test_case_params *)test->param_value; 38 + 39 + KUNIT_EXPECT_EQ(test, tc->expected_result, gcd(tc->val1, tc->val2)); 40 + } 41 + 42 + static struct kunit_case math_gcd_test_cases[] = { 43 + KUNIT_CASE_PARAM(gcd_test, gcd_gen_params), 44 + {} 45 + }; 46 + 47 + static struct kunit_suite gcd_test_suite = { 48 + .name = "math-gcd", 49 + .test_cases = math_gcd_test_cases, 50 + }; 51 + 52 + kunit_test_suite(gcd_test_suite); 53 + 54 + MODULE_LICENSE("GPL"); 55 + MODULE_DESCRIPTION("math.gcd KUnit test suite"); 56 + MODULE_AUTHOR("Yu-Chun Lin <eleanor15x@gmail.com>");