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.
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
*/
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 @@
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 @@
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 @@
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 @@
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 @@
Assertions.assertEquals(0L, first.longValue());
Assertions.assertEquals(1L, second.longValue());
+
+ Assertions.assertEquals(0L, ZERO_P.longValue());
+ Assertions.assertEquals(0L, ZERO_N.longValue());
}
@Test