Skip to content

Commit 354607c

Browse files
committed
Add test confiming unit#getDistance works as expected
1 parent e14d930 commit 354607c

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666
<version>4.12</version>
6767
<scope>test</scope>
6868
</dependency>
69+
70+
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
71+
<dependency>
72+
<groupId>org.mockito</groupId>
73+
<artifactId>mockito-core</artifactId>
74+
<version>2.23.4</version>
75+
<scope>test</scope>
76+
</dependency>
6977
</dependencies>
7078

7179
</project>

src/main/java/bwapi/Unit.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public Position getPosition() {
9191
}
9292

9393
public int getX() {
94-
return position.x;
94+
return getPosition().x;
9595
}
9696

9797
public int getY() {
98-
return position.y;
98+
return getPosition().y;
9999
}
100100

101101
public TilePosition getTilePosition() {
@@ -122,19 +122,19 @@ public Region getRegion() {
122122
}
123123

124124
public int getLeft() {
125-
return position.x - getType().dimensionLeft();
125+
return getX() - getType().dimensionLeft();
126126
}
127127

128128
public int getTop() {
129-
return position.y - getType().dimensionUp();
129+
return getY() - getType().dimensionUp();
130130
}
131131

132132
public int getRight() {
133-
return position.x + getType().dimensionRight();
133+
return getX() + getType().dimensionRight();
134134
}
135135

136136
public int getBottom() {
137-
return position.y + getType().dimensionDown();
137+
return getY() + getType().dimensionDown();
138138
}
139139

140140
public int getHitPoints() {

src/test/java/bwapi/UnitTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)