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