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 55e1ab819ad098b4b26ae750db498f3e2ec0820e Author: Schamschi <heinrich.bo...@gmx.at> AuthorDate: Thu Jun 20 00:43:41 2019 +0200 NUMBERS-118: Extract common num-den-constructor and double-constructor test cases --- .../commons/numbers/fraction/BigFractionTest.java | 64 +++-------- .../commons/numbers/fraction/CommonTestCases.java | 123 +++++++++++++++++++++ .../commons/numbers/fraction/FractionTest.java | 64 +++-------- 3 files changed, 152 insertions(+), 99 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 039e8a6..8a5b9dd 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 @@ -39,15 +39,15 @@ public class BigFractionTest { @Test public void testConstructor() { - assertFraction(0, 1, BigFraction.of(0, 1)); + for (CommonTestCases.NumDenConstructorTestCase testCase : CommonTestCases.numDenConstructorTestCases()) { + assertFraction( + testCase.expectedNumerator, + testCase.expectedDenominator, + BigFraction.of(testCase.constructorNumerator, testCase.constructorDenominator) + ); + } + assertFraction(0, 1, BigFraction.of(0l, 2l)); - assertFraction(0, 1, BigFraction.of(0, -1)); - assertFraction(1, 2, BigFraction.of(1, 2)); - assertFraction(1, 2, BigFraction.of(2, 4)); - assertFraction(-1, 2, BigFraction.of(-1, 2)); - assertFraction(-1, 2, BigFraction.of(1, -2)); - assertFraction(-1, 2, BigFraction.of(-2, 4)); - assertFraction(-1, 2, BigFraction.of(2, -4)); assertFraction(11, 1, BigFraction.of(11)); assertFraction(11, 1, BigFraction.of(11l)); assertFraction(11, 1, BigFraction.of(new BigInteger("11"))); @@ -98,47 +98,13 @@ public class BigFractionTest { // MATH-179 @Test public void testDoubleConstructor() throws Exception { - assertFraction(1, 2, BigFraction.from((double) 1 / (double) 2, 1.0e-5, 100)); - assertFraction(1, 3, BigFraction.from((double) 1 / (double) 3, 1.0e-5, 100)); - assertFraction(2, 3, BigFraction.from((double) 2 / (double) 3, 1.0e-5, 100)); - assertFraction(1, 4, BigFraction.from((double) 1 / (double) 4, 1.0e-5, 100)); - assertFraction(3, 4, BigFraction.from((double) 3 / (double) 4, 1.0e-5, 100)); - assertFraction(1, 5, BigFraction.from((double) 1 / (double) 5, 1.0e-5, 100)); - assertFraction(2, 5, BigFraction.from((double) 2 / (double) 5, 1.0e-5, 100)); - assertFraction(3, 5, BigFraction.from((double) 3 / (double) 5, 1.0e-5, 100)); - assertFraction(4, 5, BigFraction.from((double) 4 / (double) 5, 1.0e-5, 100)); - assertFraction(1, 6, BigFraction.from((double) 1 / (double) 6, 1.0e-5, 100)); - assertFraction(5, 6, BigFraction.from((double) 5 / (double) 6, 1.0e-5, 100)); - assertFraction(1, 7, BigFraction.from((double) 1 / (double) 7, 1.0e-5, 100)); - assertFraction(2, 7, BigFraction.from((double) 2 / (double) 7, 1.0e-5, 100)); - assertFraction(3, 7, BigFraction.from((double) 3 / (double) 7, 1.0e-5, 100)); - assertFraction(4, 7, BigFraction.from((double) 4 / (double) 7, 1.0e-5, 100)); - assertFraction(5, 7, BigFraction.from((double) 5 / (double) 7, 1.0e-5, 100)); - assertFraction(6, 7, BigFraction.from((double) 6 / (double) 7, 1.0e-5, 100)); - assertFraction(1, 8, BigFraction.from((double) 1 / (double) 8, 1.0e-5, 100)); - assertFraction(3, 8, BigFraction.from((double) 3 / (double) 8, 1.0e-5, 100)); - assertFraction(5, 8, BigFraction.from((double) 5 / (double) 8, 1.0e-5, 100)); - assertFraction(7, 8, BigFraction.from((double) 7 / (double) 8, 1.0e-5, 100)); - assertFraction(1, 9, BigFraction.from((double) 1 / (double) 9, 1.0e-5, 100)); - assertFraction(2, 9, BigFraction.from((double) 2 / (double) 9, 1.0e-5, 100)); - assertFraction(4, 9, BigFraction.from((double) 4 / (double) 9, 1.0e-5, 100)); - assertFraction(5, 9, BigFraction.from((double) 5 / (double) 9, 1.0e-5, 100)); - assertFraction(7, 9, BigFraction.from((double) 7 / (double) 9, 1.0e-5, 100)); - assertFraction(8, 9, BigFraction.from((double) 8 / (double) 9, 1.0e-5, 100)); - assertFraction(1, 10, BigFraction.from((double) 1 / (double) 10, 1.0e-5, 100)); - assertFraction(3, 10, BigFraction.from((double) 3 / (double) 10, 1.0e-5, 100)); - assertFraction(7, 10, BigFraction.from((double) 7 / (double) 10, 1.0e-5, 100)); - assertFraction(9, 10, BigFraction.from((double) 9 / (double) 10, 1.0e-5, 100)); - assertFraction(1, 11, BigFraction.from((double) 1 / (double) 11, 1.0e-5, 100)); - assertFraction(2, 11, BigFraction.from((double) 2 / (double) 11, 1.0e-5, 100)); - assertFraction(3, 11, BigFraction.from((double) 3 / (double) 11, 1.0e-5, 100)); - assertFraction(4, 11, BigFraction.from((double) 4 / (double) 11, 1.0e-5, 100)); - assertFraction(5, 11, BigFraction.from((double) 5 / (double) 11, 1.0e-5, 100)); - assertFraction(6, 11, BigFraction.from((double) 6 / (double) 11, 1.0e-5, 100)); - assertFraction(7, 11, BigFraction.from((double) 7 / (double) 11, 1.0e-5, 100)); - assertFraction(8, 11, BigFraction.from((double) 8 / (double) 11, 1.0e-5, 100)); - assertFraction(9, 11, BigFraction.from((double) 9 / (double) 11, 1.0e-5, 100)); - assertFraction(10, 11, BigFraction.from((double) 10 / (double) 11, 1.0e-5, 100)); + for (CommonTestCases.DoubleConstructorTestCase testCase : CommonTestCases.doubleConstructorTestCases()) { + assertFraction( + testCase.expectedNumerator, + testCase.expectedDenominator, + BigFraction.from(testCase.constructorArgument, 1.0e-5, 100) + ); + } } // MATH-181 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 new file mode 100644 index 0000000..cbaa029 --- /dev/null +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java @@ -0,0 +1,123 @@ +package org.apache.commons.numbers.fraction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * A collection of test cases that can be applied both + * to {@link Fraction} and {@link BigFraction}, e.g. + * for operations and expected results that involve + * numerators and denominators in the {@code int} range. + */ +public class CommonTestCases { + private static final List<NumDenConstructorTestCase> numDenConstructorTestCasesList; + + private static final List<DoubleConstructorTestCase> doubleConstructorTestCasesList; + + static { + numDenConstructorTestCasesList = new ArrayList<>(); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(0, 1, 0, 1)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(0, 2, 0, 1)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(0, -1, 0, 1)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(1, 2, 1, 2)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(2, 4, 1, 2)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(-1, 2, -1, 2)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(1, -2, -1, 2)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(-2, 4, -1, 2)); + numDenConstructorTestCasesList.add(new NumDenConstructorTestCase(2, -4, -1, 2)); + + doubleConstructorTestCasesList = new ArrayList<>(); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/2d, 1, 2)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/3d, 1, 3)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(2d/3d, 2, 3)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/4d, 1, 4)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(3d/4d, 3, 4)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/5d, 1, 5)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(2d/5d, 2, 5)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(3d/5d, 3, 5)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(4d/5d, 4, 5)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/6d, 1, 6)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(5d/6d, 5, 6)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/7d, 1, 7)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(2d/7d, 2, 7)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(3d/7d, 3, 7)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(4d/7d, 4, 7)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(5d/7d, 5, 7)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(6d/7d, 6, 7)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/8d, 1, 8)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(3d/8d, 3, 8)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(5d/8d, 5, 8)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(7d/8d, 7, 8)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/9d, 1, 9)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(2d/9d, 2, 9)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(4d/9d, 4, 9)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(5d/9d, 5, 9)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(7d/9d, 7, 9)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(8d/9d, 8, 9)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/10d, 1, 10)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(3d/10d, 3, 10)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(7d/10d, 7, 10)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(9d/10d, 9, 10)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(1d/11d, 1, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(2d/11d, 2, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(3d/11d, 3, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(4d/11d, 4, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(5d/11d, 5, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(6d/11d, 6, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(7d/11d, 7, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(8d/11d, 8, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(9d/11d, 9, 11)); + doubleConstructorTestCasesList.add(new DoubleConstructorTestCase(10d/11d, 10, 11)); + } + + public static List<NumDenConstructorTestCase> numDenConstructorTestCases() { + return Collections.unmodifiableList(numDenConstructorTestCasesList); + } + + public static List<DoubleConstructorTestCase> doubleConstructorTestCases() { + return Collections.unmodifiableList(doubleConstructorTestCasesList); + } + + /** + * Represents a test case where a fraction should be created from a + * specified numerator and denominator, both in the {@code int} range. + */ + public static class NumDenConstructorTestCase { + public final int constructorNumerator; + public final int constructorDenominator; + public final int expectedNumerator; + public final int expectedDenominator; + + public NumDenConstructorTestCase( + int constructorNumerator, + int constructorDenominator, + int expectedNumerator, + int expectedDenominator) { + this.constructorNumerator = constructorNumerator; + this.constructorDenominator = constructorDenominator; + this.expectedNumerator = expectedNumerator; + this.expectedDenominator = expectedDenominator; + } + } + + /** + * Represents a test case where a fraction should be created from a + * {@code double} value and the expected numerator and denominator + * are in the {@code int} range. + */ + public static class DoubleConstructorTestCase { + public final double constructorArgument; + public final int expectedNumerator; + public final int expectedDenominator; + + public DoubleConstructorTestCase( + double constructorArgument, + int expectedNumerator, + int expectedDenominator) { + this.constructorArgument = constructorArgument; + this.expectedNumerator = expectedNumerator; + this.expectedDenominator = expectedDenominator; + } + } +} 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 f6c224c..293ffbd 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 @@ -33,15 +33,13 @@ public class FractionTest { @Test public void testConstructor() { - assertFraction(0, 1, Fraction.of(0, 1)); - assertFraction(0, 1, Fraction.of(0, 2)); - assertFraction(0, 1, Fraction.of(0, -1)); - assertFraction(1, 2, Fraction.of(1, 2)); - assertFraction(1, 2, Fraction.of(2, 4)); - assertFraction(-1, 2, Fraction.of(-1, 2)); - assertFraction(-1, 2, Fraction.of(1, -2)); - assertFraction(-1, 2, Fraction.of(-2, 4)); - assertFraction(-1, 2, Fraction.of(2, -4)); + for (CommonTestCases.NumDenConstructorTestCase testCase : CommonTestCases.numDenConstructorTestCases()) { + assertFraction( + testCase.expectedNumerator, + testCase.expectedDenominator, + Fraction.of(testCase.constructorNumerator, testCase.constructorDenominator) + ); + } // overflow Assertions.assertThrows(ArithmeticException.class, @@ -67,47 +65,13 @@ public class FractionTest { // MATH-179 @Test public void testDoubleConstructor() throws Exception { - assertFraction(1, 2, Fraction.from((double)1 / (double)2)); - assertFraction(1, 3, Fraction.from((double)1 / (double)3)); - assertFraction(2, 3, Fraction.from((double)2 / (double)3)); - assertFraction(1, 4, Fraction.from((double)1 / (double)4)); - assertFraction(3, 4, Fraction.from((double)3 / (double)4)); - assertFraction(1, 5, Fraction.from((double)1 / (double)5)); - assertFraction(2, 5, Fraction.from((double)2 / (double)5)); - assertFraction(3, 5, Fraction.from((double)3 / (double)5)); - assertFraction(4, 5, Fraction.from((double)4 / (double)5)); - assertFraction(1, 6, Fraction.from((double)1 / (double)6)); - assertFraction(5, 6, Fraction.from((double)5 / (double)6)); - assertFraction(1, 7, Fraction.from((double)1 / (double)7)); - assertFraction(2, 7, Fraction.from((double)2 / (double)7)); - assertFraction(3, 7, Fraction.from((double)3 / (double)7)); - assertFraction(4, 7, Fraction.from((double)4 / (double)7)); - assertFraction(5, 7, Fraction.from((double)5 / (double)7)); - assertFraction(6, 7, Fraction.from((double)6 / (double)7)); - assertFraction(1, 8, Fraction.from((double)1 / (double)8)); - assertFraction(3, 8, Fraction.from((double)3 / (double)8)); - assertFraction(5, 8, Fraction.from((double)5 / (double)8)); - assertFraction(7, 8, Fraction.from((double)7 / (double)8)); - assertFraction(1, 9, Fraction.from((double)1 / (double)9)); - assertFraction(2, 9, Fraction.from((double)2 / (double)9)); - assertFraction(4, 9, Fraction.from((double)4 / (double)9)); - assertFraction(5, 9, Fraction.from((double)5 / (double)9)); - assertFraction(7, 9, Fraction.from((double)7 / (double)9)); - assertFraction(8, 9, Fraction.from((double)8 / (double)9)); - assertFraction(1, 10, Fraction.from((double)1 / (double)10)); - assertFraction(3, 10, Fraction.from((double)3 / (double)10)); - assertFraction(7, 10, Fraction.from((double)7 / (double)10)); - assertFraction(9, 10, Fraction.from((double)9 / (double)10)); - assertFraction(1, 11, Fraction.from((double)1 / (double)11)); - assertFraction(2, 11, Fraction.from((double)2 / (double)11)); - assertFraction(3, 11, Fraction.from((double)3 / (double)11)); - assertFraction(4, 11, Fraction.from((double)4 / (double)11)); - assertFraction(5, 11, Fraction.from((double)5 / (double)11)); - assertFraction(6, 11, Fraction.from((double)6 / (double)11)); - assertFraction(7, 11, Fraction.from((double)7 / (double)11)); - assertFraction(8, 11, Fraction.from((double)8 / (double)11)); - assertFraction(9, 11, Fraction.from((double)9 / (double)11)); - assertFraction(10, 11, Fraction.from((double)10 / (double)11)); + for (CommonTestCases.DoubleConstructorTestCase testCase : CommonTestCases.doubleConstructorTestCases()) { + assertFraction( + testCase.expectedNumerator, + testCase.expectedDenominator, + Fraction.from(testCase.constructorArgument) + ); + } } // MATH-181