Skip to content

Commit 22d32a7

Browse files
author
Bytekeeper
committed
Swap x/y in unit test (also render path and waypoints correctly)
1 parent 437d267 commit 22d32a7

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/test/java/org/bk/ass/path/JpsTest.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package org.bk.ass.path;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
3+
import org.junit.jupiter.api.Test;
44

5-
import java.awt.Color;
6-
import java.awt.Graphics;
5+
import javax.imageio.ImageIO;
6+
import java.awt.*;
77
import java.awt.image.BufferedImage;
88
import java.io.File;
99
import java.io.IOException;
1010
import java.util.SplittableRandom;
11-
import javax.imageio.ImageIO;
12-
import org.junit.jupiter.api.Test;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
1313

1414
class JpsTest {
1515

@@ -67,7 +67,7 @@ void shouldFindDiagonalPath() {
6767
Jps sut =
6868
new Jps(
6969
Map.fromBooleanArray(
70-
new boolean[][]{{true, true, true}, {true, true, true}, {true, true, true}}));
70+
new boolean[][]{{true, true, true}, {true, true, true}, {true, true, true}}));
7171

7272
// WHEN
7373
Result result = sut.findPath(new Position(2, 2), new Position(0, 0));
@@ -82,7 +82,7 @@ void shouldFindPathWithObstacle() {
8282
Jps sut =
8383
new Jps(
8484
Map.fromBooleanArray(
85-
new boolean[][]{{true, true, true}, {true, false, false}, {true, true, true}}));
85+
new boolean[][]{{true, true, true}, {true, false, false}, {true, true, true}}));
8686

8787
// WHEN
8888
Result result = sut.findPath(new Position(2, 2), new Position(0, 0));
@@ -99,12 +99,12 @@ void shouldNotFindPathWhenBlockedButWithCircle() {
9999
Jps sut =
100100
new Jps(
101101
Map.fromBooleanArray(
102-
new boolean[][]{
103-
{true, true, true, true, true},
104-
{true, false, false, false, true},
105-
{true, false, true, false, true},
106-
{true, false, false, false, true},
107-
{true, true, true, true, true}
102+
new boolean[][]{
103+
{true, true, true, true, true},
104+
{true, false, false, false, true},
105+
{true, false, true, false, true},
106+
{true, false, false, false, true},
107+
{true, true, true, true, true}
108108
}));
109109

110110
// WHEN
@@ -120,12 +120,12 @@ void shouldFindPathWhenCircleHasHole() {
120120
Jps sut =
121121
new Jps(
122122
Map.fromBooleanArray(
123-
new boolean[][]{
124-
{true, true, true, true, true},
125-
{true, false, false, false, true},
126-
{true, false, true, false, true},
127-
{true, false, false, true, true},
128-
{true, true, true, true, true}
123+
new boolean[][]{
124+
{true, true, true, true, true},
125+
{true, false, false, false, true},
126+
{true, false, true, false, true},
127+
{true, false, false, true, true},
128+
{true, true, true, true, true}
129129
}));
130130

131131
// WHEN
@@ -146,8 +146,8 @@ public boolean isWalkable(int x, int y) {
146146
return y >= 0
147147
&& y <= 999
148148
&& (x == 0 && y % 4 == 1
149-
|| x == 999 && y % 4 == 3
150-
|| y % 2 == 0 && x >= 0 && x <= 999);
149+
|| x == 999 && y % 4 == 3
150+
|| y % 2 == 0 && x >= 0 && x <= 999);
151151
}
152152

153153
@Override
@@ -173,16 +173,16 @@ void shouldFindPathInDemoMap() throws IOException {
173173
// GIVEN
174174
BufferedImage image = ImageIO.read(JpsTest.class.getResourceAsStream("/dungeon_map.bmp"));
175175
boolean[][] data = new boolean[image.getHeight()][image.getWidth()];
176-
for (int y = 0; y < image.getHeight(); y++) {
177-
for (int x = 0; x < image.getWidth(); x++) {
178-
data[y][x] = image.getRGB(x, y) == -1;
176+
for (int x = 0; x < image.getWidth(); x++) {
177+
for (int y = 0; y < image.getHeight(); y++) {
178+
data[x][y] = image.getRGB(x, y) == -1;
179179
}
180180
}
181181
Map map = Map.fromBooleanArray(data);
182182
Jps sut = new Jps(map);
183183
SplittableRandom rnd = new SplittableRandom(123456);
184184
Result result = null;
185-
for (int i = 0; i < 1000; i++) {
185+
for (int i = 0; i < 500; i++) {
186186
Position start;
187187
do {
188188
start = new Position(rnd.nextInt(image.getWidth()), rnd.nextInt(image.getHeight()));
@@ -201,14 +201,18 @@ void shouldFindPathInDemoMap() throws IOException {
201201
new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
202202
Graphics g = out.getGraphics();
203203
g.drawImage(image, 0, 0, null);
204-
g.setColor(Color.GREEN);
205204
Position last = null;
205+
g.setColor(Color.GREEN);
206206
for (Position p : result.path) {
207207
if (last != null) {
208208
g.drawLine(last.x, last.y, p.x, p.y);
209209
}
210210
last = p;
211211
}
212+
g.setColor(Color.RED);
213+
for (Position p : result.path) {
214+
g.drawLine(p.x, p.y, p.x, p.y);
215+
}
212216
ImageIO.write(out, "PNG", new File("build/map_with_path.png"));
213217
}
214218
}

0 commit comments

Comments
 (0)