forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigIntegerExampleTest.java
More file actions
61 lines (52 loc) · 1.87 KB
/
BigIntegerExampleTest.java
File metadata and controls
61 lines (52 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.examplehub.basics.number;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigInteger;
import org.junit.jupiter.api.Test;
class BigIntegerExampleTest {
@Test
void testInit() {
BigInteger bigInteger = new BigInteger("1234567891011121314151617181920");
assertEquals("1234567891011121314151617181920", bigInteger.toString());
}
@Test
void testAdd() {
BigInteger i1 = new BigInteger("1234567891011121314151617181920");
BigInteger i2 = new BigInteger("1234567891011121314151617181920");
assertEquals("2469135782022242628303234363840", i1.add(i2).toString());
}
@Test
void testPow() {
BigInteger bigInteger = new BigInteger("123456789");
assertEquals("1881676371789154860897069", bigInteger.pow(3).toString());
bigInteger = new BigInteger("1234567891011121314151617181920");
assertEquals(
"1881676376412480405375011631213584661038332261704414240874801542371684441105634548133888000",
bigInteger.pow(3).toString());
}
@Test
void testMultiply() {
BigInteger b1 = new BigInteger("123456789");
BigInteger b2 = new BigInteger("987654321");
assertEquals("121932631112635269", b1.multiply(b2).toString());
}
@Test
void testDivide() {
BigInteger b1 = new BigInteger("1234567891011121314151617181920");
BigInteger b2 = new BigInteger("12345678910");
assertEquals("100000000000900826453", b1.divide(b2).toString());
}
@Test
void testValueExact() {
BigInteger bigInteger = new BigInteger("123456789");
assertEquals(123456789, bigInteger.intValue());
assertEquals(123456789, bigInteger.intValueExact());
assertEquals(123456789, bigInteger.longValueExact());
BigInteger i1 = new BigInteger("1234567891011121314151617181920");
try {
long val = i1.longValueExact();
fail();
} catch (ArithmeticException e) {
assertTrue(true);
}
}
}