You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/06/22 13:33:25 UTC

[commons-numbers] 11/15: NUMBERS-118: Extract common divide-by-fraction test cases

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

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

commit 7427fd0639557f25a2d7274597be70882527ffd0
Author: Schamschi <he...@gmx.at>
AuthorDate: Thu Jun 20 22:16:16 2019 +0200

    NUMBERS-118: Extract common divide-by-fraction test cases
---
 .../commons/numbers/fraction/BigFractionTest.java  | 42 +++++----------
 .../commons/numbers/fraction/CommonTestCases.java  | 59 ++++++++++++++++++----
 .../commons/numbers/fraction/FractionTest.java     | 31 ++----------
 3 files changed, 67 insertions(+), 65 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 3245544..a16d35d 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
@@ -314,9 +314,9 @@ public class BigFractionTest {
         }
 
         {
-            final BigFraction f3 = BigFraction.of(-17 - 2*13*2, 13*13*17*2*2);
+            final BigFraction f = BigFraction.of(-17 - 2*13*2, 13*13*17*2*2);
             Assertions.assertThrows(NullPointerException.class,
-                    () -> f3.add((BigFraction) null)
+                    () -> f.add((BigFraction) null)
             );
         }
 
@@ -351,13 +351,11 @@ public class BigFractionTest {
 
     @Test
     public void testDivide() {
-        BigFraction a = BigFraction.of(1, 2);
-        BigFraction b = BigFraction.of(2, 3);
-
-        assertFraction(1, 1, a.divide(a));
-        assertFraction(3, 4, a.divide(b));
-        assertFraction(4, 3, b.divide(a));
-        assertFraction(1, 1, b.divide(b));
+        for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.divideByFractionTestCases()) {
+            BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+            BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
+            assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(f2));
+        }
 
         BigFraction f1 = BigFraction.of(3, 5);
         BigFraction f2 = BigFraction.ZERO;
