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/08 21:17:18 UTC

[commons-numbers] branch master updated (51973c3 -> 5768299)

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

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


    from 51973c3  Fix fraction hashCode
     new c7a2734  Clean-up Fraction hashcode.
     new ddc1ee7  Fraction: Document possible exception thrown by reciprocal()
     new 40dae8c  Fraction.of(numerator) to directly invoke special fraction constructor.
     new d293dd0  Explicit test for divide by zero in fraction constructor
     new 25ce3fa  Use assertThrows to detect exceptions
     new 8089d08  Fraction: test multiply by integer with zero cases.
     new 01cb55a  Fraction constructor cannot throw an arithmetic exception
     new 5768299  Fraction: Use of(numerator) to construct the public constants.

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/numbers/fraction/BigFraction.java      | 27 ++++++++---
 .../apache/commons/numbers/fraction/Fraction.java  | 41 +++++++++++-----
 .../commons/numbers/fraction/BigFractionTest.java  | 54 +++++++---------------
 .../commons/numbers/fraction/FractionTest.java     | 17 ++++---
 4 files changed, 76 insertions(+), 63 deletions(-)


[commons-numbers] 05/08: Use assertThrows to detect exceptions

Posted by ah...@apache.org.
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 25ce3fab796588830b4da9adfae2370548c20c1c
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 22:01:17 2020 +0100

    Use assertThrows to detect exceptions
---
 .../commons/numbers/fraction/BigFractionTest.java  | 48 ++++++----------------
 .../commons/numbers/fraction/FractionTest.java     |  4 +-
 2 files changed, 14 insertions(+), 38 deletions(-)

diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index 0b7030d..b0ae45f 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -69,30 +69,17 @@ public class BigFractionTest {
         Assertions.assertEquals(15.0000000000001, BigFraction.from(15.0000000000001).doubleValue(), 0.0);
         assertFraction(3602879701896487L, 9007199254740992L, BigFraction.from(0.40000000000001));
         assertFraction(1055531162664967L, 70368744177664L, BigFraction.from(15.0000000000001));
-        try {
-            BigFraction.of(null, BigInteger.ONE);
-            Assertions.fail("Expecting NullPointerException");
-        } catch (NullPointerException npe) {
-            // expected
-        }
-        try {
-            BigFraction.of(BigInteger.ONE, null);
-            Assertions.fail("Expecting NullPointerException");
-        } catch (NullPointerException npe) {
-            // expected
-        }
-        try {
-            BigFraction.of(BigInteger.ONE, BigInteger.ZERO);
-            Assertions.fail("Expecting ArithmeticException");
-        } catch (ArithmeticException ignored) {
-            // expected
-        }
-        try {
-            BigFraction.from(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000);
-            Assertions.fail("Expecting ArithmeticException");
-        } catch (ArithmeticException ignored) {
-            // expected
-        }
+
+        // Divide by zero
+        Assertions.assertThrows(ArithmeticException.class, () -> BigFraction.of(BigInteger.ONE, BigInteger.ZERO));
+
+        // Null pointers
+        Assertions.assertThrows(NullPointerException.class, () -> BigFraction.of(null, BigInteger.ONE));
+        Assertions.assertThrows(NullPointerException.class, () -> BigFraction.of(BigInteger.ONE, null));
+        Assertions.assertThrows(NullPointerException.class, () -> BigFraction.of(null));
+
+        Assertions.assertThrows(ArithmeticException.class,
+            () -> BigFraction.from(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000));
     }
 
     @Test
@@ -410,12 +397,7 @@ public class BigFractionTest {
         assertFraction(-6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / -100.0));
         assertFraction(-1784551352345559L, 562949953421312L, BigFraction.from(-317.0 / 100.0));
         for (double v : new double[] {Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) {
-            try {
-                BigFraction.from(v);
-                Assertions.fail("Expecting IllegalArgumentException");
-            } catch (IllegalArgumentException iae) {
-                // expected
-            }
+            Assertions.assertThrows(IllegalArgumentException.class, () -> BigFraction.from(v));
         }
         Assertions.assertEquals(1L, BigFraction.from(Double.MAX_VALUE).getDenominatorAsLong());
         Assertions.assertEquals(1L, BigFraction.from(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong());
@@ -438,11 +420,7 @@ public class BigFractionTest {
         }
 
         BigFraction f = BigFraction.of(0, 3);
-        try {
-            f = f.reciprocal();
-            Assertions.fail("expecting ArithmeticException");
-        } catch (ArithmeticException ignored) {
-        }
+        Assertions.assertThrows(ArithmeticException.class, f::reciprocal);
     }
 
     @Test
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 e075d56..8e0d979 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
@@ -224,9 +224,7 @@ public class FractionTest {
         }
 
         final Fraction f = Fraction.of(0, 3);
-        Assertions.assertThrows(ArithmeticException.class,
-                f::reciprocal
-        );
+        Assertions.assertThrows(ArithmeticException.class, f::reciprocal);
     }
 
     @Test


