// Relies on the correctness of log10(BigInteger, {HALF_UP,HALF_DOWN}). @GwtIncompatible // TODO public void testLog10HalfEven() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { int halfEven = BigIntegerMath.log10(x, HALF_EVEN); // Now figure out what rounding mode we should behave like (it depends if FLOOR was // odd/even). boolean floorWasEven = (BigIntegerMath.log10(x, FLOOR) & 1) == 0; assertEquals(BigIntegerMath.log10(x, floorWasEven ? HALF_DOWN : HALF_UP), halfEven); } }
// Relies on the correctness of log10(BigInteger, FLOOR). @GwtIncompatible // TODO public void testLog10Exact() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { int logFloor = BigIntegerMath.log10(x, FLOOR); boolean expectSuccess = TEN.pow(logFloor).equals(x); try { assertEquals(logFloor, BigIntegerMath.log10(x, UNNECESSARY)); assertTrue(expectSuccess); } catch (ArithmeticException e) { assertFalse(expectSuccess); } } }
@GwtIncompatible // TODO public void testLog10TrivialOnPowerOf10() { BigInteger x = BigInteger.TEN.pow(100); for (RoundingMode mode : ALL_ROUNDING_MODES) { assertEquals(100, BigIntegerMath.log10(x, mode)); } }
@GwtIncompatible // TODO public void testLog10Ceiling() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { for (RoundingMode mode : asList(CEILING, UP)) { int result = BigIntegerMath.log10(x, mode); assertTrue(TEN.pow(result).compareTo(x) >= 0); assertTrue(result == 0 || TEN.pow(result - 1).compareTo(x) < 0); } } }
@GwtIncompatible // TODO public void testLog10Floor() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { for (RoundingMode mode : asList(FLOOR, DOWN)) { int result = BigIntegerMath.log10(x, mode); assertTrue(TEN.pow(result).compareTo(x) <= 0); assertTrue(TEN.pow(result + 1).compareTo(x) > 0); } } }
@GwtIncompatible // TODO public void testLog10NegativeAlwaysThrows() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { BigIntegerMath.log10(BigInteger.valueOf(-1), mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } }
@GwtIncompatible // TODO public void testLog10HalfDown() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { int result = BigIntegerMath.log10(x, HALF_DOWN); BigInteger x2 = x.pow(2); // x^2 <= 10^(2 * result + 1), or else we would have rounded up assertTrue(TEN.pow(2 * result + 1).compareTo(x2) >= 0); // x^2 > 10^(2 * result - 1), or else we would have rounded down assertTrue(result == 0 || TEN.pow(2 * result - 1).compareTo(x2) < 0); } }