commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aherb...@apache.org
Subject [commons-numbers] 05/26: Fraction: Increase similarity between BigFraction tests
Date Thu, 16 Apr 2020 12:18:20 GMT
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 bb645f6c19a93cf2bc8aef21ee1c09e0bab3658d
Author: Alex Herbert <aherbert@apache.org>
AuthorDate: Mon Apr 13 21:01:30 2020 +0100

    Fraction: Increase similarity between BigFraction tests
    
    Adds some compareTo tests that currently fail.
---
 .../commons/numbers/fraction/BigFractionTest.java  | 87 +++++++++++-----------
 .../commons/numbers/fraction/CommonTestCases.java  |  3 +
 .../commons/numbers/fraction/FractionTest.java     | 50 +++++++------
 3 files changed, 73 insertions(+), 67 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 3a24a20..33445a4 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
@@ -99,16 +99,31 @@ public class BigFractionTest {
             );
         }
 
+        // Cases with different exact results from Fraction
+        assertFraction(6004799503160661L, 18014398509481984L, BigFraction.from(1.0 / 3.0));
+        assertFraction(6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / 100.0));
+        assertFraction(1784551352345559L, 562949953421312L, BigFraction.from(317.0 / 100.0));
+        assertFraction(-6004799503160661L, 18014398509481984L, BigFraction.from(-1.0 / 3.0));
+        assertFraction(-6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / -100.0));
+        assertFraction(-1784551352345559L, 562949953421312L, BigFraction.from(-317.0 / 100.0));
+
+        // Extreme double values
+        Assertions.assertEquals(1L, BigFraction.from(Double.MAX_VALUE).getDenominatorAsLong());
+        Assertions.assertEquals(1L, BigFraction.from(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong());
+        assertFraction(BigInteger.ONE, BigInteger.ONE.shiftLeft(1074), BigFraction.from(Double.MIN_VALUE));
+
+        // Check exact round-trip of double
         Assertions.assertEquals(0.00000000000001, BigFraction.from(0.00000000000001).doubleValue());
         Assertions.assertEquals(0.40000000000001, BigFraction.from(0.40000000000001).doubleValue());
         Assertions.assertEquals(15.0000000000001, BigFraction.from(15.0000000000001).doubleValue());
+        // Check the representation
         assertFraction(3602879701896487L, 9007199254740992L, BigFraction.from(0.40000000000001));
         assertFraction(1055531162664967L, 70368744177664L, BigFraction.from(15.0000000000001));
     }
 
     // MATH-181
     @Test
