|
1 | 1 | package src.test.java.com.others;
|
2 | 2 |
|
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.*; |
7 | 9 |
|
8 | 10 | 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 | + } |
9 | 35 | }
|
0 commit comments