Example #1
0
 public void testIsPowerOfTwo() {
   for (BigInteger x : ALL_BIGINTEGER_CANDIDATES) {
     // Checks for a single bit set.
     boolean expected = x.signum() > 0 & x.and(x.subtract(ONE)).equals(ZERO);
     assertEquals(expected, BigIntegerMath.isPowerOfTwo(x));
   }
 }
Example #2
0
 public void testFloorPowerOfTwo() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     BigInteger result = BigIntegerMath.floorPowerOfTwo(x);
     assertTrue(BigIntegerMath.isPowerOfTwo(result));
     assertTrue(result.compareTo(x) <= 0);
     assertTrue(result.add(result).compareTo(x) > 0);
   }
 }
Example #3
0
 public void testCeilingPowerOfTwo() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     BigInteger result = BigIntegerMath.ceilingPowerOfTwo(x);
     assertTrue(BigIntegerMath.isPowerOfTwo(result));
     assertTrue(result.compareTo(x) >= 0);
     assertTrue(result.compareTo(x.add(x)) < 0);
   }
 }
Example #4
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);
     }
   }
 }