Skip to content

Commit ada660a

Browse files
authored
refactor: update FastPower
1 parent cac1b0b commit ada660a

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed
Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
package src.test.java.com.others;
22

3-
/**
4-
* @author bingo
5-
* @since 2019/5/10
6-
*/
3+
import org.junit.Test;
4+
import src.main.java.com.others.FastPower;
5+
6+
import java.math.BigInteger;
7+
8+
import static org.junit.Assert.*;
79

810
public class FastPowerTest {
11+
12+
@Test
13+
void testLong(long n, long k, long m) {
14+
long result = FastPower.calculate(n, k, m);
15+
assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue());
16+
}
17+
18+
@Test
19+
void testBigInteger(BigInteger n, BigInteger k, BigInteger m) {
20+
BigInteger result = FastPower.calculate(n, k, m);
21+
assertEquals(result, n.modPow(k, m));
22+
}
23+
24+
@Test
25+
public void test() {
26+
testLong(2, 2, 10);
27+
testLong(100, 1000, 20);
28+
testLong(123456, 123456789, 234);
29+
30+
testBigInteger(BigInteger.TEN, BigInteger.TEN, BigInteger.valueOf(4));
31+
testBigInteger(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234"));
32+
testBigInteger(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890"));
33+
34+
}
935
}

0 commit comments

Comments
 (0)