gcd_kunit.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <kunit/test.h>
  3. #include <linux/gcd.h>
  4. #include <linux/limits.h>
  5. struct test_case_params {
  6. unsigned long val1;
  7. unsigned long val2;
  8. unsigned long expected_result;
  9. const char *name;
  10. };
  11. static const struct test_case_params params[] = {
  12. { 48, 18, 6, "GCD of 48 and 18" },
  13. { 18, 48, 6, "GCD of 18 and 48" },
  14. { 56, 98, 14, "GCD of 56 and 98" },
  15. { 17, 13, 1, "Coprime numbers" },
  16. { 101, 103, 1, "Coprime numbers" },
  17. { 270, 192, 6, "GCD of 270 and 192" },
  18. { 0, 5, 5, "GCD with zero" },
  19. { 7, 0, 7, "GCD with zero reversed" },
  20. { 36, 36, 36, "GCD of identical numbers" },
  21. { ULONG_MAX, 1, 1, "GCD of max ulong and 1" },
  22. { ULONG_MAX, ULONG_MAX, ULONG_MAX, "GCD of max ulong values" },
  23. };
  24. static void get_desc(const struct test_case_params *tc, char *desc)
  25. {
  26. strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
  27. }
  28. KUNIT_ARRAY_PARAM(gcd, params, get_desc);
  29. static void gcd_test(struct kunit *test)
  30. {
  31. const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
  32. KUNIT_EXPECT_EQ(test, tc->expected_result, gcd(tc->val1, tc->val2));
  33. }
  34. static struct kunit_case math_gcd_test_cases[] = {
  35. KUNIT_CASE_PARAM(gcd_test, gcd_gen_params),
  36. {}
  37. };
  38. static struct kunit_suite gcd_test_suite = {
  39. .name = "math-gcd",
  40. .test_cases = math_gcd_test_cases,
  41. };
  42. kunit_test_suite(gcd_test_suite);
  43. MODULE_LICENSE("GPL");
  44. MODULE_DESCRIPTION("math.gcd KUnit test suite");
  45. MODULE_AUTHOR("Yu-Chun Lin <eleanor15x@gmail.com>");