use of java.math.BigInteger in project guava by google.
the class LongMathTest method testFloorPowerOfTwo.
public void testFloorPowerOfTwo() {
for (long x : POSITIVE_LONG_CANDIDATES) {
BigInteger expectedResult = BigIntegerMath.floorPowerOfTwo(BigInteger.valueOf(x));
assertEquals(expectedResult.longValue(), LongMath.floorPowerOfTwo(x));
}
}
use of java.math.BigInteger in project guava by google.
the class LongMathTest method testLessThanBranchFree.
// slow
@AndroidIncompatible
public void testLessThanBranchFree() {
for (long x : ALL_LONG_CANDIDATES) {
for (long y : ALL_LONG_CANDIDATES) {
BigInteger difference = BigInteger.valueOf(x).subtract(BigInteger.valueOf(y));
if (fitsInLong(difference)) {
int expected = (x < y) ? 1 : 0;
int actual = LongMath.lessThanBranchFree(x, y);
assertEquals(expected, actual);
}
}
}
}
use of java.math.BigInteger in project guava by google.
the class LongMathTest method testCheckedSubtract.
// TODO
@GwtIncompatible
// slow
@AndroidIncompatible
public void testCheckedSubtract() {
for (long a : ALL_LONG_CANDIDATES) {
for (long b : ALL_LONG_CANDIDATES) {
BigInteger expectedResult = valueOf(a).subtract(valueOf(b));
boolean expectedSuccess = fitsInLong(expectedResult);
try {
assertEquals(a - b, LongMath.checkedSubtract(a, b));
assertTrue(expectedSuccess);
} catch (ArithmeticException e) {
if (expectedSuccess) {
failFormat("expected checkedSubtract(%s, %s) = %s; got ArithmeticException", a, b, expectedResult);
}
}
}
}
}
use of java.math.BigInteger in project guava by google.
the class LongMathTest method testCheckedMultiply.
// TODO
@GwtIncompatible
// slow
@AndroidIncompatible
public void testCheckedMultiply() {
boolean isAndroid = System.getProperties().getProperty("java.runtime.name").contains("Android");
for (long a : ALL_LONG_CANDIDATES) {
for (long b : ALL_LONG_CANDIDATES) {
if (isAndroid && a == -4294967296L && b == 2147483648L) {
/*
* Bug in older versions of Android we test against, since fixed: -9223372036854775808L /
* -4294967296L = -9223372036854775808L!
*
* To be clear, this bug affects not the test's computation of the expected result but the
* _actual prod code_. But it probably affects only unusual cases.
*/
continue;
}
BigInteger expectedResult = valueOf(a).multiply(valueOf(b));
boolean expectedSuccess = fitsInLong(expectedResult);
try {
assertEquals(a * b, LongMath.checkedMultiply(a, b));
assertTrue(expectedSuccess);
} catch (ArithmeticException e) {
if (expectedSuccess) {
failFormat("expected checkedMultiply(%s, %s) = %s; got ArithmeticException", a, b, expectedResult);
}
}
}
}
}
use of java.math.BigInteger in project guava by google.
the class LongMathTest method testConstantsHalfPowersOf10.
// TODO
@GwtIncompatible
public void testConstantsHalfPowersOf10() {
for (int i = 0; i < LongMath.halfPowersOf10.length; i++) {
assertEquals(BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR), BigInteger.valueOf(LongMath.halfPowersOf10[i]));
}
BigInteger nextBigger = BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR);
assertTrue(nextBigger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0);
}
Aggregations