commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aherb...@apache.org
Subject [commons-numbers] 01/26: Fraction: Add tests for two representations of zero.
Date Thu, 16 Apr 2020 12:18:16 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 828f6a728d92e1ae911233ea2da9c100586f8d2d
Author: Alex Herbert <aherbert@apache.org>
AuthorDate: Sun Apr 12 12:30:55 2020 +0100

    Fraction: Add tests for two representations of zero.
    
    BigFraction handles the conversion to double and float differently than
    Fraction as it does not support negative zero.
---
 .../commons/numbers/fraction/BigFractionTest.java     | 19 +++++++++++++++++++
 .../apache/commons/numbers/fraction/FractionTest.java | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)

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 40dc9e9..6afa2cf 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
@@ -27,6 +27,11 @@ import org.junit.jupiter.api.Test;
 
 public class BigFractionTest {
 
+    /** The zero representation with positive denominator. */
+    private static final BigFraction ZERO_P = BigFraction.of(0, 1);
+    /** The zero representation with negative denominator. */
+    private static final BigFraction ZERO_N = BigFraction.of(0, -1);
+
     private static void assertFraction(long expectedNumerator, long expectedDenominator,
BigFraction actual) {
         Assertions.assertEquals(BigInteger.valueOf(expectedNumerator), actual.getNumerator());
         Assertions.assertEquals(BigInteger.valueOf(expectedDenominator), actual.getDenominator());
@@ -186,6 +191,8 @@ public class BigFractionTest {
         Assertions.assertEquals(-1, pi1.compareTo(pi2));
         Assertions.assertEquals(1, pi2.compareTo(pi1));
         Assertions.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20);
+
+        Assertions.assertEquals(0, ZERO_P.compareTo(ZERO_N));
     }
 
     @Test
@@ -198,6 +205,9 @@ public class BigFractionTest {
         assertDoubleValue(0.5, -1, -2);
         assertDoubleValue(1.0 / 3.0, 1, 3);
 
+        Assertions.assertEquals(0.0, ZERO_P.doubleValue());
+        Assertions.assertEquals(0.0, ZERO_N.doubleValue());
+
         //NUMBERS-120
         assertDoubleValue(
                 2d - 0x1P-52,
@@ -357,6 +367,9 @@ public class BigFractionTest {
         Assertions.assertEquals(e, BigFraction.of(-1, -3).floatValue(), 0.0f);
         Assertions.assertEquals(-e, BigFraction.of(-1, 3).floatValue(), 0.0f);
         Assertions.assertEquals(-e, BigFraction.of(1, -3).floatValue(), 0.0f);
+
+        Assertions.assertEquals(0.0f, ZERO_P.floatValue());
+        Assertions.assertEquals(0.0f, ZERO_N.floatValue());
     }
 
     @Test
@@ -370,6 +383,9 @@ public class BigFractionTest {
         Assertions.assertEquals(1, BigFraction.of(-3, -2).intValue());
         Assertions.assertEquals(-1, BigFraction.of(-3, 2).intValue());
         Assertions.assertEquals(-1, BigFraction.of(3, -2).intValue());
+
+        Assertions.assertEquals(0, ZERO_P.intValue());
+        Assertions.assertEquals(0, ZERO_N.intValue());
     }
 
     @Test
@@ -383,6 +399,9 @@ public class BigFractionTest {
         Assertions.assertEquals(1L, BigFraction.of(-3, -2).longValue());
         Assertions.assertEquals(-1L, BigFraction.of(-3, 2).longValue());
         Assertions.assertEquals(-1L, BigFraction.of(3, -2).longValue());
+
+        Assertions.assertEquals(0, ZERO_P.longValue());
+        Assertions.assertEquals(0, ZERO_N.longValue());
     }
 
     @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 5644c72..9f663e3 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
@@ -26,6 +26,11 @@ import org.junit.jupiter.api.Test;
  */
 public class FractionTest {
 
+    /** The zero representation with positive denominator. */
+    private static final Fraction ZERO_P = Fraction.of(0, 1);
+    /** The zero representation with negative denominator. */
+    private static final Fraction ZERO_N = Fraction.of(0, -1);
+
     private void assertFraction(int expectedNumerator, int expectedDenominator, Fraction
actual) {
         Assertions.assertEquals(expectedNumerator, actual.getNumerator());
         Assertions.assertEquals(expectedDenominator, actual.getDenominator());
@@ -147,6 +152,8 @@ public class FractionTest {
         Assertions.assertEquals(-1, pi1.compareTo(pi2));
         Assertions.assertEquals(1, pi2.compareTo(pi1));
         Assertions.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20);
+
+        Assertions.assertEquals(0, ZERO_P.compareTo(ZERO_N));
     }
 
     @Test
@@ -156,6 +163,9 @@ public class FractionTest {
 
         Assertions.assertEquals(0.5, first.doubleValue(), 0.0);
         Assertions.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
+
+        Assertions.assertEquals(0.0, ZERO_P.doubleValue());
+        Assertions.assertEquals(-0.0, ZERO_N.doubleValue());
     }
 
     @Test
@@ -165,6 +175,9 @@ public class FractionTest {
 
         Assertions.assertEquals(0.5f, first.floatValue(), 0.0f);
         Assertions.assertEquals((float)(1.0 / 3.0), second.floatValue(), 0.0f);
+
+        Assertions.assertEquals(0.0f, ZERO_P.floatValue());
+        Assertions.assertEquals(-0.0f, ZERO_N.floatValue());
     }
 
     @Test
@@ -174,6 +187,9 @@ public class FractionTest {
 
         Assertions.assertEquals(0, first.intValue());
         Assertions.assertEquals(1, second.intValue());
+
+        Assertions.assertEquals(0, ZERO_P.intValue());
+        Assertions.assertEquals(0, ZERO_N.intValue());
     }
 
     @Test
@@ -183,6 +199,9 @@ public class FractionTest {
 
         Assertions.assertEquals(0L, first.longValue());
         Assertions.assertEquals(1L, second.longValue());
+
+        Assertions.assertEquals(0L, ZERO_P.longValue());
+        Assertions.assertEquals(0L, ZERO_N.longValue());
     }
 
     @Test


Mime
View raw message