|
| 1 | +import static org.junit.Assert.assertEquals; |
| 2 | +import static org.junit.Assert.assertTrue; |
| 3 | + |
| 4 | +import java.math.BigInteger; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class BigIntegerDemoUnitTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + void whenBigIntegerCreatedFromConstructor_thenExpectedResult() { |
| 13 | + BigInteger biFromString = new BigInteger("1234567890987654321"); |
| 14 | + BigInteger biFromByteArray = new BigInteger( |
| 15 | + new byte[] { 64, 64, 64, 64, 64, 64 }); |
| 16 | + BigInteger biFromSignMagnitude = new BigInteger(-1, |
| 17 | + new byte[] { 64, 64, 64, 64, 64, 64 }); |
| 18 | + |
| 19 | + assertEquals("1234567890987654321", biFromString.toString()); |
| 20 | + assertEquals("70644700037184", biFromByteArray.toString()); |
| 21 | + assertEquals("-70644700037184", biFromSignMagnitude.toString()); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void whenLongConvertedToBigInteger_thenValueMatches() { |
| 26 | + BigInteger bi = BigInteger.valueOf(2305843009213693951L); |
| 27 | + |
| 28 | + assertEquals("2305843009213693951", bi.toString()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void givenBigIntegers_whentCompared_thenExpectedResult() { |
| 33 | + BigInteger i = new BigInteger("123456789012345678901234567890"); |
| 34 | + BigInteger j = new BigInteger("123456789012345678901234567891"); |
| 35 | + BigInteger k = new BigInteger("123456789012345678901234567892"); |
| 36 | + |
| 37 | + assertTrue(i.compareTo(i) == 0); |
| 38 | + assertTrue(j.compareTo(i) > 0); |
| 39 | + assertTrue(j.compareTo(k) < 0); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() { |
| 44 | + BigInteger i = new BigInteger("4"); |
| 45 | + BigInteger j = new BigInteger("2"); |
| 46 | + |
| 47 | + BigInteger sum = i.add(j); |
| 48 | + BigInteger difference = i.subtract(j); |
| 49 | + BigInteger quotient = i.divide(j); |
| 50 | + BigInteger product = i.multiply(j); |
| 51 | + |
| 52 | + assertEquals(new BigInteger("6"), sum); |
| 53 | + assertEquals(new BigInteger("2"), difference); |
| 54 | + assertEquals(new BigInteger("2"), quotient); |
| 55 | + assertEquals(new BigInteger("8"), product); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() { |
| 60 | + BigInteger i = new BigInteger("17"); |
| 61 | + BigInteger j = new BigInteger("7"); |
| 62 | + |
| 63 | + BigInteger and = i.and(j); |
| 64 | + BigInteger or = i.or(j); |
| 65 | + BigInteger not = j.not(); |
| 66 | + BigInteger xor = i.xor(j); |
| 67 | + BigInteger andNot = i.andNot(j); |
| 68 | + BigInteger shiftLeft = i.shiftLeft(1); |
| 69 | + BigInteger shiftRight = i.shiftRight(1); |
| 70 | + |
| 71 | + assertEquals(new BigInteger("1"), and); |
| 72 | + assertEquals(new BigInteger("23"), or); |
| 73 | + assertEquals(new BigInteger("-8"), not); |
| 74 | + assertEquals(new BigInteger("22"), xor); |
| 75 | + assertEquals(new BigInteger("16"), andNot); |
| 76 | + assertEquals(new BigInteger("34"), shiftLeft); |
| 77 | + assertEquals(new BigInteger("8"), shiftRight); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() { |
| 82 | + BigInteger i = new BigInteger("1018"); |
| 83 | + |
| 84 | + int bitCount = i.bitCount(); |
| 85 | + int bitLength = i.bitLength(); |
| 86 | + int getLowestSetBit = i.getLowestSetBit(); |
| 87 | + boolean testBit3 = i.testBit(3); |
| 88 | + BigInteger setBit12 = i.setBit(12); |
| 89 | + BigInteger flipBit0 = i.flipBit(0); |
| 90 | + BigInteger clearBit3 = i.clearBit(3); |
| 91 | + |
| 92 | + assertEquals(8, bitCount); |
| 93 | + assertEquals(10, bitLength); |
| 94 | + assertEquals(1, getLowestSetBit); |
| 95 | + assertEquals(true, testBit3); |
| 96 | + assertEquals(new BigInteger("5114"), setBit12); |
| 97 | + assertEquals(new BigInteger("1019"), flipBit0); |
| 98 | + assertEquals(new BigInteger("1010"), clearBit3); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void givenBigIntegers_whenModularCalculation_thenExpectedResult() { |
| 103 | + BigInteger i = new BigInteger("31"); |
| 104 | + BigInteger j = new BigInteger("24"); |
| 105 | + BigInteger k = new BigInteger("16"); |
| 106 | + |
| 107 | + BigInteger gcd = j.gcd(k); |
| 108 | + BigInteger multiplyAndmod = j.multiply(k) |
| 109 | + .mod(i); |
| 110 | + BigInteger modInverse = j.modInverse(i); |
| 111 | + BigInteger modPow = j.modPow(k, i); |
| 112 | + |
| 113 | + assertEquals(new BigInteger("8"), gcd); |
| 114 | + assertEquals(new BigInteger("12"), multiplyAndmod); |
| 115 | + assertEquals(new BigInteger("22"), modInverse); |
| 116 | + assertEquals(new BigInteger("7"), modPow); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void givenBigIntegers_whenPrimeOperations_thenExpectedResult() { |
| 121 | + BigInteger i = BigInteger.probablePrime(100, new Random()); |
| 122 | + |
| 123 | + boolean isProbablePrime = i.isProbablePrime(1000); |
| 124 | + assertEquals(true, isProbablePrime); |
| 125 | + } |
| 126 | +} |
0 commit comments