use of java.math.BigInteger in project guava by google.
the class IntMathTest method testCheckedMultiply.
// presumably slow
@AndroidIncompatible
public void testCheckedMultiply() {
for (int a : ALL_INTEGER_CANDIDATES) {
for (int b : ALL_INTEGER_CANDIDATES) {
BigInteger expectedResult = valueOf(a).multiply(valueOf(b));
boolean expectedSuccess = fitsInInt(expectedResult);
try {
assertEquals(a * b, IntMath.checkedMultiply(a, b));
assertTrue(expectedSuccess);
} catch (ArithmeticException e) {
assertFalse(expectedSuccess);
}
}
}
}
use of java.math.BigInteger in project guava by google.
the class IntMathTest method testBinomial.
// Depends on the correctness of BigIntegerMath.binomial.
// BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath
@GwtIncompatible
public void testBinomial() {
for (int n = 0; n <= 50; n++) {
for (int k = 0; k <= n; k++) {
BigInteger expectedBig = BigIntegerMath.binomial(n, k);
int expectedInt = fitsInInt(expectedBig) ? expectedBig.intValue() : Integer.MAX_VALUE;
assertEquals(expectedInt, IntMath.binomial(n, k));
}
}
}
use of java.math.BigInteger in project guava by google.
the class IntMathTest method testCheckedAdd.
// slow
@AndroidIncompatible
public void testCheckedAdd() {
for (int a : ALL_INTEGER_CANDIDATES) {
for (int b : ALL_INTEGER_CANDIDATES) {
BigInteger expectedResult = valueOf(a).add(valueOf(b));
boolean expectedSuccess = fitsInInt(expectedResult);
try {
assertEquals(a + b, IntMath.checkedAdd(a, b));
assertTrue(expectedSuccess);
} catch (ArithmeticException e) {
assertFalse(expectedSuccess);
}
}
}
}
use of java.math.BigInteger in project j2objc by google.
the class BigIntegerTest method test_Constructor$B.
/**
* @tests java.math.BigInteger#BigInteger(byte[])
*/
public void test_Constructor$B() {
byte[] myByteArray;
myByteArray = new byte[] { (byte) 0x00, (byte) 0xFF, (byte) 0xFE };
bi = new BigInteger(myByteArray);
assertTrue("Incorrect value for pos number", bi.equals(BigInteger.ZERO.setBit(16).subtract(two)));
myByteArray = new byte[] { (byte) 0xFF, (byte) 0xFE };
bi = new BigInteger(myByteArray);
assertTrue("Incorrect value for neg number", bi.equals(minusTwo));
}
use of java.math.BigInteger in project j2objc by google.
the class BigIntegerTest method test_andNotLjava_math_BigInteger.
/**
* @tests java.math.BigInteger#andNot(java.math.BigInteger)
*/
public void test_andNotLjava_math_BigInteger() {
for (BigInteger[] element : booleanPairs) {
BigInteger i1 = element[0], i2 = element[1];
BigInteger res = i1.andNot(i2);
int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
for (int i = 0; i < len; i++) {
assertTrue("andNot", (i1.testBit(i) && !i2.testBit(i)) == res.testBit(i));
}
// asymmetrical
i1 = element[1];
i2 = element[0];
res = i1.andNot(i2);
for (int i = 0; i < len; i++) {
assertTrue("andNot reversed", (i1.testBit(i) && !i2.testBit(i)) == res.testBit(i));
}
}
//regression for HARMONY-4653
try {
BigInteger.ZERO.andNot(null);
fail("should throw NPE");
} catch (Exception e) {
//expected
}
BigInteger bi = new BigInteger(0, new byte[] {});
assertEquals(BigInteger.ZERO, bi.andNot(BigInteger.ZERO));
}
Aggregations