Search in sources :

Example 66 with BigInteger

use of java.math.BigInteger in project guava by google.

the class LongMathTest method testCheckedAdd.

// slow
@AndroidIncompatible
// TODO
@GwtIncompatible
public void testCheckedAdd() {
    for (long a : ALL_LONG_CANDIDATES) {
        for (long b : ALL_LONG_CANDIDATES) {
            BigInteger expectedResult = valueOf(a).add(valueOf(b));
            boolean expectedSuccess = fitsInLong(expectedResult);
            try {
                assertEquals(a + b, LongMath.checkedAdd(a, b));
                assertTrue(expectedSuccess);
            } catch (ArithmeticException e) {
                if (expectedSuccess) {
                    failFormat("expected checkedAdd(%s, %s) = %s; got ArithmeticException", a, b, expectedResult);
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 67 with BigInteger

use of java.math.BigInteger in project guava by google.

the class LongMathTest method computeMeanSafely.

/**
   * Computes the mean in a way that is obvious and resilient to
   * overflow by using BigInteger arithmetic.
   */
private static long computeMeanSafely(long x, long y) {
    BigInteger bigX = BigInteger.valueOf(x);
    BigInteger bigY = BigInteger.valueOf(y);
    BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
    // parseInt blows up on overflow as opposed to intValue() which does not.
    return Long.parseLong(bigMean.toString());
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 68 with BigInteger

use of java.math.BigInteger in project guava by google.

the class LongMathTest method testCheckedPow.

// TODO
@GwtIncompatible
public void testCheckedPow() {
    for (long b : ALL_LONG_CANDIDATES) {
        for (int exp : EXPONENTS) {
            BigInteger expectedResult = valueOf(b).pow(exp);
            boolean expectedSuccess = fitsInLong(expectedResult);
            try {
                assertEquals(expectedResult.longValue(), LongMath.checkedPow(b, exp));
                assertTrue(expectedSuccess);
            } catch (ArithmeticException e) {
                if (expectedSuccess) {
                    failFormat("expected checkedPow(%s, %s) = %s; got ArithmeticException", b, exp, expectedResult);
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 69 with BigInteger

use of java.math.BigInteger in project guava by google.

the class LongMathTest method testBinomial.

// Depends on the correctness of BigIntegerMath.binomial.
public void testBinomial() {
    for (int n = 0; n <= 70; n++) {
        for (int k = 0; k <= n; k++) {
            BigInteger expectedBig = BigIntegerMath.binomial(n, k);
            long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE;
            assertEquals(expectedLong, LongMath.binomial(n, k));
        }
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 70 with BigInteger

use of java.math.BigInteger in project guava by google.

the class IntMathTest method testFactorial.

// Depends on the correctness of BigIntegerMath.factorial.
public void testFactorial() {
    for (int n = 0; n <= 50; n++) {
        BigInteger expectedBig = BigIntegerMath.factorial(n);
        int expectedInt = fitsInInt(expectedBig) ? expectedBig.intValue() : Integer.MAX_VALUE;
        assertEquals(expectedInt, IntMath.factorial(n));
    }
}
Also used : BigInteger(java.math.BigInteger)

Aggregations

BigInteger (java.math.BigInteger)3500 BigDecimal (java.math.BigDecimal)635 Test (org.junit.Test)337 IOException (java.io.IOException)113 ArrayList (java.util.ArrayList)105 MessageDigest (java.security.MessageDigest)101 RoundingMode (java.math.RoundingMode)88 Date (java.util.Date)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)75 MathContext (java.math.MathContext)71 Random (java.util.Random)65 Test (org.junit.jupiter.api.Test)50 QuickTest (com.hazelcast.test.annotation.QuickTest)47 ParallelTest (com.hazelcast.test.annotation.ParallelTest)46 KeyFactory (java.security.KeyFactory)46 HashMap (java.util.HashMap)45 List (java.util.List)44 SecureRandom (java.security.SecureRandom)42 X509Certificate (java.security.cert.X509Certificate)42 EllipticCurve (java.security.spec.EllipticCurve)40