Skip to content

Commit ac76cd9

Browse files
committed
test: improve MiniMaxAlgorithm test coverage for CodeCov
- Add comprehensive test cases for isPowerOfTwo function edge cases - Test setScores method with various invalid array lengths (0, 3, 5, 6, 7, 9, 10, 15) - Add tests for large valid powers of 2 (up to 64 elements) - Ensure complete coverage of error handling branches - Increase test count from 14 to 19 tests - Fix partial coverage issues identified in CodeCov report Resolves CodeCov coverage gaps in MiniMaxAlgorithm.java
1 parent ccadeab commit ac76cd9

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/test/java/com/thealgorithms/others/MiniMaxAlgorithmTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,46 @@ void testSetScoresWithInvalidLength() {
6060
assertEquals(8, miniMax.getScores().length);
6161
}
6262

63+
@Test
64+
void testSetScoresWithZeroLength() {
65+
int[] emptyScores = {}; // Length 0 is not a power of 2
66+
miniMax.setScores(emptyScores);
67+
68+
// Should print error message and not change the scores
69+
String output = outputStream.toString();
70+
assertTrue(output.contains("The number of scores must be a power of 2."));
71+
72+
// Scores should remain unchanged (original length 8)
73+
assertEquals(8, miniMax.getScores().length);
74+
}
75+
76+
@Test
77+
void testSetScoresWithVariousInvalidLengths() {
78+
// Test multiple invalid lengths to ensure isPowerOfTwo function is fully covered
79+
int[][] invalidScoreArrays = {
80+
{1, 2, 3, 4, 5}, // Length 5
81+
{1, 2, 3, 4, 5, 6}, // Length 6
82+
{1, 2, 3, 4, 5, 6, 7}, // Length 7
83+
new int[9], // Length 9
84+
new int[10], // Length 10
85+
new int[15] // Length 15
86+
};
87+
88+
for (int[] invalidScores : invalidScoreArrays) {
89+
// Clear the output stream for each test
90+
outputStream.reset();
91+
miniMax.setScores(invalidScores);
92+
93+
// Should print error message for each invalid length
94+
String output = outputStream.toString();
95+
assertTrue(output.contains("The number of scores must be a power of 2."),
96+
"Failed for array length: " + invalidScores.length);
97+
}
98+
99+
// Scores should remain unchanged (original length 8)
100+
assertEquals(8, miniMax.getScores().length);
101+
}
102+
63103
@Test
64104
void testSetScoresWithSingleElement() {
65105
int[] singleScore = {42};
@@ -185,4 +225,56 @@ void testEdgeCaseWithNegativeScores() {
185225
void tearDown() {
186226
System.setOut(originalOut);
187227
}
228+
229+
@Test
230+
void testSetScoresWithNegativeLength() {
231+
// This test ensures the first condition of isPowerOfTwo (n > 0) is tested
232+
// Although we can't directly create an array with negative length,
233+
// we can test edge cases around zero and ensure proper validation
234+
235+
// Test with array length 0 (edge case for n > 0 condition)
236+
int[] emptyArray = new int[0];
237+
outputStream.reset();
238+
miniMax.setScores(emptyArray);
239+
240+
String output = outputStream.toString();
241+
assertTrue(output.contains("The number of scores must be a power of 2."));
242+
assertEquals(8, miniMax.getScores().length); // Should remain unchanged
243+
}
244+
245+
@Test
246+
void testSetScoresWithLargePowerOfTwo() {
247+
// Test with a large power of 2 to ensure the algorithm works correctly
248+
int[] largeValidScores = new int[32]; // 32 = 2^5
249+
for (int i = 0; i < largeValidScores.length; i++) {
250+
largeValidScores[i] = i + 1;
251+
}
252+
253+
miniMax.setScores(largeValidScores);
254+
assertArrayEquals(largeValidScores, miniMax.getScores());
255+
assertEquals(5, miniMax.getHeight()); // log2(32) = 5
256+
}
257+
258+
@Test
259+
void testSetScoresValidEdgeCases() {
260+
// Test valid powers of 2 to ensure isPowerOfTwo returns true correctly
261+
int[][] validPowersOf2 = {
262+
new int[1], // 1 = 2^0
263+
new int[2], // 2 = 2^1
264+
new int[4], // 4 = 2^2
265+
new int[8], // 8 = 2^3
266+
new int[16], // 16 = 2^4
267+
new int[64] // 64 = 2^6
268+
};
269+
270+
int[] expectedHeights = {0, 1, 2, 3, 4, 6};
271+
272+
for (int i = 0; i < validPowersOf2.length; i++) {
273+
miniMax.setScores(validPowersOf2[i]);
274+
assertEquals(validPowersOf2[i].length, miniMax.getScores().length,
275+
"Failed for array length: " + validPowersOf2[i].length);
276+
assertEquals(expectedHeights[i], miniMax.getHeight(),
277+
"Height calculation failed for array length: " + validPowersOf2[i].length);
278+
}
279+
}
188280
}

0 commit comments

Comments
 (0)