forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastPowerTest.java
33 lines (24 loc) · 1.04 KB
/
FastPowerTest.java
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
package src.test.java.com.others;
import org.junit.Test;
import src.main.java.com.others.FastPower;
import java.math.BigInteger;
import static org.junit.Assert.*;
public class FastPowerTest {
private void testLong(long n, long k, long m) {
long result = FastPower.calculate(n, k, m);
assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue());
}
private void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
BigInteger result = FastPower.calculate(n, k, m);
assertEquals(result, n.modPow(k, m));
}
@Test
public void test() {
testLong(2, 2, 10);
testLong(100, 1000, 20);
testLong(123456, 123456789, 234);
testBigInteger(BigInteger.TEN, BigInteger.TEN, BigInteger.valueOf(4));
testBigInteger(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234"));
testBigInteger(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890"));
}
}