Пример #1
0
 @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);
     }
   }
 }
Пример #2
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);
     }
   }
 }
Пример #3
0
 @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);
   }
 }
  @Override
  public BigInteger generate(SourceOfRandomness random, GenerationStatus status) {
    int numberOfBits = status.size() + 1;

    if (min == null && max == null) return random.nextBigInteger(numberOfBits);

    BigInteger minToUse = min;
    BigInteger maxToUse = max;
    if (minToUse == null) minToUse = maxToUse.subtract(TEN.pow(numberOfBits));
    else if (maxToUse == null) maxToUse = minToUse.add(TEN.pow(numberOfBits));

    return Ranges.randomBigIntegerInRange(random, minToUse, maxToUse);
  }
 @Override
 protected void primeSourceOfRandomness() {
   first = TEN.subtract(TEN.negate());
   second = TEN.pow(2).subtract(TEN.pow(2).negate());
   third = TEN.pow(3).subtract(TEN.pow(3).negate());
   when(randomForParameterGenerator.nextBigInteger(first.toBigInteger().bitLength()))
       .thenReturn(new BigInteger("999999999999"))
       .thenReturn(BigInteger.ONE);
   when(randomForParameterGenerator.nextBigInteger(second.toBigInteger().bitLength()))
       .thenReturn(new BigInteger("136"));
   when(randomForParameterGenerator.nextBigInteger(third.toBigInteger().bitLength()))
       .thenReturn(new BigInteger("768"));
   when(distro.sampleWithMean(1, randomForParameterGenerator)).thenReturn(0);
   when(distro.sampleWithMean(2, randomForParameterGenerator)).thenReturn(1);
   when(distro.sampleWithMean(3, randomForParameterGenerator)).thenReturn(2);
 }
Пример #6
0
 // 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);
     }
   }
 }