Search in sources :

Example 61 with BigInteger

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));
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 62 with BigInteger

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);
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 63 with BigInteger

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);
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 64 with BigInteger

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);
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 65 with BigInteger

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);
}
Also used : BigInteger(java.math.BigInteger) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

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