-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChickenAttackerAreaTest.java
More file actions
518 lines (465 loc) · 20.5 KB
/
ChickenAttackerAreaTest.java
File metadata and controls
518 lines (465 loc) · 20.5 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package ch.epfl.chacun;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class ChickenAttackerAreaTest {
@Test
void areaIsImmutable() {
var immutableZones = Set.of(new Zone.Meadow(0, List.of(), null));
var zones = new HashSet<Zone>(immutableZones);
var immutableOccupants = List.of(PlayerColor.RED);
var occupants = new ArrayList<>(immutableOccupants);
var area = new Area<>(zones, occupants, 0);
zones.clear();
assertEquals(immutableZones, area.zones());
occupants.clear();
assertEquals(immutableOccupants, area.occupants());
try {
area.zones().clear();
} catch (UnsupportedOperationException e) {
// Expected
}
assertEquals(immutableZones, area.zones());
try {
area.occupants().clear();
} catch (UnsupportedOperationException e) {
// Expected
}
assertEquals(immutableOccupants, area.occupants());
}
@Test
void areaConstructorSortsOccupants() {
var sortedOccupants = List.of(
PlayerColor.RED,
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.YELLOW,
PlayerColor.YELLOW,
PlayerColor.YELLOW,
PlayerColor.YELLOW,
PlayerColor.PURPLE);
var rng = new Random(2024);
for (int i = 0; i < 10; i += 1) {
var shuffledOccupants = new ArrayList<>(sortedOccupants);
Collections.shuffle(shuffledOccupants, rng);
var area = new Area<>(Set.of(), shuffledOccupants, 0);
assertEquals(sortedOccupants, area.occupants());
}
}
@Test
void areaConstructorThrowsOnNegativeOpenConnections() {
assertThrows(IllegalArgumentException.class, () -> {
new Area<>(Set.of(), List.of(), -1);
});
}
@Test
void areaHasMenhirWorks() {
var nonMenhirForests = List.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(1, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN),
new Zone.Forest(3, Zone.Forest.Kind.PLAIN),
new Zone.Forest(4, Zone.Forest.Kind.PLAIN),
new Zone.Forest(5, Zone.Forest.Kind.WITH_MUSHROOMS),
new Zone.Forest(6, Zone.Forest.Kind.WITH_MUSHROOMS),
new Zone.Forest(7, Zone.Forest.Kind.WITH_MUSHROOMS),
new Zone.Forest(8, Zone.Forest.Kind.WITH_MUSHROOMS));
var menhirForests = List.of(
new Zone.Forest(9, Zone.Forest.Kind.WITH_MENHIR),
new Zone.Forest(10, Zone.Forest.Kind.WITH_MENHIR));
for (int i = 0; i < nonMenhirForests.size(); i += 1) {
var forests = Set.copyOf(nonMenhirForests.subList(0, i));
var area = new Area<>(forests, List.of(), 0);
assertFalse(Area.hasMenhir(area));
}
for (int i = 0; i < nonMenhirForests.size(); i += 1) {
var forests = new HashSet<>(nonMenhirForests.subList(0, i));
for (int j = 1; j < menhirForests.size(); j += 1) {
forests.addAll(menhirForests.subList(0, j));
var area = new Area<>(forests, List.of(), 0);
assertTrue(Area.hasMenhir(area));
}
}
}
@Test
void areaMushroomGroupCountWorks() {
var nonMushroomForests = List.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(1, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN),
new Zone.Forest(3, Zone.Forest.Kind.PLAIN),
new Zone.Forest(4, Zone.Forest.Kind.PLAIN),
new Zone.Forest(9, Zone.Forest.Kind.WITH_MENHIR),
new Zone.Forest(10, Zone.Forest.Kind.WITH_MENHIR));
var mushroomForests = List.of(
new Zone.Forest(5, Zone.Forest.Kind.WITH_MUSHROOMS),
new Zone.Forest(6, Zone.Forest.Kind.WITH_MUSHROOMS),
new Zone.Forest(7, Zone.Forest.Kind.WITH_MUSHROOMS),
new Zone.Forest(8, Zone.Forest.Kind.WITH_MUSHROOMS));
for (int i = 0; i < nonMushroomForests.size(); i += 1) {
var forests = Set.copyOf(nonMushroomForests.subList(0, i));
var area = new Area<>(forests, List.of(), 0);
assertEquals(0, Area.mushroomGroupCount(area));
}
for (int i = 0; i < nonMushroomForests.size(); i += 1) {
var forests = new HashSet<>(nonMushroomForests.subList(0, i));
for (int j = 0; j < mushroomForests.size(); j += 1) {
forests.addAll(mushroomForests.subList(0, j));
var area = new Area<>(forests, List.of(), 0);
assertEquals(j, Area.mushroomGroupCount(area));
}
}
}
@Test
void areaAnimalsWorks() {
var rng = new Random(2024);
for (int i = 0; i < 50; i += 1) {
var animals = new ArrayList<Animal>();
for (int j = 0; j < i; j += 1) {
var animalKind = Animal.Kind.values()[rng.nextInt(Animal.Kind.values().length)];
animals.add(new Animal(j, animalKind));
}
var expectedAnimals = Set.copyOf(animals);
var cancelledAnimals = new HashSet<Animal>();
for (int j = 0; j < rng.nextInt(20); j += 1) {
var animalKind = Animal.Kind.values()[rng.nextInt(Animal.Kind.values().length)];
cancelledAnimals.add(new Animal(100 + j, animalKind));
}
var immutableCancelledAnimals = Set.copyOf(cancelledAnimals);
animals.addAll(cancelledAnimals);
var meadows = new HashSet<Zone.Meadow>();
for (int j = 0; j < rng.nextInt(animals.size()); j += 1) {
var animalCount = rng.nextInt(animals.size());
var subAnimals = animals.subList(0, animalCount);
meadows.add(new Zone.Meadow(j, List.copyOf(subAnimals), null));
subAnimals.clear();
}
meadows.add(new Zone.Meadow(i, List.copyOf(animals), null));
var area = new Area<>(meadows, List.of(), 0);
assertEquals(expectedAnimals, Area.animals(area, immutableCancelledAnimals));
}
}
@Test
void areaRiverFishCountWorksWithoutLake() {
var rivers = Set.of(
new Zone.River(0, 0, null),
new Zone.River(1, 1, null),
new Zone.River(2, 2, null),
new Zone.River(3, 3, null),
new Zone.River(4, 2, null),
new Zone.River(5, 1, null),
new Zone.River(6, 0, null));
var area = new Area<>(rivers, List.of(), 0);
assertEquals(9, Area.riverFishCount(area));
}
@Test
void areaRiverFishCountWorksWithTwoDifferentLakes() {
var rivers = Set.of(
new Zone.River(0, 0, new Zone.Lake(8, 1, null)),
new Zone.River(1, 1, null),
new Zone.River(2, 2, null),
new Zone.River(3, 3, null),
new Zone.River(4, 2, null),
new Zone.River(5, 1, null),
new Zone.River(6, 0, new Zone.Lake(9, 2, null)));
var area = new Area<>(rivers, List.of(), 0);
assertEquals(12, Area.riverFishCount(area));
}
@Test
void areaRiverFishCountWorksWithOneSharedLake() {
var lake = new Zone.Lake(8, 1, null);
var rivers = Set.of(
new Zone.River(0, 0, lake),
new Zone.River(1, 1, null),
new Zone.River(2, 2, null),
new Zone.River(3, 3, null),
new Zone.River(4, 2, null),
new Zone.River(5, 1, null),
new Zone.River(6, 0, lake));
var area = new Area<>(rivers, List.of(), 0);
assertEquals(10, Area.riverFishCount(area));
}
@Test
void areaRiverSystemFishCountWorks() {
var lake1 = new Zone.Lake(8, 2, null);
var lake2 = new Zone.Lake(9, 3, null);
var waterZones = Set.<Zone.Water>of(
lake1,
new Zone.River(0, 0, lake1),
new Zone.River(1, 1, lake1),
new Zone.River(2, 2, lake1),
new Zone.River(3, 3, lake1),
new Zone.River(4, 2, lake2),
new Zone.River(5, 1, lake2),
new Zone.River(6, 0, null),
lake2);
var area = new Area<>(waterZones, List.of(), 0);
assertEquals(14, Area.riverSystemFishCount(area));
}
@Test
void areaLakeCountWorks() {
var lake1 = new Zone.Lake(8, 2, null);
var lake2 = new Zone.Lake(9, 3, null);
var lake3 = new Zone.Lake(18, 2, null);
var waterZones = Set.<Zone.Water>of(
lake1,
new Zone.River(0, 0, lake1),
new Zone.River(1, 1, lake1),
new Zone.River(2, 2, lake1),
new Zone.River(3, 3, lake3),
lake3,
new Zone.River(4, 2, lake2),
new Zone.River(5, 1, lake2),
new Zone.River(6, 0, null),
lake2);
var area = new Area<>(waterZones, List.of(), 0);
assertEquals(3, Area.lakeCount(area));
}
@Test
void areaIsClosedWorks() {
assertTrue(new Area<>(Set.of(), List.of(), 0).isClosed());
for (int i = 1; i < 10; i += 1) assertFalse(new Area<>(Set.of(), List.of(), i).isClosed());
}
@Test
void areaIsOccupiedWorks() {
assertFalse(new Area<>(Set.of(), List.of(), 0).isOccupied());
for (int i = 1; i < PlayerColor.ALL.size(); i += 1)
assertTrue(new Area<>(Set.of(), PlayerColor.ALL.subList(0, i), 0).isOccupied());
}
@Test
void areaMajorityOccupantsReturnsEmptySetWhenOccupantsIsEmpty() {
var area = new Area<>(Set.of(), List.of(), 0);
assertEquals(Set.of(), area.majorityOccupants());
}
@Test
void areaMajorityOccupantsWorksWithSingleOccupant() {
var area = new Area<>(Set.of(), List.of(PlayerColor.RED), 0);
assertEquals(Set.of(PlayerColor.RED), area.majorityOccupants());
}
@Test
void areaMajorityOccupantWorksWithThreeEqualOccupants() {
var occupants = List.of(
PlayerColor.RED,
PlayerColor.RED,
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.BLUE,
PlayerColor.BLUE,
PlayerColor.GREEN,
PlayerColor.GREEN,
PlayerColor.GREEN);
var area = new Area<>(Set.of(), occupants, 0);
assertEquals(
Set.of(PlayerColor.RED, PlayerColor.BLUE, PlayerColor.GREEN),
area.majorityOccupants());
}
@Test
void areaMajorityOccupantsWorksInGeneralCase() {
var occupants = List.of(
PlayerColor.RED,
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.GREEN,
PlayerColor.GREEN,
PlayerColor.GREEN,
PlayerColor.YELLOW,
PlayerColor.YELLOW,
PlayerColor.YELLOW);
var area = new Area<>(Set.of(), occupants, 0);
assertEquals(
Set.of(PlayerColor.GREEN, PlayerColor.YELLOW),
area.majorityOccupants());
}
@Test
void areaConnectToWorksWhenConnectingAreaWithItself() {
var zones = List.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(1, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN),
new Zone.Forest(3, Zone.Forest.Kind.PLAIN),
new Zone.Forest(4, Zone.Forest.Kind.PLAIN));
var occupants = List.of(
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.GREEN);
for (int i = 2; i < zones.size(); i += 1) {
var areaZones = Set.copyOf(zones.subList(0, i));
for (int j = 0; j < occupants.size(); j += 1) {
var areaOccupants = (List<PlayerColor>) new ArrayList<>(occupants.subList(0, j + 1));
Collections.sort(areaOccupants);
areaOccupants = Collections.unmodifiableList(areaOccupants);
for (int openConnections = 2; openConnections < 5; openConnections += 1) {
var area = new Area<>(areaZones, areaOccupants, openConnections);
var area1 = area.connectTo(area);
assertEquals(areaZones, area1.zones());
assertEquals(areaOccupants, area1.occupants());
assertEquals(openConnections - 2, area1.openConnections());
}
}
}
}
@Test
void areaConnectToWorksWhenConnectingTwoDifferentAreas() {
var zones = List.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(1, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN),
new Zone.Forest(3, Zone.Forest.Kind.PLAIN),
new Zone.Forest(4, Zone.Forest.Kind.PLAIN),
new Zone.Forest(5, Zone.Forest.Kind.PLAIN),
new Zone.Forest(6, Zone.Forest.Kind.PLAIN),
new Zone.Forest(7, Zone.Forest.Kind.PLAIN));
var occupants = List.of(
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.RED,
PlayerColor.BLUE,
PlayerColor.GREEN);
var expectedZones = Set.copyOf(zones);
var expectedOccupants = (List<PlayerColor>) new ArrayList<>(occupants);
Collections.sort(expectedOccupants);
expectedOccupants = Collections.unmodifiableList(expectedOccupants);
for (int i = 1; i < zones.size() - 1; i += 1) {
var areaZones1 = Set.copyOf(zones.subList(0, i));
var areaZones2 = Set.copyOf(zones.subList(i, zones.size()));
for (int j = 0; j < occupants.size(); j += 1) {
var areaOccupants1 = occupants.subList(0, j);
var areaOccupants2 = occupants.subList(j, occupants.size());
for (int openConnections = 1; openConnections < 5; openConnections += 1) {
var area1 = new Area<>(areaZones1, areaOccupants1, openConnections);
var area2 = new Area<>(areaZones2, areaOccupants2, openConnections);
var area12 = area1.connectTo(area2);
assertEquals(expectedZones, area12.zones());
assertEquals(expectedOccupants, area12.occupants());
assertEquals(2 * openConnections - 2, area12.openConnections());
}
}
}
}
@Test
void areaWithInitialOccupantThrowsIfAreaAlreadyOccupied() {
var area = new Area<>(
Set.of(new Zone.Forest(0, Zone.Forest.Kind.PLAIN)),
List.of(PlayerColor.RED),
1);
assertThrows(IllegalArgumentException.class, () -> {
area.withInitialOccupant(PlayerColor.RED);
});
}
@Test
void areaWithInitialOccupantWorksWithUnoccupiedArea() {
var unoccupiedArea = new Area<>(
Set.of(new Zone.Forest(0, Zone.Forest.Kind.PLAIN)),
List.of(),
1);
for (var occupant : PlayerColor.values()) {
var occupiedArea = unoccupiedArea.withInitialOccupant(occupant);
assertEquals(List.of(occupant), occupiedArea.occupants());
}
}
@Test
void areaWithoutOccupantThrowsIfOccupantDoesNotExist() {
var possibleOccupants = List.of(PlayerColor.values());
for (var occupant : possibleOccupants) {
var unoccupiedArea = new Area<>(
Set.of(new Zone.Forest(0, Zone.Forest.Kind.PLAIN)),
List.of(),
1);
assertThrows(IllegalArgumentException.class, () -> {
unoccupiedArea.withoutOccupant(occupant);
});
var occupiedArea = new Area<>(
Set.of(new Zone.Forest(0, Zone.Forest.Kind.PLAIN)),
List.of(occupant),
1);
var wrongOccupant = possibleOccupants.get((occupant.ordinal() + 1) % possibleOccupants.size());
assertThrows(IllegalArgumentException.class, () -> {
occupiedArea.withoutOccupant(wrongOccupant);
});
}
}
@Test
void areaWithoutOccupantWorks() {
var possibleOccupants = List.of(PlayerColor.values());
var area = new Area<>(
Set.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(1, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN)),
possibleOccupants,
5);
for (int i = 0; i < possibleOccupants.size(); i += 1) {
var occupantToRemove = possibleOccupants.get(i);
var area1 = area.withoutOccupant(occupantToRemove);
var expectedOccupants = new ArrayList<>(possibleOccupants);
expectedOccupants.remove(i);
assertEquals(area.zones(), area1.zones());
assertEquals(area.openConnections(), area1.openConnections());
assertEquals(expectedOccupants, area1.occupants());
}
}
@Test
void areaWithoutOccupantsWorks() {
var possibleOccupants = List.of(PlayerColor.values());
var area = new Area<>(
Set.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(1, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN)),
possibleOccupants,
5);
var area1 = area.withoutOccupants();
assertEquals(area.zones(), area1.zones());
assertEquals(area.openConnections(), area1.openConnections());
assertEquals(List.of(), area1.occupants());
}
@Test
void areaTileIdsWorks() {
var zones = Set.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN),
new Zone.Forest(3, Zone.Forest.Kind.PLAIN),
new Zone.Forest(10, Zone.Forest.Kind.PLAIN),
new Zone.Forest(13, Zone.Forest.Kind.PLAIN),
new Zone.Forest(20, Zone.Forest.Kind.PLAIN),
new Zone.Forest(21, Zone.Forest.Kind.PLAIN),
new Zone.Forest(22, Zone.Forest.Kind.PLAIN),
new Zone.Forest(23, Zone.Forest.Kind.PLAIN),
new Zone.Forest(30, Zone.Forest.Kind.PLAIN));
var area = new Area<>(zones, List.of(), 0);
assertEquals(Set.of(0, 1, 2, 3), area.tileIds());
}
@Test
void areaZoneWithSpecialPowerWorksWithInexistantSpecialPower() {
var zones = Set.of(
new Zone.Forest(0, Zone.Forest.Kind.PLAIN),
new Zone.Forest(2, Zone.Forest.Kind.PLAIN),
new Zone.Forest(3, Zone.Forest.Kind.PLAIN),
new Zone.Forest(10, Zone.Forest.Kind.PLAIN),
new Zone.Forest(13, Zone.Forest.Kind.PLAIN),
new Zone.Forest(20, Zone.Forest.Kind.PLAIN),
new Zone.Forest(21, Zone.Forest.Kind.PLAIN),
new Zone.Forest(22, Zone.Forest.Kind.PLAIN),
new Zone.Forest(23, Zone.Forest.Kind.PLAIN),
new Zone.Forest(30, Zone.Forest.Kind.PLAIN));
var area = new Area<>(zones, List.of(), 0);
for (var specialPower : Zone.SpecialPower.values())
assertNull(area.zoneWithSpecialPower(specialPower));
}
@Test
void areaZoneWithSpecialPowerFindsCorrectZone() {
var zones = List.of(
new Zone.Meadow(0, List.of(), null),
new Zone.Meadow(1, List.of(), Zone.SpecialPower.SHAMAN),
new Zone.Meadow(2, List.of(), null),
new Zone.Meadow(3, List.of(), Zone.SpecialPower.PIT_TRAP),
new Zone.Meadow(4, List.of(), null),
new Zone.Meadow(5, List.of(), Zone.SpecialPower.WILD_FIRE));
var area = new Area<>(Set.copyOf(zones), List.of(), 0);
assertEquals(zones.get(1), area.zoneWithSpecialPower(Zone.SpecialPower.SHAMAN));
assertEquals(zones.get(3), area.zoneWithSpecialPower(Zone.SpecialPower.PIT_TRAP));
assertEquals(zones.get(5), area.zoneWithSpecialPower(Zone.SpecialPower.WILD_FIRE));
}
}