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 8b2ab21d8bc4e08ae6291339418487d9424da88f Author: Schamschi <[email protected]> AuthorDate: Thu Jun 20 13:18:53 2019 +0200 NUMBERS-118: Extract common abs test cases and add some overall Javadoc --- .../commons/numbers/fraction/BigFractionTest.java | 11 ++-- .../commons/numbers/fraction/CommonTestCases.java | 59 ++++++++++++++++++++++ .../commons/numbers/fraction/FractionTest.java | 11 ++-- 3 files changed, 67 insertions(+), 14 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 6e050d2..cd23162 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 @@ -276,13 +276,10 @@ public class BigFractionTest { @Test public void testAbs() { - BigFraction a = BigFraction.of(10, 21); - BigFraction b = BigFraction.of(-10, 21); - BigFraction c = BigFraction.of(10, -21); - - assertFraction(10, 21, a.abs()); - assertFraction(10, 21, b.abs()); - assertFraction(10, 21, c.abs()); + for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.absTestCases()) { + BigFraction f = BigFraction.of(testCase.operandNumerator, testCase.operandDenominator); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.abs()); + } } @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 0a82549..be03b21 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 @@ -11,15 +11,32 @@ import java.util.List; * numerators and denominators in the {@code int} range. */ public class CommonTestCases { + /** + * See {@link #numDenConstructorTestCases()} + */ private static final List<UnaryOperatorTestCase> numDenConstructorTestCasesList; + /** + * See {@link #doubleConstructorTestCases()} + */ private static final List<DoubleToFractionTestCase> doubleConstructorTestCasesList; + /** + * See {@link #absTestCases()} + */ + private static final List<UnaryOperatorTestCase> absTestCasesList; + static { numDenConstructorTestCasesList = collectNumDenConstructorTestCases(); doubleConstructorTestCasesList = collectDoubleConstructorTestCases(); + absTestCasesList = collectAbsTestCases(); } + /** + * Defines test cases as described in {@link #numDenConstructorTestCases()} and collects + * them into a {@code List}. + * @return a list of test cases as described above + */ private static List<UnaryOperatorTestCase> collectNumDenConstructorTestCases() { List<UnaryOperatorTestCase> testCases = new ArrayList<>(); @@ -36,6 +53,11 @@ public class CommonTestCases { return testCases; } + /** + * Defines test cases as described in {@link #doubleConstructorTestCases()} and collects + * them into a {@code List}. + * @return a list of test cases as described above + */ private static List<DoubleToFractionTestCase> collectDoubleConstructorTestCases() { List<DoubleToFractionTestCase> testCases = new ArrayList<>(); @@ -88,15 +110,52 @@ public class CommonTestCases { return testCases; } + /** + * Defines test cases as described in {@link #collectAbsTestCases()} and collects + * them into a {@code List}. + * @return a list of test cases as described above + */ + private static List<UnaryOperatorTestCase> collectAbsTestCases() { + List<UnaryOperatorTestCase> testCases = new ArrayList<>(); + + testCases.add(new UnaryOperatorTestCase(10, 21, 10, 21)); + testCases.add(new UnaryOperatorTestCase(-10, 21, 10, 21)); + testCases.add(new UnaryOperatorTestCase(10, -21, 10, 21)); + + 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. + * @return a list of test cases as described above + */ public static List<UnaryOperatorTestCase> numDenConstructorTestCases() { return Collections.unmodifiableList(numDenConstructorTestCasesList); } + /** + * Provides a list of test cases where a {@code double} value should be converted + * to a fraction with a certain amount of absolute error allowed, and the expected + * numerator and denominator of the resulting fraction are in the {@code int} range. + * @return a list of test cases as described above + */ public static List<DoubleToFractionTestCase> doubleConstructorTestCases() { return Collections.unmodifiableList(doubleConstructorTestCasesList); } /** + * 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. + * @return a list of test cases as described above + */ + public static List<UnaryOperatorTestCase> absTestCases() { + return Collections.unmodifiableList(absTestCasesList); + } + + /** * 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 510e4c3..aa0daff 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 @@ -187,13 +187,10 @@ public class FractionTest { @Test public void testAbs() { - Fraction a = Fraction.of(10, 21); - Fraction b = Fraction.of(-10, 21); - Fraction c = Fraction.of(10, -21); - - assertFraction(10, 21, a.abs()); - assertFraction(10, 21, b.abs()); - assertFraction(10, 21, c.abs()); + for (CommonTestCases.UnaryOperatorTestCase testCase : CommonTestCases.absTestCases()) { + Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator); + assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.abs()); + } } @Test
