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

[commons-numbers] 07/15: NUMBERS-118: Extract common negate 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 27a4aa18af2f5c27e82a2e93a8f67d4ea35f5162
Author: Schamschi <he...@gmx.at>
AuthorDate: Thu Jun 20 14:51:22 2019 +0200

    NUMBERS-118: Extract common negate test cases
---
 .../commons/numbers/fraction/BigFractionTest.java  | 23 ++++------------
 .../commons/numbers/fraction/CommonTestCases.java  | 32 ++++++++++++++++++++++
 .../commons/numbers/fraction/FractionTest.java     | 19 ++-----------
 3 files changed, 40 insertions(+), 34 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 857a385..1e17656 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
@@ -300,24 +300,11 @@ public class BigFractionTest {
 
     @Test
     public void testNegate() {
-        BigFraction f = null;
-
-        f = BigFraction.of(50, 75);
-        f = f.negate();
-        Assertions.assertEquals(-2, f.getNumeratorAsInt());
-        Assertions.assertEquals(3, f.getDenominatorAsInt());
-
-        f = BigFraction.of(-50, 75);
-        f = f.negate();
-        Assertions.assertEquals(2, f.getNumeratorAsInt());
-        Assertions.assertEquals(3, f.getDenominatorAsInt());
-
-        // large values
-        f = BigFraction.of(Integer.MAX_VALUE - 1, Integer.MAX_VALUE);
-        f = f.negate();
-        Assertions.assertEquals(Integer.MIN_VALUE + 2, f.getNumeratorAsInt());
-        Assertions.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
-
+        for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.negateTestCases()) {
+            BigFraction f = BigFraction.of(testCase.operandNumerator, testCase.operandDenominator);
+            f = f.negate();
+            assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f);
+        }
     }
 
     @Test
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 82cb1e2..ad6e917 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
@@ -31,11 +31,17 @@ public class CommonTestCases {
      */
     private static final List<UnaryOperatorTestCase> reciprocalTestCasesList;
 
+    /**
+     * See {@link #negateTestCases()}
+     */
+    private static final List<UnaryOperatorTestCase> negateTestCasesList;
+
     static {
         numDenConstructorTestCasesList = collectNumDenConstructorTestCases();
         doubleConstructorTestCasesList = collectDoubleConstructorTestCases();
         absTestCasesList = collectAbsTestCases();
         reciprocalTestCasesList = collectReciprocalTestCases();
+        negateTestCasesList = collectNegateTestCases();
     }
 
     /**
@@ -148,6 +154,21 @@ public class CommonTestCases {
     }
 
     /**
+     * Defines test cases as described in {@link #negateTestCases()} and collects
+     * them into a {@code List}.
+     * @return a list of test cases as described above
+     */
+    private static List<UnaryOperatorTestCase> collectNegateTestCases() {
+        List<UnaryOperatorTestCase> testCases = new ArrayList<>();
+
+        testCases.add(new UnaryOperatorTestCase(50, 75, -2, 3));
+        testCases.add(new UnaryOperatorTestCase(-50, 75, 2, 3));
+        testCases.add(new UnaryOperatorTestCase(Integer.MAX_VALUE - 1, Integer.MAX_VALUE, Integer.MIN_VALUE + 2, Integer.MAX_VALUE));
+
+        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.
@@ -190,6 +211,17 @@ public class CommonTestCases {
     }
 
     /**
+     * Provides a list of test cases where the additive inverse of a fraction created from a specified
+     * numerator and denominator, both in the {@code int} range, should be
+     * calculated, and the expected
+     * numerator and denominator of the resulting fraction are also in the {@code int} range.
+     * @return a list of test cases as described above
+     */
+    public static List<UnaryOperatorTestCase> negateTestCases() {
+        return Collections.unmodifiableList(negateTestCasesList);
+    }
+
+    /**
      * 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 543a06e..521741b 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
@@ -226,23 +226,10 @@ public class FractionTest {
 
     @Test
     public void testNegate() {
-        {
-            Fraction f = Fraction.of(50, 75);
-            f = f.negate();
-            assertFraction(-2, 3, f);
-        }
-
-        {
-            Fraction f = Fraction.of(-50, 75);
-            f = f.negate();
-            assertFraction(2, 3, f);
-        }
-
-        // large values
-        {
-            Fraction f = Fraction.of(Integer.MAX_VALUE - 1, Integer.MAX_VALUE);
+        for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.negateTestCases()) {
+            Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator);
             f = f.negate();
-            assertFraction(Integer.MIN_VALUE + 2, Integer.MAX_VALUE, f);
+            assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f);
         }
 
         {