Search in sources :

Example 76 with BigInteger

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

the class BigIntegerTest method test_shiftLeftI.

/**
	 * @tests java.math.BigInteger#shiftLeft(int)
	 */
public void test_shiftLeftI() {
    assertTrue("1 << 0", one.shiftLeft(0).equals(one));
    assertTrue("1 << 1", one.shiftLeft(1).equals(two));
    assertTrue("1 << 63", one.shiftLeft(63).equals(new BigInteger("8000000000000000", 16)));
    assertTrue("1 << 64", one.shiftLeft(64).equals(new BigInteger("10000000000000000", 16)));
    assertTrue("1 << 65", one.shiftLeft(65).equals(new BigInteger("20000000000000000", 16)));
    assertTrue("-1 << 0", minusOne.shiftLeft(0).equals(minusOne));
    assertTrue("-1 << 1", minusOne.shiftLeft(1).equals(minusTwo));
    assertTrue("-1 << 63", minusOne.shiftLeft(63).equals(new BigInteger("-9223372036854775808")));
    assertTrue("-1 << 64", minusOne.shiftLeft(64).equals(new BigInteger("-18446744073709551616")));
    assertTrue("-1 << 65", minusOne.shiftLeft(65).equals(new BigInteger("-36893488147419103232")));
    BigInteger a = bi3;
    BigInteger c = minusOne;
    for (int i = 0; i < 200; i++) {
        BigInteger b = bi3.shiftLeft(i);
        assertTrue("a==b", a.equals(b));
        assertTrue("a >> i == bi3", a.shiftRight(i).equals(bi3));
        a = a.shiftLeft(1);
        assertTrue("<<1 == *2", b.multiply(two).equals(a));
        assertTrue("a non-neg", a.signum() >= 0);
        assertTrue("a.bitCount==b.bitCount", a.bitCount() == b.bitCount());
        BigInteger d = minusOne.shiftLeft(i);
        assertTrue("c==d", c.equals(d));
        c = c.shiftLeft(1);
        assertTrue("<<1 == *2 negative", d.multiply(two).equals(c));
        assertTrue("c negative", c.signum() == -1);
        assertTrue("d >> i == minusOne", d.shiftRight(i).equals(minusOne));
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 77 with BigInteger

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

the class BigDecimalTest method test_abs.

/**
	 * @tests java.math.BigDecimal#abs()
	 */
public void test_abs() {
    BigDecimal big = new BigDecimal("-1234");
    BigDecimal bigabs = big.abs();
    assertTrue("the absolute value of -1234 is not 1234", bigabs.toString().equals("1234"));
    big = new BigDecimal(new BigInteger("2345"), 2);
    bigabs = big.abs();
    assertTrue("the absolute value of 23.45 is not 23.45", bigabs.toString().equals("23.45"));
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 78 with BigInteger

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

the class BigIntegerTest method testDiv.

private void testDiv(BigInteger i1, BigInteger i2) {
    BigInteger q = i1.divide(i2);
    BigInteger r = i1.remainder(i2);
    BigInteger[] temp = i1.divideAndRemainder(i2);
    assertTrue("divide and divideAndRemainder do not agree", q.equals(temp[0]));
    assertTrue("remainder and divideAndRemainder do not agree", r.equals(temp[1]));
    assertTrue("signum and equals(zero) do not agree on quotient", q.signum() != 0 || q.equals(zero));
    assertTrue("signum and equals(zero) do not agree on remainder", r.signum() != 0 || r.equals(zero));
    assertTrue("wrong sign on quotient", q.signum() == 0 || q.signum() == i1.signum() * i2.signum());
    assertTrue("wrong sign on remainder", r.signum() == 0 || r.signum() == i1.signum());
    assertTrue("remainder out of range", r.abs().compareTo(i2.abs()) < 0);
    assertTrue("quotient too small", q.abs().add(one).multiply(i2.abs()).compareTo(i1.abs()) > 0);
    assertTrue("quotient too large", q.abs().multiply(i2.abs()).compareTo(i1.abs()) <= 0);
    BigInteger p = q.multiply(i2);
    BigInteger a = p.add(r);
    assertTrue("(a/b)*b+(a%b) != a", a.equals(i1));
    try {
        BigInteger mod = i1.mod(i2);
        assertTrue("mod is negative", mod.signum() >= 0);
        assertTrue("mod out of range", mod.abs().compareTo(i2.abs()) < 0);
        assertTrue("positive remainder == mod", r.signum() < 0 || r.equals(mod));
        assertTrue("negative remainder == mod - divisor", r.signum() >= 0 || r.equals(mod.subtract(i2)));
    } catch (ArithmeticException e) {
        assertTrue("mod fails on negative divisor only", i2.signum() <= 0);
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 79 with BigInteger

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

the class BigIntegerTest method test_addLjava_math_BigInteger.

/**
	 * @tests java.math.BigInteger#add(java.math.BigInteger)
	 */
public void test_addLjava_math_BigInteger() {
    assertTrue("Incorrect sum--wanted a zillion", aZillion.add(aZillion).add(aZillion.negate()).equals(aZillion));
    assertTrue("0+0", zero.add(zero).equals(zero));
    assertTrue("0+1", zero.add(one).equals(one));
    assertTrue("1+0", one.add(zero).equals(one));
    assertTrue("1+1", one.add(one).equals(two));
    assertTrue("0+(-1)", zero.add(minusOne).equals(minusOne));
    assertTrue("(-1)+0", minusOne.add(zero).equals(minusOne));
    assertTrue("(-1)+(-1)", minusOne.add(minusOne).equals(minusTwo));
    assertTrue("1+(-1)", one.add(minusOne).equals(zero));
    assertTrue("(-1)+1", minusOne.add(one).equals(zero));
    for (int i = 0; i < 200; i++) {
        BigInteger midbit = zero.setBit(i);
        assertTrue("add fails to carry on bit " + i, midbit.add(midbit).equals(zero.setBit(i + 1)));
    }
    BigInteger bi2p3 = bi2.add(bi3);
    BigInteger bi3p2 = bi3.add(bi2);
    assertTrue("bi2p3=bi3p2", bi2p3.equals(bi3p2));
// add large positive + small positive
// add large positive + small negative
// add large negative + small positive
// add large negative + small negative
}
Also used : BigInteger(java.math.BigInteger)

Example 80 with BigInteger

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

the class BigIntegerTest method test_isProbablePrimeI.

/**
	 * @tests java.math.BigInteger#isProbablePrime(int)
	 */
public void test_isProbablePrimeI() {
    int fails = 0;
    bi = new BigInteger(20, 20, rand);
    if (!bi.isProbablePrime(17)) {
        fails++;
    }
    bi = new BigInteger("4", 10);
    if (bi.isProbablePrime(17)) {
        fail("isProbablePrime failed for: " + bi);
    }
    bi = BigInteger.valueOf(17L * 13L);
    if (bi.isProbablePrime(17)) {
        fail("isProbablePrime failed for: " + bi);
    }
    for (long a = 2; a < 1000; a++) {
        if (isPrime(a)) {
            assertTrue("false negative on prime number <1000", BigInteger.valueOf(a).isProbablePrime(5));
        } else if (BigInteger.valueOf(a).isProbablePrime(17)) {
            System.out.println("isProbablePrime failed for: " + a);
            fails++;
        }
    }
    for (int a = 0; a < 1000; a++) {
        bi = BigInteger.valueOf(rand.nextInt(1000000)).multiply(BigInteger.valueOf(rand.nextInt(1000000)));
        if (bi.isProbablePrime(17)) {
            System.out.println("isProbablePrime failed for: " + bi);
            fails++;
        }
    }
    for (int a = 0; a < 200; a++) {
        bi = new BigInteger(70, rand).multiply(new BigInteger(70, rand));
        if (bi.isProbablePrime(17)) {
            System.out.println("isProbablePrime failed for: " + bi);
            fails++;
        }
    }
    assertTrue("Too many false positives - may indicate a problem", fails <= 1);
}
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