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 2d5783cb8a911fca8e886be57e74536e50eaf95c Author: Schamschi <heinrich.bo...@gmx.at> AuthorDate: Thu Jun 20 17:05:12 2019 +0200 NUMBERS-118: Extract common add-fraction test cases --- .../commons/numbers/fraction/BigFractionTest.java | 47 ++++-------- .../commons/numbers/fraction/CommonTestCases.java | 83 ++++++++++++++++++++++ .../commons/numbers/fraction/FractionTest.java | 40 ++--------- 3 files changed, 102 insertions(+), 68 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 1e17656..14422c2 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 @@ -286,8 +286,7 @@ public class BigFractionTest { public void testReciprocal() { for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.reciprocalTestCases()) { BigFraction f = BigFraction.of(testCase.operandNumerator, testCase.operandDenominator); - f = f.reciprocal(); - assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.reciprocal()); } BigFraction f = BigFraction.of(0, 3); @@ -302,20 +301,17 @@ public class BigFractionTest { public void testNegate() { for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.negateTestCases()) { BigFraction f = BigFraction.of(testCase.operandNumerator, testCase.operandDenominator); - f = f.negate(); - assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.negate()); } } @Test public void testAdd() { - BigFraction a = BigFraction.of(1, 2); - BigFraction b = BigFraction.of(2, 3); - - assertFraction(1, 1, a.add(a)); - assertFraction(7, 6, a.add(b)); - assertFraction(7, 6, b.add(a)); - assertFraction(4, 3, b.add(b)); + for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.addFractionTestCases()) { + BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator); + BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(f2)); + } BigFraction f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1); BigFraction f2 = BigFraction.ONE; @@ -323,32 +319,13 @@ public class BigFractionTest { Assertions.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assertions.assertEquals(1, f.getDenominatorAsInt()); - f1 = BigFraction.of(-1, 13 * 13 * 2 * 2); - f2 = BigFraction.of(-2, 13 * 17 * 2); - f = f1.add(f2); - Assertions.assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominatorAsInt()); - Assertions.assertEquals(-17 - 2 * 13 * 2, f.getNumeratorAsInt()); - - try { - f.add((BigFraction) null); - Assertions.fail("expecting NullPointerException"); - } catch (NullPointerException ex) { + { + final BigFraction f3 = BigFraction.of(-17 - 2*13*2, 13*13*17*2*2); + Assertions.assertThrows(NullPointerException.class, + () -> f3.add((BigFraction) null) + ); } - // if this fraction is added naively, it will overflow. - // check that it doesn't. - f1 = BigFraction.of(1, 32768 * 3); - f2 = BigFraction.of(1, 59049); - f = f1.add(f2); - Assertions.assertEquals(52451, f.getNumeratorAsInt()); - Assertions.assertEquals(1934917632, f.getDenominatorAsInt()); - - f1 = BigFraction.of(Integer.MIN_VALUE, 3); - f2 = BigFraction.of(1, 3); - f = f1.add(f2); - Assertions.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); - Assertions.assertEquals(3, f.getDenominatorAsInt()); - f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1); f = f1.add(BigInteger.ONE); Assertions.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); 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 17e3596..6208698 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 @@ -36,12 +36,18 @@ class CommonTestCases { */ private static final List<UnaryOperatorTestCase> negateTestCasesList; + /** + * See {@link #addFractionTestCases()} + */ + private static final List<BinaryOperatorTestCase> addFractionTestCasesList; + static { numDenConstructorTestCasesList = collectNumDenConstructorTestCases(); doubleConstructorTestCasesList = collectDoubleConstructorTestCases(); absTestCasesList = collectAbsTestCases(); reciprocalTestCasesList = collectReciprocalTestCases(); negateTestCasesList = collectNegateTestCases(); + addFractionTestCasesList = collectAddFractionTestCases(); } /** @@ -169,6 +175,44 @@ class CommonTestCases { } /** + * Defines test cases as described in {@link #addFractionTestCases()} and collects + * them into a {@code List}. + * @return a list of test cases as described above + */ + 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, 13*13*2*2, + -2, 13*17*2, + -17 - 2*13*2, 13*13*17*2*2)); + + // if this fraction is added naively, it will overflow. + // check that it doesn't. + testCases.add(new BinaryOperatorTestCase( + 1, 32768 * 3, + 1, 59049, + 52451, 1934917632)); + + testCases.add(new BinaryOperatorTestCase( + Integer.MIN_VALUE, 3, + 1, 3, + Integer.MIN_VALUE + 1, 3)); + + 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. @@ -222,6 +266,16 @@ class CommonTestCases { } /** + * Provides a list of test cases where two fractions, each created from a specified numerator and denominator + * in the {@code int} range, should be added, 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 + */ + static List<BinaryOperatorTestCase> addFractionTestCases() { + return Collections.unmodifiableList(addFractionTestCasesList); + } + + /** * 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. @@ -245,6 +299,35 @@ class CommonTestCases { } /** + * Represents a test case where a binary operation should be performed on two specified combinations + * of numerator and denominator, with the numerator and denominator of each combination in the + * {@code int} range, and the numerator and denominator of the expected result are also in the {@code int} range. + */ + static class BinaryOperatorTestCase { + final int firstOperandNumerator; + final int firstOperandDenominator; + final int secondOperandNumerator; + final int secondOperandDenominator; + final int expectedNumerator; + final int expectedDenominator; + + BinaryOperatorTestCase( + int firstOperandNumerator, + int firstOperandDenominator, + int secondOperandNumerator, + int secondOperandDenominator, + int expectedNumerator, + int expectedDenominator) { + this.firstOperandNumerator = firstOperandNumerator; + this.firstOperandDenominator = firstOperandDenominator; + this.secondOperandNumerator = secondOperandNumerator; + this.secondOperandDenominator = secondOperandDenominator; + this.expectedNumerator = expectedNumerator; + this.expectedDenominator = expectedDenominator; + } + } + + /** * Represents a test case where an operation that yields a fraction should be performed * on a {@code double} value and the numerator and denominator of the expected result * are 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 521741b..3915d3e 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 @@ -212,8 +212,7 @@ public class FractionTest { public void testReciprocal() { for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.reciprocalTestCases()) { Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator); - f = f.reciprocal(); - assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.reciprocal()); } { @@ -228,8 +227,7 @@ public class FractionTest { public void testNegate() { for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.negateTestCases()) { Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator); - f = f.negate(); - assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.negate()); } { @@ -242,14 +240,10 @@ public class FractionTest { @Test public void testAdd() { - { - Fraction a = Fraction.of(1, 2); - Fraction b = Fraction.of(2, 3); - - assertFraction(1, 1, a.add(a)); - assertFraction(7, 6, a.add(b)); - assertFraction(7, 6, b.add(a)); - assertFraction(4, 3, b.add(b)); + for (CommonTestCases.BinaryOperatorTestCase testCase : CommonTestCases.addFractionTestCases()) { + Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator); + Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(f2)); } { @@ -266,33 +260,13 @@ public class FractionTest { } { - Fraction f1 = Fraction.of(-1, 13*13*2*2); - Fraction f2 = Fraction.of(-2, 13*17*2); - final Fraction f = f1.add(f2); - assertFraction(-17 - 2*13*2, 13*13*17*2*2, f); - + final Fraction f = Fraction.of(-17 - 2*13*2, 13*13*17*2*2); Assertions.assertThrows(NullPointerException.class, () -> f.add(null) ); } { - // if this fraction is added 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.add(f2); - assertFraction(52451, 1934917632, f); - } - - { - Fraction f1 = Fraction.of(Integer.MIN_VALUE, 3); - Fraction f2 = Fraction.of(1, 3); - Fraction f = f1.add(f2); - assertFraction(Integer.MIN_VALUE + 1, 3, f); - } - - { Fraction f1 = Fraction.of(Integer.MAX_VALUE - 1, 1); Fraction f2 = Fraction.ONE; final Fraction f = f1.add(f2);