Skip to content

Commit 84ec093

Browse files
Bruno Sobreira Françakees
authored andcommitted
lib/math: Add int_log test suite
This commit introduces KUnit tests for the intlog2 and intlog10 functions, which compute logarithms in base 2 and base 10, respectively. The tests cover a range of inputs to ensure the correctness of these functions across common and edge cases. Signed-off-by: Bruno Sobreira França <brunofrancadevsec@gmail.com> Reviewed-by: David Gow <davidgow@google.com> Reviewed-by: Rae Moar <rmoar@google.com> Link: https://lore.kernel.org/r/20241202075545.3648096-3-davidgow@google.com Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 3e50ba8 commit 84ec093

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

lib/Kconfig.debug

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,6 +3197,17 @@ config INT_SQRT_KUNIT_TEST
31973197

31983198
If unsure, say N
31993199

3200+
config INT_LOG_KUNIT_TEST
3201+
tristate "Integer log (int_log) test" if !KUNIT_ALL_TESTS
3202+
depends on KUNIT
3203+
default KUNIT_ALL_TESTS
3204+
help
3205+
This option enables the KUnit test suite for the int_log library, which
3206+
provides two functions to compute the integer logarithm in base 2 and
3207+
base 10, called respectively as intlog2 and intlog10.
3208+
3209+
If unsure, say N
3210+
32003211
endif # RUNTIME_TESTING_MENU
32013212

32023213
config ARCH_USE_MEMTEST

lib/math/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22

3+
obj-$(CONFIG_INT_LOG_KUNIT_TEST) += int_log_kunit.o
34
obj-$(CONFIG_INT_POW_KUNIT_TEST) += int_pow_kunit.o
45
obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
56
obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational_kunit.o

lib/math/tests/int_log_kunit.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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");

0 commit comments

Comments
 (0)