public void testRoundInfiniteToIntAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToInt(Double.POSITIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } try { DoubleMath.roundToInt(Double.NEGATIVE_INFINITY, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
public void testRoundExactFractionalDoubleToIntFails() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { try { DoubleMath.roundToInt(d, UNNECESSARY); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
public void testRoundNaNToIntAlwaysFails() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { DoubleMath.roundToInt(Double.NaN, mode); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
public void testRoundExactIntegralDoubleToInt() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { BigDecimal expected = new BigDecimal(d).setScale(0, UNNECESSARY); boolean isInBounds = expected.compareTo(MAX_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.intValue(), DoubleMath.roundToInt(d, UNNECESSARY)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } }
public void testRoundFractionalDoubleToInt() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { BigDecimal expected = new BigDecimal(d).setScale(0, mode); boolean isInBounds = expected.compareTo(MAX_INT_AS_BIG_DECIMAL) <= 0 & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0; try { assertEquals(expected.intValue(), DoubleMath.roundToInt(d, mode)); assertTrue(isInBounds); } catch (ArithmeticException e) { assertFalse(isInBounds); } } } }