]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib/math: Add Kunit test suite for gcd()
authorYu-Chun Lin <eleanor15x@gmail.com>
Mon, 3 Feb 2025 07:54:00 +0000 (15:54 +0800)
committerKees Cook <kees@kernel.org>
Wed, 12 Feb 2025 22:00:11 +0000 (14:00 -0800)
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>
lib/Kconfig.debug
lib/math/tests/Makefile
lib/math/tests/gcd_kunit.c [new file with mode: 0644]

index 86ddf568cbca5a529c7e04a2026080fa71ecf6d9..ad172546587029b964cc205a489520e602cbfff5 100644 (file)
@@ -3222,6 +3222,19 @@ config INT_LOG_KUNIT_TEST
 
           If unsure, say N
 
+config GCD_KUNIT_TEST
+       tristate "Greatest common divisor test" if !KUNIT_ALL_TESTS
+       depends on KUNIT
+       default KUNIT_ALL_TESTS
+       help
+         This option enables the KUnit test suite for the gcd() function,
+         which computes the greatest common divisor of two numbers.
+
+         This test suite verifies the correctness of gcd() across various
+         scenarios, including edge cases.
+
+         If unsure, say N
+
 endif # RUNTIME_TESTING_MENU
 
 config ARCH_USE_MEMTEST
index 14a245778394d1e05cb72af76b7c60c173e11382..1d2dd6424df89ae72b75b93d2f56f4de38324e2c 100644 (file)
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
+obj-$(CONFIG_GCD_KUNIT_TEST)           += gcd_kunit.o
 obj-$(CONFIG_INT_LOG_KUNIT_TEST)       += int_log_kunit.o
 obj-$(CONFIG_INT_POW_KUNIT_TEST)       += int_pow_kunit.o
 obj-$(CONFIG_INT_SQRT_KUNIT_TEST)      += int_sqrt_kunit.o
diff --git a/lib/math/tests/gcd_kunit.c b/lib/math/tests/gcd_kunit.c
new file mode 100644 (file)
index 0000000..ede1883
--- /dev/null
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <kunit/test.h>
+#include <linux/gcd.h>
+#include <linux/limits.h>
+
+struct test_case_params {
+       unsigned long val1;
+       unsigned long val2;
+       unsigned long expected_result;
+       const char *name;
+};
+
+static const struct test_case_params params[] = {
+       { 48, 18, 6, "GCD of 48 and 18" },
+       { 18, 48, 6, "GCD of 18 and 48" },
+       { 56, 98, 14, "GCD of 56 and 98" },
+       { 17, 13, 1, "Coprime numbers" },
+       { 101, 103, 1, "Coprime numbers" },
+       { 270, 192, 6, "GCD of 270 and 192" },
+       { 0, 5, 5, "GCD with zero" },
+       { 7, 0, 7, "GCD with zero reversed" },
+       { 36, 36, 36, "GCD of identical numbers" },
+       { ULONG_MAX, 1, 1, "GCD of max ulong and 1" },
+       { ULONG_MAX, ULONG_MAX, ULONG_MAX, "GCD of max ulong values" },
+};
+
+static void get_desc(const struct test_case_params *tc, char *desc)
+{
+       strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(gcd, params, get_desc);
+
+static void gcd_test(struct kunit *test)
+{
+       const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
+
+       KUNIT_EXPECT_EQ(test, tc->expected_result, gcd(tc->val1, tc->val2));
+}
+
+static struct kunit_case math_gcd_test_cases[] = {
+       KUNIT_CASE_PARAM(gcd_test, gcd_gen_params),
+       {}
+};
+
+static struct kunit_suite gcd_test_suite = {
+       .name = "math-gcd",
+       .test_cases = math_gcd_test_cases,
+};
+
+kunit_test_suite(gcd_test_suite);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("math.gcd KUnit test suite");
+MODULE_AUTHOR("Yu-Chun Lin <eleanor15x@gmail.com>");