forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastPowerTest.java
More file actions
31 lines (23 loc) · 1.01 KB
/
FastPowerTest.java
File metadata and controls
31 lines (23 loc) · 1.01 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
package com.others;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.BigInteger;
class FastPowerTest {
private void testLong(long n, long k, long m) {
long result = FastPower.calculate(n, k, m);
Assertions.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);
Assertions.assertEquals(result, n.modPow(k, m));
}
@Test
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"));
}
}