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:26 UTC

[commons-numbers] 12/15: NUMBERS-118: Extract common multiply-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 2e41297bccccf685e84e56783afeab11e59a114b
Author: Schamschi <he...@gmx.at>
AuthorDate: Fri Jun 21 00:43:14 2019 +0200

    NUMBERS-118: Extract common multiply-by-fraction test cases
---
 .../commons/numbers/fraction/BigFractionTest.java  | 19 ++++---------
 .../commons/numbers/fraction/CommonTestCases.java  | 33 ++++++++++++++++++++++
 .../commons/numbers/fraction/FractionTest.java     | 18 ++++--------
 3 files changed, 44 insertions(+), 26 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 a16d35d..26fc8a1 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
@@ -396,21 +396,14 @@ public class BigFractionTest {
 
     @Test
     public void testMultiply() {
-        BigFraction a = BigFraction.of(1, 2);
-        BigFraction b = BigFraction.of(2, 3);
-
-        assertFraction(1, 4, a.multiply(a));
-        assertFraction(1, 3, a.multiply(b));
-        assertFraction(1, 3, b.multiply(a));
-        assertFraction(4, 9, b.multiply(b));
+        for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.multiplyByFractionTestCases()) {
+            BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+            BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
+            assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(f2));
+        }
 
-        BigFraction f1 = BigFraction.of(Integer.MAX_VALUE, 1);
         BigFraction f2 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
-        BigFraction f = f1.multiply(f2);
-        Assertions.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt());
-        Assertions.assertEquals(1, f.getDenominatorAsInt());
-
-        f = f2.multiply(Integer.MAX_VALUE);
+        BigFraction f = f2.multiply(Integer.MAX_VALUE);
         Assertions.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt());
         Assertions.assertEquals(1, f.getDenominatorAsInt());
 
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 c91f6e3..be19fdb 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
@@ -46,6 +46,11 @@ class CommonTestCases {
      */
     private static final List<BinaryOperatorTestCase> divideByFractionTestCasesList;
 
+    /**
+     * See {@link #multiplyByFractionTestCases()}
+     */
+    private static final List<BinaryOperatorTestCase> multiplyByFractionTestCasesList;
+
     static {
         numDenConstructorTestCasesList = collectNumDenConstructorTestCases();
         doubleConstructorTestCasesList = collectDoubleConstructorTestCases();
@@ -54,6 +59,7 @@ class CommonTestCases {
         negateTestCasesList = collectNegateTestCases();
         addFractionTestCasesList = collectAddFractionTestCases();
         divideByFractionTestCasesList = collectDivideByFractionTestCases();
+        multiplyByFractionTestCasesList = collectMultiplyByFractionTestCases();
     }
 
     /**
@@ -247,6 +253,22 @@ class CommonTestCases {
         return testCases;
     }
 
+    private static List<BinaryOperatorTestCase> collectMultiplyByFractionTestCases() {
+        List<BinaryOperatorTestCase> testCases = new ArrayList<>();
+
+        testCases.add(new BinaryOperatorTestCase(1, 2, 1, 2, 1, 4));
+        testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, 1, 3));
+        testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 1, 3));
+        testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 4, 9));
+
+        testCases.add(new BinaryOperatorTestCase(
+                Integer.MAX_VALUE, 1,
+                Integer.MIN_VALUE, 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
@@ -322,6 +344,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 multiplied 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> multiplyByFractionTestCases() {
+        return Collections.unmodifiableList(multiplyByFractionTestCasesList);
+    }
+
+    /**
      * 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 4d71a39..977e702 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
@@ -353,22 +353,14 @@ public class FractionTest {
 
     @Test
     public void testMultiply() {
-        {
-            Fraction a = Fraction.of(1, 2);
-            Fraction b = Fraction.of(2, 3);
-
-            assertFraction(1, 4, a.multiply(a));
-            assertFraction(1, 3, a.multiply(b));
-            assertFraction(1, 3, b.multiply(a));
-            assertFraction(4, 9, b.multiply(b));
+        for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.multiplyByFractionTestCases()) {
+            Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+            Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
+            assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(f2));
         }
 
         {
-            Fraction f1 = Fraction.of(Integer.MAX_VALUE, 1);
-            Fraction f2 = Fraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
-            final Fraction f = f1.multiply(f2);
-            assertFraction(Integer.MIN_VALUE, 1, f);
-
+            final Fraction f = Fraction.of(Integer.MIN_VALUE, 1);
             Assertions.assertThrows(NullPointerException.class,
                     () -> f.multiply(null)
             );