Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/math: add int_sqrt test suite

Adds test suite for integer based square root function.

The test suite is designed to verify the correctness of the int_sqrt()
math library function.

Link: https://lkml.kernel.org/r/20241213042701.1037467-1-luis.hernandez093@gmail.com
Signed-off-by: Luis Felipe Hernandez <luis.hernandez093@gmail.com>
Reviewed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: David Gow <davidgow@google.com>
Cc: Ricardo B. Marliere <rbm@suse.com>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Luis Felipe Hernandez and committed by
Andrew Morton
0fafc9e1 eb7a18eb

+83
+15
lib/Kconfig.debug
··· 3174 3174 3175 3175 If unsure, say N 3176 3176 3177 + config INT_SQRT_KUNIT_TEST 3178 + tristate "Integer square root test" if !KUNIT_ALL_TESTS 3179 + depends on KUNIT 3180 + default KUNIT_ALL_TESTS 3181 + help 3182 + This option enables the KUnit test suite for the int_sqrt() function, 3183 + which performs square root calculation. The test suite checks 3184 + various scenarios, including edge cases, to ensure correctness. 3185 + 3186 + Enabling this option will include tests that check various scenarios 3187 + and edge cases to ensure the accuracy and reliability of the square root 3188 + function. 3189 + 3190 + If unsure, say N 3191 + 3177 3192 endif # RUNTIME_TESTING_MENU 3178 3193 3179 3194 config ARCH_USE_MEMTEST
+1
lib/math/Makefile
··· 9 9 obj-$(CONFIG_TEST_DIV64) += test_div64.o 10 10 obj-$(CONFIG_TEST_MULDIV64) += test_mul_u64_u64_div_u64.o 11 11 obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o 12 + obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += tests/int_sqrt_kunit.o
+1
lib/math/tests/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 3 3 obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o 4 + obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
+66
lib/math/tests/int_sqrt_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + #include <kunit/test.h> 4 + #include <linux/limits.h> 5 + #include <linux/math.h> 6 + #include <linux/module.h> 7 + #include <linux/string.h> 8 + 9 + struct test_case_params { 10 + unsigned long x; 11 + unsigned long expected_result; 12 + const char *name; 13 + }; 14 + 15 + static const struct test_case_params params[] = { 16 + { 0, 0, "edge case: square root of 0" }, 17 + { 1, 1, "perfect square: square root of 1" }, 18 + { 2, 1, "non-perfect square: square root of 2" }, 19 + { 3, 1, "non-perfect square: square root of 3" }, 20 + { 4, 2, "perfect square: square root of 4" }, 21 + { 5, 2, "non-perfect square: square root of 5" }, 22 + { 6, 2, "non-perfect square: square root of 6" }, 23 + { 7, 2, "non-perfect square: square root of 7" }, 24 + { 8, 2, "non-perfect square: square root of 8" }, 25 + { 9, 3, "perfect square: square root of 9" }, 26 + { 15, 3, "non-perfect square: square root of 15 (N-1 from 16)" }, 27 + { 16, 4, "perfect square: square root of 16" }, 28 + { 17, 4, "non-perfect square: square root of 17 (N+1 from 16)" }, 29 + { 80, 8, "non-perfect square: square root of 80 (N-1 from 81)" }, 30 + { 81, 9, "perfect square: square root of 81" }, 31 + { 82, 9, "non-perfect square: square root of 82 (N+1 from 81)" }, 32 + { 255, 15, "non-perfect square: square root of 255 (N-1 from 256)" }, 33 + { 256, 16, "perfect square: square root of 256" }, 34 + { 257, 16, "non-perfect square: square root of 257 (N+1 from 256)" }, 35 + { 2147483648, 46340, "large input: square root of 2147483648" }, 36 + { 4294967295, 65535, "edge case: ULONG_MAX for 32-bit" }, 37 + }; 38 + 39 + static void get_desc(const struct test_case_params *tc, char *desc) 40 + { 41 + strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE); 42 + } 43 + 44 + KUNIT_ARRAY_PARAM(int_sqrt, params, get_desc); 45 + 46 + static void int_sqrt_test(struct kunit *test) 47 + { 48 + const struct test_case_params *tc = (const struct test_case_params *)test->param_value; 49 + 50 + KUNIT_EXPECT_EQ(test, tc->expected_result, int_sqrt(tc->x)); 51 + } 52 + 53 + static struct kunit_case math_int_sqrt_test_cases[] = { 54 + KUNIT_CASE_PARAM(int_sqrt_test, int_sqrt_gen_params), 55 + {} 56 + }; 57 + 58 + static struct kunit_suite int_sqrt_test_suite = { 59 + .name = "math-int_sqrt", 60 + .test_cases = math_int_sqrt_test_cases, 61 + }; 62 + 63 + kunit_test_suites(&int_sqrt_test_suite); 64 + 65 + MODULE_DESCRIPTION("math.int_sqrt KUnit test suite"); 66 + MODULE_LICENSE("GPL");