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 9f6615301679650cb0ec23a1549c0ea7dc8f6e87 Author: Schamschi <heinrich.bo...@gmx.at> AuthorDate: Thu Jun 20 14:08:35 2019 +0200 NUMBERS-118: Extract common reciprocal test cases and clarify Javadoc --- .../commons/numbers/fraction/BigFractionTest.java | 29 ++++------------ .../commons/numbers/fraction/CommonTestCases.java | 40 ++++++++++++++++++++-- .../commons/numbers/fraction/FractionTest.java | 25 ++------------ 3 files changed, 46 insertions(+), 48 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 cd23162..857a385 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 @@ -284,35 +284,18 @@ public class BigFractionTest { @Test public void testReciprocal() { - BigFraction f = null; - - f = BigFraction.of(50, 75); - f = f.reciprocal(); - Assertions.assertEquals(3, f.getNumeratorAsInt()); - Assertions.assertEquals(2, f.getDenominatorAsInt()); - - f = BigFraction.of(4, 3); - f = f.reciprocal(); - Assertions.assertEquals(3, f.getNumeratorAsInt()); - Assertions.assertEquals(4, f.getDenominatorAsInt()); - - f = BigFraction.of(-15, 47); - f = f.reciprocal(); - Assertions.assertEquals(-47, f.getNumeratorAsInt()); - Assertions.assertEquals(15, f.getDenominatorAsInt()); + for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.reciprocalTestCases()) { + BigFraction f = BigFraction.of(testCase.operandNumerator, testCase.operandDenominator); + f = f.reciprocal(); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f); + } - f = BigFraction.of(0, 3); + BigFraction f = BigFraction.of(0, 3); try { f = f.reciprocal(); Assertions.fail("expecting ArithmeticException"); } catch (ArithmeticException ignored) { } - - // large values - f = BigFraction.of(Integer.MAX_VALUE, 1); - f = f.reciprocal(); - Assertions.assertEquals(1, f.getNumeratorAsInt()); - Assertions.assertEquals(Integer.MAX_VALUE, 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 be03b21..82cb1e2 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 @@ -26,10 +26,16 @@ public class CommonTestCases { */ private static final List<UnaryOperatorTestCase> absTestCasesList; + /** + * See {@link #reciprocalTestCases()} + */ + private static final List<UnaryOperatorTestCase> reciprocalTestCasesList; + static { numDenConstructorTestCasesList = collectNumDenConstructorTestCases(); doubleConstructorTestCasesList = collectDoubleConstructorTestCases(); absTestCasesList = collectAbsTestCases(); + reciprocalTestCasesList = collectReciprocalTestCases(); } /** @@ -126,6 +132,22 @@ public class CommonTestCases { } /** + * Defines test cases as described in {@link #reciprocalTestCases()} and collects + * them into a {@code List}. + * @return a list of test cases as described above + */ + private static List<UnaryOperatorTestCase> collectReciprocalTestCases() { + List<UnaryOperatorTestCase> testCases = new ArrayList<>(); + + testCases.add(new UnaryOperatorTestCase(50, 75, 3, 2)); + testCases.add(new UnaryOperatorTestCase(4, 3, 3, 4)); + testCases.add(new UnaryOperatorTestCase(-15, 47, -47, 15)); + testCases.add(new UnaryOperatorTestCase(Integer.MAX_VALUE, 1, 1, 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. @@ -146,9 +168,10 @@ public class CommonTestCases { } /** - * Provides a list of test cases where the absolute value of a fraction should be - * calculated, with both the operand's and the expected result's numerator and denominator - * in the {@code int} range. + * Provides a list of test cases where the absolute value 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> absTestCases() { @@ -156,6 +179,17 @@ public class CommonTestCases { } /** + * Provides a list of test cases where the multiplicative 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> reciprocalTestCases() { + return Collections.unmodifiableList(reciprocalTestCasesList); + } + + /** * 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 aa0daff..543a06e 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 @@ -210,22 +210,10 @@ public class FractionTest { @Test public void testReciprocal() { - { - Fraction f = Fraction.of(50, 75); - f = f.reciprocal(); - assertFraction(3, 2, f); - } - - { - Fraction f = Fraction.of(4, 3); - f = f.reciprocal(); - assertFraction(3, 4, f); - } - - { - Fraction f = Fraction.of(-15, 47); + for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.reciprocalTestCases()) { + Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator); f = f.reciprocal(); - assertFraction(-47, 15, f); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f); } { @@ -234,13 +222,6 @@ public class FractionTest { f::reciprocal ); } - - { - // large values - Fraction f = Fraction.of(Integer.MAX_VALUE, 1); - f = f.reciprocal(); - assertFraction(1, Integer.MAX_VALUE, f); - } } @Test