[commons-numbers] 02/08: Fraction: Document possible exception thrown by reciprocal()

Posted by ah...@apache.org.
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 ddc1ee77d15a915632c215fb6a49987500bff16a
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 21:27:31 2020 +0100

    Fraction: Document possible exception thrown by reciprocal()
---
 .../main/java/org/apache/commons/numbers/fraction/Fraction.java    | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index 2ab8932..85ee30d 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -394,6 +394,13 @@ public final class Fraction
             new Fraction(-numerator, denominator);
     }
 
+    /**
+     * {@inheritDoc}
+     * 
+     * <p>Raises an exception if the fraction is equal to zero.
+     * 
+     * @throws ArithmeticException if the current numerator is {@code zero}
+     */
     @Override
     public Fraction reciprocal() {
         return new Fraction(denominator, numerator);


[commons-numbers] 08/08: Fraction: Use of(numerator) to construct the public constants.

Posted by ah...@apache.org.
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 5768299d477dce8b72d4d28f93f2277256495981
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 22:14:43 2020 +0100

    Fraction: Use of(numerator) to construct the public constants.
---
 .../src/main/java/org/apache/commons/numbers/fraction/Fraction.java   | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index 7fc5c7e..8f41989 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -36,10 +36,10 @@ public final class Fraction
                NativeOperators<Fraction>,
                Serializable {
     /** A fraction representing "0". */
-    public static final Fraction ZERO = new Fraction(0, 1);
+    public static final Fraction ZERO = of(0);
 
     /** A fraction representing "1". */
-    public static final Fraction ONE = new Fraction(1, 1);
+    public static final Fraction ONE = of(1);
 
     /** Serializable version identifier. */
     private static final long serialVersionUID = 20190701L;


[commons-numbers] 06/08: Fraction: test multiply by integer with zero cases.

Posted by ah...@apache.org.
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 8089d08aab20d9d35b446513620ebf69e1bb46a3
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 22:02:01 2020 +0100

    Fraction: test multiply by integer with zero cases.
---
 .../test/java/org/apache/commons/numbers/fraction/FractionTest.java   | 4 ++++
 1 file changed, 4 insertions(+)

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 8e0d979..718ca34 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
@@ -352,6 +352,10 @@ public class FractionTest {
         Fraction f1 = Fraction.of(6, 35);
         Fraction f = f1.multiply(15);
         assertFraction(18, 7, f);
+
+        // Test zero with multiply by integer
+        Assertions.assertEquals(Fraction.ZERO, Fraction.ZERO.multiply(42));
+        Assertions.assertEquals(Fraction.ZERO, Fraction.of(1, 3).multiply(0));
     }
 
     @Test


[commons-numbers] 07/08: Fraction constructor cannot throw an arithmetic exception

Posted by ah...@apache.org.
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 01cb55acd3068e0518b88730c73f0081a557efdd
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 22:13:15 2020 +0100

    Fraction constructor cannot throw an arithmetic exception
    
    The greatest common divisor is never called with the 3 cases where
    ArithmeticUtils.gcd can throw an exception.
---
 .../main/java/org/apache/commons/numbers/fraction/Fraction.java    | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index b6dcef6..7fc5c7e 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -61,8 +61,7 @@ public final class Fraction
      *
      * @param num Numerator.
      * @param den Denominator.
-     * @throws ArithmeticException if the denominator is {@code zero}
-     * or if integer overflow occurs.
+     * @throws ArithmeticException if the denominator is {@code zero}.
      */
     private Fraction(int num, int den) {
         if (den == 0) {
@@ -88,6 +87,7 @@ public final class Fraction
                 q = den;
             }
 
+            // Will not throw
             final int d = ArithmeticUtils.gcd(p, q);
             if (d > 1) {
                 p /= d;
@@ -297,8 +297,7 @@ public final class Fraction
      *
      * @param num Numerator.
      * @param den Denominator.
-     * @throws ArithmeticException if the denominator is {@code zero}
-     * or if integer overflow occurs.
+     * @throws ArithmeticException if the denominator is {@code zero}.
      * @return a new instance.
      */
     public static Fraction of(final int num, final int den) {


[commons-numbers] 03/08: Fraction.of(numerator) to directly invoke special fraction constructor.

Posted by ah...@apache.org.
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 40dae8c00511e44fcd0d904f1569520dd5586b33
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 21:29:04 2020 +0100

    Fraction.of(numerator) to directly invoke special fraction constructor.
    
    When the denominator is 1 there is no requirement to use the reduction
    logic.
    
    This method is invoked for use in arithmetic with integers with a
    corresponding performance gain for avoiding the reduction logic.
---
 .../apache/commons/numbers/fraction/BigFraction.java | 20 +++++++++++++++++---
 .../apache/commons/numbers/fraction/Fraction.java    | 18 +++++++++++++++---
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index d5c52b1..d0411f0 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -20,6 +20,7 @@ import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.math.RoundingMode;
+import java.util.Objects;
 import org.apache.commons.numbers.core.ArithmeticUtils;
 import org.apache.commons.numbers.core.NativeOperators;
 
@@ -80,6 +81,18 @@ public final class BigFraction
     }
 
     /**
+     * Private constructor: Instances are created using factory methods.
+     *
+     * <p>This sets the denominator to 1.
+     *
+     * @param num Numerator (must not be null).
+     */
+    private BigFraction(BigInteger num) {
+        numerator = num;
+        denominator = BigInteger.ONE;
+    }
+
+    /**
      * Create a fraction given the double value and either the maximum
      * error allowed or the maximum number of denominator digits.
      *
@@ -308,7 +321,7 @@ public final class BigFraction
      * @return a new instance.
      */
     public static BigFraction of(final int num) {
-        return new BigFraction(BigInteger.valueOf(num), BigInteger.ONE);
+        return new BigFraction(BigInteger.valueOf(num));
     }
 
     /**
@@ -318,7 +331,7 @@ public final class BigFraction
      * @return a new instance.
      */
     public static BigFraction of(final long num) {
-        return new BigFraction(BigInteger.valueOf(num), BigInteger.ONE);
+        return new BigFraction(BigInteger.valueOf(num));
     }
 
     /**
@@ -329,7 +342,8 @@ public final class BigFraction
      * @throws NullPointerException if numerator is null.
      */
     public static BigFraction of(final BigInteger num) {
-        return new BigFraction(num, BigInteger.ONE);
+        Objects.requireNonNull(num, "numerator");
+        return new BigFraction(num);
     }
 
     /**
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index 85ee30d..b6dcef6 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -100,6 +100,18 @@ public final class Fraction
     }
 
     /**
+     * Private constructor: Instances are created using factory methods.
+     *
+     * <p>This sets the denominator to 1.
+     *
+     * @param num Numerator.
+     */
+    private Fraction(int num) {
+        numerator = num;
+        denominator = 1;
+    }
+
+    /**
      * Create a fraction given the double value and either the maximum error
      * allowed or the maximum number of denominator digits.
      *
@@ -276,7 +288,7 @@ public final class Fraction
      * @return a new instance.
      */
     public static Fraction of(final int num) {
-        return of(num, 1);
+        return new Fraction(num);
     }
 
     /**
@@ -396,9 +408,9 @@ public final class Fraction
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * <p>Raises an exception if the fraction is equal to zero.
-     * 
+     *
      * @throws ArithmeticException if the current numerator is {@code zero}
      */
     @Override


[commons-numbers] 01/08: Clean-up Fraction hashcode.

Posted by ah...@apache.org.
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 c7a2734c4db92354d34a6feec64d89a3055cc600
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 21:09:23 2020 +0100

    Clean-up Fraction hashcode.
    
    Removed reference to Arrays.hashCode in the comments.
    
    Changed computation to be the hash generated from Array.hashCode with
    the magnitude of the numerator and the denominator then multiplied by
    the sign.
---
 .../java/org/apache/commons/numbers/fraction/BigFraction.java    | 7 +++----
 .../main/java/org/apache/commons/numbers/fraction/Fraction.java  | 9 ++++-----
 .../org/apache/commons/numbers/fraction/BigFractionTest.java     | 6 +++---
 .../java/org/apache/commons/numbers/fraction/FractionTest.java   | 6 +++---
 4 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 3962e2e..d5c52b1 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -1020,16 +1020,15 @@ public final class BigFraction
     @Override
     public int hashCode() {
         // Incorporate the sign and absolute values of the numerator and denominator.
-        // Equivalent to
-        // Arrays.hashCode(new int[] {signum(), numerator.abs(), denominator.abs()})
+        // Equivalent to:
         // int hash = 1;
-        // hash = 31 * hash + signum();
         // hash = 31 * hash + numerator.abs().hashCode();
         // hash = 31 * hash + denominator.abs().hashCode();
+        // hash = hash * signum()
         // Note: BigInteger.hashCode() * BigInteger.signum() == BigInteger.abs().hashCode().
         final int numS = numerator.signum();
         final int denS = denominator.signum();
-        return 31 * (31 * (31 + numS * denS) + numerator.hashCode() * numS) + denominator.hashCode() * denS;
+        return (31 * (31 + numerator.hashCode() * numS) + denominator.hashCode() * denS) * numS * denS;
     }
 
     /**
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index be85fb3..2ab8932 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -707,16 +707,15 @@ public final class Fraction
     @Override
     public int hashCode() {
         // Incorporate the sign and absolute values of the numerator and denominator.
-        // Equivalent to
-        // Arrays.hashCode(new int[] {signum(), Math.abs(numerator), Math.abs(denominator)})
+        // Equivalent to:
         // int hash = 1;
-        // hash = 31 * hash + signum();
         // hash = 31 * hash + Math.abs(numerator);
         // hash = 31 * hash + Math.abs(denominator);
-        // Note: x * Integer.signum(x) is equivalent to Math.abs(x).
+        // hash = hash * signum()
+        // Note: x * Integer.signum(x) == Math.abs(x).
         final int numS = Integer.signum(numerator);
         final int denS = Integer.signum(denominator);
-        return 31 * (31 * (31 + numS * denS) + numerator * numS) + denominator * denS;
+        return (31 * (31 + numerator * numS) + denominator * denS) * numS * denS;
     }
 
     /**
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index 4d3377c..0b7030d 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -647,10 +647,10 @@ public class BigFractionTest {
         Assertions.assertNotSame(f1, f2, "Do not call this assertion with the same object");
         Assertions.assertEquals(f1, f2);
         Assertions.assertEquals(f1.hashCode(), f2.hashCode(), "Equal fractions have different hashCode");
-        // Check the hashcode computation.
+        // Check the computation matches the result of Arrays.hashCode and the signum.
         // This is not mandated but is a recommendation.
-        final int expected = Arrays.hashCode(new Object[] {f1.signum(),
-                                                           f1.getNumerator().abs(),
+        final int expected = f1.signum() *
+                             Arrays.hashCode(new Object[] {f1.getNumerator().abs(),
                                                            f1.getDenominator().abs()});
         Assertions.assertEquals(expected, f1.hashCode(), "Hashcode not equal to using Arrays.hashCode");
     }
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 ada4269..4c5f847 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
@@ -490,10 +490,10 @@ public class FractionTest {
         Assertions.assertNotSame(f1, f2, "Do not call this assertion with the same object");
         Assertions.assertEquals(f1, f2);
         Assertions.assertEquals(f1.hashCode(), f2.hashCode(), "Equal fractions have different hashCode");
-        // Check the hashcode computation.
+        // Check the computation matches the result of Arrays.hashCode and the signum.
         // This is not mandated but is a recommendation.
-        final int expected = Arrays.hashCode(new int[] {f1.signum(),
-                                                        Math.abs(f1.getNumerator()),
+        final int expected = f1.signum() *
+                             Arrays.hashCode(new int[] {Math.abs(f1.getNumerator()),
                                                         Math.abs(f1.getDenominator())});
         Assertions.assertEquals(expected, f1.hashCode(), "Hashcode not equal to using Arrays.hashCode");
     }


[commons-numbers] 04/08: Explicit test for divide by zero in fraction constructor

Posted by ah...@apache.org.
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 d293dd0bdb5faa2e33fcf75c7a1097634244631f
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 8 21:53:42 2020 +0100

    Explicit test for divide by zero in fraction constructor
---
 .../test/java/org/apache/commons/numbers/fraction/FractionTest.java    | 3 +++
 1 file changed, 3 insertions(+)

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 4c5f847..e075d56 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
@@ -49,6 +49,9 @@ public class FractionTest {
         assertFraction(1, Integer.MIN_VALUE, Fraction.of(1, Integer.MIN_VALUE));
         assertFraction(-1, Integer.MIN_VALUE, Fraction.of(-1, Integer.MIN_VALUE));
         assertFraction(1, 1, Fraction.of(Integer.MIN_VALUE, Integer.MIN_VALUE));
+
+        // Divide by zero
+        Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(1, 0));
     }
 
     @Test