@@ -372,27 +370,11 @@ public class BigFractionTest {
         BigFraction f = f1.divide(f2);
         Assertions.assertSame(BigFraction.ZERO, f);
 
-        f1 = BigFraction.of(2, 7);
-        f2 = BigFraction.ONE;
-        f = f1.divide(f2);
-        Assertions.assertEquals(2, f.getNumeratorAsInt());
-        Assertions.assertEquals(7, f.getDenominatorAsInt());
-
-        f1 = BigFraction.of(1, Integer.MAX_VALUE);
-        f = f1.divide(f1);
-        Assertions.assertEquals(1, f.getNumeratorAsInt());
-        Assertions.assertEquals(1, f.getDenominatorAsInt());
-
-        f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
-        f2 = BigFraction.of(1, Integer.MAX_VALUE);
-        f = f1.divide(f2);
-        Assertions.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt());
-        Assertions.assertEquals(1, f.getDenominatorAsInt());
-
-        try {
-            f.divide((BigFraction) null);
-            Assertions.fail("expecting NullPointerException");
-        } catch (NullPointerException ex) {
+        {
+            final BigFraction f3 = BigFraction.of(Integer.MIN_VALUE, 1);
+            Assertions.assertThrows(NullPointerException.class,
+                    () -> f3.divide((BigFraction) null)
+            );
         }
 
         f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
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 e405b48..c91f6e3 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
@@ -41,6 +41,11 @@ class CommonTestCases {
      */
     private static final List<BinaryOperatorTestCase> addFractionTestCasesList;
 
+    /**
+     * See {@link #divideByFractionTestCases()}
+     */
+    private static final List<BinaryOperatorTestCase> divideByFractionTestCasesList;
+
     static {
         numDenConstructorTestCasesList = collectNumDenConstructorTestCases();
         doubleConstructorTestCasesList = collectDoubleConstructorTestCases();
@@ -48,6 +53,7 @@ class CommonTestCases {
         reciprocalTestCasesList = collectReciprocalTestCases();
         negateTestCasesList = collectNegateTestCases();
         addFractionTestCasesList = collectAddFractionTestCases();
+        divideByFractionTestCasesList = collectDivideByFractionTestCases();
     }
 
     /**
@@ -182,15 +188,10 @@ class CommonTestCases {
     private static List<BinaryOperatorTestCase> collectAddFractionTestCases() {
         List<BinaryOperatorTestCase> testCases = new ArrayList<>();
 
-        {
-            int[] a = new int[]{1, 2};
-            int[] b = new int[]{2, 3};
-
-            testCases.add(new BinaryOperatorTestCase(a[0], a[1], a[0], a[1], 1, 1));
-            testCases.add(new BinaryOperatorTestCase(a[0], a[1], b[0], b[1], 7, 6));
-            testCases.add(new BinaryOperatorTestCase(b[0], b[1], a[0], a[1], 7, 6));
-            testCases.add(new BinaryOperatorTestCase(b[0], b[1], b[0], b[1], 4, 3));
-        }
+        testCases.add(new BinaryOperatorTestCase(1, 2, 1, 2, 1, 1));
+        testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, 7, 6));
+        testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 7, 6));
+        testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 4, 3));
 
         testCases.add(new BinaryOperatorTestCase(
                 -1, 13*13*2*2,
@@ -218,6 +219,35 @@ class CommonTestCases {
     }
 
     /**
+     * Defines test cases as described in {@link #divideByFractionTestCases()} and collects
+     * them into a {@code List}.
+     * @return a list of test cases as described above
+     */
+    private static List<BinaryOperatorTestCase> collectDivideByFractionTestCases() {
+        List<BinaryOperatorTestCase> testCases = new ArrayList<>();
+
+        testCases.add(new BinaryOperatorTestCase(1, 2, 1, 2, 1, 1));
+        testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, 3, 4));
+        testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 4, 3));
+        testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 1, 1));
+
+        testCases.add(new BinaryOperatorTestCase(
+                2, 7,
+                1, 1,
+                2, 7));
+        testCases.add(new BinaryOperatorTestCase(
+                1, Integer.MAX_VALUE,
+                1, Integer.MAX_VALUE,
+                1, 1));
+        testCases.add(new BinaryOperatorTestCase(
+                Integer.MIN_VALUE, Integer.MAX_VALUE,
+                1, Integer.MAX_VALUE,
+                Integer.MIN_VALUE, 1));
+
+        return testCases;
+    }
+
+    /**
      * Provides a list of test cases where a fraction should be created from a specified
      * numerator and denominator, both in the {@code int} range, and the expected
      * numerator and denominator of the created fraction are also in the {@code int} range.
@@ -281,6 +311,17 @@ class CommonTestCases {
     }
 
     /**
+     * Provides a list of test cases where a fraction, created from a specified numerator and denominator
+     * in the {@code int} range, should be divided by another fraction, also created from a specified numerator and denominator
+     * in the {@code int} range, and the expected numerator and denominator of the resulting fraction
+     * are in the {@code int} range as well.
+     * @return a list of test cases as described above
+     */
+    static List<BinaryOperatorTestCase> divideByFractionTestCases() {
+        return Collections.unmodifiableList(divideByFractionTestCasesList);
+    }
+
+    /**
      * Represents a test case where a unary operation should be performed on a specified combination
      * of numerator and denominator, both in the {@code int} range, and the numerator and
      * denominator of the expected result are also in the {@code int} range.
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 84f16d6..4d71a39 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
@@ -302,14 +302,10 @@ public class FractionTest {
 
     @Test
     public void testDivide() {
-        {
-            Fraction a = Fraction.of(1, 2);
-            Fraction b = Fraction.of(2, 3);
-
-            assertFraction(1, 1, a.divide(a));
-            assertFraction(3, 4, a.divide(b));
-            assertFraction(4, 3, b.divide(a));
-            assertFraction(1, 1, b.divide(b));
+        for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.divideByFractionTestCases()) {
+            Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+            Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
+            assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(f2));
         }
 
         {
@@ -328,24 +324,7 @@ public class FractionTest {
         }
 
         {
-            Fraction f1 = Fraction.of(2, 7);
-            Fraction f2 = Fraction.ONE;
-            Fraction f = f1.divide(f2);
-            assertFraction(2, 7, f);
-        }
-
-        {
-            Fraction f1 = Fraction.of(1, Integer.MAX_VALUE);
-            Fraction f = f1.divide(f1);
-            assertFraction(1, 1, f);
-        }
-
-        {
-            Fraction f1 = Fraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
-            Fraction f2 = Fraction.of(1, Integer.MAX_VALUE);
-            final Fraction f = f1.divide(f2);
-            assertFraction(Integer.MIN_VALUE, 1, f);
-
+            final Fraction f = Fraction.of(Integer.MIN_VALUE, 1);
             Assertions.assertThrows(NullPointerException.class,
                     () -> f.divide(null)
             );