[PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX

From: Joel Cruz Cabrera

Date: Tue Dec 01 2026 - 07:47:05 EST


Add a case where the best semi-convergent term is ULONG_MAX - 1, which
made "2u * t" overflow before the previous patch: for 1/ULONG_MAX with
max_denominator = ULONG_MAX - 1, the closest fraction is
1/(ULONG_MAX - 1), not 0/1. The case is written with ULONG_MAX so that
it covers both 32-bit and 64-bit kernels.

This test was written with the help of an AI assistant (Claude, by
Anthropic); I reviewed it.




Signed-off-by: Joel Cruz Cabrera <tf.joel.work@xxxxxxxxx>
---
lib/math/tests/rational_kunit.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/lib/math/tests/rational_kunit.c b/lib/math/tests/rational_kunit.c
index 47486a95f..c8dac1904 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,8 @@ 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, ULONG_MAX, ULONG_MAX - 1, 1, ULONG_MAX - 1,
+ "Semiconvergent, term above LONG_MAX" },
};

static void get_desc(const struct rational_test_param *param, char *desc)
--
2.50.1 (Apple Git-155)