示例#1
0
 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) {
     }
   }
 }
示例#2
0
 public void testRoundExactFractionalDoubleToIntFails() {
   for (double d : FRACTIONAL_DOUBLE_CANDIDATES) {
     try {
       DoubleMath.roundToInt(d, UNNECESSARY);
       fail("Expected ArithmeticException");
     } catch (ArithmeticException expected) {
     }
   }
 }
示例#3
0
 public void testRoundNaNToIntAlwaysFails() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       DoubleMath.roundToInt(Double.NaN, mode);
       fail("Expected ArithmeticException");
     } catch (ArithmeticException expected) {
     }
   }
 }
示例#4
0
  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);
      }
    }
  }
示例#5
0
  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);
        }
      }
    }
  }