Exemplo n.º 1
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);
   }
 }
Exemplo n.º 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);
   }
 }
Exemplo n.º 3
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);
   }
 }
Exemplo n.º 4
0
 // Relies on the correctness of sqrt(BigInteger, {HALF_UP,HALF_DOWN}).
 @GwtIncompatible // TODO
 public void testSqrtHalfEven() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     BigInteger halfEven = BigIntegerMath.sqrt(x, HALF_EVEN);
     // Now figure out what rounding mode we should behave like (it depends if FLOOR was
     // odd/even).
     boolean floorWasOdd = BigIntegerMath.sqrt(x, FLOOR).testBit(0);
     assertEquals(BigIntegerMath.sqrt(x, floorWasOdd ? HALF_UP : HALF_DOWN), halfEven);
   }
 }
Exemplo n.º 5
0
 // Depends on the correctness of BigIntegerMath.factorial
 private static void runBinomialTest(int firstN, int lastN) {
   for (int n = firstN; n <= lastN; n++) {
     for (int k = 0; k <= n; k++) {
       BigInteger expected =
           BigIntegerMath.factorial(n)
               .divide(BigIntegerMath.factorial(k))
               .divide(BigIntegerMath.factorial(n - k));
       assertEquals(expected, BigIntegerMath.binomial(n, k));
     }
   }
 }
Exemplo n.º 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);
     }
   }
 }
Exemplo n.º 7
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);
     }
   }
 }
Exemplo n.º 8
0
 // Relies on the correctness of sqrt(BigInteger, FLOOR).
 @GwtIncompatible // TODO
 public void testSqrtExact() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     BigInteger floor = BigIntegerMath.sqrt(x, FLOOR);
     // We only expect an exception if x was not a perfect square.
     boolean isPerfectSquare = floor.pow(2).equals(x);
     try {
       assertEquals(floor, BigIntegerMath.sqrt(x, UNNECESSARY));
       assertTrue(isPerfectSquare);
     } catch (ArithmeticException e) {
       assertFalse(isPerfectSquare);
     }
   }
 }
Exemplo n.º 9
0
 public void testBinomialOutside() {
   for (int n = 0; n <= 50; n++) {
     try {
       BigIntegerMath.binomial(n, -1);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
     try {
       BigIntegerMath.binomial(n, n + 1);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
Exemplo n.º 10
0
 public void testFactorial() {
   for (int i = 0; i <= DoubleMath.MAX_FACTORIAL; i++) {
     double actual = BigIntegerMath.factorial(i).doubleValue();
     double result = DoubleMath.factorial(i);
     assertEquals(actual, result, Math.ulp(actual));
   }
 }
Exemplo n.º 11
0
 @GwtIncompatible // TODO
 public void testConstantSqrt2PrecomputedBits() {
   assertEquals(
       BigIntegerMath.sqrt(
           BigInteger.ZERO.setBit(2 * BigIntegerMath.SQRT2_PRECOMPUTE_THRESHOLD + 1), FLOOR),
       BigIntegerMath.SQRT2_PRECOMPUTED_BITS);
 }
Exemplo n.º 12
0
 public void testFactorialNegative() {
   try {
     BigIntegerMath.factorial(-1);
     fail("Expected IllegalArgumentException");
   } catch (IllegalArgumentException expected) {
   }
 }
Exemplo n.º 13
0
 public void testCeilingPowerOfTwoZero() {
   try {
     BigIntegerMath.ceilingPowerOfTwo(BigInteger.ZERO);
     fail("Expected IllegalArgumentException");
   } catch (IllegalArgumentException expected) {
   }
 }
Exemplo n.º 14
0
 public void testFactorial() {
   BigInteger expected = BigInteger.ONE;
   for (int i = 1; i <= 200; i++) {
     expected = expected.multiply(BigInteger.valueOf(i));
     assertEquals(expected, BigIntegerMath.factorial(i));
   }
 }
Exemplo n.º 15
0
  @GwtIncompatible // TODO
  @AndroidIncompatible // slow
  public void testDivNonZeroExact() {
    boolean isAndroid = System.getProperties().getProperty("java.runtime.name").contains("Android");
    for (BigInteger p : NONZERO_BIGINTEGER_CANDIDATES) {
      for (BigInteger q : NONZERO_BIGINTEGER_CANDIDATES) {
        if (isAndroid && p.equals(BAD_FOR_ANDROID_P) && q.equals(BAD_FOR_ANDROID_Q)) {
          // https://code.google.com/p/android/issues/detail?id=196555
          continue;
        }
        if (isAndroid && p.equals(BAD_FOR_GINGERBREAD_P) && q.equals(BAD_FOR_GINGERBREAD_Q)) {
          // Works fine under Marshmallow, so I haven't filed a bug.
          continue;
        }

        boolean dividesEvenly = p.remainder(q).equals(ZERO);

        try {
          BigInteger quotient = BigIntegerMath.divide(p, q, UNNECESSARY);
          BigInteger undone = quotient.multiply(q);
          if (!p.equals(undone)) {
            failFormat("expected %s.multiply(%s) = %s; got %s", quotient, q, p, undone);
          }
          assertTrue(dividesEvenly);
        } catch (ArithmeticException e) {
          assertFalse(dividesEvenly);
        }
      }
    }
  }
Exemplo n.º 16
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));
   }
 }
