forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMaxAlgorithmTest.java
More file actions
344 lines (279 loc) · 11.8 KB
/
MiniMaxAlgorithmTest.java
File metadata and controls
344 lines (279 loc) · 11.8 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.thealgorithms.others;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Test class for MiniMaxAlgorithm
* Tests the minimax algorithm implementation for game tree evaluation
*/
class MiniMaxAlgorithmTest {
private MiniMaxAlgorithm miniMax;
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
@BeforeEach
void setUp() {
miniMax = new MiniMaxAlgorithm();
System.setOut(new PrintStream(outputStream));
}
@AfterEach
void tearDown() {
System.setOut(originalOut);
}
@Test
void testConstructorCreatesValidScores() {
// The default constructor should create scores array of length 8 (2^3)
Assertions.assertEquals(8, miniMax.getScores().length);
Assertions.assertEquals(3, miniMax.getHeight());
// All scores should be positive (between 1 and 99)
for (int score : miniMax.getScores()) {
Assertions.assertTrue(score >= 1 && score <= 99);
}
}
@Test
void testConstructorWithValidScores() {
int[] validScores = {10, 20, 30, 40};
MiniMaxAlgorithm customMiniMax = new MiniMaxAlgorithm(validScores);
Assertions.assertArrayEquals(validScores, customMiniMax.getScores());
Assertions.assertEquals(2, customMiniMax.getHeight()); // log2(4) = 2
}
@Test
void testConstructorWithInvalidScoresThrowsException() {
int[] invalidScores = {10, 20, 30}; // Length 3 is not a power of 2
Assertions.assertThrows(IllegalArgumentException.class, () -> new MiniMaxAlgorithm(invalidScores));
}
@Test
void testConstructorDoesNotModifyOriginalArray() {
int[] originalScores = {10, 20, 30, 40};
int[] copyOfOriginal = {10, 20, 30, 40};
MiniMaxAlgorithm customMiniMax = new MiniMaxAlgorithm(originalScores);
// Modify the original array
originalScores[0] = 999;
// Constructor should have made a copy, so internal state should be unchanged
Assertions.assertArrayEquals(copyOfOriginal, customMiniMax.getScores());
}
@Test
void testSetScoresWithValidPowerOfTwo() {
int[] validScores = {10, 20, 30, 40};
miniMax.setScores(validScores);
Assertions.assertArrayEquals(validScores, miniMax.getScores());
Assertions.assertEquals(2, miniMax.getHeight()); // log2(4) = 2
}
@Test
void testSetScoresWithInvalidLength() {
int[] invalidScores = {10, 20, 30}; // Length 3 is not a power of 2
Assertions.assertThrows(IllegalArgumentException.class, () -> miniMax.setScores(invalidScores));
// Scores should remain unchanged (original length 8)
Assertions.assertEquals(8, miniMax.getScores().length);
}
@Test
void testSetScoresWithZeroLength() {
int[] emptyScores = {}; // Length 0 is not a power of 2
Assertions.assertThrows(IllegalArgumentException.class, () -> miniMax.setScores(emptyScores));
// Scores should remain unchanged (original length 8)
Assertions.assertEquals(8, miniMax.getScores().length);
}
@Test
void testSetScoresWithVariousInvalidLengths() {
// Test multiple invalid lengths to ensure isPowerOfTwo function is fully
// covered
int[][] invalidScoreArrays = {
{1, 2, 3, 4, 5}, // Length 5
{1, 2, 3, 4, 5, 6}, // Length 6
{1, 2, 3, 4, 5, 6, 7}, // Length 7
new int[9], // Length 9
new int[10], // Length 10
new int[15] // Length 15
};
for (int[] invalidScores : invalidScoreArrays) {
Assertions.assertThrows(IllegalArgumentException.class, () -> miniMax.setScores(invalidScores), "Failed for array length: " + invalidScores.length);
}
// Scores should remain unchanged (original length 8)
Assertions.assertEquals(8, miniMax.getScores().length);
}
@Test
void testSetScoresWithSingleElement() {
int[] singleScore = {42};
miniMax.setScores(singleScore);
Assertions.assertArrayEquals(singleScore, miniMax.getScores());
Assertions.assertEquals(0, miniMax.getHeight()); // log2(1) = 0
}
@Test
void testMiniMaxWithKnownScores() {
// Test with a known game tree: [3, 12, 8, 2]
int[] testScores = {3, 12, 8, 2};
miniMax.setScores(testScores);
// Maximizer starts: should choose max(min(3,12), min(8,2)) = max(3, 2) = 3
int result = miniMax.miniMax(0, true, 0, false);
Assertions.assertEquals(3, result);
}
@Test
void testMiniMaxWithMinimizerFirst() {
// Test with minimizer starting first
int[] testScores = {3, 12, 8, 2};
miniMax.setScores(testScores);
// Minimizer starts: should choose min(max(3,12), max(8,2)) = min(12, 8) = 8
int result = miniMax.miniMax(0, false, 0, false);
Assertions.assertEquals(8, result);
}
@Test
void testMiniMaxWithLargerTree() {
// Test with 8 elements: [5, 6, 7, 4, 5, 3, 6, 2]
int[] testScores = {5, 6, 7, 4, 5, 3, 6, 2};
miniMax.setScores(testScores);
// Maximizer starts
int result = miniMax.miniMax(0, true, 0, false);
// Expected: max(min(max(5,6), max(7,4)), min(max(5,3), max(6,2)))
// = max(min(6, 7), min(5, 6)) = max(6, 5) = 6
Assertions.assertEquals(6, result);
}
@Test
void testMiniMaxVerboseOutput() {
int[] testScores = {3, 12, 8, 2};
miniMax.setScores(testScores);
miniMax.miniMax(0, true, 0, true);
String output = outputStream.toString();
Assertions.assertTrue(output.contains("Maximizer"));
Assertions.assertTrue(output.contains("Minimizer"));
Assertions.assertTrue(output.contains("chooses"));
}
@Test
void testGetRandomScoresLength() {
int[] randomScores = MiniMaxAlgorithm.getRandomScores(4, 50);
Assertions.assertEquals(16, randomScores.length); // 2^4 = 16
// All scores should be between 1 and 50
for (int score : randomScores) {
Assertions.assertTrue(score >= 1 && score <= 50);
}
}
@Test
void testGetRandomScoresWithDifferentParameters() {
int[] randomScores = MiniMaxAlgorithm.getRandomScores(2, 10);
Assertions.assertEquals(4, randomScores.length); // 2^2 = 4
// All scores should be between 1 and 10
for (int score : randomScores) {
Assertions.assertTrue(score >= 1 && score <= 10);
}
}
@Test
void testMainMethod() {
// Test that main method runs without errors
Assertions.assertDoesNotThrow(() -> MiniMaxAlgorithm.main(new String[] {}));
String output = outputStream.toString();
Assertions.assertTrue(output.contains("The best score for"));
Assertions.assertTrue(output.contains("Maximizer"));
}
@Test
void testHeightCalculation() {
// Test height calculation for different array sizes
int[] scores2 = {1, 2};
miniMax.setScores(scores2);
Assertions.assertEquals(1, miniMax.getHeight()); // log2(2) = 1
int[] scores16 = new int[16];
miniMax.setScores(scores16);
Assertions.assertEquals(4, miniMax.getHeight()); // log2(16) = 4
}
@Test
void testEdgeCaseWithZeroScores() {
int[] zeroScores = {0, 0, 0, 0};
miniMax.setScores(zeroScores);
int result = miniMax.miniMax(0, true, 0, false);
Assertions.assertEquals(0, result);
}
@Test
void testEdgeCaseWithNegativeScores() {
int[] negativeScores = {-5, -2, -8, -1};
miniMax.setScores(negativeScores);
// Tree evaluation with maximizer first:
// Level 1 (minimizer): min(-5,-2) = -5, min(-8,-1) = -8
// Level 0 (maximizer): max(-5, -8) = -5
int result = miniMax.miniMax(0, true, 0, false);
Assertions.assertEquals(-5, result);
}
@Test
void testSetScoresWithNegativeLength() {
// This test ensures the first condition of isPowerOfTwo (n > 0) is tested
// Although we can't directly create an array with negative length,
// we can test edge cases around zero and ensure proper validation
// Test with array length 0 (edge case for n > 0 condition)
int[] emptyArray = new int[0];
Assertions.assertThrows(IllegalArgumentException.class, () -> miniMax.setScores(emptyArray));
Assertions.assertEquals(8, miniMax.getScores().length); // Should remain unchanged
}
@Test
void testSetScoresWithLargePowerOfTwo() {
// Test with a large power of 2 to ensure the algorithm works correctly
int[] largeValidScores = new int[32]; // 32 = 2^5
for (int i = 0; i < largeValidScores.length; i++) {
largeValidScores[i] = i + 1;
}
miniMax.setScores(largeValidScores);
Assertions.assertArrayEquals(largeValidScores, miniMax.getScores());
Assertions.assertEquals(5, miniMax.getHeight()); // log2(32) = 5
}
@Test
void testSetScoresValidEdgeCases() {
// Test valid powers of 2 to ensure isPowerOfTwo returns true correctly
int[][] validPowersOf2 = {
new int[1], // 1 = 2^0
new int[2], // 2 = 2^1
new int[4], // 4 = 2^2
new int[8], // 8 = 2^3
new int[16], // 16 = 2^4
new int[64] // 64 = 2^6
};
int[] expectedHeights = {0, 1, 2, 3, 4, 6};
for (int i = 0; i < validPowersOf2.length; i++) {
miniMax.setScores(validPowersOf2[i]);
Assertions.assertEquals(validPowersOf2[i].length, miniMax.getScores().length, "Failed for array length: " + validPowersOf2[i].length);
Assertions.assertEquals(expectedHeights[i], miniMax.getHeight(), "Height calculation failed for array length: " + validPowersOf2[i].length);
}
}
@Test
void testGetScoresReturnsDefensiveCopy() {
int[] originalScores = {10, 20, 30, 40};
miniMax.setScores(originalScores);
// Get the scores and modify them
int[] retrievedScores = miniMax.getScores();
retrievedScores[0] = 999;
// Internal state should remain unchanged
Assertions.assertEquals(10, miniMax.getScores()[0]);
}
@Test
void testSetScoresCreatesDefensiveCopy() {
int[] originalScores = {10, 20, 30, 40};
miniMax.setScores(originalScores);
// Modify the original array after setting
originalScores[0] = 999;
// Internal state should remain unchanged
Assertions.assertEquals(10, miniMax.getScores()[0]);
}
@Test
void testMiniMaxWithAllSameScores() {
int[] sameScores = {5, 5, 5, 5};
miniMax.setScores(sameScores);
// When all scores are the same, result should be that score
int result = miniMax.miniMax(0, true, 0, false);
Assertions.assertEquals(5, result);
}
@Test
void testMiniMaxAtDifferentDepths() {
int[] testScores = {3, 12, 8, 2, 14, 5, 2, 9};
miniMax.setScores(testScores);
// Test maximizer first
int result = miniMax.miniMax(0, true, 0, false);
// Expected: max(min(max(3,12), max(8,2)), min(max(14,5), max(2,9)))
// = max(min(12, 8), min(14, 9)) = max(8, 9) = 9
Assertions.assertEquals(9, result);
}
@Test
void testMiniMaxWithMinIntAndMaxInt() {
int[] extremeScores = {Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 1};
miniMax.setScores(extremeScores);
int result = miniMax.miniMax(0, true, 0, false);
// Expected: max(min(MIN, MAX), min(0, 1)) = max(MIN, 0) = 0
Assertions.assertEquals(0, result);
}
}