Repository navigation
Expand file tree
/
Copy pathSolutionTest.java
More file actions
55 lines (44 loc) Β· 1.65 KB
/
Copy pathSolutionTest.java
File metadata and controls
55 lines (44 loc) Β· 1.65 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
package math.power;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
class SolutionTest {
private final Solution solution = new Solution();
@Test
void powerNaive_FiveCubed_ExpectedResult() {
long result = solution.powerNaive(5, 4);
assertThat(result).isEqualTo(625);
}
@Test
void powerEfficient_ZeroExponent_AlwaysOne() {
long result = solution.powerEfficient(111, 0);
assertThat(result).isEqualTo(1);
}
@Test
void powerEfficient_ExponentPowerOfTwo_ExpectedResult() {
long result = solution.powerEfficient(2, 8);
assertThat(result).isEqualTo(256); // 2 ^ 8 β 4 ^ 4 β 16 ^ 2 β 256 ^ 1
}
@Test
void powerEfficient_ExponentNotPowerOfTwo_ExpectedResult() {
long result = solution.powerEfficient(3, 5);
assertThat(result).isEqualTo(243); // 3 ^ 5 β 9 ^ 2 * 3 β 81 ^ 1 * 3
}
/**
* @see <a href="https://en.wikipedia.org/wiki/Zero_to_the_power_of_zero">Zero to the power of zero</a>
*/
@Test
void powerEfficient_ZeroToZero_UndefinedBehaviorReturnOne() {
long result = solution.powerEfficient(0, 0);
assertThat(result).isEqualTo(1);
}
@Test
void powerEfficientBinOperators_TwoToFive_ExpectedResult() {
long result = solution.powerEfficientBinOperators(2, 5);
assertThat(result).isEqualTo(32);
}
@Test
void powerEfficientRecursive_EightToEight_ExpectedResult() {
long result = solution.powerEfficientRecursive(8, 8);
assertThat(result).isEqualTo(16_777_216); // 8 ^ 8 β 64 ^ 4 β 4096 ^ 2 β 16_777_216 ^ 1
}
}