Exemplo n.º 17
0
 @GwtIncompatible // TODO
 public void testLog10TrivialOnPowerOf10() {
   BigInteger x = BigInteger.TEN.pow(100);
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     assertEquals(100, BigIntegerMath.log10(x, mode));
   }
 }
Exemplo n.º 18
0
 @GwtIncompatible // TODO
 public void testZeroDivIsAlwaysZero() {
   for (BigInteger q : NONZERO_BIGINTEGER_CANDIDATES) {
     for (RoundingMode mode : ALL_ROUNDING_MODES) {
       assertEquals(ZERO, BigIntegerMath.divide(ZERO, q, mode));
     }
   }
 }
Exemplo n.º 19
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);
     }
   }
 }
Exemplo n.º 20
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);
     }
   }
 }
Exemplo n.º 21
0
 public void testFloorPowerOfTwoNegative() {
   for (BigInteger x : NEGATIVE_BIGINTEGER_CANDIDATES) {
     try {
       BigIntegerMath.floorPowerOfTwo(x);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
Exemplo n.º 22
0
 public void testLog2NegativeAlwaysThrows() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       BigIntegerMath.log2(BigInteger.valueOf(-1), mode);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
Exemplo n.º 23
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);
   }
 }
Exemplo n.º 24
0
 @GwtIncompatible // TODO
 public void testLog10ZeroAlwaysThrows() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       BigIntegerMath.log10(ZERO, mode);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
Exemplo n.º 25
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);
     }
   }
 }
Exemplo n.º 26
0
 @GwtIncompatible // TODO
 public void testSqrtNegativeAlwaysThrows() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       BigIntegerMath.sqrt(BigInteger.valueOf(-1), mode);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
Exemplo n.º 27
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);
     }
   }
 }
Exemplo n.º 28
0
 @GwtIncompatible // TODO
 public void testSqrtCeiling() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     for (RoundingMode mode : asList(CEILING, UP)) {
       BigInteger result = BigIntegerMath.sqrt(x, mode);
       assertTrue(result.compareTo(ZERO) > 0);
       assertTrue(result.pow(2).compareTo(x) >= 0);
       assertTrue(result.signum() == 0 || result.subtract(ONE).pow(2).compareTo(x) < 0);
     }
   }
 }
Exemplo n.º 29
0
 @GwtIncompatible // TODO
 public void testSqrtFloor() {
   for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
     for (RoundingMode mode : asList(FLOOR, DOWN)) {
       BigInteger result = BigIntegerMath.sqrt(x, mode);
       assertTrue(result.compareTo(ZERO) > 0);
       assertTrue(result.pow(2).compareTo(x) <= 0);
       assertTrue(result.add(ONE).pow(2).compareTo(x) > 0);
     }
   }
 }
Exemplo n.º 30
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);
   }
 }