[PATCH 2/2] lib/math: rational: test large semiconvergent final terms

From: 961222243021 SHYAM J

Date: Sun Oct 11 2026 - 04:26:11 EST


Add cases where doubling the semiconvergent final term would overflow
an unsigned long, including odd and even continued-fraction final terms.
Check the cases immediately below and at the halfway boundary as well,
so selecting a semiconvergent too eagerly is detected.

Use ULONG_MAX so the same cases exercise the native unsigned-long width
on both 32-bit and 64-bit builds. Three of the added cases fail without
the overflow guard.

Assisted-by: LLM
---
lib/math/tests/rational_kunit.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/lib/math/tests/rational_kunit.c b/lib/math/tests/rational_kunit.c
index 47486a95f..743fb16f8 100644
--- a/lib/math/tests/rational_kunit.c
+++ b/lib/math/tests/rational_kunit.c
@@ -2,6 +2,7 @@

#include <kunit/test.h>

+#include <linux/limits.h>
#include <linux/rational.h>

struct rational_test_param {
@@ -21,6 +22,16 @@ static const struct rational_test_param test_parameters[] = {
{ 1155, 7735, 255, 255, 33, 221, "Exact answer" },
{ 87, 32, 70, 32, 68, 25, "Semiconvergent,
numerator limit" },
{ 14533, 4626, 15000, 2400, 7433, 2366, "Semiconvergent,
denominator limit" },
+ { 1, ULONG_MAX, 1, ULONG_MAX - 1, 1, ULONG_MAX - 1,
+ "Semiconvergent term multiplication overflows" },
+ { 1, ULONG_MAX, 1, ULONG_MAX / 2 + 1, 1, ULONG_MAX / 2 + 1,
+ "Odd final term, above half" },
+ { 1, ULONG_MAX, 1, ULONG_MAX / 2, 0, 1,
+ "Odd final term, below half" },
+ { 1, ULONG_MAX - 1, 1, ULONG_MAX / 2 + 1, 1, ULONG_MAX / 2 + 1,
+ "Even final term, above half" },
+ { 1, ULONG_MAX - 1, 1, ULONG_MAX / 2, 0, 1,
+ "Even final term, exactly half" },
};

static void get_desc(const struct rational_test_param *param, char *desc)
--
2.51.1