Search in sources :

Example 81 with BigInteger

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

the class BigIntegerTest method test_modInverseLjava_math_BigInteger.

/**
	 * @tests java.math.BigInteger#modInverse(java.math.BigInteger)
	 */
public void test_modInverseLjava_math_BigInteger() {
    BigInteger a = zero, mod, inv;
    for (int j = 3; j < 50; j++) {
        mod = BigInteger.valueOf(j);
        for (int i = -j + 1; i < j; i++) {
            try {
                a = BigInteger.valueOf(i);
                inv = a.modInverse(mod);
                assertTrue("bad inverse: " + a + " inv mod " + mod + " equals " + inv, one.equals(a.multiply(inv).mod(mod)));
                assertTrue("inverse greater than modulo: " + a + " inv mod " + mod + " equals " + inv, inv.compareTo(mod) < 0);
                assertTrue("inverse less than zero: " + a + " inv mod " + mod + " equals " + inv, inv.compareTo(BigInteger.ZERO) >= 0);
            } catch (ArithmeticException e) {
                assertTrue("should have found inverse for " + a + " mod " + mod, !one.equals(a.gcd(mod)));
            }
        }
    }
    for (int j = 1; j < 10; j++) {
        mod = bi2.add(BigInteger.valueOf(j));
        for (int i = 0; i < 20; i++) {
            try {
                a = bi3.add(BigInteger.valueOf(i));
                inv = a.modInverse(mod);
                assertTrue("bad inverse: " + a + " inv mod " + mod + " equals " + inv, one.equals(a.multiply(inv).mod(mod)));
                assertTrue("inverse greater than modulo: " + a + " inv mod " + mod + " equals " + inv, inv.compareTo(mod) < 0);
                assertTrue("inverse less than zero: " + a + " inv mod " + mod + " equals " + inv, inv.compareTo(BigInteger.ZERO) >= 0);
            } catch (ArithmeticException e) {
                assertTrue("should have found inverse for " + a + " mod " + mod, !one.equals(a.gcd(mod)));
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 82 with BigInteger

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

the class BigIntegerTest method test_andLjava_math_BigInteger.

/**
	 * @tests java.math.BigInteger#and(java.math.BigInteger)
	 */
public void test_andLjava_math_BigInteger() {
    for (BigInteger[] element : booleanPairs) {
        BigInteger i1 = element[0], i2 = element[1];
        BigInteger res = i1.and(i2);
        assertTrue("symmetry of and", res.equals(i2.and(i1)));
        int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
        for (int i = 0; i < len; i++) {
            assertTrue("and", (i1.testBit(i) && i2.testBit(i)) == res.testBit(i));
        }
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 83 with BigInteger

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

the class BigIntegerTest method test_orLjava_math_BigInteger.

/**
	 * @tests java.math.BigInteger#or(java.math.BigInteger)
	 */
public void test_orLjava_math_BigInteger() {
    for (BigInteger[] element : booleanPairs) {
        BigInteger i1 = element[0], i2 = element[1];
        BigInteger res = i1.or(i2);
        assertTrue("symmetry of or", res.equals(i2.or(i1)));
        int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
        for (int i = 0; i < len; i++) {
            assertTrue("or", (i1.testBit(i) || i2.testBit(i)) == res.testBit(i));
        }
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 84 with BigInteger

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

the class BigIntegerTest method test_negate.

/**
	 * @tests java.math.BigInteger#negate()
	 */
public void test_negate() {
    assertTrue("Single negation of zero did not result in zero", zero.negate().equals(zero));
    assertTrue("Single negation resulted in original nonzero number", !aZillion.negate().equals(aZillion));
    assertTrue("Double negation did not result in original number", aZillion.negate().negate().equals(aZillion));
    assertTrue("0.neg", zero.negate().equals(zero));
    assertTrue("1.neg", one.negate().equals(minusOne));
    assertTrue("2.neg", two.negate().equals(minusTwo));
    assertTrue("-1.neg", minusOne.negate().equals(one));
    assertTrue("-2.neg", minusTwo.negate().equals(two));
    assertTrue("0x62EB40FEF85AA9EBL*2.neg", BigInteger.valueOf(0x62EB40FEF85AA9EBL * 2).negate().equals(BigInteger.valueOf(-0x62EB40FEF85AA9EBL * 2)));
    for (int i = 0; i < 200; i++) {
        BigInteger midbit = zero.setBit(i);
        BigInteger negate = midbit.negate();
        assertTrue("negate negate", negate.negate().equals(midbit));
        assertTrue("neg fails on bit " + i, midbit.negate().add(midbit).equals(zero));
    }
}
Also used : BigInteger(java.math.BigInteger)

Example 85 with BigInteger

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

the class BigIntegerTest method test_ConstructorIILjava_util_Random.

/**
	 * @tests java.math.BigInteger#BigInteger(int, int, java.util.Random)
	 */
public void test_ConstructorIILjava_util_Random() {
    bi = new BigInteger(10, 5, rand);
    bi2 = new BigInteger(10, 5, rand);
    assertTrue("Random number one is negative", bi.compareTo(zero) >= 0);
    assertTrue("Random number one is too big", bi.compareTo(twoToTheTen) < 0);
    assertTrue("Random number two is negative", bi2.compareTo(zero) >= 0);
    assertTrue("Random number two is too big", bi2.compareTo(twoToTheTen) < 0);
    Random rand = new Random();
    BigInteger bi;
    int[] certainty = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -2, -1 };
    for (int i = 2; i <= 20; i++) {
        for (int c = 0; c < certainty.length; c++) {
            // Create BigInteger
            bi = new BigInteger(i, c, rand);
            assertTrue("Bit length incorrect", bi.bitLength() == i);
        }
    }
}
Also used : Random(java.util.Random) 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