package comp1110.ass1;
import org.junit.jupiter.api.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Timeout(value = 1000, unit = MILLISECONDS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LocationTask3Test {
private void locationEqualsTest(Location input, Location other, boolean expected) {
boolean output = input.equals(other);
assertEquals(expected, output, “Expected equals called on Location (” + input.getX() +
“,” + input.getY() + “) and Location (” + other.getX() + “,” +
+ other.getY() + “) to return ” + expected +
“, but got ” + output + “.”);
}
private void manhattanDistanceTest(Location input, Location other, int expected) {
int output = input.manhattanDistance(other);
assertEquals(expected, output, “Expected manhattan distance called on Location (” + input.getX() +
“,” + input.getY() + “) and Location (” + other.getX() + “,” +
+ other.getY() + “) to return ” + expected +
“, but got ” + output + “.”);
}
private void isAdjacentTest(Location input, Location other, boolean expected) {
boolean output = input.isAdjacent(other);
assertEquals(expected, output, “Expected isAdjacent called on Location (” + input.getX() +
“,” + input.getY() + “) and Location (” + other.getX() + “,” +
+ other.getY() + “) to return ” + expected +
“, but got ” + output + “.”);
}
@Test
public void testLocationEquals() {
for (int x1 = 0; x1 < 4; x1++) {
for (int y1 = 0; y1 < 4; y1++) {
for (int x2 = 0; x2 < 4; x2++) {
for (int y2 = 0; y2 < 4; y2++) {
locationEqualsTest(new Location(x1, y1), new Location(x2, y2), x1==x2 && y1==y2);
}
}
}
}
}
@Test
public void testManhattanDistance() {
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
for (int x_diff = 0; x_diff < 10; x_diff++) {
for (int y_diff = 0; y_diff < 10; y_diff++) {
manhattanDistanceTest(new Location(x, y), new Location(x + x_diff, y + y_diff), x_diff + y_diff);
manhattanDistanceTest(new Location(x, y), new Location(x - x_diff, y + y_diff), x_diff + y_diff);
}
}
}
}
}
@Test
public void testIsAdjacent() {
// Test same x and y
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
for (int diff = 0; diff < 10; diff++) {
boolean expected = diff == 1;
isAdjacentTest(new Location(x, y), new Location(x + diff, y), expected);
isAdjacentTest(new Location(x, y), new Location(x - diff, y), expected);
isAdjacentTest(new Location(x, y), new Location(x, y + diff), expected);
isAdjacentTest(new Location(x, y), new Location(x, y - diff), expected);
}
}
}
// Test non-adjacent
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
for (int x_diff = 1; x_diff < 10; x_diff++) {
for (int y_diff = 1; y_diff < 10; y_diff++) {
isAdjacentTest(new Location(x, y), new Location(x + x_diff, y + y_diff), false);
isAdjacentTest(new Location(x, y), new Location(x - x_diff, y + y_diff), false);
isAdjacentTest(new Location(x, y), new Location(x + x_diff, y - y_diff), false);
isAdjacentTest(new Location(x, y), new Location(x - x_diff, y - y_diff), false);
}
}
}
}
}
}