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 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 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 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 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 testFuzzyEqualsTwoNaNs() { for (double tolerance : TOLERANCE_CANDIDATES) { assertTrue(DoubleMath.fuzzyEquals(Double.NaN, Double.NaN, tolerance)); } }