int_log_kunit.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <kunit/test.h>
  3. #include <linux/int_log.h>
  4. struct test_case_params {
  5. u32 value;
  6. unsigned int expected_result;
  7. const char *name;
  8. };
  9. /* The expected result takes into account the log error */
  10. static const struct test_case_params intlog2_params[] = {
  11. {0, 0, "Log base 2 of 0"},
  12. {1, 0, "Log base 2 of 1"},
  13. {2, 16777216, "Log base 2 of 2"},
  14. {3, 26591232, "Log base 2 of 3"},
  15. {4, 33554432, "Log base 2 of 4"},
  16. {8, 50331648, "Log base 2 of 8"},
  17. {16, 67108864, "Log base 2 of 16"},
  18. {32, 83886080, "Log base 2 of 32"},
  19. {U32_MAX, 536870911, "Log base 2 of MAX"},
  20. };
  21. static const struct test_case_params intlog10_params[] = {
  22. {0, 0, "Log base 10 of 0"},
  23. {1, 0, "Log base 10 of 1"},
  24. {6, 13055203, "Log base 10 of 6"},
  25. {10, 16777225, "Log base 10 of 10"},
  26. {100, 33554450, "Log base 10 of 100"},
  27. {1000, 50331675, "Log base 10 of 1000"},
  28. {10000, 67108862, "Log base 10 of 10000"},
  29. {U32_MAX, 161614247, "Log base 10 of MAX"}
  30. };
  31. static void get_desc(const struct test_case_params *tc, char *desc)
  32. {
  33. strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
  34. }
  35. KUNIT_ARRAY_PARAM(intlog2, intlog2_params, get_desc);
  36. static void intlog2_test(struct kunit *test)
  37. {
  38. const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
  39. KUNIT_EXPECT_EQ(test, tc->expected_result, intlog2(tc->value));
  40. }
  41. KUNIT_ARRAY_PARAM(intlog10, intlog10_params, get_desc);
  42. static void intlog10_test(struct kunit *test)
  43. {
  44. const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
  45. KUNIT_EXPECT_EQ(test, tc->expected_result, intlog10(tc->value));
  46. }
  47. static struct kunit_case math_int_log_test_cases[] = {
  48. KUNIT_CASE_PARAM(intlog2_test, intlog2_gen_params),
  49. KUNIT_CASE_PARAM(intlog10_test, intlog10_gen_params),
  50. {}
  51. };
  52. static struct kunit_suite int_log_test_suite = {
  53. .name = "math-int_log",
  54. .test_cases = math_int_log_test_cases,
  55. };
  56. kunit_test_suites(&int_log_test_suite);
  57. MODULE_DESCRIPTION("math.int_log KUnit test suite");
  58. MODULE_LICENSE("GPL");