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:27 UTC
[commons-numbers] 13/15: NUMBERS-118: Extract common
subtract-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 c45802dc9c202c50e014e64522e98e5dbba08e23
Author: Schamschi <he...@gmx.at>
AuthorDate: Fri Jun 21 13:28:49 2019 +0200
NUMBERS-118: Extract common subtract-fraction test cases
---
.../commons/numbers/fraction/BigFractionTest.java | 33 ++---------
.../commons/numbers/fraction/CommonTestCases.java | 67 +++++++++++++++++++++-
.../commons/numbers/fraction/FractionTest.java | 39 ++-----------
3 files changed, 76 insertions(+), 63 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 26fc8a1..0990357 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
@@ -421,13 +421,11 @@ public class BigFractionTest {
@Test
public void testSubtract() {
- BigFraction a = BigFraction.of(1, 2);
- BigFraction b = BigFraction.of(2, 3);
-
- assertFraction(0, 1, a.subtract(a));
- assertFraction(-1, 6, a.subtract(b));
- assertFraction(1, 6, b.subtract(a));
- assertFraction(0, 1, b.subtract(b));
+ for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.subtractFractionTestCases()) {
+ BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(f2));
+ }
BigFraction f = BigFraction.of(1, 1);
try {
@@ -435,27 +433,6 @@ public class BigFractionTest {
Assertions.fail("expecting NullPointerException");
} catch (NullPointerException ex) {
}
-
- // if this fraction is subtracted naively, it will overflow.
- // check that it doesn't.
- BigFraction f1 = BigFraction.of(1, 32768 * 3);
- BigFraction f2 = BigFraction.of(1, 59049);
- f = f1.subtract(f2);
- Assertions.assertEquals(-13085, f.getNumeratorAsInt());
- Assertions.assertEquals(1934917632, f.getDenominatorAsInt());
-
- f1 = BigFraction.of(Integer.MIN_VALUE, 3);
- f2 = BigFraction.of(1, 3).negate();
- f = f1.subtract(f2);
- Assertions.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt());
- Assertions.assertEquals(3, f.getDenominatorAsInt());
-
- f1 = BigFraction.of(Integer.MAX_VALUE, 1);
- f2 = BigFraction.ONE;
- f = f1.subtract(f2);
- Assertions.assertEquals(Integer.MAX_VALUE - 1, f.getNumeratorAsInt());
- Assertions.assertEquals(1, f.getDenominatorAsInt());
-
}
@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 be19fdb..c35b018 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
@@ -51,6 +51,11 @@ class CommonTestCases {
*/
private static final List<BinaryOperatorTestCase> multiplyByFractionTestCasesList;
+ /**
+ * See {@link #subtractFractionTestCases()}
+ */
+ private static final List<BinaryOperatorTestCase> subtractFractionTestCasesList;
+
static {
numDenConstructorTestCasesList = collectNumDenConstructorTestCases();
doubleConstructorTestCasesList = collectDoubleConstructorTestCases();
@@ -60,6 +65,7 @@ class CommonTestCases {
addFractionTestCasesList = collectAddFractionTestCases();
divideByFractionTestCasesList = collectDivideByFractionTestCases();
multiplyByFractionTestCasesList = collectMultiplyByFractionTestCases();
+ subtractFractionTestCasesList = collectSubtractFractionTestCases();
}
/**
@@ -253,6 +259,11 @@ class CommonTestCases {
return testCases;
}
+ /**
+ * Defines test cases as described in {@link #multiplyByFractionTestCases()} and collects
+ * them into a {@code List}.
+ * @return a list of test cases as described above
+ */
private static List<BinaryOperatorTestCase> collectMultiplyByFractionTestCases() {
List<BinaryOperatorTestCase> testCases = new ArrayList<>();
@@ -270,6 +281,41 @@ class CommonTestCases {
}
/**
+ * Defines test cases as described in {@link #subtractFractionTestCases()} and collects
+ * them into a {@code List}.
+ * @return a list of test cases as described above
+ */
+ private static List<BinaryOperatorTestCase> collectSubtractFractionTestCases() {
+ List<BinaryOperatorTestCase> testCases = new ArrayList<>();
+
+ testCases.add(new BinaryOperatorTestCase(1, 2, 1, 2, 0, 1));
+ testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, -1, 6));
+ testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 1, 6));
+ testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 0, 1));
+
+ // if this fraction is subtracted naively, it will overflow.
+ // check that it doesn't.
+ testCases.add(new BinaryOperatorTestCase(
+ 1, 32768 * 3,
+ 1, 59049,
+ -13085, 1934917632));
+
+ testCases.add(new BinaryOperatorTestCase(
+ Integer.MIN_VALUE, 3,
+ -1, 3,
+ Integer.MIN_VALUE + 1, 3
+ ));
+
+ testCases.add(new BinaryOperatorTestCase(
+ Integer.MAX_VALUE, 1,
+ 1, 1,
+ Integer.MAX_VALUE-1, 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.
@@ -333,10 +379,13 @@ class CommonTestCases {
}
/**
- * Provides a list of test cases where a fraction, created from a specified numerator and denominator
+ * <p>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.
+ * are in the {@code int} range as well.</p>
+ *
+ * <p>The first operand in each test case is the dividend and the second operand is the divisor.</p>
+ *
* @return a list of test cases as described above
*/
static List<BinaryOperatorTestCase> divideByFractionTestCases() {
@@ -355,6 +404,20 @@ class CommonTestCases {
}
/**
+ * <p>Provides a list of test cases where a fraction, created from a specified numerator and denominator
+ * in the {@code int} range, should be subtracted from 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.</p>
+ *
+ * <p>The first operand in each test case is the minuend and the second operand is the subtrahend.</p>
+ *
+ * @return a list of test cases as described above
+ */
+ static List<BinaryOperatorTestCase> subtractFractionTestCases() {
+ return Collections.unmodifiableList(subtractFractionTestCasesList);
+ }
+
+ /**
* 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 977e702..42c1b35 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
@@ -401,14 +401,10 @@ public class FractionTest {
@Test
public void testSubtract() {
- {
- Fraction a = Fraction.of(1, 2);
- Fraction b = Fraction.of(2, 3);
-
- assertFraction(0, 1, a.subtract(a));
- assertFraction(-1, 6, a.subtract(b));
- assertFraction(1, 6, b.subtract(a));
- assertFraction(0, 1, b.subtract(b));
+ for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.subtractFractionTestCases()) {
+ Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(f2));
}
{
@@ -419,32 +415,9 @@ public class FractionTest {
}
{
- // if this fraction is subtracted naively, it will overflow.
- // check that it doesn't.
- Fraction f1 = Fraction.of(1, 32768 * 3);
- Fraction f2 = Fraction.of(1, 59049);
- Fraction f = f1.subtract(f2);
- assertFraction(-13085, 1934917632, f);
- }
-
- {
- Fraction f1 = Fraction.of(Integer.MIN_VALUE, 3);
- Fraction f2 = Fraction.of(1, 3).negate();
- Fraction f = f1.subtract(f2);
- assertFraction(Integer.MIN_VALUE + 1, 3, f);
- }
-
- {
Fraction f1 = Fraction.of(Integer.MAX_VALUE, 1);
- {
- Fraction f2 = Fraction.ONE;
- Fraction f = f1.subtract(f2);
- assertFraction(Integer.MAX_VALUE-1, 1, f);
- }
- {
- Fraction f = f1.subtract(1);
- assertFraction(Integer.MAX_VALUE-1, 1, f);
- }
+ Fraction f = f1.subtract(1);
+ assertFraction(Integer.MAX_VALUE-1, 1, f);
}
{