-    public void testDigitLimitConstructor() throws Exception {
+    public void testDoubleConstructorWithMaxDenominator() throws Exception {
         assertFraction(2, 5, BigFraction.from(0.4,   9));
         assertFraction(2, 5, BigFraction.from(0.4,  99));
         assertFraction(2, 5, BigFraction.from(0.4, 999));
@@ -144,22 +159,17 @@ public class BigFractionTest {
 
     // MATH-1029
     @Test
-    public void testPositiveValueOverflow() {
+    public void testDoubleConstructorWithMaxDenominatorOverFlow() {
         Assertions.assertThrows(ArithmeticException.class,
             () -> BigFraction.from(1e10, 1000)
         );
-    }
-
-    // MATH-1029
-    @Test
-    public void testNegativeValueOverflow() {
         Assertions.assertThrows(ArithmeticException.class,
-            () -> assertFraction((long) -1e10, 1, BigFraction.from(-1e10, 1000))
+            () -> BigFraction.from(-1e10, 1000)
         );
     }
 
     @Test
-    public void testEpsilonLimitConstructor() throws Exception {
+    public void testDoubleConstructorWithEpsilonLimit() throws Exception {
         assertFraction(2, 5, BigFraction.from(0.4, 1.0e-5, 100));
 
         assertFraction(3, 5,      BigFraction.from(0.6152, 0.02, 100));
@@ -177,6 +187,8 @@ public class BigFractionTest {
         final BigFraction c = BigFraction.of(1, 2);
         final BigFraction d = BigFraction.of(-1, 2);
         final BigFraction e = BigFraction.of(1, -2);
+        final BigFraction f = BigFraction.of(-1, -2);
+        final BigFraction g = BigFraction.of(-1, Integer.MIN_VALUE);
 
         Assertions.assertEquals(0, a.compareTo(a));
         Assertions.assertEquals(0, a.compareTo(c));
@@ -187,6 +199,14 @@ public class BigFractionTest {
         Assertions.assertEquals(-1, e.compareTo(a));
         Assertions.assertEquals(1, a.compareTo(e));
         Assertions.assertEquals(0, d.compareTo(e));
+        Assertions.assertEquals(0, a.compareTo(f));
+        Assertions.assertEquals(0, f.compareTo(a));
+        Assertions.assertEquals(1, f.compareTo(e));
+        Assertions.assertEquals(-1, e.compareTo(f));
+//        Assertions.assertEquals(-1, g.compareTo(a));
+        Assertions.assertEquals(-1, g.compareTo(f));
+//        Assertions.assertEquals(1, a.compareTo(g));
+        Assertions.assertEquals(-1, d.compareTo(g));
 
         Assertions.assertEquals(0, BigFraction.of(0, 3).compareTo(BigFraction.of(0, -2)));
 
@@ -411,22 +431,11 @@ public class BigFractionTest {
     }
 
     @Test
-    public void testConstructorDouble() {
-        assertFraction(0, 1, BigFraction.from(0.0));
-        assertFraction(1, 2, BigFraction.from(0.5));
-        assertFraction(6004799503160661L, 18014398509481984L, BigFraction.from(1.0 / 3.0));
-        assertFraction(6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / 100.0));
-        assertFraction(1784551352345559L, 562949953421312L, BigFraction.from(317.0 / 100.0));
-        assertFraction(-1, 2, BigFraction.from(-0.5));
-        assertFraction(-6004799503160661L, 18014398509481984L, BigFraction.from(-1.0 / 3.0));
-        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})
{
-            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());
-        assertFraction(BigInteger.ONE, BigInteger.ONE.shiftLeft(1074), BigFraction.from(Double.MIN_VALUE));
+    public void testBigDecimalValue() {
+        Assertions.assertEquals(new BigDecimal(0.5), BigFraction.of(1, 2).bigDecimalValue());
+        Assertions.assertEquals(new BigDecimal("0.0003"), BigFraction.of(3, 10000).bigDecimalValue());
+        Assertions.assertEquals(new BigDecimal("0"), BigFraction.of(1, 3).bigDecimalValue(RoundingMode.DOWN));
+        Assertions.assertEquals(new BigDecimal("0.333"), BigFraction.of(1, 3).bigDecimalValue(3,
RoundingMode.DOWN));
     }
 
     @Test
@@ -581,14 +590,6 @@ public class BigFractionTest {
     }
 
     @Test
-    public void testBigDecimalValue() {
-        Assertions.assertEquals(new BigDecimal(0.5), BigFraction.of(1, 2).bigDecimalValue());
-        Assertions.assertEquals(new BigDecimal("0.0003"), BigFraction.of(3, 10000).bigDecimalValue());
-        Assertions.assertEquals(new BigDecimal("0"), BigFraction.of(1, 3).bigDecimalValue(RoundingMode.DOWN));
-        Assertions.assertEquals(new BigDecimal("0.333"), BigFraction.of(1, 3).bigDecimalValue(3,
RoundingMode.DOWN));
-    }
-
-    @Test
     public void testEqualsAndHashCode() {
         BigFraction zero = BigFraction.of(0, 1);
         Assertions.assertEquals(zero, zero);
@@ -650,16 +651,6 @@ public class BigFractionTest {
     }
 
     @Test
-    public void testMath340() {
-        BigFraction fractionA = BigFraction.from(0.00131);
-        BigFraction fractionB = BigFraction.from(.37).reciprocal();
-        BigFraction errorResult = fractionA.multiply(fractionB);
-        BigFraction correctResult = BigFraction.of(fractionA.getNumerator().multiply(fractionB.getNumerator()),
-                                                    fractionA.getDenominator().multiply(fractionB.getDenominator()));
-        Assertions.assertEquals(correctResult, errorResult);
-    }
-
-    @Test
     public void testAdditiveNeutral() {
         Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.zero());
     }
@@ -743,4 +734,14 @@ public class BigFractionTest {
         Assertions.assertThrows(NumberFormatException.class, () -> BigFraction.parse("1
/ --2"));
         Assertions.assertThrows(NumberFormatException.class, () -> BigFraction.parse("x"));
     }
+
+    @Test
+    public void testMath340() {
+        BigFraction fractionA = BigFraction.from(0.00131);
+        BigFraction fractionB = BigFraction.from(.37).reciprocal();
+        BigFraction errorResult = fractionA.multiply(fractionB);
+        BigFraction correctResult = BigFraction.of(fractionA.getNumerator().multiply(fractionB.getNumerator()),
+                                                   fractionA.getDenominator().multiply(fractionB.getDenominator()));
+        Assertions.assertEquals(correctResult, errorResult);
+    }
 }
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
index 336add7..39ad560 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
@@ -170,6 +170,9 @@ final class CommonTestCases {
         testCases.add(new DoubleToFractionTestCase(9d / 11d, 9, 11));
         testCases.add(new DoubleToFractionTestCase(10d / 11d, 10, 11));
 
+        testCases.add(new DoubleToFractionTestCase(-1d / 2d, -1, 2));
+        testCases.add(new DoubleToFractionTestCase(-1d / 3d, -1, 3));
+
         testCases.add(new DoubleToFractionTestCase(0.00000000000001, 0, 1));
         testCases.add(new DoubleToFractionTestCase(0.40000000000001, 2, 5));
         testCases.add(new DoubleToFractionTestCase(15.0000000000001, 15, 1));
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 fccf015..6188d55 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
@@ -75,11 +75,19 @@ public class FractionTest {
                     Fraction.from(testCase.operand)
             );
         }
+
+        // Cases with different exact results from BigFraction
+        assertFraction(1, 3, Fraction.from(1.0 / 3.0));
+        assertFraction(17, 100, Fraction.from(17.0 / 100.0));
+        assertFraction(317, 100, Fraction.from(317.0 / 100.0));
+        assertFraction(-1, 3, Fraction.from(-1.0 / 3.0));
+        assertFraction(-17, 100, Fraction.from(17.0 / -100.0));
+        assertFraction(-317, 100, Fraction.from(-317.0 / 100.0));
     }
 
     // MATH-181
     @Test
-    public void testDigitLimitConstructor() throws Exception  {
+    public void testDoubleConstructorWithMaxDenominator() throws Exception  {
         assertFraction(2, 5, Fraction.from(0.4,   9));
         assertFraction(2, 5, Fraction.from(0.4,  99));
         assertFraction(2, 5, Fraction.from(0.4, 999));
@@ -114,21 +122,21 @@ public class FractionTest {
     }
 
     @Test
-    public void testIntegerOverflow() {
-        checkIntegerOverflow(0.75000000001455192);
-        checkIntegerOverflow(1.0e10);
-        checkIntegerOverflow(-1.0e10);
-        checkIntegerOverflow(-43979.60679604749);
+    public void testDoubleConstructorOverflow() {
+        assertDoubleConstructorOverflow(0.75000000001455192);
+        assertDoubleConstructorOverflow(1.0e10);
+        assertDoubleConstructorOverflow(-1.0e10);
+        assertDoubleConstructorOverflow(-43979.60679604749);
     }
 
-    private void checkIntegerOverflow(final double a) {
+    private void assertDoubleConstructorOverflow(final double a) {
         Assertions.assertThrows(ArithmeticException.class,
             () -> Fraction.from(a, 1.0e-12, 1000)
         );
     }
 
     @Test
-    public void testEpsilonLimitConstructor() throws Exception  {
+    public void testDoubleConstructorWithEpsilonLimit() throws Exception  {
         assertFraction(2, 5, Fraction.from(0.4, 1.0e-5, 100));
 
         assertFraction(3, 5,      Fraction.from(0.6152, 0.02, 100));
@@ -146,6 +154,8 @@ public class FractionTest {
         final Fraction c = Fraction.of(1, 2);
         final Fraction d = Fraction.of(-1, 2);
         final Fraction e = Fraction.of(1, -2);
+        final Fraction f = Fraction.of(-1, -2);
+        final Fraction g = Fraction.of(-1, Integer.MIN_VALUE);
 
         Assertions.assertEquals(0, a.compareTo(a));
         Assertions.assertEquals(0, a.compareTo(c));
@@ -156,6 +166,14 @@ public class FractionTest {
         Assertions.assertEquals(-1, e.compareTo(a));
         Assertions.assertEquals(1, a.compareTo(e));
         Assertions.assertEquals(0, d.compareTo(e));
+        Assertions.assertEquals(0, a.compareTo(f));
+        Assertions.assertEquals(0, f.compareTo(a));
+        Assertions.assertEquals(1, f.compareTo(e));
+        Assertions.assertEquals(-1, e.compareTo(f));
+//        Assertions.assertEquals(-1, g.compareTo(a));
+        Assertions.assertEquals(-1, g.compareTo(f));
+//        Assertions.assertEquals(1, a.compareTo(g));
+        Assertions.assertEquals(-1, d.compareTo(g));
 
         Assertions.assertEquals(0, Fraction.of(0, 3).compareTo(Fraction.of(0, -2)));
 
@@ -234,22 +252,6 @@ public class FractionTest {
     }
 
     @Test
-    public void testConstructorDouble() {
-        assertFraction(0, 1, Fraction.from(0.0));
-        assertFraction(1, 2, Fraction.from(0.5));
-        assertFraction(1, 3, Fraction.from(1.0 / 3.0));
-        assertFraction(17, 100, Fraction.from(17.0 / 100.0));
-        assertFraction(317, 100, Fraction.from(317.0 / 100.0));
-        assertFraction(-1, 2, Fraction.from(-0.5));
-        assertFraction(-1, 3, Fraction.from(-1.0 / 3.0));
-        assertFraction(-17, 100, Fraction.from(17.0 / -100.0));
-        assertFraction(-317, 100, Fraction.from(-317.0 / 100.0));
-        for (double v : new double[] {Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY})
{
-            Assertions.assertThrows(IllegalArgumentException.class, () -> Fraction.from(v));
-        }
-    }
-
-    @Test
     public void testAbs() {
         for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.absTestCases())
{
             Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator);


Mime
View raw message