예제 #1
0
 // Relies on the correctness of log2(BigInteger, {HALF_UP,HALF_DOWN}).
 public void testLog2HalfEven() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     int halfEven = BigIntegerMath.log2(x, HALF_EVEN);
     // Now figure out what rounding mode we should behave like (it depends if FLOOR was
     // odd/even).
     boolean floorWasEven = (BigIntegerMath.log2(x, FLOOR) & 1) == 0;
     assertEquals(BigIntegerMath.log2(x, floorWasEven ? HALF_DOWN : HALF_UP), halfEven);
   }
 }
예제 #2
0
 public void testLog2Ceiling() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     for (RoundingMode mode : asList(CEILING, UP)) {
       int result = BigIntegerMath.log2(x, mode);
       assertTrue(ZERO.setBit(result).compareTo(x) >= 0);
       assertTrue(result == 0 || ZERO.setBit(result - 1).compareTo(x) < 0);
     }
   }
 }
예제 #3
0
 public void testLog2Floor() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     for (RoundingMode mode : asList(FLOOR, DOWN)) {
       int result = BigIntegerMath.log2(x, mode);
       assertTrue(ZERO.setBit(result).compareTo(x) <= 0);
       assertTrue(ZERO.setBit(result + 1).compareTo(x) > 0);
     }
   }
 }
예제 #4
0
 public void testLog2NegativeAlwaysThrows() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       BigIntegerMath.log2(BigInteger.valueOf(-1), mode);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
예제 #5
0
 public void testLog2HalfDown() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     int result = BigIntegerMath.log2(x, HALF_DOWN);
     BigInteger x2 = x.pow(2);
     // x^2 <= 2^(2 * result + 1), or else we would have rounded up
     assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) >= 0);
     // x^2 > 2^(2 * result - 1), or else we would have rounded down
     assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) < 0);
   }
 }
예제 #6
0
 // Relies on the correctness of isPowerOfTwo(BigInteger).
 public void testLog2Exact() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     // We only expect an exception if x was not a power of 2.
     boolean isPowerOf2 = BigIntegerMath.isPowerOfTwo(x);
     try {
       assertEquals(x, ZERO.setBit(BigIntegerMath.log2(x, UNNECESSARY)));
       assertTrue(isPowerOf2);
     } catch (ArithmeticException e) {
       assertFalse(isPowerOf2);
     }
   }
 }