7
7
8
8
public class BinaryPowTest {
9
9
10
- // --- Existing Tests (from your provided code) ---
11
10
@ Test
12
11
@ DisplayName ("Original tests for common cases" )
13
12
void testBinPow () {
14
- assertEquals (4 , BinaryPow .binPow (2 , 2 )); // No 'L' needed for int literals
13
+ assertEquals (4 , BinaryPow .binPow (2 , 2 ));
15
14
assertEquals (256 , BinaryPow .binPow (4 , 4 ));
16
15
assertEquals (729 , BinaryPow .binPow (9 , 3 ));
17
16
assertEquals (262144 , BinaryPow .binPow (8 , 6 ));
18
17
}
19
18
20
- // --- New Comprehensive Tests (integrated and adjusted for binPow(int, int)) ---
21
-
22
19
@ Test
23
20
@ DisplayName ("binPow(2, 3) should return 8" )
24
21
void testBinPow_basicCase1 () {
@@ -37,19 +34,16 @@ void testBinPow_basicCase3() {
37
34
assertEquals (10000 , BinaryPow .binPow (10 , 4 ));
38
35
}
39
36
40
- // --- Edge Cases and Special Values ---
41
-
42
37
@ Test
43
38
@ DisplayName ("binPow(base, 0) should return 1 for non-zero base" )
44
39
void testBinPow_exponentZero () {
45
40
assertEquals (1 , BinaryPow .binPow (5 , 0 ));
46
41
assertEquals (1 , BinaryPow .binPow (1 , 0 ));
47
42
assertEquals (1 , BinaryPow .binPow (-10 , 0 ));
48
- // Removed Long.MAX_VALUE as it exceeds int range
49
43
}
50
44
51
45
@ Test
52
- @ DisplayName ("binPow(0, 0) should return 1 (as per common convention for this algorithm )" )
46
+ @ DisplayName ("binPow(0, 0) should return 1 (as per convention)" )
53
47
void testBinPow_zeroToThePowerOfZero () {
54
48
assertEquals (1 , BinaryPow .binPow (0 , 0 ));
55
49
}
@@ -107,8 +101,6 @@ void testBinPow_negativeBaseOddExponent() {
107
101
assertEquals (-243 , BinaryPow .binPow (-3 , 5 ));
108
102
}
109
103
110
- // --- Exception Handling for Negative Exponent ---
111
-
112
104
@ Test
113
105
@ DisplayName ("Should throw IllegalArgumentException for negative exponent" )
114
106
void testBinPow_negativeExponentThrowsException () {
@@ -118,19 +110,15 @@ void testBinPow_negativeExponentThrowsException() {
118
110
assertThrows (IllegalArgumentException .class , () -> BinaryPow .binPow (1 , -2 ));
119
111
}
120
112
121
- // --- Large Number Tests (within int range, careful with potential overflow) ---
122
-
123
113
@ Test
124
- @ DisplayName ("binPow(2, 30) should return 1073741824 (fits in int) " )
114
+ @ DisplayName ("binPow(2, 30) should return 1073741824" )
125
115
void testBinPow_largeExponentFitsInInt () {
126
- // 2^30 = 1,073,741,824, which fits within Integer.MAX_VALUE (2,147,483,647)
127
116
assertEquals (1073741824 , BinaryPow .binPow (2 , 30 ));
128
117
}
129
118
130
119
@ Test
131
- @ DisplayName ("binPow(7, 10) should return 282475249 (fits in int) " )
120
+ @ DisplayName ("binPow(7, 10) should return 282475249" )
132
121
void testBinPow_anotherLargeExponentFitsInInt () {
133
- // 7^10 = 282,475,249, which fits within Integer.MAX_VALUE
134
122
assertEquals (282475249 , BinaryPow .binPow (7 , 10 ));
135
123
}
136
- }
124
+ }
0 commit comments