int_pow_kunit.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <kunit/test.h>
  3. #include <linux/math.h>
  4. struct test_case_params {
  5. u64 base;
  6. unsigned int exponent;
  7. u64 expected_result;
  8. const char *name;
  9. };
  10. static const struct test_case_params params[] = {
  11. { 64, 0, 1, "Power of zero" },
  12. { 64, 1, 64, "Power of one"},
  13. { 0, 5, 0, "Base zero" },
  14. { 1, 64, 1, "Base one" },
  15. { 2, 2, 4, "Two squared"},
  16. { 2, 3, 8, "Two cubed"},
  17. { 5, 5, 3125, "Five raised to the fifth power" },
  18. { U64_MAX, 1, U64_MAX, "Max base" },
  19. { 2, 63, 9223372036854775808ULL, "Large result"},
  20. };
  21. static void get_desc(const struct test_case_params *tc, char *desc)
  22. {
  23. strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
  24. }
  25. KUNIT_ARRAY_PARAM(int_pow, params, get_desc);
  26. static void int_pow_test(struct kunit *test)
  27. {
  28. const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
  29. KUNIT_EXPECT_EQ(test, tc->expected_result, int_pow(tc->base, tc->exponent));
  30. }
  31. static struct kunit_case math_int_pow_test_cases[] = {
  32. KUNIT_CASE_PARAM(int_pow_test, int_pow_gen_params),
  33. {}
  34. };
  35. static struct kunit_suite int_pow_test_suite = {
  36. .name = "math-int_pow",
  37. .test_cases = math_int_pow_test_cases,
  38. };
  39. kunit_test_suites(&int_pow_test_suite);
  40. MODULE_DESCRIPTION("math.int_pow KUnit test suite");
  41. MODULE_LICENSE("GPL");