|
| 1 | +package bwapi; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | + |
| 14 | +public class UnitTest { |
| 15 | + /** |
| 16 | + * Test data generated using BWMirror with: |
| 17 | + * |
| 18 | + * <pre> |
| 19 | + * {@code |
| 20 | + * List<Unit> mins = mirror.getGame().getStaticMinerals().stream().filter(Unit::isVisible).collect(Collectors.toList()); |
| 21 | + * System.out.println("{" + mins.stream().map(m -> "{" + m.getX() + ", " + m.getY() + "}").collect(Collectors.joining(", ")) + "}"); |
| 22 | + * mirror.getGame().self().getUnits().forEach( u -> { |
| 23 | + * System.out.print(u.getType() + ": " + u.getPosition()); |
| 24 | + * System.out.println(" {" + mins.stream().map(m -> ""+u.getDistance(m)).collect(Collectors.joining(", ")) + "}"); |
| 25 | + * }); |
| 26 | + * } |
| 27 | + * </pre> |
| 28 | + * |
| 29 | + * Output: |
| 30 | + * {{96, 144}, {96, 208}, {64, 240}, {96, 272}, {64, 176}, {128, 400}, {64, 304}, {96, 368}, {96, 336}} |
| 31 | + * Terran_SCV: [288, 296] {185, 160, 181, 149, 203, 137, 181, 154, 149} |
| 32 | + * Terran_SCV: [264, 296] {164, 139, 157, 125, 181, 115, 157, 133, 125} |
| 33 | + * Terran_SCV: [312, 296] {208, 183, 205, 173, 225, 159, 205, 177, 173} |
| 34 | + * Terran_SCV: [240, 296] {155, 116, 133, 101, 158, 96, 133, 110, 101} |
| 35 | + * Terran_Command_Center: [288, 240] {109, 102, 134, 102, 134, 121, 134, 121, 109} |
| 36 | + * |
| 37 | + */ |
| 38 | + @Test |
| 39 | + public void checkDistance() { |
| 40 | + final int[][] minPos = new int[][]{ |
| 41 | + {96, 144}, {96, 208}, {64, 240}, {96, 272}, {64, 176}, {128, 400}, {64, 304}, {96, 368}, {96, 336} |
| 42 | + }; |
| 43 | + List<Unit> minerals = Arrays.stream(minPos) |
| 44 | + .map(m -> createUnit(UnitType.Resource_Mineral_Field, new Position(m[0], m[1]))) |
| 45 | + .collect(Collectors.toList()); |
| 46 | + |
| 47 | + HashMap<Unit, List<Integer>> units = new HashMap<>(); // Unit with respective resulting distance array |
| 48 | + units.put(createUnit(UnitType.Terran_SCV, new Position(288, 296)), Arrays.asList(185, 160, 181, 149, 203, 137, 181, 154, 149)); |
| 49 | + units.put(createUnit(UnitType.Terran_SCV, new Position(264, 296)), Arrays.asList(164, 139, 157, 125, 181, 115, 157, 133, 125)); |
| 50 | + units.put(createUnit(UnitType.Terran_SCV, new Position(312, 296)), Arrays.asList(208, 183, 205, 173, 225, 159, 205, 177, 173)); |
| 51 | + units.put(createUnit(UnitType.Terran_SCV, new Position(240, 296)), Arrays.asList(155, 116, 133, 101, 158, 96, 133, 110, 101)); |
| 52 | + units.put(createUnit(UnitType.Terran_Command_Center, new Position(288, 240)), Arrays.asList(109, 102, 134, 102, 134, 121, 134, 121, 109)); |
| 53 | + |
| 54 | + for (Unit unit : units.keySet()) { |
| 55 | + List<Integer> results = units.get(unit); |
| 56 | + assertEquals(minerals.size(), results.size()); |
| 57 | + for (int i=0; i < minerals.size(); i++) { |
| 58 | + Unit mineral = minerals.get(i); |
| 59 | + when(unit.getDistance(mineral)).thenCallRealMethod(); |
| 60 | + int calculated = unit.getDistance(mineral); |
| 61 | + int expected = results.get(i); |
| 62 | + assertEquals(calculated, expected); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private Unit createUnit(UnitType type, Position position) { |
| 68 | + Unit u = mock(Unit.class); |
| 69 | + when(u.getType()).thenReturn(type); |
| 70 | + when(u.getPosition()).thenReturn(position); |
| 71 | + when(u.exists()).thenReturn(true); |
| 72 | + when(u.getLeft()).thenCallRealMethod(); |
| 73 | + when(u.getRight()).thenCallRealMethod(); |
| 74 | + when(u.getTop()).thenCallRealMethod(); |
| 75 | + when(u.getBottom()).thenCallRealMethod(); |
| 76 | + when(u.getX()).thenCallRealMethod(); |
| 77 | + when(u.getY()).thenCallRealMethod(); |
| 78 | + return u; |
| 79 | + } |
| 80 | +} |
0 commit comments