This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 6d3b5fc9d55dc5fcd28bd44058b3b810d2488975
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 15 07:53:17 2026 -0500

    Add boundary tests for Fraction addition, subtraction, and
    multiplication
    
    Reuse the division operand matrix to add 32,448 parameterized cases.
    Check reduced results and overflow against BigInteger, preserving
    operation-specific zero behavior.
    
    Assisted-By: OpenAI Codex
---
 .../apache/commons/lang3/math/FractionTest.java    | 54 ++++++++++++++++++++--
 1 file changed, 51 insertions(+), 3 deletions(-)

diff --git a/src/test/java/org/apache/commons/lang3/math/FractionTest.java 
b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
index 3acdf9fdc..de786cd3f 100644
--- a/src/test/java/org/apache/commons/lang3/math/FractionTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
@@ -28,6 +28,7 @@
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Supplier;
 import java.util.stream.Stream;
 
 import org.apache.commons.lang3.AbstractLangTest;
@@ -44,7 +45,7 @@ class FractionTest extends AbstractLangTest {
 
     private static final int SKIP = 500; // 53
 
-    private static Stream<Arguments> divideBoundaryValues() {
+    private static Stream<Arguments> arithmeticBoundaryValues() {
         final int[] numerators = {
             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1073741824, -46341, -2, 
-1, 0, 1, 2, 46341, 1073741824,
             Integer.MAX_VALUE - 1, Integer.MAX_VALUE
@@ -58,7 +59,24 @@ private static Stream<Arguments> divideBoundaryValues() {
             }
         }
         return fractions.stream()
-                .flatMap(dividend -> fractions.stream().map(divisor -> 
Arguments.of(dividend, divisor)));
+                .flatMap(left -> fractions.stream().map(right -> 
Arguments.of(left, right)));
+    }
+
+    private static void assertReducedResult(final BigInteger numerator, final 
BigInteger denominator, final Supplier<Fraction> operation) {
+        // Use arbitrary-precision arithmetic as an independent oracle for the 
reduced result and its range.
+        final BigInteger gcd = numerator.gcd(denominator);
+        final BigInteger expectedNumerator = numerator.divide(gcd);
+        final BigInteger expectedDenominator = denominator.divide(gcd);
+        final BigInteger minValue = BigInteger.valueOf(Integer.MIN_VALUE);
+        final BigInteger maxValue = BigInteger.valueOf(Integer.MAX_VALUE);
+        if (expectedNumerator.compareTo(minValue) < 0 || 
expectedNumerator.compareTo(maxValue) > 0
+                || expectedDenominator.compareTo(maxValue) > 0) {
+            assertThrows(ArithmeticException.class, operation::get);
+        } else {
+            final Fraction result = operation.get();
+            assertEquals(expectedNumerator.intValueExact(), 
result.getNumerator());
+            assertEquals(expectedDenominator.intValueExact(), 
result.getDenominator());
+        }
     }
 
     @Test
@@ -201,6 +219,15 @@ void testAdd() {
         assertEquals(2114962910, f.getDenominator());
     }
 
+    @ParameterizedTest(name = "[{index}] {0} + {1}")
+    @MethodSource("arithmeticBoundaryValues")
+    void testAddBoundaryValues(final Fraction left, final Fraction right) {
+        final BigInteger numerator = 
BigInteger.valueOf(left.getNumerator()).multiply(BigInteger.valueOf(right.getDenominator()))
+                
.add(BigInteger.valueOf(right.getNumerator()).multiply(BigInteger.valueOf(left.getDenominator())));
+        final BigInteger denominator = 
BigInteger.valueOf(left.getDenominator()).multiply(BigInteger.valueOf(right.getDenominator()));
+        assertReducedResult(numerator, denominator, () -> left.add(right));
+    }
+
     @Test
     void testAddSubtractUnreducedOperands() {
         // 1073741823/2147483646 is 1/2, and 1/2 + 3/5 is 11/10.
@@ -421,7 +448,7 @@ void testDivide() {
     }
 
     @ParameterizedTest(name = "[{index}] {0} / {1}")
-    @MethodSource("divideBoundaryValues")
+    @MethodSource("arithmeticBoundaryValues")
     void testDivideBoundaryValues(final Fraction dividend, final Fraction 
divisor) {
         if (divisor.getNumerator() == 0) {
             assertThrows(ArithmeticException.class, () -> 
dividend.divideBy(divisor));
@@ -971,6 +998,18 @@ void testMultiply() {
         assertEquals(100000000, f.getDenominator());
     }
 
+    @ParameterizedTest(name = "[{index}] {0} * {1}")
+    @MethodSource("arithmeticBoundaryValues")
+    void testMultiplyBoundaryValues(final Fraction left, final Fraction right) 
{
+        if (left.getNumerator() == 0 || right.getNumerator() == 0) {
+            assertSame(Fraction.ZERO, left.multiplyBy(right));
+            return;
+        }
+        final BigInteger numerator = 
BigInteger.valueOf(left.getNumerator()).multiply(BigInteger.valueOf(right.getNumerator()));
+        final BigInteger denominator = 
BigInteger.valueOf(left.getDenominator()).multiply(BigInteger.valueOf(right.getDenominator()));
+        assertReducedResult(numerator, denominator, () -> 
left.multiplyBy(right));
+    }
+
     @Test
     void testNegate() {
         Fraction f;
@@ -1337,6 +1376,15 @@ void testSubtract() {
         assertEquals(2, f.getDenominator());
     }
 
+    @ParameterizedTest(name = "[{index}] {0} - {1}")
+    @MethodSource("arithmeticBoundaryValues")
+    void testSubtractBoundaryValues(final Fraction left, final Fraction right) 
{
+        final BigInteger numerator = 
BigInteger.valueOf(left.getNumerator()).multiply(BigInteger.valueOf(right.getDenominator()))
+                
.subtract(BigInteger.valueOf(right.getNumerator()).multiply(BigInteger.valueOf(left.getDenominator())));
+        final BigInteger denominator = 
BigInteger.valueOf(left.getDenominator()).multiply(BigInteger.valueOf(right.getDenominator()));
+        assertReducedResult(numerator, denominator, () -> 
left.subtract(right));
+    }
+
     @Test
     void testToProperString() {
         Fraction f;

Reply via email to