Re: [PATCH v5] lib/math: Add int_sqrt test suite

From: Alan Stern
Date: Wed Dec 11 2024 - 15:42:45 EST


On Wed, Dec 11, 2024 at 03:34:24PM -0500, Luis Felipe Hernandez wrote:
> 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.
>
> Signed-off-by: Luis Felipe Hernandez <luis.hernandez093@xxxxxxxxx>
> ---

I don't know why you CC'ed linux-usb for this patch. But as long as you
did...

> diff --git a/lib/math/tests/int_sqrt_kunit.c b/lib/math/tests/int_sqrt_kunit.c
> new file mode 100644
> index 000000000000..a94c68816a1a
> --- /dev/null
> +++ b/lib/math/tests/int_sqrt_kunit.c
> @@ -0,0 +1,60 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <kunit/test.h>
> +#include <linux/limits.h>
> +#include <linux/math.h>
> +#include <linux/module.h>
> +#include <linux/string.h>
> +
> +struct test_case_params {
> + unsigned long x;
> + unsigned long expected_result;
> + const char *name;
> +};
> +
> +static const struct test_case_params params[] = {
> + { 0, 0, "edge case: square root of 0" },
> + { 1, 1, "perfect square: square root of 1" },
> + { 2, 1, "non-perfect square: square root of 2" },
> + { 3, 1, "non-perfect square: sqaure root of 3" },

s/sqau/squa/

> + { 4, 2, "perfect square: square root of 4" },
> + { 5, 2, "non-perfect square: square root of 5" },

s/square root/square root/

> + { 6, 2, "non-perfect square: square root of 6" },
> + { 7, 2, "non-perfect square: square root of 7" },
> + { 8, 2, "non-perfect square: square root of 8" },
> + { 9, 3, "perfect square: square root of 9" },
> + { 16, 4, "perfect square: square root of 16" },
> + { 81, 9, "perfect square: square root of 81" },
> + { 256, 16, "perfect square: square root of 256" },
> + { 2147483648, 46340, "large input: square root of 2147483648" },
> + { 4294967295, 65535, "edge case: ULONG_MAX for 32-bit" },
> +};

For the higher numbers (16, 81, etc.), you should test N-1 (and maybe
also N+1) as well as N.

Alan Stern