You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/16 12:18:34 UTC

[commons-numbers] 19/26: FractionTest: overflow check in multiply/divide by int

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

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

commit 83098b7bec08e11829dc419a556b34a142809776
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Tue Apr 14 22:59:37 2020 +0100

    FractionTest: overflow check in multiply/divide by int
---
 .../commons/numbers/fraction/FractionTest.java     | 33 ++++++++++++++++------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
index 96b0a25..7042e82 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
@@ -351,16 +351,19 @@ public class FractionTest {
         Assertions.assertThrows(FractionException.class, () -> Fraction.of(1, 2).divide(Fraction.ZERO));
         Assertions.assertThrows(FractionException.class, () -> Fraction.of(1, 2).divide(0));
 
-        // Special cases
+        // Special cases for overflow
+        final Fraction two = Fraction.of(2);
         final Fraction f3 = Fraction.of(1, Integer.MAX_VALUE);
-        Assertions.assertThrows(ArithmeticException.class,
-            () -> f3.divide(f3.reciprocal())  // should overflow
-        );
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.divide(two));
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.divide(two.negate()));
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.divide(2));
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.divide(-2));
 
-        final Fraction f4 = Fraction.of(1, -Integer.MAX_VALUE);
-        Assertions.assertThrows(ArithmeticException.class,
-            () -> f4.divide(f4.reciprocal())  // should overflow
-        );
+        final Fraction f4 = Fraction.of(1, Integer.MIN_VALUE);
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.divide(two));
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.divide(two.negate()));
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.divide(2));
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.divide(-2));
     }
 
     @Test
@@ -377,6 +380,20 @@ public class FractionTest {
         }
 
         Assertions.assertThrows(NullPointerException.class, () -> Fraction.ONE.multiply((Fraction) null));
+
+        // Special cases for overflow
+        final Fraction two = Fraction.of(2);
+        final Fraction f3 = Fraction.of(Integer.MAX_VALUE);
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.multiply(two));
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.multiply(two.negate()));
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.multiply(2));
+        Assertions.assertThrows(ArithmeticException.class, () -> f3.multiply(-2));
+
+        final Fraction f4 = Fraction.of(Integer.MIN_VALUE);
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.multiply(two));
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.multiply(two.negate()));
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.multiply(2));
+        Assertions.assertThrows(ArithmeticException.class, () -> f4.multiply(-2));
     }
 
     @Test