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/05 21:42:17 UTC

[commons-numbers] 02/05: Increase coverage in FractionTest

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 489d90a544970584e79ad69fe4f62e185773776d
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Apr 5 15:24:18 2020 +0100

    Increase coverage in FractionTest
---
 .../commons/numbers/fraction/CommonTestCases.java  |  1 +
 .../commons/numbers/fraction/FractionTest.java     | 30 ++++++++++++++++++++++
 2 files changed, 31 insertions(+)

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 3e23798..1c5df47 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
@@ -213,6 +213,7 @@ final class CommonTestCases {
         testCases.add(new UnaryOperatorTestCase(1, Integer.MIN_VALUE, -1, Integer.MIN_VALUE));
 
         // XXX Failed by "BigFraction" (whose implementation differs from "Fraction").
+        // These are tested explicitly in FractionTest.
         // testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, Integer.MIN_VALUE, -1, 1));
         // testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, 1, Integer.MIN_VALUE, -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 0dd7c5f..9d19663 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
@@ -218,6 +218,18 @@ public class FractionTest {
         }
     }
 
+    /**
+     * Test special cases of negation that differ from BigFraction.
+     */
+    @Test
+    public void testNegateMinValue() {
+        final Fraction one = Fraction.of(Integer.MIN_VALUE, Integer.MIN_VALUE);
+        assertFraction(-1, 1, one.negate());
+        // Special case where the negation of the numerator is not possible.
+        final Fraction minValue = Fraction.of(Integer.MIN_VALUE, 1);
+        assertFraction(Integer.MIN_VALUE, -1, minValue.negate());
+    }
+
     @Test
     public void testAdd() {
         for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.addFractionTestCases()) {
@@ -419,6 +431,23 @@ public class FractionTest {
         Fraction minusOne2 = Fraction.of(1, -1);
         Assertions.assertEquals(minusOne2, minusOne);
         Assertions.assertEquals(minusOne, minusOne2);
+
+        // Same numerator or denominator as 1/1
+        Fraction half = Fraction.of(1, 2);
+        Fraction two = Fraction.of(2, 1);
+        Assertions.assertNotEquals(one, half);
+        Assertions.assertNotEquals(one, two);
+
+        // Check worst case fractions which will have a component using MIN_VALUE.
+        // Note: abs(MIN_VALUE) is negative but this should not effect the equals result.
+        Fraction almostOne = Fraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
+        Fraction almostOne2 = Fraction.of(Integer.MIN_VALUE, -Integer.MAX_VALUE);
+        Assertions.assertEquals(almostOne, almostOne);
+        Assertions.assertNotEquals(almostOne, almostOne2);
+        Fraction almostZero = Fraction.of(-1, Integer.MIN_VALUE);
+        Fraction almostZero2 = Fraction.of(1, Integer.MIN_VALUE);
+        Assertions.assertEquals(almostZero, almostZero);
+        Assertions.assertNotEquals(almostZero, almostZero2);
     }
 
     @Test
@@ -433,6 +462,7 @@ public class FractionTest {
     @Test
     public void testToString() {
         Assertions.assertEquals("0", Fraction.of(0, 3).toString());
+        Assertions.assertEquals("0", Fraction.of(0, -3).toString());
         Assertions.assertEquals("3", Fraction.of(6, 2).toString());
         Assertions.assertEquals("2 / 3", Fraction.of(18, 27).toString());
         Assertions.assertEquals("-10 / 11", Fraction.of(-10, 11).toString());