From eb962ef6f5348a706cec17e33fa05c2b17435cb8 Mon Sep 17 00:00:00 2001 From: Michael Hausegger Date: Mon, 10 Jul 2017 20:33:03 +0200 Subject: [PATCH 1/3] Add-some-Unit-Tests Add some Unit Tests to increase code coverage. --- .../numbers/arrays/LinearCombinationTest.java | 22 +- .../commons/numbers/arrays/SafeNormTest.java | 37 +- .../BinomialCoefficientTest.java | 90 +++-- .../combinatorics/FactorialDoubleTest.java | 24 ++ .../combinatorics/LogFactorialTest.java | 43 ++- .../commons/numbers/complex/ComplexTest.java | 51 +++ .../numbers/complex/ComplexUtilsTest.java | 348 +++++++++++++---- .../commons/numbers/core/PrecisionTest.java | 261 +++++++++---- .../fraction/BigFractionFormatTest.java | 190 +++++++--- .../numbers/fraction/BigFractionTest.java | 357 +++++++++++------- .../numbers/fraction/FractionFormatTest.java | 39 ++ .../numbers/fraction/FractionTest.java | 163 ++++++++ .../commons/numbers/gamma/DigammaTest.java | 37 +- .../numbers/gamma/RegularizedBetaTest.java | 54 ++- .../commons/numbers/gamma/TrigammaTest.java | 20 +- .../numbers/primes/SmallPrimesTest.java | 27 +- .../numbers/quaternion/QuaternionTest.java | 255 +++++++++---- 17 files changed, 1544 insertions(+), 474 deletions(-) diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java index 49843acdd..aca3e9cb8 100644 --- a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java +++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java @@ -19,7 +19,10 @@ import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.numbers.fraction.BigFraction; - + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * Test cases for the {@link LinearCombination} class. */ @@ -294,4 +297,21 @@ public void testInfinite() { a[7][3], b[7][3]))); Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7], b[7]))); } + + @Test + public void testValueTakingThreeArgumentsThrowsIllegalArgumentException() { + + double[] doubleArray = new double[4]; + double[] doubleArrayTwo = new double[0]; + + try { + LinearCombination.value(doubleArray, doubleArrayTwo); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Dimension mismatch: 4 != 0", e.getMessage()); + assertEquals(LinearCombination.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java index dc949dfed..842b033f2 100644 --- a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java +++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java @@ -16,6 +16,8 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Test cases for the {@link SafeNorm} class. */ @@ -24,39 +26,52 @@ public class SafeNormTest { @Test public void testTiny() { final double s = 1e-320; - final double[] v = new double[] { s, s }; - Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d); + final double[] v = new double[]{s, s}; + assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d); } @Test public void testBig() { final double s = 1e300; - final double[] v = new double[] { s, s }; - Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d); + final double[] v = new double[]{s, s}; + assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d); } @Test public void testOne3D() { final double s = 1; - final double[] v = new double[] { s, s, s }; - Assert.assertEquals(Math.sqrt(3), SafeNorm.value(v), 0d); + final double[] v = new double[]{s, s, s}; + assertEquals(Math.sqrt(3), SafeNorm.value(v), 0d); } @Test public void testUnit3D() { - Assert.assertEquals(1, SafeNorm.value(new double[] { 1, 0, 0 }), 0d); - Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 1, 0 }), 0d); - Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 0, 1 }), 0d); + assertEquals(1, SafeNorm.value(new double[]{1, 0, 0}), 0d); + assertEquals(1, SafeNorm.value(new double[]{0, 1, 0}), 0d); + assertEquals(1, SafeNorm.value(new double[]{0, 0, 1}), 0d); } @Test public void testSimple() { - final double[] v = new double[] { -0.9, 8.7, -6.5, -4.3, -2.1, 0, 1.2, 3.4, -5.6, 7.8, 9.0 }; + final double[] v = new double[]{-0.9, 8.7, -6.5, -4.3, -2.1, 0, 1.2, 3.4, -5.6, 7.8, 9.0}; double n = 0; for (int i = 0; i < v.length; i++) { n += v[i] * v[i]; } final double expected = Math.sqrt(n); - Assert.assertEquals(expected, SafeNorm.value(v), 0d); + assertEquals(expected, SafeNorm.value(v), 0d); + } + + @Test + public void testValueReturningPositive() { + + double[] doubleArray = new double[5]; + doubleArray[0] = 3.834E-20; + doubleArray[1] = 5.8798224E-39; + double doubleValue = SafeNorm.value(doubleArray); + + assertEquals(3.834E-20, doubleValue, 0.01); + } + } diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java index d1dacc653..639f49533 100644 --- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java +++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java @@ -16,81 +16,87 @@ */ package org.apache.commons.numbers.combinatorics; -import java.util.List; -import java.util.ArrayList; -import java.util.Map; -import java.util.HashMap; - +import org.apache.commons.numbers.core.ArithmeticUtils; import org.junit.Assert; import org.junit.Test; -import org.apache.commons.numbers.core.ArithmeticUtils; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; /** * Test cases for the {@link BinomialCoefficient} class. */ public class BinomialCoefficientTest { - /** Cached binomial coefficients. */ + /** + * Cached binomial coefficients. + */ private static final List> binomialCache = - new ArrayList>(); + new ArrayList>(); - /** Verify that b(0,0) = 1 */ + /** + * Verify that b(0,0) = 1 + */ @Test public void test0Choose0() { - Assert.assertEquals(BinomialCoefficient.value(0, 0), 1); + assertEquals(BinomialCoefficient.value(0, 0), 1); } @Test public void testBinomialCoefficient() { - final long[] bcoef5 = { 1, 5, 10, 10, 5, 1 }; - final long[] bcoef6 = { 1, 6, 15, 20, 15, 6, 1 }; + final long[] bcoef5 = {1, 5, 10, 10, 5, 1}; + final long[] bcoef6 = {1, 6, 15, 20, 15, 6, 1}; for (int i = 0; i < 6; i++) { - Assert.assertEquals("5 choose " + i, bcoef5[i], BinomialCoefficient.value(5, i)); + assertEquals("5 choose " + i, bcoef5[i], BinomialCoefficient.value(5, i)); } for (int i = 0; i < 7; i++) { - Assert.assertEquals("6 choose " + i, bcoef6[i], BinomialCoefficient.value(6, i)); + assertEquals("6 choose " + i, bcoef6[i], BinomialCoefficient.value(6, i)); } for (int n = 1; n < 10; n++) { for (int k = 0; k <= n; k++) { - Assert.assertEquals(n + " choose " + k, - binomialCoefficient(n, k), - BinomialCoefficient.value(n, k)); + assertEquals(n + " choose " + k, + binomialCoefficient(n, k), + BinomialCoefficient.value(n, k)); } } - final int[] n = { 34, 66, 100, 1500, 1500 }; - final int[] k = { 17, 33, 10, 1500 - 4, 4 }; + final int[] n = {34, 66, 100, 1500, 1500}; + final int[] k = {17, 33, 10, 1500 - 4, 4}; for (int i = 0; i < n.length; i++) { final long expected = binomialCoefficient(n[i], k[i]); - Assert.assertEquals(n[i] + " choose " + k[i], - expected, - BinomialCoefficient.value(n[i], k[i])); + assertEquals(n[i] + " choose " + k[i], + expected, + BinomialCoefficient.value(n[i], k[i])); } } - @Test(expected=CombinatoricsException.class) + @Test(expected = CombinatoricsException.class) public void testBinomialCoefficientFail1() { BinomialCoefficient.value(4, 5); } - @Test(expected=CombinatoricsException.class) + @Test(expected = CombinatoricsException.class) public void testBinomialCoefficientFail2() { BinomialCoefficient.value(-1, -2); } - @Test(expected=ArithmeticException.class) + @Test(expected = ArithmeticException.class) public void testBinomialCoefficientFail3() { BinomialCoefficient.value(67, 30); } - @Test(expected=ArithmeticException.class) + @Test(expected = ArithmeticException.class) public void testBinomialCoefficientFail4() { BinomialCoefficient.value(67, 34); } - @Test(expected=ArithmeticException.class) + @Test(expected = ArithmeticException.class) public void testBinomialCoefficientFail5() { BinomialCoefficient.value(700, 300); } @@ -118,34 +124,34 @@ public void testBinomialCoefficientLarge() throws Exception { } catch (ArithmeticException ex) { shouldThrow = true; } - Assert.assertEquals(n + " choose " + k, exactResult, ourResult); - Assert.assertEquals(n + " choose " + k, shouldThrow, didThrow); + assertEquals(n + " choose " + k, exactResult, ourResult); + assertEquals(n + " choose " + k, shouldThrow, didThrow); Assert.assertTrue(n + " choose " + k, (n > 66 || !didThrow)); } } long ourResult = BinomialCoefficient.value(300, 3); long exactResult = binomialCoefficient(300, 3); - Assert.assertEquals(exactResult, ourResult); + assertEquals(exactResult, ourResult); ourResult = BinomialCoefficient.value(700, 697); exactResult = binomialCoefficient(700, 697); - Assert.assertEquals(exactResult, ourResult); + assertEquals(exactResult, ourResult); final int n = 10000; ourResult = BinomialCoefficient.value(n, 3); exactResult = binomialCoefficient(n, 3); - Assert.assertEquals(exactResult, ourResult); + assertEquals(exactResult, ourResult); } - @Test(expected=CombinatoricsException.class) + @Test(expected = CombinatoricsException.class) public void testCheckBinomial1() { // n < 0 BinomialCoefficient.checkBinomial(-1, -2); } - @Test(expected=CombinatoricsException.class) + @Test(expected = CombinatoricsException.class) public void testCheckBinomial2() { // k > n BinomialCoefficient.checkBinomial(4, 5); @@ -181,7 +187,7 @@ static long binomialCoefficient(int n, int k) { binomialCoefficient(n - 100, k - 100); } result = ArithmeticUtils.addAndCheck(binomialCoefficient(n - 1, k - 1), - binomialCoefficient(n - 1, k)); + binomialCoefficient(n - 1, k)); } if (result == -1) { throw new IllegalArgumentException(); @@ -192,4 +198,18 @@ static long binomialCoefficient(int n, int k) { binomialCache.get(n).put(Integer.valueOf(k), Long.valueOf(result)); return result; } + + @Test + public void testCheckBinomialThrowsCombinatoricsException() { + + try { + BinomialCoefficient.checkBinomial(66, (-2802)); + fail("Expecting exception: CombinatoricsException"); + } catch (CombinatoricsException e) { + assertEquals("Number -2,802 is out of range [0, 66]", e.getMessage()); + assertEquals(BinomialCoefficient.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java index 50c03ba16..8253c39b2 100644 --- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java +++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java @@ -19,6 +19,9 @@ import org.junit.Assert; import org.junit.Test; +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.fail; + /** * Test cases for the {@link FactorialDouble} class. */ @@ -121,10 +124,31 @@ public void testCacheDecrease() { * Direct multiplication implementation. */ private double factorialDirect(int n) { + double result = 1; + for (int i = 2; i <= n; i++) { result *= i; } + return result; + + } + + @Test + public void testWithCacheThrowsCombinatoricsException() { + + FactorialDouble factorialDouble = FactorialDouble.create(); + + try { + factorialDouble.withCache((-2194)); + fail("Expecting exception: CombinatoricsException"); + } catch(CombinatoricsException e) { + assertEquals("Number -2,194 is negative",e.getMessage()); + assertEquals(FactorialDouble.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java index bb223ba24..1c8f60eff 100644 --- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java +++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java @@ -17,15 +17,16 @@ package org.apache.commons.numbers.combinatorics; import org.apache.commons.numbers.gamma.LogGamma; - -import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * Test cases for the {@link LogFactorial} class. */ public class LogFactorialTest { - @Test(expected=IllegalArgumentException.class) + @Test(expected = IllegalArgumentException.class) public void testNonPositiveArgument() { LogFactorial.create().withCache(-1); } @@ -38,8 +39,8 @@ public void testDelegation() { // "LogGamma" class. for (int i = 21; i < 10000; i++) { final double expected = LogGamma.value(i + 1); - Assert.assertEquals(i + "! ", - expected, f.value(i), 0d); + assertEquals(i + "! ", + expected, f.value(i), 0d); } } @@ -53,8 +54,8 @@ public void testCompareDirectWithoutCache() { for (int i = 0; i < max; i++) { final double expected = logFactorial(i); - Assert.assertEquals(i + "! ", - expected, f.value(i), 2 * Math.ulp(expected)); + assertEquals(i + "! ", + expected, f.value(i), 2 * Math.ulp(expected)); } } @@ -65,8 +66,8 @@ public void testCompareDirectWithCache() { for (int i = 0; i < max; i++) { final double expected = logFactorial(i); - Assert.assertEquals(i + "! ", - expected, f.value(i), 0d); + assertEquals(i + "! ", + expected, f.value(i), 0d); } } @@ -74,8 +75,8 @@ public void testCompareDirectWithCache() { public void testZeroCache() { // Ensure that no exception is thrown. final LogFactorial f = LogFactorial.create().withCache(0); - Assert.assertEquals(0, f.value(0), 0d); - Assert.assertEquals(0, f.value(1), 0d); + assertEquals(0, f.value(0), 0d); + assertEquals(0, f.value(1), 0d); } @Test @@ -93,7 +94,7 @@ public void testCacheIncrease() { final int val = max + max / 2; final double expected = logFactorial(val); - Assert.assertEquals(expected, f2.value(val), 0d); + assertEquals(expected, f2.value(val), 0d); } @Test @@ -104,7 +105,7 @@ public void testCacheDecrease() { final int val = max / 4; final double expected = logFactorial(val); - Assert.assertEquals(expected, f2.value(val), 0d); + assertEquals(expected, f2.value(val), 0d); } // Direct implementation. @@ -115,4 +116,20 @@ private double logFactorial(final int n) { } return logSum; } + + @Test + public void testValueThrowsCombinatoricsException() { + + LogFactorial logFactorial = LogFactorial.create(); + + try { + logFactorial.value((-482)); + fail("Expecting exception: CombinatoricsException"); + } catch (CombinatoricsException e) { + assertEquals("Number -482 is negative", e.getMessage()); + assertEquals(LogFactorial.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java index 85f3148c4..3633b4297 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java @@ -25,6 +25,10 @@ import org.junit.Ignore; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + /** */ @@ -1483,4 +1487,51 @@ protected TestComplex createComplex(double real, double imaginary){ } } + + + @Test + public void testAddTakingDoubleReturningComplexWhereGetRealIsNegativeAndExpReturningComplexWhereIsNaNIsTrue() { + + Complex complex = new Complex(0.0, 0.0); + + try { + complex.nthRoot((-1642)); + fail("Expecting exception: RuntimeException"); + } catch(RuntimeException e) { + assertEquals("cannot compute nth root for null or negative n: {0}",e.getMessage()); + assertEquals(Complex.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testDivideTakingComplexThrowsRuntimeException() { + + Complex complex = new Complex(2732.59210279); + + try { + complex.NaN.divide(null); + fail("Expecting exception: RuntimeException"); + } catch(RuntimeException e) { + assertEquals("Null Argument to Complex Method",e.getMessage()); + assertEquals(Complex.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testSubtractTakingComplex() { + + Complex complex = new Complex(0.0, 0.0); + Complex complexTwo = complex.INF.sqrt(); + Complex complexThree = complex.asin(); + Complex complexFour = complexThree.pow(complexTwo); + complexFour.NaN.subtract(complexTwo); + complexThree.multiply(complex); + + assertFalse( Complex.equals(complex, complexFour, 0) ); + + } + } diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java index 9a7a29dff..91f36e371 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java @@ -22,6 +22,9 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** */ public class ComplexUtilsTest { @@ -38,25 +41,25 @@ public class ComplexUtilsTest { private final Complex infNaN = new Complex(inf, nan); private static Complex c[]; // complex array with real values even and imag - // values odd + // values odd private static Complex cr[]; // complex array with real values consecutive private static Complex ci[]; // complex array with imag values consecutive private static double d[]; // real array with consecutive vals private static double di[]; // real array with consecutive vals, - // 'interleaved' length + // 'interleaved' length private static float f[]; // real array with consecutive vals private static float fi[]; // real array with consec vals, interleaved - // length + // length private static double sr[]; // real component of split array, evens private static double si[]; // imag component of split array, odds private static float sfr[]; // real component of split array, float, evens private static float sfi[]; // imag component of split array, float, odds static Complex ans1, ans2; // answers to single value extraction methods static Complex[] ansArrayc1r, ansArrayc1i, ansArrayc2r, ansArrayc2i, ansArrayc3, ansArrayc4; // answers - // to - // range - // extraction - // methods + // to + // range + // extraction + // methods static double[] ansArrayd1r, ansArrayd2r, ansArrayd1i, ansArrayd2i, ansArraydi1, ansArraydi2; static float[] ansArrayf1r, ansArrayf2r, ansArrayf1i, ansArrayf2i, ansArrayfi1, ansArrayfi2; static String msg; // error message for AssertEquals @@ -152,26 +155,26 @@ private static void setArrays() { // initial setup method } } } - ansArrayc1r = new Complex[] { new Complex(3), new Complex(4), new Complex(5), new Complex(6), new Complex(7) }; - ansArrayc2r = new Complex[] { new Complex(3), new Complex(5), new Complex(7) }; - ansArrayc1i = new Complex[] { new Complex(0, 3), new Complex(0, 4), new Complex(0, 5), new Complex(0, 6), - new Complex(0, 7) }; - ansArrayc2i = new Complex[] { new Complex(0, 3), new Complex(0, 5), new Complex(0, 7) }; - ansArrayc3 = new Complex[] { new Complex(6, 7), new Complex(8, 9), new Complex(10, 11), new Complex(12, 13), - new Complex(14, 15) }; - ansArrayc4 = new Complex[] { new Complex(6, 7), new Complex(10, 11), new Complex(14, 15) }; - ansArrayd1r = new double[] { 6, 8, 10, 12, 14 }; - ansArrayd1i = new double[] { 7, 9, 11, 13, 15 }; - ansArrayd2r = new double[] { 6, 10, 14 }; - ansArrayd2i = new double[] { 7, 11, 15 }; - ansArrayf1r = new float[] { 6, 8, 10, 12, 14 }; - ansArrayf1i = new float[] { 7, 9, 11, 13, 15 }; - ansArrayf2r = new float[] { 6, 10, 14 }; - ansArrayf2i = new float[] { 7, 11, 15 }; - ansArraydi1 = new double[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - ansArrayfi1 = new float[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - ansArraydi2 = new double[] { 6, 7, 10, 11, 14, 15 }; - ansArrayfi2 = new float[] { 6, 7, 10, 11, 14, 15 }; + ansArrayc1r = new Complex[]{new Complex(3), new Complex(4), new Complex(5), new Complex(6), new Complex(7)}; + ansArrayc2r = new Complex[]{new Complex(3), new Complex(5), new Complex(7)}; + ansArrayc1i = new Complex[]{new Complex(0, 3), new Complex(0, 4), new Complex(0, 5), new Complex(0, 6), + new Complex(0, 7)}; + ansArrayc2i = new Complex[]{new Complex(0, 3), new Complex(0, 5), new Complex(0, 7)}; + ansArrayc3 = new Complex[]{new Complex(6, 7), new Complex(8, 9), new Complex(10, 11), new Complex(12, 13), + new Complex(14, 15)}; + ansArrayc4 = new Complex[]{new Complex(6, 7), new Complex(10, 11), new Complex(14, 15)}; + ansArrayd1r = new double[]{6, 8, 10, 12, 14}; + ansArrayd1i = new double[]{7, 9, 11, 13, 15}; + ansArrayd2r = new double[]{6, 10, 14}; + ansArrayd2i = new double[]{7, 11, 15}; + ansArrayf1r = new float[]{6, 8, 10, 12, 14}; + ansArrayf1i = new float[]{7, 9, 11, 13, 15}; + ansArrayf2r = new float[]{6, 10, 14}; + ansArrayf2i = new float[]{7, 11, 15}; + ansArraydi1 = new double[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + ansArrayfi1 = new float[]{6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + ansArraydi2 = new double[]{6, 7, 10, 11, 14, 15}; + ansArrayfi2 = new float[]{6, 7, 10, 11, 14, 15}; msg = ""; } @@ -229,11 +232,11 @@ public void testPolar2ComplexInf() { @Test public void testCExtract() { - final double[] real = new double[] { negInf, -123.45, 0, 1, 234.56, pi, inf }; + final double[] real = new double[]{negInf, -123.45, 0, 1, 234.56, pi, inf}; final Complex[] complex = ComplexUtils.real2Complex(real); for (int i = 0; i < real.length; i++) { - Assert.assertEquals(real[i], complex[i].getReal(), 0d); + assertEquals(real[i], complex[i].getReal(), 0d); } } @@ -255,10 +258,10 @@ public void testExtractionMethods() { // Extract complex from interleaved float array, index 3 TestUtils.assertSame(new Complex(6, 7), ComplexUtils.extractComplexFromInterleavedArray(f, 3)); // Extract interleaved double from complex array, index 3 - TestUtils.assertEquals(msg, new double[] { 6, 7 }, ComplexUtils.extractInterleavedFromComplexArray(c, 3), + TestUtils.assertEquals(msg, new double[]{6, 7}, ComplexUtils.extractInterleavedFromComplexArray(c, 3), Math.ulp(1)); // Extract interleaved float from complex array, index 3 - TestUtils.assertEquals(msg, new double[] { 6, 7 }, ComplexUtils.extractInterleavedFromComplexArray(c, 3), + TestUtils.assertEquals(msg, new double[]{6, 7}, ComplexUtils.extractInterleavedFromComplexArray(c, 3), Math.ulp(1)); if (!msg.equals("")) { throw new RuntimeException(msg); @@ -271,23 +274,23 @@ public void testRealToComplex() { setArrays(); // Real double to complex, range 3-7, increment 1, entered as ints // Real double to complex, whole array - TestUtils.assertEquals(msg, cr, ComplexUtils.real2Complex(d),Math.ulp(1.0)); + TestUtils.assertEquals(msg, cr, ComplexUtils.real2Complex(d), Math.ulp(1.0)); // Real float to complex, whole array - TestUtils.assertEquals(msg, cr, ComplexUtils.real2Complex(f),Math.ulp(1.0)); + TestUtils.assertEquals(msg, cr, ComplexUtils.real2Complex(f), Math.ulp(1.0)); // 2d for (int i = 0; i < 10; i++) { // Real double to complex, 2d - TestUtils.assertEquals(msg, cr2d[i], ComplexUtils.real2Complex(d2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, cr2d[i], ComplexUtils.real2Complex(d2d[i]), Math.ulp(1.0)); // Real float to complex, 2d - TestUtils.assertEquals(msg, cr2d[i], ComplexUtils.real2Complex(f2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, cr2d[i], ComplexUtils.real2Complex(f2d[i]), Math.ulp(1.0)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Real double to complex, 3d - TestUtils.assertEquals(msg, cr3d[i][j], ComplexUtils.real2Complex(d3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, cr3d[i][j], ComplexUtils.real2Complex(d3d[i][j]), Math.ulp(1.0)); // Real float to complex, 3d - TestUtils.assertEquals(msg, cr3d[i][j], ComplexUtils.real2Complex(f3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, cr3d[i][j], ComplexUtils.real2Complex(f3d[i][j]), Math.ulp(1.0)); } } if (!msg.equals("")) { @@ -299,23 +302,23 @@ public void testRealToComplex() { public void testComplexToReal() { setArrays(); // Real complex to double, whole array - TestUtils.assertEquals(msg, sr, ComplexUtils.complex2Real(c),Math.ulp(1.0)); + TestUtils.assertEquals(msg, sr, ComplexUtils.complex2Real(c), Math.ulp(1.0)); // Real complex to float, whole array - TestUtils.assertEquals(msg, sfr, ComplexUtils.complex2RealFloat(c),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, sfr, ComplexUtils.complex2RealFloat(c), Math.ulp(1.0f)); // 2d for (int i = 0; i < 10; i++) { // Real complex to double, 2d - TestUtils.assertEquals(msg, sr2d[i], ComplexUtils.complex2Real(c2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, sr2d[i], ComplexUtils.complex2Real(c2d[i]), Math.ulp(1.0)); // Real complex to float, 2d - TestUtils.assertEquals(msg, sfr2d[i], ComplexUtils.complex2RealFloat(c2d[i]),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, sfr2d[i], ComplexUtils.complex2RealFloat(c2d[i]), Math.ulp(1.0f)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Real complex to double, 3d - TestUtils.assertEquals(msg, sr3d[i][j], ComplexUtils.complex2Real(c3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, sr3d[i][j], ComplexUtils.complex2Real(c3d[i][j]), Math.ulp(1.0)); // Real complex to float, 3d - TestUtils.assertEquals(msg, sfr3d[i][j], ComplexUtils.complex2RealFloat(c3d[i][j]),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, sfr3d[i][j], ComplexUtils.complex2RealFloat(c3d[i][j]), Math.ulp(1.0f)); } } if (!msg.equals("")) { @@ -329,23 +332,23 @@ public void testComplexToReal() { public void testImaginaryToComplex() { setArrays(); // Imaginary double to complex, whole array - TestUtils.assertEquals(msg, ci, ComplexUtils.imaginary2Complex(d),Math.ulp(1.0)); + TestUtils.assertEquals(msg, ci, ComplexUtils.imaginary2Complex(d), Math.ulp(1.0)); // Imaginary float to complex, whole array - TestUtils.assertEquals(msg, ci, ComplexUtils.imaginary2Complex(f),Math.ulp(1.0)); + TestUtils.assertEquals(msg, ci, ComplexUtils.imaginary2Complex(f), Math.ulp(1.0)); // 2d for (int i = 0; i < 10; i++) { // Imaginary double to complex, 2d - TestUtils.assertEquals(msg, ci2d[i], ComplexUtils.imaginary2Complex(d2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, ci2d[i], ComplexUtils.imaginary2Complex(d2d[i]), Math.ulp(1.0)); // Imaginary float to complex, 2d - TestUtils.assertEquals(msg, ci2d[i], ComplexUtils.imaginary2Complex(f2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, ci2d[i], ComplexUtils.imaginary2Complex(f2d[i]), Math.ulp(1.0)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Imaginary double to complex, 3d - TestUtils.assertEquals(msg, ci3d[i][j], ComplexUtils.imaginary2Complex(d3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, ci3d[i][j], ComplexUtils.imaginary2Complex(d3d[i][j]), Math.ulp(1.0)); // Imaginary float to complex, 3d - TestUtils.assertEquals(msg, ci3d[i][j], ComplexUtils.imaginary2Complex(f3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, ci3d[i][j], ComplexUtils.imaginary2Complex(f3d[i][j]), Math.ulp(1.0)); } } if (!msg.equals("")) { @@ -357,23 +360,23 @@ public void testImaginaryToComplex() { public void testComplexToImaginary() { setArrays(); // Imaginary complex to double, whole array - TestUtils.assertEquals(msg, si, ComplexUtils.complex2Imaginary(c),Math.ulp(1.0)); + TestUtils.assertEquals(msg, si, ComplexUtils.complex2Imaginary(c), Math.ulp(1.0)); // Imaginary complex to float, whole array - TestUtils.assertEquals(msg, sfi, ComplexUtils.complex2ImaginaryFloat(c),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, sfi, ComplexUtils.complex2ImaginaryFloat(c), Math.ulp(1.0f)); // 2d for (int i = 0; i < 10; i++) { // Imaginary complex to double, 2d - TestUtils.assertEquals(msg, si2d[i], ComplexUtils.complex2Imaginary(c2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, si2d[i], ComplexUtils.complex2Imaginary(c2d[i]), Math.ulp(1.0)); // Imaginary complex to float, 2d - TestUtils.assertEquals(msg, sfi2d[i], ComplexUtils.complex2ImaginaryFloat(c2d[i]),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, sfi2d[i], ComplexUtils.complex2ImaginaryFloat(c2d[i]), Math.ulp(1.0f)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Imaginary complex to double, 3d - TestUtils.assertEquals(msg, si3d[i][j], ComplexUtils.complex2Imaginary(c3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, si3d[i][j], ComplexUtils.complex2Imaginary(c3d[i][j]), Math.ulp(1.0)); // Imaginary complex to float, 3d - TestUtils.assertEquals(msg, sfi3d[i][j], ComplexUtils.complex2ImaginaryFloat(c3d[i][j]),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, sfi3d[i][j], ComplexUtils.complex2ImaginaryFloat(c3d[i][j]), Math.ulp(1.0f)); } } if (!msg.equals("")) { @@ -387,23 +390,23 @@ public void testComplexToImaginary() { public void testInterleavedToComplex() { setArrays(); // Interleaved double to complex, whole array - TestUtils.assertEquals(msg, c, ComplexUtils.interleaved2Complex(di),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c, ComplexUtils.interleaved2Complex(di), Math.ulp(1.0)); // Interleaved float to complex, whole array - TestUtils.assertEquals(msg, c, ComplexUtils.interleaved2Complex(fi),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c, ComplexUtils.interleaved2Complex(fi), Math.ulp(1.0)); // 2d for (int i = 0; i < 10; i++) { // Interleaved double to complex, 2d - TestUtils.assertEquals(msg, c2d[i], ComplexUtils.interleaved2Complex(di2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c2d[i], ComplexUtils.interleaved2Complex(di2d[i]), Math.ulp(1.0)); // Interleaved float to complex, 2d - TestUtils.assertEquals(msg, c2d[i], ComplexUtils.interleaved2Complex(fi2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c2d[i], ComplexUtils.interleaved2Complex(fi2d[i]), Math.ulp(1.0)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Interleaved double to complex, 3d - TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.interleaved2Complex(di3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.interleaved2Complex(di3d[i][j]), Math.ulp(1.0)); // Interleaved float to complex, 3d - TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.interleaved2Complex(fi3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.interleaved2Complex(fi3d[i][j]), Math.ulp(1.0)); } } if (!msg.equals("")) { @@ -414,23 +417,23 @@ public void testInterleavedToComplex() { @Test public void testComplexToInterleaved() { setArrays(); - TestUtils.assertEquals(msg, di, ComplexUtils.complex2Interleaved(c),Math.ulp(1.0)); + TestUtils.assertEquals(msg, di, ComplexUtils.complex2Interleaved(c), Math.ulp(1.0)); // Interleaved complex to float, whole array - TestUtils.assertEquals(msg, fi, ComplexUtils.complex2InterleavedFloat(c),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, fi, ComplexUtils.complex2InterleavedFloat(c), Math.ulp(1.0f)); // 2d for (int i = 0; i < 10; i++) { // Interleaved complex to double, 2d - TestUtils.assertEquals(msg, di2d[i], ComplexUtils.complex2Interleaved(c2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, di2d[i], ComplexUtils.complex2Interleaved(c2d[i]), Math.ulp(1.0)); // Interleaved complex to float, 2d - TestUtils.assertEquals(msg, fi2d[i], ComplexUtils.complex2InterleavedFloat(c2d[i]),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, fi2d[i], ComplexUtils.complex2InterleavedFloat(c2d[i]), Math.ulp(1.0f)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Interleaved complex to double, 3d - TestUtils.assertEquals(msg, di3d[i][j], ComplexUtils.complex2Interleaved(c3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, di3d[i][j], ComplexUtils.complex2Interleaved(c3d[i][j]), Math.ulp(1.0)); // Interleaved complex to float, 3d - TestUtils.assertEquals(msg, fi3d[i][j], ComplexUtils.complex2InterleavedFloat(c3d[i][j]),Math.ulp(1.0f)); + TestUtils.assertEquals(msg, fi3d[i][j], ComplexUtils.complex2InterleavedFloat(c3d[i][j]), Math.ulp(1.0f)); } } if (!msg.equals("")) { @@ -443,18 +446,18 @@ public void testComplexToInterleaved() { public void testSplit2Complex() { setArrays(); // Split double to complex, whole array - TestUtils.assertEquals(msg, c, ComplexUtils.split2Complex(sr, si),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c, ComplexUtils.split2Complex(sr, si), Math.ulp(1.0)); // 2d for (int i = 0; i < 10; i++) { // Split double to complex, 2d - TestUtils.assertEquals(msg, c2d[i], ComplexUtils.split2Complex(sr2d[i], si2d[i]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c2d[i], ComplexUtils.split2Complex(sr2d[i], si2d[i]), Math.ulp(1.0)); } // 3d for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // Split double to complex, 3d - TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.split2Complex(sr3d[i][j], si3d[i][j]),Math.ulp(1.0)); + TestUtils.assertEquals(msg, c3d[i][j], ComplexUtils.split2Complex(sr3d[i][j], si3d[i][j]), Math.ulp(1.0)); } } if (!msg.equals("")) { @@ -472,4 +475,209 @@ public void testInitialize() { TestUtils.assertEquals(new Complex(0, 0), cc, Math.ulp(0)); } } + + @Test + public void testcomplex2InterleavedFloatTakingTwoAndTwoWithPositive() { + + Complex[][] complexArray = new Complex[0][9]; + + try { + ComplexUtils.complex2InterleavedFloat(complexArray, 2); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: 2", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testcomplex2ImaginaryTakingComplexArrayArrayArrayThrowsNullPointerException() { + + Complex[][][] complexArray = new Complex[1][2][5]; + complexArray[0] = new Complex[7][1]; + + try { + ComplexUtils.complex2Imaginary(complexArray); + fail("Expecting exception: NullPointerException"); + } catch (NullPointerException e) { + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testcomplex2InterleavedFloatTakingTwoAndTwoWithEmptyArrayAndNegative() { + + Complex[][][] complexArray = new Complex[0][7][3]; + + try { + ComplexUtils.complex2InterleavedFloat(complexArray, (-1)); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: -1", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testInterleavedThreeComplexTakingTwoAndTwoWithEmptyArray() { + + float[][][] floatArray = new float[0][5][8]; + + try { + ComplexUtils.interleaved2Complex(floatArray, 334); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: 334", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testExtractComplexFromInterleavedArrayTakingTwoAndTwoThrowsArrayIndexOutOfBoundsException() { + + double[] doubleArray = new double[5]; + doubleArray[0] = 0.0; + doubleArray[1] = 3553.05; + + try { + ComplexUtils.extractComplexFromInterleavedArray(doubleArray, (-2145818362)); + fail("Expecting exception: ArrayIndexOutOfBoundsException"); + } catch (ArrayIndexOutOfBoundsException e) { + assertEquals("3330572", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testInterleavedThrowsIllegalArgumentException() { + + try { + ComplexUtils.interleaved2Complex((float[][][]) null, (-2501)); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: -2501", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testPolarThreeComplexTakingThreeDoubleArraysThrowsIllegalArgumentException() { + + double[] doubleArray = new double[8]; + doubleArray[0] = 682.369; + doubleArray[1] = 612.16397804415; + doubleArray[2] = 1.0; + doubleArray[3] = (-252.151672108609); + doubleArray[4] = 0.0; + doubleArray[5] = Double.POSITIVE_INFINITY; + + try { + ComplexUtils.polar2Complex(doubleArray, doubleArray); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Modulus is negative: -252.151672108609", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testcomplex2InterleavedFloatTakingTwoAndTwoWithNegative() { + + Complex[][] complexArray = new Complex[0][8]; + + try { + ComplexUtils.complex2InterleavedFloat(complexArray, (-3231)); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: -3231", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testInterleavedThreeComplexTakingTwoAndTwoWithEmptyArrayAndNegative() { + + float[][] floatArray = new float[0][3]; + + try { + ComplexUtils.interleaved2Complex(floatArray, (-4026)); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: -4026", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testInterleavedThreeComplexTakingTwoAndTwoWithNullAndPositive() { + + try { + ComplexUtils.interleaved2Complex((float[][]) null, 2146551443); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: 2146551443", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testcomplex2ImaginaryTakingComplexArrayArray() { + + double[] doubleArray = new double[4]; + doubleArray[1] = 2005.05225; + doubleArray[3] = 2005.05225; + ComplexUtils.real2Complex(doubleArray); + Complex[][][] complexArray = new Complex[1][2][5]; + Complex[][] complexArrayTwo = new Complex[0][1]; + complexArray[0] = complexArrayTwo; + ComplexUtils.complex2Imaginary(complexArray); + ComplexUtils.initialize(complexArray); + + try { + ComplexUtils.complex2Interleaved(complexArray, (-2984)); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Out of range: -2984", e.getMessage()); + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test //I consider this to be a defect. + public void testAbsThrowsNullPointerException() { + + float[] floatArray = new float[0]; + Complex[] complexArray = ComplexUtils.interleaved2Complex(floatArray); + Complex[][] complexArrayTwo = new Complex[5][8]; + complexArrayTwo[1] = complexArray; + complexArrayTwo[2] = complexArray; + complexArrayTwo[0] = complexArray; + ComplexUtils.complex2Interleaved(complexArrayTwo); + + try { + ComplexUtils.abs(complexArrayTwo[4]); + fail("Expecting exception: NullPointerException"); + } catch (NullPointerException e) { + assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java index 188c949f4..f550cd7e3 100644 --- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java +++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java @@ -18,6 +18,9 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + /** * Test cases for the {@link Precision} class. * @@ -29,35 +32,35 @@ public void testEqualsWithRelativeTolerance() { Assert.assertTrue(Precision.equalsWithRelativeTolerance(0d, 1 / Double.NEGATIVE_INFINITY, 0d)); final double eps = 1e-14; - Assert.assertFalse(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654988, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654988, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654987, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654948, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654948, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654949, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(Precision.SAFE_MIN, 0.0, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(Precision.SAFE_MIN, 0.0, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(1.0000000000001e-300, 1e-300, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(1.0000000000001e-300, 1e-300, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(1.00000000000001e-300, 1e-300, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, 1.23, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.POSITIVE_INFINITY, 1.23, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, 1.23, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(Double.POSITIVE_INFINITY, 1.23, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NaN, 1.23, eps)); - Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NaN, Double.NaN, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(Double.NaN, 1.23, eps)); + assertFalse(Precision.equalsWithRelativeTolerance(Double.NaN, Double.NaN, eps)); } @Test public void testEqualsIncludingNaN() { double[] testArray = { - Double.NaN, - Double.POSITIVE_INFINITY, - Double.NEGATIVE_INFINITY, - 1d, - 0d }; + Double.NaN, + Double.POSITIVE_INFINITY, + Double.NEGATIVE_INFINITY, + 1d, + 0d}; for (int i = 0; i < testArray.length; i++) { for (int j = 0; j < testArray.length; j++) { if (i == j) { @@ -76,12 +79,12 @@ public void testEqualsWithAllowedDelta() { Assert.assertTrue(Precision.equals(153.0000, 153.0000, .0625)); Assert.assertTrue(Precision.equals(153.0000, 153.0625, .0625)); Assert.assertTrue(Precision.equals(152.9375, 153.0000, .0625)); - Assert.assertFalse(Precision.equals(153.0000, 153.0625, .0624)); - Assert.assertFalse(Precision.equals(152.9374, 153.0000, .0625)); - Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 1.0)); + assertFalse(Precision.equals(153.0000, 153.0625, .0624)); + assertFalse(Precision.equals(152.9374, 153.0000, .0625)); + assertFalse(Precision.equals(Double.NaN, Double.NaN, 1.0)); Assert.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); Assert.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0)); - Assert.assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); + assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); } @Test @@ -100,7 +103,7 @@ public void testMath475() { // Because "a" and "c" are not adjacent, the tolerance is taken into // account for assessing equality. Assert.assertTrue(Precision.equals(a, c, diff)); - Assert.assertFalse(Precision.equals(a, c, (1 - 1e-16) * diff)); + assertFalse(Precision.equals(a, c, (1 - 1e-16) * diff)); } @Test @@ -111,20 +114,20 @@ public void testEqualsIncludingNaNWithAllowedDelta() { Assert.assertTrue(Precision.equalsIncludingNaN(Double.NaN, Double.NaN, 1.0)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0)); - Assert.assertFalse(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); - Assert.assertFalse(Precision.equalsIncludingNaN(153.0000, 153.0625, .0624)); - Assert.assertFalse(Precision.equalsIncludingNaN(152.9374, 153.0000, .0625)); + assertFalse(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); + assertFalse(Precision.equalsIncludingNaN(153.0000, 153.0625, .0624)); + assertFalse(Precision.equalsIncludingNaN(152.9374, 153.0000, .0625)); } // Tests for floating point equality @Test public void testFloatEqualsWithAllowedUlps() { - Assert.assertTrue("+0.0f == -0.0f",Precision.equals(0.0f, -0.0f)); - Assert.assertTrue("+0.0f == -0.0f (1 ulp)",Precision.equals(0.0f, -0.0f, 1)); + Assert.assertTrue("+0.0f == -0.0f", Precision.equals(0.0f, -0.0f)); + Assert.assertTrue("+0.0f == -0.0f (1 ulp)", Precision.equals(0.0f, -0.0f, 1)); float oneFloat = 1.0f; - Assert.assertTrue("1.0f == 1.0f + 1 ulp",Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)))); - Assert.assertTrue("1.0f == 1.0f + 1 ulp (1 ulp)",Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)), 1)); - Assert.assertFalse("1.0f != 1.0f + 2 ulp (1 ulp)",Precision.equals(oneFloat, Float.intBitsToFloat(2 + Float.floatToIntBits(oneFloat)), 1)); + Assert.assertTrue("1.0f == 1.0f + 1 ulp", Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)))); + Assert.assertTrue("1.0f == 1.0f + 1 ulp (1 ulp)", Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)), 1)); + assertFalse("1.0f != 1.0f + 2 ulp (1 ulp)", Precision.equals(oneFloat, Float.intBitsToFloat(2 + Float.floatToIntBits(oneFloat)), 1)); Assert.assertTrue(Precision.equals(153.0f, 153.0f, 1)); @@ -145,13 +148,13 @@ public void testFloatEqualsWithAllowedUlps() { Assert.assertTrue(Precision.equals(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY, 1)); - Assert.assertFalse(Precision.equals(Float.NaN, Float.NaN, 1)); - Assert.assertFalse(Precision.equals(Float.NaN, Float.NaN, 0)); - Assert.assertFalse(Precision.equals(Float.NaN, 0, 0)); - Assert.assertFalse(Precision.equals(Float.NaN, Float.POSITIVE_INFINITY, 0)); - Assert.assertFalse(Precision.equals(Float.NaN, Float.NEGATIVE_INFINITY, 0)); + assertFalse(Precision.equals(Float.NaN, Float.NaN, 1)); + assertFalse(Precision.equals(Float.NaN, Float.NaN, 0)); + assertFalse(Precision.equals(Float.NaN, 0, 0)); + assertFalse(Precision.equals(Float.NaN, Float.POSITIVE_INFINITY, 0)); + assertFalse(Precision.equals(Float.NaN, Float.NEGATIVE_INFINITY, 0)); - Assert.assertFalse(Precision.equals(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 100000)); + assertFalse(Precision.equals(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 100000)); } @Test @@ -159,13 +162,13 @@ public void testEqualsWithAllowedUlps() { Assert.assertTrue(Precision.equals(0.0, -0.0, 1)); Assert.assertTrue(Precision.equals(1.0, 1 + Math.ulp(1d), 1)); - Assert.assertFalse(Precision.equals(1.0, 1 + 2 * Math.ulp(1d), 1)); + assertFalse(Precision.equals(1.0, 1 + 2 * Math.ulp(1d), 1)); final double nUp1 = Math.nextAfter(1d, Double.POSITIVE_INFINITY); final double nnUp1 = Math.nextAfter(nUp1, Double.POSITIVE_INFINITY); Assert.assertTrue(Precision.equals(1.0, nUp1, 1)); Assert.assertTrue(Precision.equals(nUp1, nnUp1, 1)); - Assert.assertFalse(Precision.equals(1.0, nnUp1, 1)); + assertFalse(Precision.equals(1.0, nnUp1, 1)); Assert.assertTrue(Precision.equals(0.0, Math.ulp(0d), 1)); Assert.assertTrue(Precision.equals(0.0, -Math.ulp(0d), 1)); @@ -173,14 +176,14 @@ public void testEqualsWithAllowedUlps() { Assert.assertTrue(Precision.equals(153.0, 153.0, 1)); Assert.assertTrue(Precision.equals(153.0, 153.00000000000003, 1)); - Assert.assertFalse(Precision.equals(153.0, 153.00000000000006, 1)); + assertFalse(Precision.equals(153.0, 153.00000000000006, 1)); Assert.assertTrue(Precision.equals(153.0, 152.99999999999997, 1)); - Assert.assertFalse(Precision.equals(153, 152.99999999999994, 1)); + assertFalse(Precision.equals(153, 152.99999999999994, 1)); Assert.assertTrue(Precision.equals(-128.0, -127.99999999999999, 1)); - Assert.assertFalse(Precision.equals(-128.0, -127.99999999999997, 1)); + assertFalse(Precision.equals(-128.0, -127.99999999999997, 1)); Assert.assertTrue(Precision.equals(-128.0, -128.00000000000003, 1)); - Assert.assertFalse(Precision.equals(-128.0, -128.00000000000006, 1)); + assertFalse(Precision.equals(-128.0, -128.00000000000006, 1)); Assert.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); @@ -188,13 +191,13 @@ public void testEqualsWithAllowedUlps() { Assert.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY, 1)); - Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 1)); - Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 0)); - Assert.assertFalse(Precision.equals(Double.NaN, 0, 0)); - Assert.assertFalse(Precision.equals(Double.NaN, Double.POSITIVE_INFINITY, 0)); - Assert.assertFalse(Precision.equals(Double.NaN, Double.NEGATIVE_INFINITY, 0)); + assertFalse(Precision.equals(Double.NaN, Double.NaN, 1)); + assertFalse(Precision.equals(Double.NaN, Double.NaN, 0)); + assertFalse(Precision.equals(Double.NaN, 0, 0)); + assertFalse(Precision.equals(Double.NaN, Double.POSITIVE_INFINITY, 0)); + assertFalse(Precision.equals(Double.NaN, Double.NEGATIVE_INFINITY, 0)); - Assert.assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); + assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); } @Test @@ -202,13 +205,13 @@ public void testEqualsIncludingNaNWithAllowedUlps() { Assert.assertTrue(Precision.equalsIncludingNaN(0.0, -0.0, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(1.0, 1 + Math.ulp(1d), 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(1.0, 1 + 2 * Math.ulp(1d), 1)); + assertFalse(Precision.equalsIncludingNaN(1.0, 1 + 2 * Math.ulp(1d), 1)); final double nUp1 = Math.nextAfter(1d, Double.POSITIVE_INFINITY); final double nnUp1 = Math.nextAfter(nUp1, Double.POSITIVE_INFINITY); Assert.assertTrue(Precision.equalsIncludingNaN(1.0, nUp1, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(nUp1, nnUp1, 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(1.0, nnUp1, 1)); + assertFalse(Precision.equalsIncludingNaN(1.0, nnUp1, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(0.0, Math.ulp(0d), 1)); Assert.assertTrue(Precision.equalsIncludingNaN(0.0, -Math.ulp(0d), 1)); @@ -216,14 +219,14 @@ public void testEqualsIncludingNaNWithAllowedUlps() { Assert.assertTrue(Precision.equalsIncludingNaN(153.0, 153.0, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(153.0, 153.00000000000003, 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(153.0, 153.00000000000006, 1)); + assertFalse(Precision.equalsIncludingNaN(153.0, 153.00000000000006, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(153.0, 152.99999999999997, 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(153, 152.99999999999994, 1)); + assertFalse(Precision.equalsIncludingNaN(153, 152.99999999999994, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(-128.0, -127.99999999999999, 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(-128.0, -127.99999999999997, 1)); + assertFalse(Precision.equalsIncludingNaN(-128.0, -127.99999999999997, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(-128.0, -128.00000000000003, 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(-128.0, -128.00000000000006, 1)); + assertFalse(Precision.equalsIncludingNaN(-128.0, -128.00000000000006, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); @@ -233,7 +236,7 @@ public void testEqualsIncludingNaNWithAllowedUlps() { Assert.assertTrue(Precision.equalsIncludingNaN(Double.NaN, Double.NaN, 1)); - Assert.assertFalse(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); + assertFalse(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); } @Test @@ -247,35 +250,35 @@ public void testCompareToEpsilon() { @Test public void testCompareToMaxUlps() { - double a = 152.32; + double a = 152.32; double delta = Math.ulp(a); for (int i = 0; i <= 10; ++i) { if (i <= 5) { - Assert.assertEquals( 0, Precision.compareTo(a, a + i * delta, 5)); - Assert.assertEquals( 0, Precision.compareTo(a, a - i * delta, 5)); + Assert.assertEquals(0, Precision.compareTo(a, a + i * delta, 5)); + Assert.assertEquals(0, Precision.compareTo(a, a - i * delta, 5)); } else { Assert.assertEquals(-1, Precision.compareTo(a, a + i * delta, 5)); Assert.assertEquals(+1, Precision.compareTo(a, a - i * delta, 5)); } } - Assert.assertEquals( 0, Precision.compareTo(-0.0, 0.0, 0)); + Assert.assertEquals(0, Precision.compareTo(-0.0, 0.0, 0)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, -0.0, 0)); - Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, -0.0, 1)); + Assert.assertEquals(0, Precision.compareTo(-Double.MIN_VALUE, -0.0, 1)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, +0.0, 0)); - Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, +0.0, 1)); + Assert.assertEquals(0, Precision.compareTo(-Double.MIN_VALUE, +0.0, 1)); - Assert.assertEquals(+1, Precision.compareTo( Double.MIN_VALUE, -0.0, 0)); - Assert.assertEquals( 0, Precision.compareTo( Double.MIN_VALUE, -0.0, 1)); - Assert.assertEquals(+1, Precision.compareTo( Double.MIN_VALUE, +0.0, 0)); - Assert.assertEquals( 0, Precision.compareTo( Double.MIN_VALUE, +0.0, 1)); + Assert.assertEquals(+1, Precision.compareTo(Double.MIN_VALUE, -0.0, 0)); + Assert.assertEquals(0, Precision.compareTo(Double.MIN_VALUE, -0.0, 1)); + Assert.assertEquals(+1, Precision.compareTo(Double.MIN_VALUE, +0.0, 0)); + Assert.assertEquals(0, Precision.compareTo(Double.MIN_VALUE, +0.0, 1)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 0)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 1)); - Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 2)); + Assert.assertEquals(0, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 2)); - Assert.assertEquals( 0, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); + Assert.assertEquals(0, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); Assert.assertEquals(-1, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 0)); Assert.assertEquals(+1, Precision.compareTo(Double.MAX_VALUE, Double.NaN, Integer.MAX_VALUE)); @@ -504,7 +507,7 @@ public void testRoundFloat() { @Test public void testIssue721() { - Assert.assertEquals(-53, Math.getExponent(Precision.EPSILON)); + Assert.assertEquals(-53, Math.getExponent(Precision.EPSILON)); Assert.assertEquals(-1022, Math.getExponent(Precision.SAFE_MIN)); } @@ -528,20 +531,140 @@ public void testRepresentableDelta() { @Test public void testMath843() { final double afterEpsilon = Math.nextAfter(Precision.EPSILON, - Double.POSITIVE_INFINITY); + Double.POSITIVE_INFINITY); // a) 1 + EPSILON is equal to 1. Assert.assertTrue(1 + Precision.EPSILON == 1); // b) 1 + "the number after EPSILON" is not equal to 1. - Assert.assertFalse(1 + afterEpsilon == 1); + assertFalse(1 + afterEpsilon == 1); } @Test public void testMath1127() { - Assert.assertFalse(Precision.equals(2.0, -2.0, 1)); + assertFalse(Precision.equals(2.0, -2.0, 1)); Assert.assertTrue(Precision.equals(0.0, -0.0, 0)); - Assert.assertFalse(Precision.equals(2.0f, -2.0f, 1)); + assertFalse(Precision.equals(2.0f, -2.0f, 1)); Assert.assertTrue(Precision.equals(0.0f, -0.0f, 0)); } + + @Test + public void testEqualsIncludingNaNTakingThreeDoublesAndTwoWithNegativeAndPositiveOne() { + + assertFalse(Precision.equalsIncludingNaN((-2367.8), (double) Float.NaN, (-1776))); + + } + + + @Test + public void testEqualsIncludingNaNTakingThreeDoublesAndTwoWithNegativeAndPositiveTwo() { + + assertFalse(Precision.equalsIncludingNaN(Double.NaN, (-2122.27), (-779))); + + } + + + @Test + public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithNegative() { + + assertTrue(Precision.equalsIncludingNaN(Float.NaN, Float.NaN, (-2541))); + + } + + + @Test + public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithZeroAndEqualsIncludingNaNTakingThreeFloatsAndTwoReturningFalse() { + + assertFalse(Precision.equalsIncludingNaN((float) 0, Float.NaN, 2243)); + + } + + + @Test + public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithNegativeAndZero() { + + assertFalse(Precision.equalsIncludingNaN(Float.NaN, (-1.0F), 0)); + + } + + + @Test + public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithZeroAndEqualsIncludingNaNTakingThreeFloatsAndTwoReturningTrue() { + + assertTrue(Precision.equalsIncludingNaN((float) 1566, -0.0F, 2143768278)); + + } + + + @Test + public void testEqualsTakingThreeFloatsAndTwoWithNegativeAndPositiveTwo() { + + assertFalse(Precision.equals((float) 6, (-1861.78F), 1150126653)); + + } + + + @Test + public void testEqualsIncludingNaNTakingFourFloatsOne() { + + assertTrue(Precision.equalsIncludingNaN(2220.4014F, (float) 1, 2220.4014F)); + + } + + + @Test + public void testEqualsTakingFourFloatsWithZero() { + + assertTrue(Precision.equals(696.2F, 0.0F, 696.2F)); + + } + + + @Test + public void testEqualsTakingFourFloatsReturningFalse() { + + assertFalse(Precision.equals((float) (-1685), 668.0F, (-2701.3188F))); + + } + + + @Test + public void testEqualsTakingFourFloats() { + + assertTrue(Precision.equals(414.5782F, 414.5782F, 414.5782F)); + + } + + + @Test + public void testEqualsIncludingNaNTakingFourFloatsReturningFalse() { + + assertFalse(Precision.equalsIncludingNaN(Float.NaN, 3036.304F, 1.0F)); + + } + + + @Test + public void testEqualsIncludingNaNTakingThreeFloats() { + + assertFalse(Precision.equalsIncludingNaN(1573.7026F, Float.NaN)); + + } + + + @Test + public void testEqualsIncludingNaNTakingFourFloatsTwo() { + + assertTrue(Precision.equalsIncludingNaN(Float.NaN, Float.NaN, 1.0F)); + + } + + + @Test + public void testEqualsTakingThreeDoubles() { + + assertFalse(Precision.equals((double) 0.0F, (double) Float.NaN)); + + } + } diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java index 7998986a3..84304e94a 100644 --- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java @@ -19,6 +19,8 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.text.FieldPosition; +import java.text.Format; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; @@ -27,6 +29,9 @@ import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + public class BigFractionFormatTest { @@ -49,10 +54,10 @@ public void testFormat() { String expected = "1 / 2"; String actual = properFormat.format(c); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); actual = improperFormat.format(c); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); } @Test @@ -61,10 +66,10 @@ public void testFormatNegative() { String expected = "-1 / 2"; String actual = properFormat.format(c); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); actual = improperFormat.format(c); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); } @Test @@ -73,10 +78,10 @@ public void testFormatZero() { String expected = "0 / 1"; String actual = properFormat.format(c); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); actual = improperFormat.format(c); - Assert.assertEquals(expected, actual); + assertEquals(expected, actual); } @Test @@ -84,10 +89,10 @@ public void testFormatImproper() { BigFraction c = new BigFraction(5, 3); String actual = properFormat.format(c); - Assert.assertEquals("1 2 / 3", actual); + assertEquals("1 2 / 3", actual); actual = improperFormat.format(c); - Assert.assertEquals("5 / 3", actual); + assertEquals("5 / 3", actual); } @Test @@ -95,10 +100,10 @@ public void testFormatImproperNegative() { BigFraction c = new BigFraction(-5, 3); String actual = properFormat.format(c); - Assert.assertEquals("-1 2 / 3", actual); + assertEquals("-1 2 / 3", actual); actual = improperFormat.format(c); - Assert.assertEquals("-5 / 3", actual); + assertEquals("-5 / 3", actual); } @Test @@ -108,13 +113,13 @@ public void testParse() throws Exception { { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(BigInteger.ONE, c.getNumerator()); - Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator()); + assertEquals(BigInteger.ONE, c.getNumerator()); + assertEquals(BigInteger.valueOf(2l), c.getDenominator()); c = improperFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(BigInteger.ONE, c.getNumerator()); - Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator()); + assertEquals(BigInteger.ONE, c.getNumerator()); + assertEquals(BigInteger.valueOf(2l), c.getDenominator()); } } @@ -124,14 +129,14 @@ public void testParseInteger() throws Exception { { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(BigInteger.TEN, c.getNumerator()); - Assert.assertEquals(BigInteger.ONE, c.getDenominator()); + assertEquals(BigInteger.TEN, c.getNumerator()); + assertEquals(BigInteger.ONE, c.getDenominator()); } { BigFraction c = improperFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(BigInteger.TEN, c.getNumerator()); - Assert.assertEquals(BigInteger.ONE, c.getDenominator()); + assertEquals(BigInteger.TEN, c.getNumerator()); + assertEquals(BigInteger.ONE, c.getDenominator()); } } @@ -141,13 +146,13 @@ public void testParseInvalid() { String msg = "should not be able to parse '10 / a'."; try { properFormat.parse(source); - Assert.fail(msg); + fail(msg); } catch (ParseException ex) { // success } try { improperFormat.parse(source); - Assert.fail(msg); + fail(msg); } catch (ParseException ex) { // success } @@ -159,13 +164,13 @@ public void testParseInvalidDenominator() { String msg = "should not be able to parse '10 / a'."; try { properFormat.parse(source); - Assert.fail(msg); + fail(msg); } catch (ParseException ex) { // success } try { improperFormat.parse(source); - Assert.fail(msg); + fail(msg); } catch (ParseException ex) { // success } @@ -178,24 +183,24 @@ public void testParseNegative() throws Exception { String source = "-1 / 2"; BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(-1, c.getNumeratorAsInt()); - Assert.assertEquals(2, c.getDenominatorAsInt()); + assertEquals(-1, c.getNumeratorAsInt()); + assertEquals(2, c.getDenominatorAsInt()); c = improperFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(-1, c.getNumeratorAsInt()); - Assert.assertEquals(2, c.getDenominatorAsInt()); + assertEquals(-1, c.getNumeratorAsInt()); + assertEquals(2, c.getDenominatorAsInt()); source = "1 / -2"; c = properFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(-1, c.getNumeratorAsInt()); - Assert.assertEquals(2, c.getDenominatorAsInt()); + assertEquals(-1, c.getNumeratorAsInt()); + assertEquals(2, c.getDenominatorAsInt()); c = improperFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(-1, c.getNumeratorAsInt()); - Assert.assertEquals(2, c.getDenominatorAsInt()); + assertEquals(-1, c.getNumeratorAsInt()); + assertEquals(2, c.getDenominatorAsInt()); } } @@ -206,13 +211,13 @@ public void testParseProper() throws Exception { { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(5, c.getNumeratorAsInt()); - Assert.assertEquals(3, c.getDenominatorAsInt()); + assertEquals(5, c.getNumeratorAsInt()); + assertEquals(3, c.getDenominatorAsInt()); } try { improperFormat.parse(source); - Assert.fail("invalid improper fraction."); + fail("invalid improper fraction."); } catch (ParseException ex) { // success } @@ -224,13 +229,13 @@ public void testParseProperNegative() throws Exception { { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); - Assert.assertEquals(-5, c.getNumeratorAsInt()); - Assert.assertEquals(3, c.getDenominatorAsInt()); + assertEquals(-5, c.getNumeratorAsInt()); + assertEquals(3, c.getDenominatorAsInt()); } try { improperFormat.parse(source); - Assert.fail("invalid improper fraction."); + fail("invalid improper fraction."); } catch (ParseException ex) { // success } @@ -241,14 +246,14 @@ public void testParseProperInvalidMinus() { String source = "2 -2 / 3"; try { properFormat.parse(source); - Assert.fail("invalid minus in improper fraction."); + fail("invalid minus in improper fraction."); } catch (ParseException ex) { // expected } source = "2 2 / -3"; try { properFormat.parse(source); - Assert.fail("invalid minus in improper fraction."); + fail("invalid minus in improper fraction."); } catch (ParseException ex) { // expected } @@ -257,20 +262,20 @@ public void testParseProperInvalidMinus() { @Test public void testParseBig() throws Exception { BigFraction f1 = - improperFormat.parse("167213075789791382630275400487886041651764456874403" + - " / " + - "53225575123090058458126718248444563466137046489291"); - Assert.assertEquals(Math.PI, f1.doubleValue(), 0.0); + improperFormat.parse("167213075789791382630275400487886041651764456874403" + + " / " + + "53225575123090058458126718248444563466137046489291"); + assertEquals(Math.PI, f1.doubleValue(), 0.0); BigFraction f2 = - properFormat.parse("3 " + - "7536350420521207255895245742552351253353317406530" + - " / " + - "53225575123090058458126718248444563466137046489291"); - Assert.assertEquals(Math.PI, f2.doubleValue(), 0.0); - Assert.assertEquals(f1, f2); + properFormat.parse("3 " + + "7536350420521207255895245742552351253353317406530" + + " / " + + "53225575123090058458126718248444563466137046489291"); + assertEquals(Math.PI, f2.doubleValue(), 0.0); + assertEquals(f1, f2); BigDecimal pi = - new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"); - Assert.assertEquals(pi, f1.bigDecimalValue(99, BigDecimal.ROUND_HALF_EVEN)); + new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"); + assertEquals(pi, f1.bigDecimalValue(99, BigDecimal.ROUND_HALF_EVEN)); } @Test @@ -279,14 +284,14 @@ public void testNumeratorFormat() { NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); properFormat.setNumeratorFormat(nf); - Assert.assertEquals(nf, properFormat.getNumeratorFormat()); + assertEquals(nf, properFormat.getNumeratorFormat()); properFormat.setNumeratorFormat(old); old = improperFormat.getNumeratorFormat(); nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); improperFormat.setNumeratorFormat(nf); - Assert.assertEquals(nf, improperFormat.getNumeratorFormat()); + assertEquals(nf, improperFormat.getNumeratorFormat()); improperFormat.setNumeratorFormat(old); } @@ -296,36 +301,105 @@ public void testDenominatorFormat() { NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); properFormat.setDenominatorFormat(nf); - Assert.assertEquals(nf, properFormat.getDenominatorFormat()); + assertEquals(nf, properFormat.getDenominatorFormat()); properFormat.setDenominatorFormat(old); old = improperFormat.getDenominatorFormat(); nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); improperFormat.setDenominatorFormat(nf); - Assert.assertEquals(nf, improperFormat.getDenominatorFormat()); + assertEquals(nf, improperFormat.getDenominatorFormat()); improperFormat.setDenominatorFormat(old); } @Test public void testWholeFormat() { - ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat; + ProperBigFractionFormat format = (ProperBigFractionFormat) properFormat; NumberFormat old = format.getWholeFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); format.setWholeFormat(nf); - Assert.assertEquals(nf, format.getWholeFormat()); + assertEquals(nf, format.getWholeFormat()); format.setWholeFormat(old); } @Test public void testLongFormat() { - Assert.assertEquals("10 / 1", improperFormat.format(10l)); + assertEquals("10 / 1", improperFormat.format(10l)); } @Test public void testDoubleFormat() { - Assert.assertEquals("1 / 16", improperFormat.format(0.0625)); + assertEquals("1 / 16", improperFormat.format(0.0625)); + } + + @Test + public void testCreatesBigFractionFormatTakingNoArguments() throws Exception{ + + BigFractionFormat bigFractionFormat = new BigFractionFormat(); + + try { + bigFractionFormat.parseObject("5/%zC*"); + fail("Expecting exception: ParseException"); + } catch (ParseException e) { + assertEquals("Format.parseObject(String) failed", e.getMessage()); + assertEquals(Format.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testFormatAndGetProperInstanceTakingLocale() { + + Locale locale = Locale.ROOT; + BigFractionFormat bigFractionFormat = BigFractionFormat.getProperInstance(locale); + BigFractionFormat bigFractionFormatTwo = new BigFractionFormat(bigFractionFormat); + Byte myByte =new Byte((byte) 5); + StringBuffer stringBuffer = new StringBuffer((byte) 5); + FieldPosition fieldPosition = new FieldPosition((byte) 5); + bigFractionFormatTwo.format( myByte,stringBuffer, fieldPosition); + + assertEquals("5 0 / 1 / 1 0 / 1", stringBuffer.toString()); + assertEquals(17, stringBuffer.length()); + } + + + @Test + public void testFormatBigFraction() { + + BigFraction bigFraction = new BigFraction(0L); + String string = BigFractionFormat.formatBigFraction(bigFraction); + + assertEquals("0 / 1", string); + + } + + + @Test + public void testGetProperInstanceTakingNoArguments() throws ParseException { + + BigFractionFormat bigFractionFormat = BigFractionFormat.getProperInstance(); + BigFraction bigFraction = bigFractionFormat.parse("-704 0 / 1"); + + assertEquals((short) (-704), bigFraction.shortValue()); + + } + + + @Test + public void testFormatBigFractionThrowsIllegalArgumentException() { + + try { + BigFractionFormat.formatBigFraction((BigFraction) null); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("cannot format given object as a fraction number", e.getMessage()); + assertEquals(BigFractionFormat.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } 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 e17eb2a59..0533b1b3f 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 @@ -23,17 +23,20 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + public class BigFractionTest { private void assertFraction(int expectedNumerator, int expectedDenominator, BigFraction actual) { - Assert.assertEquals(expectedNumerator, actual.getNumeratorAsInt()); - Assert.assertEquals(expectedDenominator, actual.getDenominatorAsInt()); + assertEquals(expectedNumerator, actual.getNumeratorAsInt()); + assertEquals(expectedDenominator, actual.getDenominatorAsInt()); } private void assertFraction(long expectedNumerator, long expectedDenominator, BigFraction actual) { - Assert.assertEquals(expectedNumerator, actual.getNumeratorAsLong()); - Assert.assertEquals(expectedDenominator, actual.getDenominatorAsLong()); + assertEquals(expectedNumerator, actual.getNumeratorAsLong()); + assertEquals(expectedDenominator, actual.getDenominatorAsLong()); } @Test @@ -55,38 +58,38 @@ public void testConstructor() { assertFraction(2, 5, new BigFraction(0.40000000000001, 1.0e-5, 100)); assertFraction(15, 1, new BigFraction(15.0000000000001, 1.0e-5, 100)); - Assert.assertEquals(0.00000000000001, new BigFraction(0.00000000000001).doubleValue(), 0.0); - Assert.assertEquals(0.40000000000001, new BigFraction(0.40000000000001).doubleValue(), 0.0); - Assert.assertEquals(15.0000000000001, new BigFraction(15.0000000000001).doubleValue(), 0.0); + assertEquals(0.00000000000001, new BigFraction(0.00000000000001).doubleValue(), 0.0); + assertEquals(0.40000000000001, new BigFraction(0.40000000000001).doubleValue(), 0.0); + assertEquals(15.0000000000001, new BigFraction(15.0000000000001).doubleValue(), 0.0); assertFraction(3602879701896487l, 9007199254740992l, new BigFraction(0.40000000000001)); assertFraction(1055531162664967l, 70368744177664l, new BigFraction(15.0000000000001)); try { new BigFraction(null, BigInteger.ONE); - Assert.fail("Expecting NullPointerException"); + fail("Expecting NullPointerException"); } catch (NullPointerException npe) { // expected } try { new BigFraction(BigInteger.ONE, null); - Assert.fail("Expecting NullPointerException"); + fail("Expecting NullPointerException"); } catch (NullPointerException npe) { // expected } try { new BigFraction(BigInteger.ONE, BigInteger.ZERO); - Assert.fail("Expecting ArithmeticException"); + fail("Expecting ArithmeticException"); } catch (ArithmeticException ignored) { // expected } try { new BigFraction(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000); - Assert.fail("Expecting ArithmeticException"); + fail("Expecting ArithmeticException"); } catch (ArithmeticException ignored) { // expected } } - @Test(expected=FractionException.class) + @Test(expected = FractionException.class) public void testGoldenRatio() { // the golden ratio is notoriously a difficult number for continuous fraction new BigFraction((1 + Math.sqrt(5)) / 2, 1.0e-12, 25); @@ -155,13 +158,13 @@ public void testDigitLimitConstructor() throws Exception { } // MATH-1029 - @Test(expected=ArithmeticException.class) + @Test(expected = ArithmeticException.class) public void testPositiveValueOverflow() { assertFraction((long) 1e10, 1, new BigFraction(1e10, 1000)); } // MATH-1029 - @Test(expected=ArithmeticException.class) + @Test(expected = ArithmeticException.class) public void testNegativeValueOverflow() { assertFraction((long) -1e10, 1, new BigFraction(-1e10, 1000)); } @@ -184,19 +187,19 @@ public void testCompareTo() { BigFraction second = new BigFraction(1, 3); BigFraction third = new BigFraction(1, 2); - Assert.assertEquals(0, first.compareTo(first)); - Assert.assertEquals(0, first.compareTo(third)); - Assert.assertEquals(1, first.compareTo(second)); - Assert.assertEquals(-1, second.compareTo(first)); + assertEquals(0, first.compareTo(first)); + assertEquals(0, first.compareTo(third)); + assertEquals(1, first.compareTo(second)); + 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 BigFraction pi1 = new BigFraction(1068966896, 340262731); - BigFraction pi2 = new BigFraction( 411557987, 131002976); - Assert.assertEquals(-1, pi1.compareTo(pi2)); - Assert.assertEquals( 1, pi2.compareTo(pi1)); - Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); + BigFraction pi2 = new BigFraction(411557987, 131002976); + assertEquals(-1, pi1.compareTo(pi2)); + assertEquals(1, pi2.compareTo(pi1)); + assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); } @@ -205,8 +208,8 @@ public void testDoubleValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(1, 3); - Assert.assertEquals(0.5, first.doubleValue(), 0.0); - Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0); + assertEquals(0.5, first.doubleValue(), 0.0); + assertEquals(1.0 / 3.0, second.doubleValue(), 0.0); } // MATH-744 @@ -216,9 +219,9 @@ public void testDoubleValueForLargeNumeratorAndDenominator() { final BigInteger pow401 = BigInteger.TEN.pow(401); final BigInteger two = new BigInteger("2"); final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE), - pow400.multiply(two)); + pow400.multiply(two)); - Assert.assertEquals(5, large.doubleValue(), 1e-15); + assertEquals(5, large.doubleValue(), 1e-15); } // MATH-744 @@ -228,9 +231,9 @@ public void testFloatValueForLargeNumeratorAndDenominator() { final BigInteger pow401 = BigInteger.TEN.pow(401); final BigInteger two = new BigInteger("2"); final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE), - pow400.multiply(two)); + pow400.multiply(two)); - Assert.assertEquals(5, large.floatValue(), 1e-15); + assertEquals(5, large.floatValue(), 1e-15); } // NUMBERS-15 @@ -239,9 +242,9 @@ public void testDoubleValueForLargeNumeratorAndSmallDenominator() { final BigInteger pow300 = BigInteger.TEN.pow(300); final BigInteger pow330 = BigInteger.TEN.pow(330); final BigFraction large = new BigFraction(pow330.add(BigInteger.ONE), - pow300); + pow300); - Assert.assertEquals(1e30, large.doubleValue(), 1e-15); + assertEquals(1e30, large.doubleValue(), 1e-15); } // NUMBERS-15 @@ -252,7 +255,7 @@ public void testFloatValueForLargeNumeratorAndSmallDenominator() { final BigFraction large = new BigFraction(pow40.add(BigInteger.ONE), pow30); - Assert.assertEquals(1e10f, large.floatValue(), 1e-15); + assertEquals(1e10f, large.floatValue(), 1e-15); } @Test @@ -260,8 +263,8 @@ public void testFloatValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(1, 3); - Assert.assertEquals(0.5f, first.floatValue(), 0.0f); - Assert.assertEquals((float) (1.0 / 3.0), second.floatValue(), 0.0f); + assertEquals(0.5f, first.floatValue(), 0.0f); + assertEquals((float) (1.0 / 3.0), second.floatValue(), 0.0f); } @Test @@ -269,8 +272,8 @@ public void testIntValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(3, 2); - Assert.assertEquals(0, first.intValue()); - Assert.assertEquals(1, second.intValue()); + assertEquals(0, first.intValue()); + assertEquals(1, second.intValue()); } @Test @@ -278,8 +281,8 @@ public void testLongValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(3, 2); - Assert.assertEquals(0L, first.longValue()); - Assert.assertEquals(1L, second.longValue()); + assertEquals(0L, first.longValue()); + assertEquals(1L, second.longValue()); } @Test @@ -292,17 +295,17 @@ public void testConstructorDouble() { assertFraction(-6004799503160661l, 18014398509481984l, new BigFraction(-1.0 / 3.0)); assertFraction(-6124895493223875l, 36028797018963968l, new BigFraction(17.0 / -100.0)); assertFraction(-1784551352345559l, 562949953421312l, new BigFraction(-317.0 / 100.0)); - for (double v : new double[] { Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) { + for (double v : new double[]{Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) { try { new BigFraction(v); - Assert.fail("Expecting IllegalArgumentException"); + fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException iae) { // expected } } - Assert.assertEquals(1l, new BigFraction(Double.MAX_VALUE).getDenominatorAsLong()); - Assert.assertEquals(1l, new BigFraction(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong()); - Assert.assertEquals(1l, new BigFraction(Double.MIN_VALUE).getNumeratorAsLong()); + assertEquals(1l, new BigFraction(Double.MAX_VALUE).getDenominatorAsLong()); + assertEquals(1l, new BigFraction(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong()); + assertEquals(1l, new BigFraction(Double.MIN_VALUE).getNumeratorAsLong()); } @Test @@ -322,31 +325,31 @@ public void testReciprocal() { f = new BigFraction(50, 75); f = f.reciprocal(); - Assert.assertEquals(3, f.getNumeratorAsInt()); - Assert.assertEquals(2, f.getDenominatorAsInt()); + assertEquals(3, f.getNumeratorAsInt()); + assertEquals(2, f.getDenominatorAsInt()); f = new BigFraction(4, 3); f = f.reciprocal(); - Assert.assertEquals(3, f.getNumeratorAsInt()); - Assert.assertEquals(4, f.getDenominatorAsInt()); + assertEquals(3, f.getNumeratorAsInt()); + assertEquals(4, f.getDenominatorAsInt()); f = new BigFraction(-15, 47); f = f.reciprocal(); - Assert.assertEquals(-47, f.getNumeratorAsInt()); - Assert.assertEquals(15, f.getDenominatorAsInt()); + assertEquals(-47, f.getNumeratorAsInt()); + assertEquals(15, f.getDenominatorAsInt()); f = new BigFraction(0, 3); try { f = f.reciprocal(); - Assert.fail("expecting ArithmeticException"); + fail("expecting ArithmeticException"); } catch (ArithmeticException ignored) { } // large values f = new BigFraction(Integer.MAX_VALUE, 1); f = f.reciprocal(); - Assert.assertEquals(1, f.getNumeratorAsInt()); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); + assertEquals(1, f.getNumeratorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); } @Test @@ -355,19 +358,19 @@ public void testNegate() { f = new BigFraction(50, 75); f = f.negate(); - Assert.assertEquals(-2, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); + assertEquals(-2, f.getNumeratorAsInt()); + assertEquals(3, f.getDenominatorAsInt()); f = new BigFraction(-50, 75); f = f.negate(); - Assert.assertEquals(2, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); + assertEquals(2, f.getNumeratorAsInt()); + assertEquals(3, f.getDenominatorAsInt()); // large values f = new BigFraction(Integer.MAX_VALUE - 1, Integer.MAX_VALUE); f = f.negate(); - Assert.assertEquals(Integer.MIN_VALUE + 2, f.getNumeratorAsInt()); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE + 2, f.getNumeratorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); } @@ -384,18 +387,18 @@ public void testAdd() { BigFraction f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); BigFraction f2 = BigFraction.ONE; BigFraction f = f1.add(f2); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(-1, 13 * 13 * 2 * 2); f2 = new BigFraction(-2, 13 * 17 * 2); f = f1.add(f2); - Assert.assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominatorAsInt()); - Assert.assertEquals(-17 - 2 * 13 * 2, f.getNumeratorAsInt()); + assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominatorAsInt()); + assertEquals(-17 - 2 * 13 * 2, f.getNumeratorAsInt()); try { f.add((BigFraction) null); - Assert.fail("expecting NullPointerException"); + fail("expecting NullPointerException"); } catch (NullPointerException ex) { } @@ -404,41 +407,41 @@ public void testAdd() { f1 = new BigFraction(1, 32768 * 3); f2 = new BigFraction(1, 59049); f = f1.add(f2); - Assert.assertEquals(52451, f.getNumeratorAsInt()); - Assert.assertEquals(1934917632, f.getDenominatorAsInt()); + assertEquals(52451, f.getNumeratorAsInt()); + assertEquals(1934917632, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, 3); f2 = new BigFraction(1, 3); f = f1.add(f2); - Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); + assertEquals(3, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); f = f1.add(BigInteger.ONE); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f = f.add(BigInteger.ZERO); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); f = f1.add(1); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f = f.add(0); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); f = f1.add(1l); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f = f.add(0l); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); } @@ -456,7 +459,7 @@ public void testDivide() { BigFraction f2 = BigFraction.ZERO; try { f1.divide(f2); - Assert.fail("expecting ArithmeticException"); + fail("expecting ArithmeticException"); } catch (ArithmeticException ex) { } @@ -468,40 +471,40 @@ public void testDivide() { f1 = new BigFraction(2, 7); f2 = BigFraction.ONE; f = f1.divide(f2); - Assert.assertEquals(2, f.getNumeratorAsInt()); - Assert.assertEquals(7, f.getDenominatorAsInt()); + assertEquals(2, f.getNumeratorAsInt()); + assertEquals(7, f.getDenominatorAsInt()); f1 = new BigFraction(1, Integer.MAX_VALUE); f = f1.divide(f1); - Assert.assertEquals(1, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(1, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f2 = new BigFraction(1, Integer.MAX_VALUE); f = f1.divide(f2); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); try { f.divide((BigFraction) null); - Assert.fail("expecting NullPointerException"); + fail("expecting NullPointerException"); } catch (NullPointerException ex) { } f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide(BigInteger.valueOf(Integer.MIN_VALUE)); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - Assert.assertEquals(1, f.getNumeratorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); + assertEquals(1, f.getNumeratorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide(Integer.MIN_VALUE); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - Assert.assertEquals(1, f.getNumeratorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); + assertEquals(1, f.getNumeratorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide((long) Integer.MIN_VALUE); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - Assert.assertEquals(1, f.getNumeratorAsInt()); + assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); + assertEquals(1, f.getNumeratorAsInt()); } @@ -518,20 +521,20 @@ public void testMultiply() { BigFraction f1 = new BigFraction(Integer.MAX_VALUE, 1); BigFraction f2 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); BigFraction f = f1.multiply(f2); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f = f2.multiply(Integer.MAX_VALUE); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); f = f2.multiply((long) Integer.MAX_VALUE); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); try { f.multiply((BigFraction) null); - Assert.fail("expecting NullPointerException"); + fail("expecting NullPointerException"); } catch (NullPointerException ex) { } @@ -550,7 +553,7 @@ public void testSubtract() { BigFraction f = new BigFraction(1, 1); try { f.subtract((BigFraction) null); - Assert.fail("expecting NullPointerException"); + fail("expecting NullPointerException"); } catch (NullPointerException ex) { } @@ -559,29 +562,29 @@ public void testSubtract() { BigFraction f1 = new BigFraction(1, 32768 * 3); BigFraction f2 = new BigFraction(1, 59049); f = f1.subtract(f2); - Assert.assertEquals(-13085, f.getNumeratorAsInt()); - Assert.assertEquals(1934917632, f.getDenominatorAsInt()); + assertEquals(-13085, f.getNumeratorAsInt()); + assertEquals(1934917632, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, 3); f2 = new BigFraction(1, 3).negate(); f = f1.subtract(f2); - Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); + assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); + assertEquals(3, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE, 1); f2 = BigFraction.ONE; f = f1.subtract(f2); - Assert.assertEquals(Integer.MAX_VALUE - 1, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); + assertEquals(Integer.MAX_VALUE - 1, f.getNumeratorAsInt()); + assertEquals(1, f.getDenominatorAsInt()); } @Test public void testBigDecimalValue() { - Assert.assertEquals(new BigDecimal(0.5), new BigFraction(1, 2).bigDecimalValue()); - Assert.assertEquals(new BigDecimal("0.0003"), new BigFraction(3, 10000).bigDecimalValue()); - Assert.assertEquals(new BigDecimal("0"), new BigFraction(1, 3).bigDecimalValue(BigDecimal.ROUND_DOWN)); - Assert.assertEquals(new BigDecimal("0.333"), new BigFraction(1, 3).bigDecimalValue(3, BigDecimal.ROUND_DOWN)); + assertEquals(new BigDecimal(0.5), new BigFraction(1, 2).bigDecimalValue()); + assertEquals(new BigDecimal("0.0003"), new BigFraction(3, 10000).bigDecimalValue()); + assertEquals(new BigDecimal("0"), new BigFraction(1, 3).bigDecimalValue(BigDecimal.ROUND_DOWN)); + assertEquals(new BigDecimal("0.333"), new BigFraction(1, 3).bigDecimalValue(3, BigDecimal.ROUND_DOWN)); } @Test @@ -593,7 +596,7 @@ public void testEqualsAndHashCode() { Assert.assertFalse(zero.equals(Double.valueOf(0))); BigFraction zero2 = new BigFraction(0, 2); Assert.assertTrue(zero.equals(zero2)); - Assert.assertEquals(zero.hashCode(), zero2.hashCode()); + assertEquals(zero.hashCode(), zero2.hashCode()); BigFraction one = new BigFraction(1, 1); Assert.assertFalse((one.equals(zero) || zero.equals(one))); Assert.assertTrue(one.equals(BigFraction.ONE)); @@ -606,30 +609,30 @@ public void testGetReducedFraction() { Assert.assertTrue(BigFraction.ZERO.equals(BigFraction.getReducedFraction(0, -1))); try { BigFraction.getReducedFraction(1, 0); - Assert.fail("expecting ArithmeticException"); + fail("expecting ArithmeticException"); } catch (ArithmeticException ex) { // expected } - Assert.assertEquals(BigFraction.getReducedFraction(2, Integer.MIN_VALUE).getNumeratorAsInt(), -1); - Assert.assertEquals(BigFraction.getReducedFraction(1, -1).getNumeratorAsInt(), -1); + assertEquals(BigFraction.getReducedFraction(2, Integer.MIN_VALUE).getNumeratorAsInt(), -1); + assertEquals(BigFraction.getReducedFraction(1, -1).getNumeratorAsInt(), -1); } @Test public void testPercentage() { - Assert.assertEquals(50.0, new BigFraction(1, 2).percentageValue(), 1.0e-15); + assertEquals(50.0, new BigFraction(1, 2).percentageValue(), 1.0e-15); } @Test public void testPow() { - Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13)); - Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13l)); - Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(BigInteger.valueOf(13l))); - Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0)); - Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0l)); - Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(BigInteger.valueOf(0l))); - Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13)); - Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13l)); - Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(BigInteger.valueOf(-13l))); + assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13)); + assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13l)); + assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(BigInteger.valueOf(13l))); + assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0)); + assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0l)); + assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(BigInteger.valueOf(0l))); + assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13)); + assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13l)); + assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(BigInteger.valueOf(-13l))); } @Test @@ -638,19 +641,113 @@ public void testMath340() { BigFraction fractionB = new BigFraction(.37).reciprocal(); BigFraction errorResult = fractionA.multiply(fractionB); BigFraction correctResult = new BigFraction(fractionA.getNumerator().multiply(fractionB.getNumerator()), - fractionA.getDenominator().multiply(fractionB.getDenominator())); - Assert.assertEquals(correctResult, errorResult); + fractionA.getDenominator().multiply(fractionB.getDenominator())); + assertEquals(correctResult, errorResult); } @Test public void testSerial() { BigFraction[] fractions = { - new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO, - new BigFraction(17), new BigFraction(Math.PI, 1000), - new BigFraction(-5, 2) + new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO, + new BigFraction(17), new BigFraction(Math.PI, 1000), + new BigFraction(-5, 2) }; for (BigFraction fraction : fractions) { - Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); + assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); + } + } + + @Test + public void testAddTakingBigIntegerThrowsNullPointerException() { + + BigFraction bigFractionOne = new BigFraction((-742.12673), 642.94391, (-3522)); + BigFraction bigFractionTwo = bigFractionOne.multiply((long) (-244)); + + try { + bigFractionTwo.THREE_FIFTHS.add((BigInteger) null); + fail("Expecting exception: NullPointerException"); + } catch (NullPointerException e) { + assertEquals("bg", e.getMessage()); + assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testReciprocalReturningBigFractionWhereShortValueIsZero() { + + BigFraction bigFraction = new BigFraction(342L).reciprocal(); + + + try { + bigFraction.bigDecimalValue(100, 629); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Invalid rounding mode", e.getMessage()); + assertEquals(BigDecimal.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testDivideTakingIntThrowsFractionException() { + + BigFraction bigFraction = new BigFraction(105L); + BigFraction bigFractionThree = bigFraction.negate(); + bigFractionThree.abs(); + + try { + bigFractionThree.divide(0); + fail("Expecting exception: FractionException"); + } catch (FractionException e) { + assertEquals("denominator must be different from 0", e.getMessage()); + assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testFailsToCreateBigFractionTakingFourArgumentsThrowsFractionException() { + + try { + new BigFraction(2634.663520270196, (-1.0), 3757); + fail("Expecting exception: FractionException"); + } catch (FractionException e) { + assertEquals("Overflow trying to convert 2,634.664 to fraction (23,470,894,701/8,908,498)", e.getMessage()); + assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testFailsToCreateBigFractionTakingThreeArgumentsThrowsFractionException() { + + try { + new BigFraction(0.0, 1); + fail("Expecting exception: FractionException"); + } catch (FractionException e) { + assertEquals("Overflow trying to convert 0 to fraction (1/9,223,372,036,854,775,807)", e.getMessage()); + assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); } + } + + + @Test + public void testBigDecimalValueTakingNoArgumentsThrowsArithmeticException() { + + BigFraction bigFraction = new BigFraction((-1379.78857054451), 3464); + + try { + bigFraction.bigDecimalValue(); + fail("Expecting exception: ArithmeticException"); + } catch (ArithmeticException e) { + assertEquals("Non-terminating decimal expansion; no exact representable decimal result.", e.getMessage()); + assertEquals(BigDecimal.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java index 1cb3d6d6c..a3ac1e550 100644 --- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java @@ -25,6 +25,9 @@ import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + public class FractionFormatTest { @@ -347,4 +350,40 @@ public void testLongFormat() { public void testDoubleFormat() { Assert.assertEquals("355 / 113", improperFormat.format(Math.PI)); } + + @Test + public void testParseTakingString() throws ParseException { + + FractionFormat fractionFormat = FractionFormat.getProperInstance(); + Fraction fraction = fractionFormat.parse("0 / 1"); + + assertEquals((byte) 0, fraction.byteValue()); + + } + + + @Test + public void testFormatFractionThrowsIllegalArgumentException() { + + try { + FractionFormat.formatFraction((Fraction) null); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("cannot format given object as a fraction number", e.getMessage()); + assertEquals(FractionFormat.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testFormatFraction() { + + Fraction fraction = new Fraction(0); + String string = FractionFormat.formatFraction(fraction); + + assertEquals("0 / 1", string); + + } + } 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 d35092b38..b53eeafba 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 @@ -20,6 +20,10 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + /** */ @@ -621,4 +625,163 @@ public void testSerial() { Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); } } + + @Test + public void testGetReducedFractionWithNegativeThrowsFractionException() { + + try { + Fraction.getReducedFraction(Integer.MIN_VALUE, (-2198)); + fail("Expecting exception: FractionException"); + } catch (FractionException e) { + assertEquals("overflow in fraction -2,147,483,648/-2,198, cannot negate", e.getMessage()); + assertEquals(Fraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testGetReducedFractionWithPositiveThrowsFractionException() { + + try { + Fraction.getReducedFraction(63, Integer.MIN_VALUE); + fail("Expecting exception: FractionException"); + } catch (FractionException e) { + assertEquals("overflow in fraction 63/-2,147,483,648, cannot negate", e.getMessage()); + assertEquals(Fraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testSubtractTakingFractionReturningFractionWhereShortValueIsZero() { + + Fraction fraction = new Fraction(0); + + assertEquals(0.0, fraction.doubleValue(), 0.01); + assertEquals(0, fraction.intValue()); + + assertEquals(0L, fraction.longValue()); + assertEquals((byte) 0, fraction.byteValue()); + + assertEquals(0, fraction.getNumerator()); + assertEquals((short) 0, fraction.shortValue()); + + assertEquals(0.0, fraction.percentageValue(), 0.01); + assertEquals(0.0F, fraction.floatValue(), 0.01F); + + assertEquals(1, fraction.getDenominator()); + + Fraction fractionTwo = fraction.subtract(fraction); + + assertTrue(fractionTwo.equals(fraction)); + assertEquals(0.0, fraction.doubleValue(), 0.01); + + assertEquals(0, fraction.intValue()); + assertEquals(0L, fraction.longValue()); + + assertEquals((byte) 0, fraction.byteValue()); + assertEquals(0, fraction.getNumerator()); + + assertEquals((short) 0, fraction.shortValue()); + assertEquals(0.0, fraction.percentageValue(), 0.01); + + assertEquals(0.0F, fraction.floatValue(), 0.01F); + assertEquals(1, fraction.getDenominator()); + + assertEquals(0, fractionTwo.intValue()); + assertEquals(0L, fractionTwo.longValue()); + + assertEquals(1, fractionTwo.getDenominator()); + assertEquals(0.0, fractionTwo.percentageValue(), 0.01); + + assertEquals((short) 0, fractionTwo.shortValue()); + assertEquals(0.0, fractionTwo.doubleValue(), 0.01); + + assertEquals(0, fractionTwo.getNumerator()); + assertEquals(0.0F, fractionTwo.floatValue(), 0.01F); + + assertEquals((byte) 0, fractionTwo.byteValue()); + + } + + + @Test + public void testAddTakingFractionReturningFractionWhereShortValueIsZero() { + + Fraction fraction = new Fraction(0.0); + + assertEquals(1, fraction.getDenominator()); + assertEquals(0, fraction.intValue()); + + assertEquals(0L, fraction.longValue()); + assertEquals(0, fraction.getNumerator()); + + assertEquals((short) 0, fraction.shortValue()); + assertEquals(0.0F, fraction.floatValue(), 0.01F); + + assertEquals((byte) 0, fraction.byteValue()); + assertEquals(0.0, fraction.doubleValue(), 0.01); + + assertEquals(0.0, fraction.percentageValue(), 0.01); + + Fraction fractionTwo = fraction.add(fraction); + + assertEquals(1, fraction.getDenominator()); + assertEquals(0, fraction.intValue()); + + assertEquals(0L, fraction.longValue()); + assertEquals(0, fraction.getNumerator()); + + assertEquals((short) 0, fraction.shortValue()); + assertEquals(0.0F, fraction.floatValue(), 0.01F); + + assertEquals((byte) 0, fraction.byteValue()); + assertEquals(0.0, fraction.doubleValue(), 0.01); + + assertEquals(0.0, fraction.percentageValue(), 0.01); + assertEquals(0.0, fractionTwo.percentageValue(), 0.01); + + assertEquals(0, fractionTwo.getNumerator()); + assertEquals((short) 0, fractionTwo.shortValue()); + + assertEquals(1, fractionTwo.getDenominator()); + assertEquals(0.0F, fractionTwo.floatValue(), 0.01F); + + assertEquals((byte) 0, fractionTwo.byteValue()); + assertEquals(0L, fractionTwo.longValue()); + + assertEquals(0.0, fractionTwo.doubleValue(), 0.01); + assertEquals(0, fractionTwo.intValue()); + + } + + + @Test + public void testFailsToCreateFractionTakingTwoArgumentsThrowsFractionException() { + + try { + new Fraction(0.0, 1); + fail("Expecting exception: FractionException"); + } catch (FractionException e) { + assertEquals("Unable to convert 0 to fraction after 1 iterations", e.getMessage()); + assertEquals(Fraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + + @Test + public void testDivideTakingInt() { + + Fraction fraction = new Fraction(1, 1); + Fraction fractionTwo = fraction.ONE_HALF.divide(5); + + assertFalse(fraction.equals(fractionTwo)); + assertEquals(0.1, fractionTwo.doubleValue(), 0.01); + + assertFalse(fractionTwo.equals(fraction)); + + } + } diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java index 17b459aae..0b24d594e 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/DigammaTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Tests for {@link Digamma}. */ @@ -26,18 +28,18 @@ public class DigammaTest { @Test public void testDigammaLargeArgs() { double eps = 1e-8; - Assert.assertEquals(4.6001618527380874002, Digamma.value(100), eps); - Assert.assertEquals(3.9019896734278921970, Digamma.value(50), eps); - Assert.assertEquals(2.9705239922421490509, Digamma.value(20), eps); - Assert.assertEquals(2.9958363947076465821, Digamma.value(20.5), eps); - Assert.assertEquals(2.2622143570941481605, Digamma.value(10.1), eps); - Assert.assertEquals(2.1168588189004379233, Digamma.value(8.8), eps); - Assert.assertEquals(1.8727843350984671394, Digamma.value(7), eps); - Assert.assertEquals(0.42278433509846713939, Digamma.value(2), eps); - Assert.assertEquals(-100.56088545786867450, Digamma.value(0.01), eps); - Assert.assertEquals(-4.0390398965921882955, Digamma.value(-0.8), eps); - Assert.assertEquals(4.2003210041401844726, Digamma.value(-6.3), eps); - Assert.assertEquals(-3.110625123035E-5, Digamma.value(1.4616), eps); + assertEquals(4.6001618527380874002, Digamma.value(100), eps); + assertEquals(3.9019896734278921970, Digamma.value(50), eps); + assertEquals(2.9705239922421490509, Digamma.value(20), eps); + assertEquals(2.9958363947076465821, Digamma.value(20.5), eps); + assertEquals(2.2622143570941481605, Digamma.value(10.1), eps); + assertEquals(2.1168588189004379233, Digamma.value(8.8), eps); + assertEquals(1.8727843350984671394, Digamma.value(7), eps); + assertEquals(0.42278433509846713939, Digamma.value(2), eps); + assertEquals(-100.56088545786867450, Digamma.value(0.01), eps); + assertEquals(-4.0390398965921882955, Digamma.value(-0.8), eps); + assertEquals(4.2003210041401844726, Digamma.value(-6.3), eps); + assertEquals(-3.110625123035E-5, Digamma.value(1.4616), eps); } @Test @@ -66,7 +68,14 @@ private void checkRelativeError(String msg, double expected, double actual, double tolerance) { - Assert.assertEquals(msg, expected, actual, Math.abs(tolerance * actual)); + assertEquals(msg, expected, actual, Math.abs(tolerance * actual)); } -} + @Test + public void testValueWithZero() { + + assertEquals(Double.NEGATIVE_INFINITY, Digamma.value(0.0), 0.01); + + } + +} diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedBetaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedBetaTest.java index 9f1c3512f..3e79aa852 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedBetaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/RegularizedBetaTest.java @@ -16,9 +16,13 @@ */ package org.apache.commons.numbers.gamma; +import org.apache.commons.numbers.fraction.ContinuedFraction; import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * Tests for {@link RegularizedBeta}. */ @@ -77,7 +81,7 @@ public void testRegularizedBetaPositivePositivePositive() { public void testRegularizedBetaTinyArgument() { double actual = RegularizedBeta.value(1e-17, 1.0, 1e12); // This value is from R: pbeta(1e-17,1,1e12) - Assert.assertEquals(9.999950000166648e-6, actual, 1e-16); + assertEquals(9.999950000166648e-6, actual, 1e-16); } @Test @@ -89,7 +93,7 @@ public void testMath1067() { try { RegularizedBeta.value(x, a, b, 1e-14, 10000); } catch (StackOverflowError error) { - Assert.fail("Infinite recursion"); + fail("Infinite recursion"); } } @@ -98,6 +102,50 @@ private void testRegularizedBeta(double expected, double a, double b) { final double actual = RegularizedBeta.value(x, a, b); - Assert.assertEquals(expected, actual, 1e-15); + assertEquals(expected, actual, 1e-15); + } + + @Test + public void testValueTaking5ArgumentsThrowsArithmeticException() { + + try { + RegularizedBeta.value(0.9972492333291466, 3668.4488, 9.795436556336103, Double.NaN, 387); + fail("Expecting exception: ArithmeticException"); + } catch (ArithmeticException e) { + assertEquals("maximal count (387) exceeded", e.getMessage()); + assertEquals(ContinuedFraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + + @Test + public void testValueTakingFourArgumentsWithPositiveAndPositive() { + + assertEquals(Double.NaN, RegularizedBeta.value(2.0, 2.0, 1280.57004088342),0.01); + + } + + + @Test + public void testCreatesRegularizedBeta() { + + RegularizedBeta regularizedBeta = new RegularizedBeta(); + + } + + + @Test + public void testValueTakingFourArgumentsThrowsArithmeticException() { + + try { + RegularizedBeta.value(0.6946630178187274, 1582.7220438354161, Double.POSITIVE_INFINITY); + fail("Expecting exception: ArithmeticException"); + } catch (ArithmeticException e) { + assertEquals("Continued fraction diverged to NaN for value 0.695", e.getMessage()); + assertEquals(ContinuedFraction.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java index 50caf0b6f..907521c8e 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Tests for {@link Trigamma}. */ @@ -45,7 +47,7 @@ public void testTrigamma() { 100, 0.010050166663333571395 }; for (int i = data.length - 2; i >= 0; i -= 2) { - Assert.assertEquals(String.format("trigamma %.0f", data[i]), data[i + 1], Trigamma.value(data[i]), eps); + assertEquals(String.format("trigamma %.0f", data[i]), data[i + 1], Trigamma.value(data[i]), eps); } } @@ -55,5 +57,19 @@ public void testTrigammaNonRealArgs() { Assert.assertTrue(Double.isInfinite(Trigamma.value(Double.POSITIVE_INFINITY))); Assert.assertTrue(Double.isInfinite(Trigamma.value(Double.NEGATIVE_INFINITY))); } -} + @Test + public void testValueReturningPositive() { + + assertEquals(9.999999999999998E9, Trigamma.value(1.0E-5),0.01); + + } + + @Test + public void testCreatesTrigamma() { + + Trigamma trigamma = new Trigamma(); + + } + +} diff --git a/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/SmallPrimesTest.java b/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/SmallPrimesTest.java index 4aa784b86..a7ce759a5 100644 --- a/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/SmallPrimesTest.java +++ b/commons-numbers-primes/src/test/java/org/apache/commons/numbers/primes/SmallPrimesTest.java @@ -16,13 +16,14 @@ */ package org.apache.commons.numbers.primes; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; + import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + public class SmallPrimesTest { // Primes larger than the small PRIMES array in SmallPrimes @@ -76,7 +77,7 @@ public void smallTrialDivision_noSmallPrimeFactors() { Assert.assertEquals(LARGE_PRIME[0]*LARGE_PRIME[1], result); Assert.assertEquals(Collections.emptyList(), factors); } - + @Test public void boundedTrialDivision_twoDifferentFactors() { final List factors = new ArrayList(); @@ -140,4 +141,20 @@ public void millerRabinPrimeTest_composites() { } } } + + @Test + public void testMillerRabinPrimeTestReturningFalse() { + + assertFalse(SmallPrimes.millerRabinPrimeTest(1105)); + + } + + + @Test + public void testBoundedTrialDivisionReturningPositive() { + + assertEquals(1, SmallPrimes.boundedTrialDivision(5021, 5021, new LinkedList())); + + } + } diff --git a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java index b96a0b4ff..56ea60cd9 100644 --- a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java +++ b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java @@ -22,6 +22,9 @@ import org.junit.Test; import org.junit.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + public class QuaternionTest { /** Epsilon for double comparison. */ private static final double EPS = Math.ulp(1d); @@ -36,10 +39,10 @@ public final void testAccessors1() { final double q3 = 0.0005; final Quaternion q = new Quaternion(q0, q1, q2, q3); - Assert.assertEquals(q0, q.getQ0(), 0); - Assert.assertEquals(q1, q.getQ1(), 0); - Assert.assertEquals(q2, q.getQ2(), 0); - Assert.assertEquals(q3, q.getQ3(), 0); + assertEquals(q0, q.getQ0(), 0); + assertEquals(q1, q.getQ1(), 0); + assertEquals(q2, q.getQ2(), 0); + assertEquals(q3, q.getQ3(), 0); } @Test @@ -53,10 +56,10 @@ public final void testAccessors2() { final double sP = q.getScalarPart(); final double[] vP = q.getVectorPart(); - Assert.assertEquals(q0, sP, 0); - Assert.assertEquals(q1, vP[0], 0); - Assert.assertEquals(q2, vP[1], 0); - Assert.assertEquals(q3, vP[2], 0); + assertEquals(q0, sP, 0); + assertEquals(q1, vP[0], 0); + assertEquals(q2, vP[1], 0); + assertEquals(q3, vP[2], 0); } @Test @@ -70,10 +73,10 @@ public final void testAccessors3() { final double sP = q.getScalarPart(); final double[] vP = q.getVectorPart(); - Assert.assertEquals(q0, sP, 0); - Assert.assertEquals(q1, vP[0], 0); - Assert.assertEquals(q2, vP[1], 0); - Assert.assertEquals(q3, vP[2], 0); + assertEquals(q0, sP, 0); + assertEquals(q1, vP[0], 0); + assertEquals(q2, vP[1], 0); + assertEquals(q3, vP[2], 0); } @Test(expected=IllegalArgumentException.class) @@ -91,10 +94,10 @@ public final void testConjugate() { final Quaternion qConjugate = q.getConjugate(); - Assert.assertEquals(q0, qConjugate.getQ0(), 0); - Assert.assertEquals(-q1, qConjugate.getQ1(), 0); - Assert.assertEquals(-q2, qConjugate.getQ2(), 0); - Assert.assertEquals(-q3, qConjugate.getQ3(), 0); + assertEquals(q0, qConjugate.getQ0(), 0); + assertEquals(-q1, qConjugate.getQ1(), 0); + assertEquals(-q2, qConjugate.getQ2(), 0); + assertEquals(-q3, qConjugate.getQ3(), 0); } /* TODO remove dependency on Vector3D @@ -205,8 +208,8 @@ public final void testDotProductQuaternionQuaternion() { final double actual1 = Quaternion.dotProduct(q1, q2); final double actual2 = q1.dotProduct(q2); - Assert.assertEquals(expected, actual1, EPS); - Assert.assertEquals(expected, actual2, EPS); + assertEquals(expected, actual1, EPS); + assertEquals(expected, actual2, EPS); } @Test @@ -222,10 +225,10 @@ public final void testScalarMultiplyDouble() { final Quaternion q = q1.multiply(a); - Assert.assertEquals(w, q.getQ0(), COMPARISON_EPS); - Assert.assertEquals(x, q.getQ1(), COMPARISON_EPS); - Assert.assertEquals(y, q.getQ2(), COMPARISON_EPS); - Assert.assertEquals(z, q.getQ3(), COMPARISON_EPS); + assertEquals(w, q.getQ0(), COMPARISON_EPS); + assertEquals(x, q.getQ1(), COMPARISON_EPS); + assertEquals(y, q.getQ2(), COMPARISON_EPS); + assertEquals(z, q.getQ3(), COMPARISON_EPS); } @Test @@ -242,15 +245,15 @@ public final void testAddQuaternionQuaternion() { final Quaternion qa = Quaternion.add(q1, q2); final Quaternion qb = q1.add(q2); - Assert.assertEquals(w, qa.getQ0(), EPS); - Assert.assertEquals(x, qa.getQ1(), EPS); - Assert.assertEquals(y, qa.getQ2(), EPS); - Assert.assertEquals(z, qa.getQ3(), EPS); + assertEquals(w, qa.getQ0(), EPS); + assertEquals(x, qa.getQ1(), EPS); + assertEquals(y, qa.getQ2(), EPS); + assertEquals(z, qa.getQ3(), EPS); - Assert.assertEquals(w, qb.getQ0(), EPS); - Assert.assertEquals(x, qb.getQ1(), EPS); - Assert.assertEquals(y, qb.getQ2(), EPS); - Assert.assertEquals(z, qb.getQ3(), EPS); + assertEquals(w, qb.getQ0(), EPS); + assertEquals(x, qb.getQ1(), EPS); + assertEquals(y, qb.getQ2(), EPS); + assertEquals(z, qb.getQ3(), EPS); } @Test @@ -267,15 +270,15 @@ public final void testSubtractQuaternionQuaternion() { final Quaternion qa = Quaternion.subtract(q1, q2); final Quaternion qb = q1.subtract(q2); - Assert.assertEquals(w, qa.getQ0(), EPS); - Assert.assertEquals(x, qa.getQ1(), EPS); - Assert.assertEquals(y, qa.getQ2(), EPS); - Assert.assertEquals(z, qa.getQ3(), EPS); + assertEquals(w, qa.getQ0(), EPS); + assertEquals(x, qa.getQ1(), EPS); + assertEquals(y, qa.getQ2(), EPS); + assertEquals(z, qa.getQ3(), EPS); - Assert.assertEquals(w, qb.getQ0(), EPS); - Assert.assertEquals(x, qb.getQ1(), EPS); - Assert.assertEquals(y, qb.getQ2(), EPS); - Assert.assertEquals(z, qb.getQ3(), EPS); + assertEquals(w, qb.getQ0(), EPS); + assertEquals(x, qb.getQ1(), EPS); + assertEquals(y, qb.getQ2(), EPS); + assertEquals(z, qb.getQ3(), EPS); } @Test @@ -289,25 +292,25 @@ public final void testNorm() { final double norm = q.getNorm(); - Assert.assertEquals(Math.sqrt(30), norm, 0); + assertEquals(Math.sqrt(30), norm, 0); final double normSquareRef = Quaternion.multiply(q, q.getConjugate()).getScalarPart(); - Assert.assertEquals(Math.sqrt(normSquareRef), norm, 0); + assertEquals(Math.sqrt(normSquareRef), norm, 0); } @Test - public final void testNormalize() { + public final void testNormalizeOne() { final Quaternion q = new Quaternion(2, 1, -4, -2); final Quaternion versor = q.normalize(); - Assert.assertEquals(2.0 / 5.0, versor.getQ0(), 0); - Assert.assertEquals(1.0 / 5.0, versor.getQ1(), 0); - Assert.assertEquals(-4.0 / 5.0, versor.getQ2(), 0); - Assert.assertEquals(-2.0 / 5.0, versor.getQ3(), 0); + assertEquals(2.0 / 5.0, versor.getQ0(), 0); + assertEquals(1.0 / 5.0, versor.getQ1(), 0); + assertEquals(-4.0 / 5.0, versor.getQ2(), 0); + assertEquals(-2.0 / 5.0, versor.getQ3(), 0); - Assert.assertEquals(1, versor.getNorm(), 0); + assertEquals(1, versor.getNorm(), 0); } @Test(expected=IllegalStateException.class) @@ -326,7 +329,7 @@ public final void testObjectEquals() { Assert.assertTrue(q2.equals(q1)); final Quaternion q3 = new Quaternion(one, Math.nextUp(one), one, one); - Assert.assertFalse(q3.equals(q1)); + assertFalse(q3.equals(q1)); } @Test @@ -338,10 +341,10 @@ public final void testQuaternionEquals() { final Quaternion q4 = new Quaternion(q1.getQ0(), q1.getQ1(), q1.getQ2() + inc, q1.getQ3()); final Quaternion q5 = new Quaternion(q1.getQ0(), q1.getQ1(), q1.getQ2(), q1.getQ3() + inc); - Assert.assertFalse(q1.equals(q2, 0.9 * inc)); - Assert.assertFalse(q1.equals(q3, 0.9 * inc)); - Assert.assertFalse(q1.equals(q4, 0.9 * inc)); - Assert.assertFalse(q1.equals(q5, 0.9 * inc)); + assertFalse(q1.equals(q2, 0.9 * inc)); + assertFalse(q1.equals(q3, 0.9 * inc)); + assertFalse(q1.equals(q4, 0.9 * inc)); + assertFalse(q1.equals(q5, 0.9 * inc)); Assert.assertTrue(q1.equals(q2, 1.1 * inc)); Assert.assertTrue(q1.equals(q3, 1.1 * inc)); @@ -356,8 +359,8 @@ public final void testQuaternionEquals2() { final Quaternion q2 = new Quaternion(1 + gap, 4 + gap, 2 + gap, 3 + gap); Assert.assertTrue(q1.equals(q2, 10 * gap)); - Assert.assertFalse(q1.equals(q2, gap)); - Assert.assertFalse(q1.equals(q2, gap / 10)); + assertFalse(q1.equals(q2, gap)); + assertFalse(q1.equals(q2, gap / 10)); } @Test @@ -371,7 +374,7 @@ public final void testIsUnitQuaternion() { } final Quaternion q = new Quaternion(1, 1, 1, 1); - Assert.assertFalse(q.isUnitQuaternion(COMPARISON_EPS)); + assertFalse(q.isUnitQuaternion(COMPARISON_EPS)); } @Test @@ -383,7 +386,7 @@ public final void testIsPureQuaternion() { Assert.assertTrue(q2.isPureQuaternion(EPS)); final Quaternion q3 = new Quaternion(0 - 1.1 * EPS, 5, 4, 8); - Assert.assertFalse(q3.isPureQuaternion(EPS)); + assertFalse(q3.isPureQuaternion(EPS)); final Random r = new Random(48); final double[] v = {r.nextDouble(), r.nextDouble(), r.nextDouble()}; @@ -428,18 +431,21 @@ public final void testGetInverse() { final Quaternion q = new Quaternion(1.5, 4, 2, -2.5); final Quaternion inverseQ = q.getInverse(); - Assert.assertEquals(1.5 / 28.5, inverseQ.getQ0(), 0); - Assert.assertEquals(-4.0 / 28.5, inverseQ.getQ1(), 0); - Assert.assertEquals(-2.0 / 28.5, inverseQ.getQ2(), 0); - Assert.assertEquals(2.5 / 28.5, inverseQ.getQ3(), 0); + + assertEquals(1.5 / 28.5, inverseQ.getQ0(), 0); + assertEquals(-4.0 / 28.5, inverseQ.getQ1(), 0); + assertEquals(-2.0 / 28.5, inverseQ.getQ2(), 0); + assertEquals(2.5 / 28.5, inverseQ.getQ3(), 0); final Quaternion product = Quaternion.multiply(inverseQ, q); - Assert.assertEquals(1, product.getQ0(), EPS); - Assert.assertEquals(0, product.getQ1(), EPS); - Assert.assertEquals(0, product.getQ2(), EPS); - Assert.assertEquals(0, product.getQ3(), EPS); + + assertEquals(1, product.getQ0(), EPS); + assertEquals(0, product.getQ1(), EPS); + assertEquals(0, product.getQ2(), EPS); + assertEquals(0, product.getQ3(), EPS); final Quaternion qNul = new Quaternion(0, 0, 0, 0); + try { final Quaternion inverseQNul = qNul.getInverse(); Assert.fail("expecting ZeroException but got : " + inverseQNul); @@ -453,4 +459,127 @@ public final void testToString() { final Quaternion q = new Quaternion(1, 2, 3, 4); Assert.assertTrue(q.toString().equals("[1.0 2.0 3.0 4.0]")); } + + @Test + public void testGetPositivePolarForm() { + + Quaternion quaternion = new Quaternion((-1432.6277148663683), (-1432.6277148663683), 0.0, 1049.5395); + + quaternion = quaternion.getPositivePolarForm(); + + assertEquals( 0.6278639114966613, quaternion.getQ0(), 0.01 ); + assertEquals( 0.6278639114966613, quaternion.getQ1(), 0.01 ); + assertEquals( 0.0, quaternion.getQ2(), 0.01 ); + assertEquals( -0.45997153964155796, quaternion.getQ3(), 0.01 ); + + } + + + @Test + public void testMultiplyTakingDouble() { + + Quaternion quaternion = new Quaternion(452.324, (-63.3504), (-63.3504), Double.POSITIVE_INFINITY); + Quaternion quaternionTwo = Quaternion.subtract(quaternion, quaternion); + Quaternion quaternionThree = quaternionTwo.ZERO.multiply((-63.3504)); + + assertEquals(-0.0, quaternionThree.getQ1(), 0.01); + assertEquals((-63.3504), quaternion.getQ2(), 0.01); + + assertFalse(quaternionThree.equals(quaternionTwo)); + assertEquals(-0.0, quaternionThree.getQ3(), 0.01); + + assertEquals((-63.3504), quaternion.getQ1(), 0.01); + assertEquals(-0.0, quaternionThree.getQ0(), 0.01); + + assertEquals(-0.0, quaternionThree.getQ2(), 0.01); + assertEquals(0.0, quaternionTwo.getQ0(), 0.01); + + assertEquals(0.0, quaternionTwo.getQ1(), 0.01); + assertEquals(0.0, quaternionTwo.getQ2(), 0.01); + + assertEquals(0.0, quaternionThree.getNorm(), 0.01); + assertEquals(Double.NaN, quaternionTwo.getNorm(), 0.01); + + assertFalse(quaternionTwo.equals( quaternionThree)); + + } + + + @Test + public void testNormalizeTwo() { + + Quaternion quaternion = new Quaternion(452.324, (-63.3504), (-63.3504), Double.POSITIVE_INFINITY); + Quaternion quaternionTwo = Quaternion.subtract(quaternion, quaternion); + Quaternion quaternionThree = quaternion.J.normalize(); + + assertEquals(1.0, quaternionThree.getQ2(), 0.01); + assertEquals(Double.POSITIVE_INFINITY, quaternion.getNorm(), 0.01); + + assertEquals(0.0, quaternionThree.getQ1(), 0.01); + assertFalse(quaternionTwo.equals( quaternionThree)); + + assertEquals(0.0, quaternionThree.getQ0(), 0.01); + assertEquals(0.0, quaternionTwo.getQ2(), 0.01); + + assertEquals(0.0, quaternionTwo.getQ1(), 0.01); + assertEquals(0.0, quaternionThree.getQ3(), 0.01); + + assertEquals((-63.3504), quaternion.getQ1(), 0.01); + assertEquals(1.0, quaternionThree.getNorm(), 0.01); + + assertEquals(0.0, quaternionTwo.getScalarPart(), 0.01); + assertEquals(Double.NaN, quaternionTwo.getNorm(), 0.01); + + } + + + @Test + public void testCreatesQuaternionTaking4ArgumentsAndCallsSubtractTakingThreeArguments() { + + Quaternion quaternion = new Quaternion(452.324, (-63.3504), (-63.3504), Double.POSITIVE_INFINITY); + Quaternion quaternionTwo = Quaternion.subtract(quaternion, quaternion); + + assertEquals((-63.3504), quaternion.getQ1(), 0.01); + assertEquals((-63.3504), quaternion.getQ2(), 0.01); + + assertEquals(452.324, quaternion.getScalarPart(), 0.01); + assertFalse(quaternionTwo.equals( quaternion)); + + assertEquals(0.0, quaternionTwo.getQ1(), 0.01); + assertEquals(0.0, quaternionTwo.getQ2(), 0.01); + + assertEquals(Double.NaN, quaternionTwo.getNorm(), 0.01); + assertEquals(Double.POSITIVE_INFINITY, quaternion.getQ3(), 0.01); + + assertEquals(0.0, quaternionTwo.getQ0(), 0.01); + + } + + + @Test + public void testCreatesQuaternionTakingDoubleArray() { + + double[] doubleArray = new double[3]; + Quaternion quaternion = new Quaternion(doubleArray); + Object object = new Object(); + + assertFalse(quaternion.equals(object)); + + } + + + @Test + public void testMultiplyTakingQuaternion() { + + Quaternion quaternion = new Quaternion((-1.0), 115.491766, 115.491766, 115.491766); + Quaternion quaternionTwo = quaternion.J.multiply(quaternion); + + assertEquals((-1.0), quaternionTwo.getQ2(), 0.01); + assertEquals((-115.491766), quaternionTwo.getQ3(), 0.01); + + assertEquals(200.04010608224607, quaternionTwo.getNorm(), 0.01); + + } + + } From b553e21fdf97047907be2cd7f2945fabbd68bb89 Mon Sep 17 00:00:00 2001 From: Michael Hausegger Date: Wed, 12 Jul 2017 19:16:30 +0200 Subject: [PATCH 2/3] Add-some-Unit-Tests Removed evil empty lines. --- .../numbers/arrays/LinearCombinationTest.java | 2 -- .../commons/numbers/arrays/SafeNormTest.java | 2 -- .../BinomialCoefficientTest.java | 2 -- .../combinatorics/FactorialDoubleTest.java | 5 ---- .../combinatorics/LogFactorialTest.java | 2 -- .../commons/numbers/complex/ComplexTest.java | 6 ---- .../numbers/complex/ComplexUtilsTest.java | 24 --------------- .../commons/numbers/core/PrecisionTest.java | 30 ------------------- .../fraction/BigFractionFormatTest.java | 10 ------- .../numbers/fraction/BigFractionTest.java | 13 -------- 10 files changed, 96 deletions(-) diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java index aca3e9cb8..c193b0d7c 100644 --- a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java +++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java @@ -300,7 +300,6 @@ public void testInfinite() { @Test public void testValueTakingThreeArgumentsThrowsIllegalArgumentException() { - double[] doubleArray = new double[4]; double[] doubleArrayTwo = new double[0]; @@ -311,7 +310,6 @@ public void testValueTakingThreeArgumentsThrowsIllegalArgumentException() { assertEquals("Dimension mismatch: 4 != 0", e.getMessage()); assertEquals(LinearCombination.class.getName(), e.getStackTrace()[0].getClassName()); } - } } diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java index 842b033f2..3f161e80b 100644 --- a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java +++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java @@ -64,14 +64,12 @@ public void testSimple() { @Test public void testValueReturningPositive() { - double[] doubleArray = new double[5]; doubleArray[0] = 3.834E-20; doubleArray[1] = 5.8798224E-39; double doubleValue = SafeNorm.value(doubleArray); assertEquals(3.834E-20, doubleValue, 0.01); - } } diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java index 639f49533..24c1995b9 100644 --- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java +++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java @@ -201,7 +201,6 @@ static long binomialCoefficient(int n, int k) { @Test public void testCheckBinomialThrowsCombinatoricsException() { - try { BinomialCoefficient.checkBinomial(66, (-2802)); fail("Expecting exception: CombinatoricsException"); @@ -209,7 +208,6 @@ public void testCheckBinomialThrowsCombinatoricsException() { assertEquals("Number -2,802 is out of range [0, 66]", e.getMessage()); assertEquals(BinomialCoefficient.class.getName(), e.getStackTrace()[0].getClassName()); } - } } diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java index 8253c39b2..b9f3c78b6 100644 --- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java +++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/FactorialDoubleTest.java @@ -124,7 +124,6 @@ public void testCacheDecrease() { * Direct multiplication implementation. */ private double factorialDirect(int n) { - double result = 1; for (int i = 2; i <= n; i++) { @@ -132,13 +131,10 @@ private double factorialDirect(int n) { } return result; - - } @Test public void testWithCacheThrowsCombinatoricsException() { - FactorialDouble factorialDouble = FactorialDouble.create(); try { @@ -148,7 +144,6 @@ public void testWithCacheThrowsCombinatoricsException() { assertEquals("Number -2,194 is negative",e.getMessage()); assertEquals(FactorialDouble.class.getName(), e.getStackTrace()[0].getClassName()); } - } } diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java index 1c8f60eff..f410fac6b 100644 --- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java +++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/LogFactorialTest.java @@ -119,7 +119,6 @@ private double logFactorial(final int n) { @Test public void testValueThrowsCombinatoricsException() { - LogFactorial logFactorial = LogFactorial.create(); try { @@ -129,7 +128,6 @@ public void testValueThrowsCombinatoricsException() { assertEquals("Number -482 is negative", e.getMessage()); assertEquals(LogFactorial.class.getName(), e.getStackTrace()[0].getClassName()); } - } } diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java index 3633b4297..98a990d81 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java @@ -1491,7 +1491,6 @@ protected TestComplex createComplex(double real, double imaginary){ @Test public void testAddTakingDoubleReturningComplexWhereGetRealIsNegativeAndExpReturningComplexWhereIsNaNIsTrue() { - Complex complex = new Complex(0.0, 0.0); try { @@ -1501,13 +1500,11 @@ public void testAddTakingDoubleReturningComplexWhereGetRealIsNegativeAndExpRetur assertEquals("cannot compute nth root for null or negative n: {0}",e.getMessage()); assertEquals(Complex.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testDivideTakingComplexThrowsRuntimeException() { - Complex complex = new Complex(2732.59210279); try { @@ -1517,12 +1514,10 @@ public void testDivideTakingComplexThrowsRuntimeException() { assertEquals("Null Argument to Complex Method",e.getMessage()); assertEquals(Complex.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testSubtractTakingComplex() { - Complex complex = new Complex(0.0, 0.0); Complex complexTwo = complex.INF.sqrt(); Complex complexThree = complex.asin(); @@ -1531,7 +1526,6 @@ public void testSubtractTakingComplex() { complexThree.multiply(complex); assertFalse( Complex.equals(complex, complexFour, 0) ); - } } diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java index 91f36e371..79caf3380 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java @@ -478,7 +478,6 @@ public void testInitialize() { @Test public void testcomplex2InterleavedFloatTakingTwoAndTwoWithPositive() { - Complex[][] complexArray = new Complex[0][9]; try { @@ -488,12 +487,10 @@ public void testcomplex2InterleavedFloatTakingTwoAndTwoWithPositive() { assertEquals("Out of range: 2", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testcomplex2ImaginaryTakingComplexArrayArrayArrayThrowsNullPointerException() { - Complex[][][] complexArray = new Complex[1][2][5]; complexArray[0] = new Complex[7][1]; @@ -503,13 +500,11 @@ public void testcomplex2ImaginaryTakingComplexArrayArrayArrayThrowsNullPointerEx } catch (NullPointerException e) { assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testcomplex2InterleavedFloatTakingTwoAndTwoWithEmptyArrayAndNegative() { - Complex[][][] complexArray = new Complex[0][7][3]; try { @@ -519,13 +514,11 @@ public void testcomplex2InterleavedFloatTakingTwoAndTwoWithEmptyArrayAndNegative assertEquals("Out of range: -1", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testInterleavedThreeComplexTakingTwoAndTwoWithEmptyArray() { - float[][][] floatArray = new float[0][5][8]; try { @@ -535,13 +528,11 @@ public void testInterleavedThreeComplexTakingTwoAndTwoWithEmptyArray() { assertEquals("Out of range: 334", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testExtractComplexFromInterleavedArrayTakingTwoAndTwoThrowsArrayIndexOutOfBoundsException() { - double[] doubleArray = new double[5]; doubleArray[0] = 0.0; doubleArray[1] = 3553.05; @@ -553,13 +544,11 @@ public void testExtractComplexFromInterleavedArrayTakingTwoAndTwoThrowsArrayInde assertEquals("3330572", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testInterleavedThrowsIllegalArgumentException() { - try { ComplexUtils.interleaved2Complex((float[][][]) null, (-2501)); fail("Expecting exception: IllegalArgumentException"); @@ -567,13 +556,11 @@ public void testInterleavedThrowsIllegalArgumentException() { assertEquals("Out of range: -2501", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testPolarThreeComplexTakingThreeDoubleArraysThrowsIllegalArgumentException() { - double[] doubleArray = new double[8]; doubleArray[0] = 682.369; doubleArray[1] = 612.16397804415; @@ -589,12 +576,10 @@ public void testPolarThreeComplexTakingThreeDoubleArraysThrowsIllegalArgumentExc assertEquals("Modulus is negative: -252.151672108609", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testcomplex2InterleavedFloatTakingTwoAndTwoWithNegative() { - Complex[][] complexArray = new Complex[0][8]; try { @@ -604,12 +589,10 @@ public void testcomplex2InterleavedFloatTakingTwoAndTwoWithNegative() { assertEquals("Out of range: -3231", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testInterleavedThreeComplexTakingTwoAndTwoWithEmptyArrayAndNegative() { - float[][] floatArray = new float[0][3]; try { @@ -619,12 +602,10 @@ public void testInterleavedThreeComplexTakingTwoAndTwoWithEmptyArrayAndNegative( assertEquals("Out of range: -4026", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testInterleavedThreeComplexTakingTwoAndTwoWithNullAndPositive() { - try { ComplexUtils.interleaved2Complex((float[][]) null, 2146551443); fail("Expecting exception: IllegalArgumentException"); @@ -632,13 +613,11 @@ public void testInterleavedThreeComplexTakingTwoAndTwoWithNullAndPositive() { assertEquals("Out of range: 2146551443", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testcomplex2ImaginaryTakingComplexArrayArray() { - double[] doubleArray = new double[4]; doubleArray[1] = 2005.05225; doubleArray[3] = 2005.05225; @@ -656,13 +635,11 @@ public void testcomplex2ImaginaryTakingComplexArrayArray() { assertEquals("Out of range: -2984", e.getMessage()); assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test //I consider this to be a defect. public void testAbsThrowsNullPointerException() { - float[] floatArray = new float[0]; Complex[] complexArray = ComplexUtils.interleaved2Complex(floatArray); Complex[][] complexArrayTwo = new Complex[5][8]; @@ -677,7 +654,6 @@ public void testAbsThrowsNullPointerException() { } catch (NullPointerException e) { assertEquals(ComplexUtils.class.getName(), e.getStackTrace()[0].getClassName()); } - } } diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java index f550cd7e3..15ddb602b 100644 --- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java +++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/PrecisionTest.java @@ -550,121 +550,91 @@ public void testMath1127() { @Test public void testEqualsIncludingNaNTakingThreeDoublesAndTwoWithNegativeAndPositiveOne() { - assertFalse(Precision.equalsIncludingNaN((-2367.8), (double) Float.NaN, (-1776))); - } @Test public void testEqualsIncludingNaNTakingThreeDoublesAndTwoWithNegativeAndPositiveTwo() { - assertFalse(Precision.equalsIncludingNaN(Double.NaN, (-2122.27), (-779))); - } @Test public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithNegative() { - assertTrue(Precision.equalsIncludingNaN(Float.NaN, Float.NaN, (-2541))); - } @Test public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithZeroAndEqualsIncludingNaNTakingThreeFloatsAndTwoReturningFalse() { - assertFalse(Precision.equalsIncludingNaN((float) 0, Float.NaN, 2243)); - } @Test public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithNegativeAndZero() { - assertFalse(Precision.equalsIncludingNaN(Float.NaN, (-1.0F), 0)); - } @Test public void testEqualsIncludingNaNTakingThreeFloatsAndTwoWithZeroAndEqualsIncludingNaNTakingThreeFloatsAndTwoReturningTrue() { - assertTrue(Precision.equalsIncludingNaN((float) 1566, -0.0F, 2143768278)); - } @Test public void testEqualsTakingThreeFloatsAndTwoWithNegativeAndPositiveTwo() { - assertFalse(Precision.equals((float) 6, (-1861.78F), 1150126653)); - } @Test public void testEqualsIncludingNaNTakingFourFloatsOne() { - assertTrue(Precision.equalsIncludingNaN(2220.4014F, (float) 1, 2220.4014F)); - } @Test public void testEqualsTakingFourFloatsWithZero() { - assertTrue(Precision.equals(696.2F, 0.0F, 696.2F)); - } @Test public void testEqualsTakingFourFloatsReturningFalse() { - assertFalse(Precision.equals((float) (-1685), 668.0F, (-2701.3188F))); - } @Test public void testEqualsTakingFourFloats() { - assertTrue(Precision.equals(414.5782F, 414.5782F, 414.5782F)); - } @Test public void testEqualsIncludingNaNTakingFourFloatsReturningFalse() { - assertFalse(Precision.equalsIncludingNaN(Float.NaN, 3036.304F, 1.0F)); - } @Test public void testEqualsIncludingNaNTakingThreeFloats() { - assertFalse(Precision.equalsIncludingNaN(1573.7026F, Float.NaN)); - } @Test public void testEqualsIncludingNaNTakingFourFloatsTwo() { - assertTrue(Precision.equalsIncludingNaN(Float.NaN, Float.NaN, 1.0F)); - } @Test public void testEqualsTakingThreeDoubles() { - assertFalse(Precision.equals((double) 0.0F, (double) Float.NaN)); - } } diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java index 84304e94a..8d879d241 100644 --- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java @@ -336,7 +336,6 @@ public void testDoubleFormat() { @Test public void testCreatesBigFractionFormatTakingNoArguments() throws Exception{ - BigFractionFormat bigFractionFormat = new BigFractionFormat(); try { @@ -346,13 +345,11 @@ public void testCreatesBigFractionFormatTakingNoArguments() throws Exception{ assertEquals("Format.parseObject(String) failed", e.getMessage()); assertEquals(Format.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testFormatAndGetProperInstanceTakingLocale() { - Locale locale = Locale.ROOT; BigFractionFormat bigFractionFormat = BigFractionFormat.getProperInstance(locale); BigFractionFormat bigFractionFormatTwo = new BigFractionFormat(bigFractionFormat); @@ -363,35 +360,29 @@ public void testFormatAndGetProperInstanceTakingLocale() { assertEquals("5 0 / 1 / 1 0 / 1", stringBuffer.toString()); assertEquals(17, stringBuffer.length()); - } @Test public void testFormatBigFraction() { - BigFraction bigFraction = new BigFraction(0L); String string = BigFractionFormat.formatBigFraction(bigFraction); assertEquals("0 / 1", string); - } @Test public void testGetProperInstanceTakingNoArguments() throws ParseException { - BigFractionFormat bigFractionFormat = BigFractionFormat.getProperInstance(); BigFraction bigFraction = bigFractionFormat.parse("-704 0 / 1"); assertEquals((short) (-704), bigFraction.shortValue()); - } @Test public void testFormatBigFractionThrowsIllegalArgumentException() { - try { BigFractionFormat.formatBigFraction((BigFraction) null); fail("Expecting exception: IllegalArgumentException"); @@ -399,7 +390,6 @@ public void testFormatBigFractionThrowsIllegalArgumentException() { assertEquals("cannot format given object as a fraction number", e.getMessage()); assertEquals(BigFractionFormat.class.getName(), e.getStackTrace()[0].getClassName()); } - } } 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 0533b1b3f..5ad5700c9 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 @@ -659,7 +659,6 @@ public void testSerial() { @Test public void testAddTakingBigIntegerThrowsNullPointerException() { - BigFraction bigFractionOne = new BigFraction((-742.12673), 642.94391, (-3522)); BigFraction bigFractionTwo = bigFractionOne.multiply((long) (-244)); @@ -670,15 +669,12 @@ public void testAddTakingBigIntegerThrowsNullPointerException() { assertEquals("bg", e.getMessage()); assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testReciprocalReturningBigFractionWhereShortValueIsZero() { - BigFraction bigFraction = new BigFraction(342L).reciprocal(); - try { bigFraction.bigDecimalValue(100, 629); fail("Expecting exception: IllegalArgumentException"); @@ -686,12 +682,10 @@ public void testReciprocalReturningBigFractionWhereShortValueIsZero() { assertEquals("Invalid rounding mode", e.getMessage()); assertEquals(BigDecimal.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testDivideTakingIntThrowsFractionException() { - BigFraction bigFraction = new BigFraction(105L); BigFraction bigFractionThree = bigFraction.negate(); bigFractionThree.abs(); @@ -703,13 +697,11 @@ public void testDivideTakingIntThrowsFractionException() { assertEquals("denominator must be different from 0", e.getMessage()); assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testFailsToCreateBigFractionTakingFourArgumentsThrowsFractionException() { - try { new BigFraction(2634.663520270196, (-1.0), 3757); fail("Expecting exception: FractionException"); @@ -717,13 +709,11 @@ public void testFailsToCreateBigFractionTakingFourArgumentsThrowsFractionExcepti assertEquals("Overflow trying to convert 2,634.664 to fraction (23,470,894,701/8,908,498)", e.getMessage()); assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testFailsToCreateBigFractionTakingThreeArgumentsThrowsFractionException() { - try { new BigFraction(0.0, 1); fail("Expecting exception: FractionException"); @@ -731,13 +721,11 @@ public void testFailsToCreateBigFractionTakingThreeArgumentsThrowsFractionExcept assertEquals("Overflow trying to convert 0 to fraction (1/9,223,372,036,854,775,807)", e.getMessage()); assertEquals(BigFraction.class.getName(), e.getStackTrace()[0].getClassName()); } - } @Test public void testBigDecimalValueTakingNoArgumentsThrowsArithmeticException() { - BigFraction bigFraction = new BigFraction((-1379.78857054451), 3464); try { @@ -747,7 +735,6 @@ public void testBigDecimalValueTakingNoArgumentsThrowsArithmeticException() { assertEquals("Non-terminating decimal expansion; no exact representable decimal result.", e.getMessage()); assertEquals(BigDecimal.class.getName(), e.getStackTrace()[0].getClassName()); } - } } From ff0124f345663e421801ca63b001e0e55dd00b37 Mon Sep 17 00:00:00 2001 From: Michael Hausegger Date: Wed, 20 Sep 2017 21:25:24 +0200 Subject: [PATCH 3/3] small-code-quality-improvements Made small code quality improvements. --- .../commons/numbers/complex/Complex.java | 2 +- .../commons/numbers/complex/ComplexUtils.java | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java index 4e9022ee3..cc72456e4 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java @@ -1304,6 +1304,6 @@ private static boolean equals(double x, double y) { * @return the hash code */ private static int hash(double value) { - return new Double(value).hashCode(); + return Double.valueOf(value).hashCode(); } } diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java index 1275b2779..6b3134638 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java @@ -293,7 +293,7 @@ public static float[] extractInterleavedFloatFromComplexArray(Complex[] complex, */ public static Complex[] real2Complex(double[] real) { int index = 0; - final Complex c[] = new Complex[real.length]; + final Complex[] c = new Complex[real.length]; for (double d : real) { c[index] = new Complex(d); index++; @@ -311,7 +311,7 @@ public static Complex[] real2Complex(double[] real) { */ public static Complex[] real2Complex(float[] real) { int index = 0; - final Complex c[] = new Complex[real.length]; + final Complex[] c = new Complex[real.length]; for (float d : real) { c[index] = new Complex(d); index++; @@ -366,7 +366,7 @@ public static Complex[][][] real2Complex(double[][][] d) { */ public static double[] complex2Real(Complex[] c) { int index = 0; - final double d[] = new double[c.length]; + final double[] d = new double[c.length]; for (Complex cc : c) { d[index] = cc.getReal(); index++; @@ -385,7 +385,7 @@ public static double[] complex2Real(Complex[] c) { */ public static float[] complex2RealFloat(Complex[] c) { int index = 0; - final float f[] = new float[c.length]; + final float[] f = new float[c.length]; for (Complex cc : c) { f[index] = (float) cc.getReal(); index++; @@ -473,7 +473,7 @@ public static float[][][] complex2RealFloat(Complex[][][] c) { */ public static Complex[] imaginary2Complex(double[] imaginary) { int index = 0; - final Complex c[] = new Complex[imaginary.length]; + final Complex[] c = new Complex[imaginary.length]; for (double d : imaginary) { c[index] = new Complex(0, d); index++; @@ -491,7 +491,7 @@ public static Complex[] imaginary2Complex(double[] imaginary) { */ public static Complex[] imaginary2Complex(float[] imaginary) { int index = 0; - final Complex c[] = new Complex[imaginary.length]; + final Complex[] c = new Complex[imaginary.length]; for (float d : imaginary) { c[index] = new Complex(0, d); index++; @@ -551,7 +551,7 @@ public static Complex[][][] imaginary2Complex(double[][][] d) { */ public static double[] complex2Imaginary(Complex[] c) { int index = 0; - final double d[] = new double[c.length]; + final double[] d = new double[c.length]; for (Complex cc : c) { d[index] = cc.getImaginary(); index++; @@ -570,7 +570,7 @@ public static double[] complex2Imaginary(Complex[] c) { */ public static float[] complex2ImaginaryFloat(Complex[] c) { int index = 0; - final float f[] = new float[c.length]; + final float[] f = new float[c.length]; for (Complex cc : c) { f[index] = (float) cc.getImaginary(); index++; @@ -660,7 +660,7 @@ public static float[][][] complex2ImaginaryFloat(Complex[][][] c) { */ public static Complex[] interleaved2Complex(double[] interleaved) { final int length = interleaved.length / 2; - final Complex c[] = new Complex[length]; + final Complex[] c = new Complex[length]; for (int n = 0; n < length; n++) { c[n] = new Complex(interleaved[n * 2], interleaved[n * 2 + 1]); } @@ -678,7 +678,7 @@ public static Complex[] interleaved2Complex(double[] interleaved) { */ public static Complex[] interleaved2Complex(float[] interleaved) { final int length = interleaved.length / 2; - final Complex c[] = new Complex[length]; + final Complex[] c = new Complex[length]; for (int n = 0; n < length; n++) { c[n] = new Complex(interleaved[n * 2], interleaved[n * 2 + 1]); } @@ -697,7 +697,7 @@ public static Complex[] interleaved2Complex(float[] interleaved) { */ public static double[] complex2Interleaved(Complex[] c) { int index = 0; - final double d[] = new double[c.length * 2]; + final double[] d = new double[c.length * 2]; for (Complex cc : c) { int real = index * 2; int imag = index * 2 + 1; @@ -720,7 +720,7 @@ public static double[] complex2Interleaved(Complex[] c) { */ public static float[] complex2InterleavedFloat(Complex[] c) { int index = 0; - final float f[] = new float[c.length * 2]; + final float[] f = new float[c.length * 2]; for (Complex cc : c) { int real = index * 2; int imag = index * 2 + 1;