Skip to content

Commit fae3497

Browse files
committed
Added more test cases as per the issue #6282
1 parent ae71861 commit fae3497

File tree

1 file changed

+124
-4
lines changed

1 file changed

+124
-4
lines changed
Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,136 @@
11
package com.thealgorithms.maths;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
3+
import org.junit.jupiter.api.DisplayName;
54
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
67

78
public class BinaryPowTest {
89

10+
// --- Existing Tests (from your provided code) ---
911
@Test
12+
@DisplayName("Original tests for common cases")
1013
void testBinPow() {
11-
assertEquals(4, BinaryPow.binPow(2, 2));
14+
assertEquals(4, BinaryPow.binPow(2, 2)); // No 'L' needed for int literals
1215
assertEquals(256, BinaryPow.binPow(4, 4));
1316
assertEquals(729, BinaryPow.binPow(9, 3));
1417
assertEquals(262144, BinaryPow.binPow(8, 6));
1518
}
16-
}
19+
20+
// --- New Comprehensive Tests (integrated and adjusted for binPow(int, int)) ---
21+
22+
@Test
23+
@DisplayName("binPow(2, 3) should return 8")
24+
void testBinPow_basicCase1() {
25+
assertEquals(8, BinaryPow.binPow(2, 3));
26+
}
27+
28+
@Test
29+
@DisplayName("binPow(5, 2) should return 25")
30+
void testBinPow_basicCase2() {
31+
assertEquals(25, BinaryPow.binPow(5, 2));
32+
}
33+
34+
@Test
35+
@DisplayName("binPow(10, 4) should return 10000")
36+
void testBinPow_basicCase3() {
37+
assertEquals(10000, BinaryPow.binPow(10, 4));
38+
}
39+
40+
// --- Edge Cases and Special Values ---
41+
42+
@Test
43+
@DisplayName("binPow(base, 0) should return 1 for non-zero base")
44+
void testBinPow_exponentZero() {
45+
assertEquals(1, BinaryPow.binPow(5, 0));
46+
assertEquals(1, BinaryPow.binPow(1, 0));
47+
assertEquals(1, BinaryPow.binPow(-10, 0));
48+
// Removed Long.MAX_VALUE as it exceeds int range
49+
}
50+
51+
@Test
52+
@DisplayName("binPow(0, 0) should return 1 (as per common convention for this algorithm)")
53+
void testBinPow_zeroToThePowerOfZero() {
54+
assertEquals(1, BinaryPow.binPow(0, 0));
55+
}
56+
57+
@Test
58+
@DisplayName("binPow(base, 1) should return base")
59+
void testBinPow_exponentOne() {
60+
assertEquals(7, BinaryPow.binPow(7, 1));
61+
assertEquals(-3, BinaryPow.binPow(-3, 1));
62+
assertEquals(1, BinaryPow.binPow(1, 1));
63+
}
64+
65+
@Test
66+
@DisplayName("binPow(0, positive_exponent) should return 0")
67+
void testBinPow_zeroBasePositiveExponent() {
68+
assertEquals(0, BinaryPow.binPow(0, 5));
69+
assertEquals(0, BinaryPow.binPow(0, 100));
70+
}
71+
72+
@Test
73+
@DisplayName("binPow(1, any_exponent) should return 1")
74+
void testBinPow_baseOne() {
75+
assertEquals(1, BinaryPow.binPow(1, 0));
76+
assertEquals(1, BinaryPow.binPow(1, 5));
77+
assertEquals(1, BinaryPow.binPow(1, 100));
78+
}
79+
80+
@Test
81+
@DisplayName("binPow(-1, even_exponent) should return 1")
82+
void testBinPow_negativeOneEvenExponent() {
83+
assertEquals(1, BinaryPow.binPow(-1, 0));
84+
assertEquals(1, BinaryPow.binPow(-1, 2));
85+
assertEquals(1, BinaryPow.binPow(-1, 100));
86+
}
87+
88+
@Test
89+
@DisplayName("binPow(-1, odd_exponent) should return -1")
90+
void testBinPow_negativeOneOddExponent() {
91+
assertEquals(-1, BinaryPow.binPow(-1, 1));
92+
assertEquals(-1, BinaryPow.binPow(-1, 3));
93+
assertEquals(-1, BinaryPow.binPow(-1, 99));
94+
}
95+
96+
@Test
97+
@DisplayName("binPow(negative_base, even_exponent) should return positive result")
98+
void testBinPow_negativeBaseEvenExponent() {
99+
assertEquals(16, BinaryPow.binPow(-2, 4));
100+
assertEquals(81, BinaryPow.binPow(-3, 4));
101+
}
102+
103+
@Test
104+
@DisplayName("binPow(negative_base, odd_exponent) should return negative result")
105+
void testBinPow_negativeBaseOddExponent() {
106+
assertEquals(-8, BinaryPow.binPow(-2, 3));
107+
assertEquals(-243, BinaryPow.binPow(-3, 5));
108+
}
109+
110+
// --- Exception Handling for Negative Exponent ---
111+
112+
@Test
113+
@DisplayName("Should throw IllegalArgumentException for negative exponent")
114+
void testBinPow_negativeExponentThrowsException() {
115+
assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(2, -1));
116+
assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(5, -10));
117+
assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(0, -5));
118+
assertThrows(IllegalArgumentException.class, () -> BinaryPow.binPow(1, -2));
119+
}
120+
121+
// --- Large Number Tests (within int range, careful with potential overflow) ---
122+
123+
@Test
124+
@DisplayName("binPow(2, 30) should return 1073741824 (fits in int)")
125+
void testBinPow_largeExponentFitsInInt() {
126+
// 2^30 = 1,073,741,824, which fits within Integer.MAX_VALUE (2,147,483,647)
127+
assertEquals(1073741824, BinaryPow.binPow(2, 30));
128+
}
129+
130+
@Test
131+
@DisplayName("binPow(7, 10) should return 282475249 (fits in int)")
132+
void testBinPow_anotherLargeExponentFitsInInt() {
133+
// 7^10 = 282,475,249, which fits within Integer.MAX_VALUE
134+
assertEquals(282475249, BinaryPow.binPow(7, 10));
135+
}
136+
}

0 commit comments

Comments
 (0)