This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 59428784a8533c1b05fc430c5c763b25f9ee44f5 Author: aherbert <[email protected]> AuthorDate: Fri Nov 8 18:17:38 2019 +0000 Fixed checkstyle in fraction. --- .../commons/numbers/fraction/BigFraction.java | 2 +- .../apache/commons/numbers/fraction/Fraction.java | 6 +- .../commons/numbers/fraction/BigFractionTest.java | 130 +++++++--------- .../commons/numbers/fraction/CommonTestCases.java | 102 +++++++------ .../commons/numbers/fraction/FractionTest.java | 168 ++++++++++----------- 5 files changed, 191 insertions(+), 217 deletions(-) diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java index 22cb6e3..fea4759 100644 --- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java +++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java @@ -120,7 +120,7 @@ public final class BigFraction long a0 = (long) Math.floor(r0); if (Math.abs(a0) > overflow) { - throw new FractionException(FractionException.ERROR_CONVERSION_OVERFLOW, value, a0, 1l); + throw new FractionException(FractionException.ERROR_CONVERSION_OVERFLOW, value, a0, 1L); } // check for (almost) integer arguments, which should not go diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java index c4393ce..07fbe7a 100644 --- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java +++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java @@ -32,7 +32,7 @@ public final class Fraction public static final Fraction ONE = new Fraction(1, 1); /** A fraction representing "0". */ public static final Fraction ZERO = new Fraction(0, 1); - /** Serializable version identifier */ + /** Serializable version identifier. */ private static final long serialVersionUID = 20190701L; /** Parameter name for fraction (to satisfy checkstyle). */ private static final String PARAM_NAME_FRACTION = "fraction"; @@ -78,7 +78,7 @@ public final class Fraction double r0 = value; long a0 = (long)Math.floor(r0); if (Math.abs(a0) > overflow) { - throw new FractionException(FractionException.ERROR_CONVERSION, value, a0, 1l); + throw new FractionException(FractionException.ERROR_CONVERSION, value, a0, 1L); } // check for (almost) integer arguments, which should not go to iterations. @@ -232,7 +232,7 @@ public final class Fraction * @return a new instance. */ public static Fraction from(double value, int maxDenominator) { - return new Fraction(value, 0, maxDenominator, 100); + return new Fraction(value, 0, maxDenominator, 100); } /** 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 82ee513..a90ecd3 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 @@ -56,16 +56,16 @@ public class BigFractionTest { ); } - assertFraction(0, 1, BigFraction.of(0l, 2l)); - assertFraction(11, 1, BigFraction.of(11)); - assertFraction(11, 1, BigFraction.of(11l)); + assertFraction(0, 1, BigFraction.of(0L, 2L)); + assertFraction(1L, 1, BigFraction.of(1L)); + assertFraction(11, 1, BigFraction.of(11L)); assertFraction(11, 1, BigFraction.of(new BigInteger("11"))); Assertions.assertEquals(0.00000000000001, BigFraction.from(0.00000000000001).doubleValue(), 0.0); Assertions.assertEquals(0.40000000000001, BigFraction.from(0.40000000000001).doubleValue(), 0.0); Assertions.assertEquals(15.0000000000001, BigFraction.from(15.0000000000001).doubleValue(), 0.0); - assertFraction(3602879701896487l, 9007199254740992l, BigFraction.from(0.40000000000001)); - assertFraction(1055531162664967l, 70368744177664l, BigFraction.from(15.0000000000001)); + assertFraction(3602879701896487L, 9007199254740992L, BigFraction.from(0.40000000000001)); + assertFraction(1055531162664967L, 70368744177664L, BigFraction.from(15.0000000000001)); try { BigFraction.of(null, BigInteger.ONE); Assertions.fail("Expecting NullPointerException"); @@ -96,7 +96,7 @@ public class BigFractionTest { public void testGoldenRatio() { // the golden ratio is notoriously a difficult number for continuous fraction Assertions.assertThrows(FractionException.class, - () -> BigFraction.from((1 + Math.sqrt(5)) / 2, 1.0e-12, 25) + () -> BigFraction.from((1 + Math.sqrt(5)) / 2, 1.0e-12, 25) ); } @@ -132,7 +132,7 @@ public class BigFractionTest { @Test public void testPositiveValueOverflow() { Assertions.assertThrows(ArithmeticException.class, - () -> assertFraction((long) 1e10, 1, BigFraction.from(1e10, 1000)) + () -> assertFraction((long) 1e10, 1, BigFraction.from(1e10, 1000)) ); } @@ -140,7 +140,7 @@ public class BigFractionTest { @Test public void testNegativeValueOverflow() { Assertions.assertThrows(ArithmeticException.class, - () -> assertFraction((long) -1e10, 1, BigFraction.from(-1e10, 1000)) + () -> assertFraction((long) -1e10, 1, BigFraction.from(-1e10, 1000)) ); } @@ -180,7 +180,7 @@ public class BigFractionTest { // the first one is approximately PI - 3.07e-18 // the second one is approximately PI + 1.936e-17 final BigFraction pi1 = BigFraction.of(1068966896, 340262731); - final BigFraction pi2 = BigFraction.of( 411557987, 131002976); + final BigFraction pi2 = BigFraction.of(411557987, 131002976); Assertions.assertEquals(-1, pi1.compareTo(pi2)); Assertions.assertEquals(1, pi2.compareTo(pi1)); Assertions.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); @@ -217,7 +217,8 @@ public class BigFractionTest { @Test public void testDoubleValueForSubnormalNumbers() { - assertDoubleValue( //Double.MIN_VALUE * 2/3 + assertDoubleValue( + //Double.MIN_VALUE * 2/3 Double.MIN_VALUE, BigInteger.ONE, BigInteger.ONE.shiftLeft(1073).multiply(BigInteger.valueOf(3L)) @@ -304,14 +305,12 @@ public class BigFractionTest { @Test public void testDoubleValueForLargeNumeratorAndSmallDenominator() { // NUMBERS-15 - { - final BigInteger pow300 = BigInteger.TEN.pow(300); - final BigInteger pow330 = BigInteger.TEN.pow(330); - final BigFraction large = BigFraction.of(pow330.add(BigInteger.ONE), - pow300); + final BigInteger pow300 = BigInteger.TEN.pow(300); + final BigInteger pow330 = BigInteger.TEN.pow(330); + final BigFraction large = BigFraction.of(pow330.add(BigInteger.ONE), + pow300); - Assertions.assertEquals(1e30, large.doubleValue(), 1e-15); - } + Assertions.assertEquals(1e30, large.doubleValue(), 1e-15); // NUMBERS-120 assertDoubleValue( @@ -387,14 +386,14 @@ public class BigFractionTest { @Test public void testConstructorDouble() { assertFraction(1, 2, BigFraction.from(0.5)); - assertFraction(6004799503160661l, 18014398509481984l, BigFraction.from(1.0 / 3.0)); - assertFraction(6124895493223875l, 36028797018963968l, BigFraction.from(17.0 / 100.0)); - assertFraction(1784551352345559l, 562949953421312l, BigFraction.from(317.0 / 100.0)); + assertFraction(6004799503160661L, 18014398509481984L, BigFraction.from(1.0 / 3.0)); + assertFraction(6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / 100.0)); + assertFraction(1784551352345559L, 562949953421312L, BigFraction.from(317.0 / 100.0)); assertFraction(-1, 2, BigFraction.from(-0.5)); - assertFraction(-6004799503160661l, 18014398509481984l, BigFraction.from(-1.0 / 3.0)); - assertFraction(-6124895493223875l, 36028797018963968l, BigFraction.from(17.0 / -100.0)); - assertFraction(-1784551352345559l, 562949953421312l, BigFraction.from(-317.0 / 100.0)); - for (double v : new double[] { Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) { + assertFraction(-6004799503160661L, 18014398509481984L, BigFraction.from(-1.0 / 3.0)); + assertFraction(-6124895493223875L, 36028797018963968L, BigFraction.from(17.0 / -100.0)); + assertFraction(-1784551352345559L, 562949953421312L, BigFraction.from(-317.0 / 100.0)); + for (double v : new double[] {Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) { try { BigFraction.from(v); Assertions.fail("Expecting IllegalArgumentException"); @@ -402,8 +401,8 @@ public class BigFractionTest { // expected } } - Assertions.assertEquals(1l, BigFraction.from(Double.MAX_VALUE).getDenominatorAsLong()); - Assertions.assertEquals(1l, BigFraction.from(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong()); + Assertions.assertEquals(1L, BigFraction.from(Double.MAX_VALUE).getDenominatorAsLong()); + Assertions.assertEquals(1L, BigFraction.from(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong()); assertFraction(BigInteger.ONE, BigInteger.ONE.shiftLeft(1074), BigFraction.from(Double.MIN_VALUE)); } @@ -446,12 +445,10 @@ public class BigFractionTest { assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(f2)); } - { - final BigFraction f = BigFraction.of(-17 - 2*13*2, 13*13*17*2*2); - Assertions.assertThrows(NullPointerException.class, - () -> f.add((BigFraction) null) - ); - } + final BigFraction f0 = BigFraction.of(-17 - 2 * 13 * 2, 13 * 13 * 17 * 2 * 2); + Assertions.assertThrows(NullPointerException.class, + () -> f0.add((BigFraction) null) + ); BigFraction f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1); BigFraction f = f1.add(BigInteger.ONE); @@ -472,11 +469,11 @@ public class BigFractionTest { Assertions.assertEquals(1, f.getDenominatorAsInt()); f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1); - f = f1.add(1l); + f = f1.add(1L); Assertions.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assertions.assertEquals(1, f.getDenominatorAsInt()); - f = f.add(0l); + f = f.add(0L); Assertions.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assertions.assertEquals(1, f.getDenominatorAsInt()); @@ -490,10 +487,7 @@ public class BigFractionTest { assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(f2)); } - { - Assertions.assertThrows(FractionException.class, - () -> BigFraction.of(1, 2).divide(BigInteger.ZERO)); - } + Assertions.assertThrows(FractionException.class, () -> BigFraction.of(1, 2).divide(BigInteger.ZERO)); BigFraction f1; BigFraction f2; @@ -503,12 +497,10 @@ public class BigFractionTest { BigFraction f = f1.divide(f2); Assertions.assertSame(BigFraction.ZERO, f); - { - final BigFraction f3 = BigFraction.of(Integer.MIN_VALUE, 1); - Assertions.assertThrows(NullPointerException.class, - () -> f3.divide((BigFraction) null) - ); - } + final BigFraction f3 = BigFraction.of(Integer.MIN_VALUE, 1); + Assertions.assertThrows(NullPointerException.class, + () -> f3.divide((BigFraction) null) + ); f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide(BigInteger.valueOf(Integer.MIN_VALUE)); @@ -545,10 +537,7 @@ public class BigFractionTest { Assertions.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); Assertions.assertEquals(1, f.getDenominatorAsInt()); - { - Assertions.assertThrows(NullPointerException.class, - () -> BigFraction.ONE.multiply((BigFraction) null)); - } + Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.multiply((BigFraction) null)); Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(BigInteger.ONE)); Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(BigInteger.ZERO)); @@ -567,10 +556,7 @@ public class BigFractionTest { } BigFraction f = BigFraction.of(1, 1); - { - Assertions.assertThrows(NullPointerException.class, - () -> f.subtract((BigFraction) null)); - } + Assertions.assertThrows(NullPointerException.class, () -> f.subtract((BigFraction) null)); Assertions.assertEquals(BigFraction.ONE, BigFraction.ONE.subtract(BigInteger.ZERO)); Assertions.assertEquals(BigFraction.of(-1), BigFraction.ZERO.subtract(BigInteger.ONE)); @@ -614,14 +600,14 @@ public class BigFractionTest { @Test public void testPow() { Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13)); - Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13l)); - Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(BigInteger.valueOf(13l))); + Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13L)); + Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(BigInteger.valueOf(13L))); Assertions.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(0)); - Assertions.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(0l)); - Assertions.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(BigInteger.valueOf(0l))); + Assertions.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(0L)); + Assertions.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(BigInteger.valueOf(0L))); Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13)); - Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13l)); - Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(BigInteger.valueOf(-13l))); + Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13L)); + Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(BigInteger.valueOf(-13L))); Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 5).pow(123)); Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 5).pow(123L)); Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 5).pow(new BigInteger("112233445566778899"))); @@ -671,13 +657,13 @@ public class BigFractionTest { @Test public void testParse() { String[] validExpressions = new String[] { - "3", - "1 / 2", - "-1 / 2", - "1 / -2", - "-1 / -2", - "2147,483,647 / 2,147,483,648", //over largest int value - "9,223,372,036,854,775,807 / 9,223,372,036,854,775,808" //over largest long value + "3", + "1 / 2", + "-1 / 2", + "1 / -2", + "-1 / -2", + "2147,483,647 / 2,147,483,648", //over largest int value + "9,223,372,036,854,775,807 / 9,223,372,036,854,775,808" //over largest long value }; BigFraction[] fractions = { BigFraction.of(3), @@ -696,15 +682,9 @@ public class BigFractionTest { inc++; } - { - Assertions.assertThrows(NumberFormatException.class, - () -> BigFraction.parse("1 // 2")); - Assertions.assertThrows(NumberFormatException.class, - () -> BigFraction.parse("1 / z")); - Assertions.assertThrows(NumberFormatException.class, - () -> BigFraction.parse("1 / --2")); - Assertions.assertThrows(NumberFormatException.class, - () -> BigFraction.parse("x")); - } + Assertions.assertThrows(NumberFormatException.class, () -> BigFraction.parse("1 // 2")); + Assertions.assertThrows(NumberFormatException.class, () -> BigFraction.parse("1 / z")); + Assertions.assertThrows(NumberFormatException.class, () -> BigFraction.parse("1 / --2")); + Assertions.assertThrows(NumberFormatException.class, () -> BigFraction.parse("x")); } } 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 a2bcdeb..193da38 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 @@ -25,7 +25,10 @@ import java.util.List; * and {@link BigFraction}, e.g. for operations and expected results that * involve numerators and denominators in the {@code int} range. */ -class CommonTestCases { +final class CommonTestCases { + + // CHECKSTYLE: stop ConstantName + /** * See {@link #numDenConstructorTestCases()} */ @@ -71,6 +74,8 @@ class CommonTestCases { */ private static final List<BinaryOperatorTestCase> subtractFractionTestCasesList; + // CHECKSTYLE: resume ConstantName + static { numDenConstructorTestCasesList = collectNumDenConstructorTestCases(); doubleConstructorTestCasesList = collectDoubleConstructorTestCases(); @@ -83,6 +88,8 @@ class CommonTestCases { subtractFractionTestCasesList = collectSubtractFractionTestCases(); } + private CommonTestCases() {} + /** * Defines test cases as described in * {@link #numDenConstructorTestCases()} and collects them into a {@code @@ -116,47 +123,47 @@ class CommonTestCases { private static List<DoubleToFractionTestCase> collectDoubleConstructorTestCases() { List<DoubleToFractionTestCase> testCases = new ArrayList<>(); - testCases.add(new DoubleToFractionTestCase(1d/2d, 1, 2)); - testCases.add(new DoubleToFractionTestCase(1d/3d, 1, 3)); - testCases.add(new DoubleToFractionTestCase(2d/3d, 2, 3)); - testCases.add(new DoubleToFractionTestCase(1d/4d, 1, 4)); - testCases.add(new DoubleToFractionTestCase(3d/4d, 3, 4)); - testCases.add(new DoubleToFractionTestCase(1d/5d, 1, 5)); - testCases.add(new DoubleToFractionTestCase(2d/5d, 2, 5)); - testCases.add(new DoubleToFractionTestCase(3d/5d, 3, 5)); - testCases.add(new DoubleToFractionTestCase(4d/5d, 4, 5)); - testCases.add(new DoubleToFractionTestCase(1d/6d, 1, 6)); - testCases.add(new DoubleToFractionTestCase(5d/6d, 5, 6)); - testCases.add(new DoubleToFractionTestCase(1d/7d, 1, 7)); - testCases.add(new DoubleToFractionTestCase(2d/7d, 2, 7)); - testCases.add(new DoubleToFractionTestCase(3d/7d, 3, 7)); - testCases.add(new DoubleToFractionTestCase(4d/7d, 4, 7)); - testCases.add(new DoubleToFractionTestCase(5d/7d, 5, 7)); - testCases.add(new DoubleToFractionTestCase(6d/7d, 6, 7)); - testCases.add(new DoubleToFractionTestCase(1d/8d, 1, 8)); - testCases.add(new DoubleToFractionTestCase(3d/8d, 3, 8)); - testCases.add(new DoubleToFractionTestCase(5d/8d, 5, 8)); - testCases.add(new DoubleToFractionTestCase(7d/8d, 7, 8)); - testCases.add(new DoubleToFractionTestCase(1d/9d, 1, 9)); - testCases.add(new DoubleToFractionTestCase(2d/9d, 2, 9)); - testCases.add(new DoubleToFractionTestCase(4d/9d, 4, 9)); - testCases.add(new DoubleToFractionTestCase(5d/9d, 5, 9)); - testCases.add(new DoubleToFractionTestCase(7d/9d, 7, 9)); - testCases.add(new DoubleToFractionTestCase(8d/9d, 8, 9)); - testCases.add(new DoubleToFractionTestCase(1d/10d, 1, 10)); - testCases.add(new DoubleToFractionTestCase(3d/10d, 3, 10)); - testCases.add(new DoubleToFractionTestCase(7d/10d, 7, 10)); - testCases.add(new DoubleToFractionTestCase(9d/10d, 9, 10)); - testCases.add(new DoubleToFractionTestCase(1d/11d, 1, 11)); - testCases.add(new DoubleToFractionTestCase(2d/11d, 2, 11)); - testCases.add(new DoubleToFractionTestCase(3d/11d, 3, 11)); - testCases.add(new DoubleToFractionTestCase(4d/11d, 4, 11)); - testCases.add(new DoubleToFractionTestCase(5d/11d, 5, 11)); - testCases.add(new DoubleToFractionTestCase(6d/11d, 6, 11)); - testCases.add(new DoubleToFractionTestCase(7d/11d, 7, 11)); - testCases.add(new DoubleToFractionTestCase(8d/11d, 8, 11)); - testCases.add(new DoubleToFractionTestCase(9d/11d, 9, 11)); - testCases.add(new DoubleToFractionTestCase(10d/11d, 10, 11)); + testCases.add(new DoubleToFractionTestCase(1d / 2d, 1, 2)); + testCases.add(new DoubleToFractionTestCase(1d / 3d, 1, 3)); + testCases.add(new DoubleToFractionTestCase(2d / 3d, 2, 3)); + testCases.add(new DoubleToFractionTestCase(1d / 4d, 1, 4)); + testCases.add(new DoubleToFractionTestCase(3d / 4d, 3, 4)); + testCases.add(new DoubleToFractionTestCase(1d / 5d, 1, 5)); + testCases.add(new DoubleToFractionTestCase(2d / 5d, 2, 5)); + testCases.add(new DoubleToFractionTestCase(3d / 5d, 3, 5)); + testCases.add(new DoubleToFractionTestCase(4d / 5d, 4, 5)); + testCases.add(new DoubleToFractionTestCase(1d / 6d, 1, 6)); + testCases.add(new DoubleToFractionTestCase(5d / 6d, 5, 6)); + testCases.add(new DoubleToFractionTestCase(1d / 7d, 1, 7)); + testCases.add(new DoubleToFractionTestCase(2d / 7d, 2, 7)); + testCases.add(new DoubleToFractionTestCase(3d / 7d, 3, 7)); + testCases.add(new DoubleToFractionTestCase(4d / 7d, 4, 7)); + testCases.add(new DoubleToFractionTestCase(5d / 7d, 5, 7)); + testCases.add(new DoubleToFractionTestCase(6d / 7d, 6, 7)); + testCases.add(new DoubleToFractionTestCase(1d / 8d, 1, 8)); + testCases.add(new DoubleToFractionTestCase(3d / 8d, 3, 8)); + testCases.add(new DoubleToFractionTestCase(5d / 8d, 5, 8)); + testCases.add(new DoubleToFractionTestCase(7d / 8d, 7, 8)); + testCases.add(new DoubleToFractionTestCase(1d / 9d, 1, 9)); + testCases.add(new DoubleToFractionTestCase(2d / 9d, 2, 9)); + testCases.add(new DoubleToFractionTestCase(4d / 9d, 4, 9)); + testCases.add(new DoubleToFractionTestCase(5d / 9d, 5, 9)); + testCases.add(new DoubleToFractionTestCase(7d / 9d, 7, 9)); + testCases.add(new DoubleToFractionTestCase(8d / 9d, 8, 9)); + testCases.add(new DoubleToFractionTestCase(1d / 10d, 1, 10)); + testCases.add(new DoubleToFractionTestCase(3d / 10d, 3, 10)); + testCases.add(new DoubleToFractionTestCase(7d / 10d, 7, 10)); + testCases.add(new DoubleToFractionTestCase(9d / 10d, 9, 10)); + testCases.add(new DoubleToFractionTestCase(1d / 11d, 1, 11)); + testCases.add(new DoubleToFractionTestCase(2d / 11d, 2, 11)); + testCases.add(new DoubleToFractionTestCase(3d / 11d, 3, 11)); + testCases.add(new DoubleToFractionTestCase(4d / 11d, 4, 11)); + testCases.add(new DoubleToFractionTestCase(5d / 11d, 5, 11)); + testCases.add(new DoubleToFractionTestCase(6d / 11d, 6, 11)); + testCases.add(new DoubleToFractionTestCase(7d / 11d, 7, 11)); + testCases.add(new DoubleToFractionTestCase(8d / 11d, 8, 11)); + testCases.add(new DoubleToFractionTestCase(9d / 11d, 9, 11)); + testCases.add(new DoubleToFractionTestCase(10d / 11d, 10, 11)); testCases.add(new DoubleToFractionTestCase(0.00000000000001, 0, 1)); testCases.add(new DoubleToFractionTestCase(0.40000000000001, 2, 5)); @@ -233,9 +240,10 @@ class CommonTestCases { testCases.add(new BinaryOperatorTestCase(2, 3, -2, 3, 0, 1)); testCases.add(new BinaryOperatorTestCase( - -1, 13*13*2*2, - -2, 13*17*2, - -17 - 2*13*2, 13*13*17*2*2)); + -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 the int range. // check that it doesn't. @@ -347,7 +355,7 @@ class CommonTestCases { testCases.add(new BinaryOperatorTestCase( Integer.MAX_VALUE, 1, 1, 1, - Integer.MAX_VALUE-1, 1 + Integer.MAX_VALUE - 1, 1 )); return testCases; @@ -467,6 +475,8 @@ class CommonTestCases { return Collections.unmodifiableList(subtractFractionTestCasesList); } + // CHECKSTYLE: stop VisibilityModifier + /** * Represents a test case where a unary operation should be performed on * a specified combination of numerator and denominator, both in the 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 33ca53e..a26d378 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 @@ -51,7 +51,7 @@ public class FractionTest { public void testGoldenRatio() { // the golden ratio is notoriously a difficult number for continuous fraction Assertions.assertThrows(ArithmeticException.class, - () -> Fraction.from((1 + Math.sqrt(5)) / 2, 1.0e-12, 25) + () -> Fraction.from((1 + Math.sqrt(5)) / 2, 1.0e-12, 25) ); } @@ -93,7 +93,7 @@ public class FractionTest { private void checkIntegerOverflow(final double a) { Assertions.assertThrows(ArithmeticException.class, - () -> Fraction.from(a, 1.0e-12, 1000) + () -> Fraction.from(a, 1.0e-12, 1000) ); } @@ -111,27 +111,23 @@ public class FractionTest { @Test public void testCompareTo() { - { - Fraction first = Fraction.of(1, 2); - Fraction second = Fraction.of(1, 3); - Fraction third = Fraction.of(1, 2); - - Assertions.assertEquals(0, first.compareTo(first)); - Assertions.assertEquals(0, first.compareTo(third)); - Assertions.assertEquals(1, first.compareTo(second)); - Assertions.assertEquals(-1, second.compareTo(first)); - } - - { - // these two values are different approximations of PI - // the first one is approximately PI - 3.07e-18 - // the second one is approximately PI + 1.936e-17 - Fraction pi1 = Fraction.of(1068966896, 340262731); - Fraction pi2 = Fraction.of( 411557987, 131002976); - Assertions.assertEquals(-1, pi1.compareTo(pi2)); - Assertions.assertEquals( 1, pi2.compareTo(pi1)); - Assertions.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); - } + Fraction first = Fraction.of(1, 2); + Fraction second = Fraction.of(1, 3); + Fraction third = Fraction.of(1, 2); + + Assertions.assertEquals(0, first.compareTo(first)); + Assertions.assertEquals(0, first.compareTo(third)); + Assertions.assertEquals(1, first.compareTo(second)); + Assertions.assertEquals(-1, second.compareTo(first)); + + // these two values are different approximations of PI + // the first one is approximately PI - 3.07e-18 + // the second one is approximately PI + 1.936e-17 + Fraction pi1 = Fraction.of(1068966896, 340262731); + Fraction pi2 = Fraction.of(411557987, 131002976); + Assertions.assertEquals(-1, pi1.compareTo(pi2)); + Assertions.assertEquals(1, pi2.compareTo(pi1)); + Assertions.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); } @Test @@ -192,17 +188,13 @@ public class FractionTest { @Test public void testMath1261() { - { - final Fraction a = Fraction.of(Integer.MAX_VALUE, 2); - final Fraction b = a.multiply(2); - Assertions.assertTrue(b.equals(Fraction.of(Integer.MAX_VALUE))); - } + final Fraction a = Fraction.of(Integer.MAX_VALUE, 2); + final Fraction b = a.multiply(2); + Assertions.assertTrue(b.equals(Fraction.of(Integer.MAX_VALUE))); - { - final Fraction c = Fraction.of(2, Integer.MAX_VALUE); - final Fraction d = c.divide(2); - Assertions.assertTrue(d.equals(Fraction.of(1, Integer.MAX_VALUE))); - } + final Fraction c = Fraction.of(2, Integer.MAX_VALUE); + final Fraction d = c.divide(2); + Assertions.assertTrue(d.equals(Fraction.of(1, Integer.MAX_VALUE))); } @Test @@ -212,12 +204,10 @@ public class FractionTest { assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.reciprocal()); } - { - final Fraction f = Fraction.of(0, 3); - Assertions.assertThrows(ArithmeticException.class, - f::reciprocal - ); - } + final Fraction f = Fraction.of(0, 3); + Assertions.assertThrows(ArithmeticException.class, + f::reciprocal + ); } @Test @@ -236,6 +226,8 @@ public class FractionTest { assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(f2)); } + // CHECKSTYLE: stop AvoidNestedBlocks + { Fraction f1 = Fraction.of(Integer.MAX_VALUE - 1, 1); Fraction f = f1.add(1); @@ -243,19 +235,19 @@ public class FractionTest { } { - final Fraction f = Fraction.of(-17 - 2*13*2, 13*13*17*2*2); + final Fraction f = Fraction.of(-17 - 2 * 13 * 2, 13 * 13 * 17 * 2 * 2); Assertions.assertThrows(NullPointerException.class, - () -> f.add(null) + () -> f.add(null) ); } { final Fraction f1 = Fraction.of(Integer.MAX_VALUE, 1); Assertions.assertThrows(ArithmeticException.class, - () -> { - Fraction f = f1.add(Fraction.ONE); // should overflow - Assertions.fail("expecting ArithmeticException but got: " + f.toString()); - } + () -> { + Fraction f = f1.add(Fraction.ONE); // should overflow + Assertions.fail("expecting ArithmeticException but got: " + f.toString()); + } ); } @@ -264,17 +256,17 @@ public class FractionTest { final Fraction f1 = Fraction.of(Integer.MIN_VALUE, 5); final Fraction f2 = Fraction.of(-1, 5); Assertions.assertThrows(ArithmeticException.class, - () -> { - Fraction f = f1.add(f2); // should overflow - Assertions.fail("expecting ArithmeticException but got: " + f.toString()); - } + () -> { + Fraction f = f1.add(f2); // should overflow + Assertions.fail("expecting ArithmeticException but got: " + f.toString()); + } ); } { final Fraction f = Fraction.of(-Integer.MAX_VALUE, 1); Assertions.assertThrows(ArithmeticException.class, - () -> f.add(f) + () -> f.add(f) ); } @@ -282,10 +274,10 @@ public class FractionTest { final Fraction f1 = Fraction.of(3, 327680); final Fraction f2 = Fraction.of(2, 59049); Assertions.assertThrows(ArithmeticException.class, - () -> { - Fraction f = f1.add(f2); // should overflow - Assertions.fail("expecting ArithmeticException but got: " + f.toString()); - } + () -> { + Fraction f = f1.add(f2); // should overflow + Assertions.fail("expecting ArithmeticException but got: " + f.toString()); + } ); } } @@ -302,7 +294,7 @@ public class FractionTest { final Fraction f1 = Fraction.of(3, 5); final Fraction f2 = Fraction.ZERO; Assertions.assertThrows(FractionException.class, - () -> f1.divide(f2) + () -> f1.divide(f2) ); } @@ -316,21 +308,21 @@ public class FractionTest { { final Fraction f = Fraction.of(Integer.MIN_VALUE, 1); Assertions.assertThrows(NullPointerException.class, - () -> f.divide(null) + () -> f.divide(null) ); } { final Fraction f1 = Fraction.of(1, Integer.MAX_VALUE); Assertions.assertThrows(ArithmeticException.class, - () -> f1.divide(f1.reciprocal()) // should overflow + () -> f1.divide(f1.reciprocal()) // should overflow ); } { final Fraction f1 = Fraction.of(1, -Integer.MAX_VALUE); Assertions.assertThrows(ArithmeticException.class, - () -> f1.divide(f1.reciprocal()) // should overflow + () -> f1.divide(f1.reciprocal()) // should overflow ); } @@ -349,18 +341,14 @@ public class FractionTest { assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(f2)); } - { - final Fraction f = Fraction.of(Integer.MIN_VALUE, 1); - Assertions.assertThrows(NullPointerException.class, - () -> f.multiply(null) - ); - } + final Fraction f0 = Fraction.of(Integer.MIN_VALUE, 1); + Assertions.assertThrows(NullPointerException.class, + () -> f0.multiply(null) + ); - { - Fraction f1 = Fraction.of(6, 35); - Fraction f = f1.multiply(15); - assertFraction(18, 7, f); - } + Fraction f1 = Fraction.of(6, 35); + Fraction f = f1.multiply(15); + assertFraction(18, 7, f); } @Test @@ -400,21 +388,22 @@ public class FractionTest { { final Fraction f = Fraction.of(1, 1); Assertions.assertThrows(NullPointerException.class, - () -> f.subtract(null) + () -> f.subtract(null) ); } + // CHECKSTYLE: stop AvoidNestedBlocks { Fraction f1 = Fraction.of(Integer.MAX_VALUE, 1); Fraction f = f1.subtract(1); - assertFraction(Integer.MAX_VALUE-1, 1, f); + assertFraction(Integer.MAX_VALUE - 1, 1, f); } { final Fraction f1 = Fraction.of(1, Integer.MAX_VALUE); final Fraction f2 = Fraction.of(1, Integer.MAX_VALUE - 1); Assertions.assertThrows(ArithmeticException.class, - () -> f1.subtract(f2) //should overflow + () -> f1.subtract(f2) //should overflow ); } @@ -423,24 +412,24 @@ public class FractionTest { final Fraction f1 = Fraction.of(Integer.MIN_VALUE, 5); final Fraction f2 = Fraction.of(1, 5); Assertions.assertThrows(ArithmeticException.class, - () -> { - Fraction f = f1.subtract(f2); // should overflow - Assertions.fail("expecting ArithmeticException but got: " + f.toString()); - } + () -> { + Fraction f = f1.subtract(f2); // should overflow + Assertions.fail("expecting ArithmeticException but got: " + f.toString()); + } ); } { final Fraction f = Fraction.of(Integer.MIN_VALUE, 1); Assertions.assertThrows(ArithmeticException.class, - () -> f.subtract(Fraction.ONE) + () -> f.subtract(Fraction.ONE) ); } { final Fraction f = Fraction.of(Integer.MAX_VALUE, 1); Assertions.assertThrows(ArithmeticException.class, - () -> f.subtract(Fraction.ONE.negate()) + () -> f.subtract(Fraction.ONE.negate()) ); } @@ -448,12 +437,13 @@ public class FractionTest { final Fraction f1 = Fraction.of(3, 327680); final Fraction f2 = Fraction.of(2, 59049); Assertions.assertThrows(ArithmeticException.class, - () -> { - Fraction f = f1.subtract(f2); // should overflow - Assertions.fail("expecting ArithmeticException but got: " + f.toString()); - } + () -> { + Fraction f = f1.subtract(f2); // should overflow + Assertions.fail("expecting ArithmeticException but got: " + f.toString()); + } ); } + // CHECKSTYLE: resume AvoidNestedBlocks } @Test @@ -551,15 +541,9 @@ public class FractionTest { inc++; } - { - Assertions.assertThrows(NumberFormatException.class, - () -> Fraction.parse("1 // 2")); - Assertions.assertThrows(NumberFormatException.class, - () -> Fraction.parse("1 / z")); - Assertions.assertThrows(NumberFormatException.class, - () -> Fraction.parse("1 / --2")); - Assertions.assertThrows(NumberFormatException.class, - () -> Fraction.parse("x")); - } + Assertions.assertThrows(NumberFormatException.class, () -> Fraction.parse("1 // 2")); + Assertions.assertThrows(NumberFormatException.class, () -> Fraction.parse("1 / z")); + Assertions.assertThrows(NumberFormatException.class, () -> Fraction.parse("1 / --2")); + Assertions.assertThrows(NumberFormatException.class, () -> Fraction.parse("x")); } }
