diff options
Diffstat (limited to 'guava-tests/test/com/google/common/math/DoubleMathTest.java')
-rw-r--r-- | guava-tests/test/com/google/common/math/DoubleMathTest.java | 145 |
1 files changed, 8 insertions, 137 deletions
diff --git a/guava-tests/test/com/google/common/math/DoubleMathTest.java b/guava-tests/test/com/google/common/math/DoubleMathTest.java index b2f9418..9fe6d90 100644 --- a/guava-tests/test/com/google/common/math/DoubleMathTest.java +++ b/guava-tests/test/com/google/common/math/DoubleMathTest.java @@ -16,11 +16,9 @@ package com.google.common.math; -import static com.google.common.math.MathTesting.*; import static com.google.common.math.MathTesting.ALL_DOUBLE_CANDIDATES; import static com.google.common.math.MathTesting.ALL_ROUNDING_MODES; import static com.google.common.math.MathTesting.ALL_SAFE_ROUNDING_MODES; -import static com.google.common.math.MathTesting.FINITE_DOUBLE_CANDIDATES; import static com.google.common.math.MathTesting.FRACTIONAL_DOUBLE_CANDIDATES; import static com.google.common.math.MathTesting.INTEGRAL_DOUBLE_CANDIDATES; import static com.google.common.math.MathTesting.NEGATIVE_INTEGER_CANDIDATES; @@ -35,9 +33,6 @@ import static java.math.RoundingMode.UNNECESSARY; import static java.math.RoundingMode.UP; import static java.util.Arrays.asList; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.primitives.Doubles; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; @@ -46,7 +41,6 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.Arrays; -import java.util.List; /** * Tests for {@code DoubleMath}. @@ -61,7 +55,7 @@ public class DoubleMathTest extends TestCase { private static final BigDecimal MAX_LONG_AS_BIG_DECIMAL = BigDecimal.valueOf(Long.MAX_VALUE); private static final BigDecimal MIN_LONG_AS_BIG_DECIMAL = BigDecimal.valueOf(Long.MIN_VALUE); - public void testConstantsMaxFactorial() { + public void testConstantsMaxFactorial(){ BigInteger MAX_DOUBLE_VALUE = BigDecimal.valueOf(Double.MAX_VALUE).toBigInteger(); assertTrue(BigIntegerMath.factorial(DoubleMath.MAX_FACTORIAL).compareTo(MAX_DOUBLE_VALUE) <= 0); assertTrue( @@ -71,10 +65,10 @@ public class DoubleMathTest extends TestCase { public void testConstantsEverySixteenthFactorial() { for (int i = 0, n = 0; n <= DoubleMath.MAX_FACTORIAL; i++, n += 16) { assertEquals( - BigIntegerMath.factorial(n).doubleValue(), DoubleMath.everySixteenthFactorial[i]); + BigIntegerMath.factorial(n).doubleValue(), DoubleMath.EVERY_SIXTEENTH_FACTORIAL[i]); } } - + public void testRoundIntegralDoubleToInt() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { @@ -356,19 +350,6 @@ public class DoubleMathTest extends TestCase { } } - public void testRoundLog2Exact() { - for (double x : POSITIVE_FINITE_DOUBLE_CANDIDATES) { - boolean isPowerOfTwo = StrictMath.pow(2.0, DoubleMath.log2(x, FLOOR)) == x; - try { - int log2 = DoubleMath.log2(x, UNNECESSARY); - assertEquals(x, Math.scalb(1.0, log2)); - assertTrue(isPowerOfTwo); - } catch (ArithmeticException e) { - assertFalse(isPowerOfTwo); - } - } - } - public void testRoundLog2ThrowsOnZerosInfinitiesAndNaN() { for (RoundingMode mode : ALL_ROUNDING_MODES) { for (double d : @@ -414,12 +395,12 @@ public class DoubleMathTest extends TestCase { } } - public void testLog2SemiMonotonic() { + public void testLog2SemiMonotonic(){ for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) { assertTrue(DoubleMath.log2(d + 0.01) >= DoubleMath.log2(d)); } } - + public void testLog2Negative() { for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) { assertTrue(Double.isNaN(DoubleMath.log2(-d))); @@ -494,120 +475,10 @@ public class DoubleMathTest extends TestCase { } catch (IllegalArgumentException expected) {} } } - - private static final ImmutableList<Double> FINITE_TOLERANCE_CANDIDATES = - ImmutableList.of(-0.0, 0.0, 1.0, 100.0, 10000.0, Double.MAX_VALUE); - - private static final Iterable<Double> TOLERANCE_CANDIDATES = - Iterables.concat(FINITE_TOLERANCE_CANDIDATES, ImmutableList.of(Double.POSITIVE_INFINITY)); - - private static final List<Double> BAD_TOLERANCE_CANDIDATES = - Doubles.asList(-Double.MIN_VALUE, -Double.MIN_NORMAL, -1, -20, Double.NaN, - Double.NEGATIVE_INFINITY, -0.001); - - public void testFuzzyEqualsFinite() { - for (double a : FINITE_DOUBLE_CANDIDATES) { - for (double b : FINITE_DOUBLE_CANDIDATES) { - for (double tolerance : FINITE_TOLERANCE_CANDIDATES) { - assertEquals( - Math.abs(a - b) <= tolerance, - DoubleMath.fuzzyEquals(a, b, tolerance)); - } - } - } - } - - public void testFuzzyInfiniteVersusFiniteWithFiniteTolerance() { - for (double inf : INFINITIES) { - for (double a : FINITE_DOUBLE_CANDIDATES) { - for (double tolerance : FINITE_TOLERANCE_CANDIDATES) { - assertFalse(DoubleMath.fuzzyEquals(a, inf, tolerance)); - assertFalse(DoubleMath.fuzzyEquals(inf, a, tolerance)); - } - } - } - } - - public void testFuzzyInfiniteVersusInfiniteWithFiniteTolerance() { - for (double inf : INFINITIES) { - for (double tolerance : FINITE_TOLERANCE_CANDIDATES) { - assertTrue(DoubleMath.fuzzyEquals(inf, inf, tolerance)); - assertFalse(DoubleMath.fuzzyEquals(inf, -inf, tolerance)); - } - } - } - - public void testFuzzyEqualsInfiniteTolerance() { - for (double a : DOUBLE_CANDIDATES_EXCEPT_NAN) { - for (double b : DOUBLE_CANDIDATES_EXCEPT_NAN) { - assertTrue(DoubleMath.fuzzyEquals(a, b, Double.POSITIVE_INFINITY)); - } - } - } - - public void testFuzzyEqualsOneNaN() { - for (double a : DOUBLE_CANDIDATES_EXCEPT_NAN) { - for (double tolerance : TOLERANCE_CANDIDATES) { - assertFalse(DoubleMath.fuzzyEquals(a, Double.NaN, tolerance)); - assertFalse(DoubleMath.fuzzyEquals(Double.NaN, a, tolerance)); - } - } - } - - public void testFuzzyEqualsTwoNaNs() { - for (double tolerance : TOLERANCE_CANDIDATES) { - assertTrue(DoubleMath.fuzzyEquals(Double.NaN, Double.NaN, tolerance)); - } - } - - public void testFuzzyEqualsZeroTolerance() { - // make sure we test -0 tolerance - for (double zero : Doubles.asList(0.0, -0.0)) { - for (double a : ALL_DOUBLE_CANDIDATES) { - for (double b : ALL_DOUBLE_CANDIDATES) { - assertEquals(a == b || (Double.isNaN(a) && Double.isNaN(b)), - DoubleMath.fuzzyEquals(a, b, zero)); - } - } - } - } - - public void testFuzzyEqualsBadTolerance() { - for (double tolerance : BAD_TOLERANCE_CANDIDATES) { - try { - DoubleMath.fuzzyEquals(1, 2, tolerance); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - // success - } - } - } - - public void testFuzzyCompare() { - for (double a : ALL_DOUBLE_CANDIDATES) { - for (double b : ALL_DOUBLE_CANDIDATES) { - for (double tolerance : TOLERANCE_CANDIDATES) { - int expected = DoubleMath.fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b); - int actual = DoubleMath.fuzzyCompare(a, b, tolerance); - assertEquals(Integer.signum(expected), Integer.signum(actual)); - } - } - } - } - - public void testFuzzyCompareBadTolerance() { - for (double tolerance : BAD_TOLERANCE_CANDIDATES) { - try { - DoubleMath.fuzzyCompare(1, 2, tolerance); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - // success - } - } - } - - public void testNullPointers() { + + public void testNullPointers() throws Exception { NullPointerTester tester = new NullPointerTester(); + tester.setDefault(RoundingMode.class, FLOOR); tester.setDefault(double.class, 3.0); tester.testAllPublicStaticMethods(DoubleMath.class); } |