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>"); 57