File Index
Files and directories
Sign in
FILE

int_log_kunit.c

File preview with syntax highlighting.

linux-6.19.14/lib/math/tests/int_log_kunit.c C
 1// SPDX-License-Identifier: GPL-2.0-only
 2#include <kunit/test.h>
 3#include <linux/int_log.h>
 4
 5struct 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 */
13static 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
25static 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
36static void get_desc(const struct test_case_params *tc, char *desc)
37{
38	strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
39}
40
41
42KUNIT_ARRAY_PARAM(intlog2, intlog2_params, get_desc);
43
44static 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
51KUNIT_ARRAY_PARAM(intlog10, intlog10_params, get_desc);
52
53static 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
60static 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
66static struct kunit_suite int_log_test_suite = {
67	.name = "math-int_log",
68	.test_cases =  math_int_log_test_cases,
69};
70
71kunit_test_suites(&int_log_test_suite);
72
73MODULE_DESCRIPTION("math.int_log KUnit test suite");
74MODULE_LICENSE("GPL");
~/ Read-only file preview.