package comp1110.ass1;
import org.junit.jupiter.api.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Timeout(value = 1000, unit = MILLISECONDS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FindLeashTest {
private void test(Objective obj, String placement, Location[] expected) {
WalkTheDog game = new WalkTheDog(obj);
Location[] output = WalkTheDog.findLeash(placement, game.getTree());
assertTrue(areLocationArraysEqual(expected, output), “Expected findLeash to be ” + printLocArray(expected) +
” for objective string ‘” + obj.getInitialState() +
“‘ and placement string ‘” + placement +
“‘ but got ” + printLocArray(output) + “.”);
}
private boolean areLocationArraysEqual(Location[] array1, Location[] array2) {
if (array1.length != array2.length)
return false;
for (int i = 0; i < array1.length; i++) {
if (!array1[i].equals(array2[i]))
return false;
}
return true;
}
private String printLocArray(Location[] array) {
String output = "[";
for (int i = 0; i < array.length; i++) {
if (i != 0)
output += ", ";
output += "(" + array[i].getX() + "," + array[i].getY() + ")";
}
return output += "]";
}
@Test
public void testSimple() {
Objective obj = new Objective("", 1);
// Test green dog placements
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
test(obj, x + "" + y, new Location[]{new Location(x, y)});
}
}
}
@Test
public void testHorizontalRight() {
Objective obj = new Objective("", 1);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
for (int x_diff = 1; x_diff + x < 4; x_diff++) {
Location[] expected = new Location[x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
expected[i] = new Location(x + i, y);
}
test(obj, x + "" + y + "" + (x + x_diff) + "" + y, expected);
}
}
}
}
@Test
public void testHorizontalLeft() {
Objective obj = new Objective("", 1);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
for (int x_diff = 1; x_diff + x < 4; x_diff++) {
Location[] expected = new Location[x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
expected[x_diff - i] = new Location(x + i, y);
}
test(obj, (x + x_diff) + "" + y + "" + x + "" + y, expected);
}
}
}
}
@Test
public void testVerticalUp() {
Objective obj = new Objective("", 1);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int y_diff = 1; y + y_diff < 4; y_diff++) {
Location[] expected = new Location[y_diff + 1];
for (int i = 0; i <= y_diff; i++) {
expected[i] = new Location(x, y + i);
}
test(obj, x + "" + y + "" + x + "" + (y + y_diff), expected);
}
}
}
}
@Test
public void testVerticalDown() {
Objective obj = new Objective("", 1);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int y_diff = 1; y + y_diff < 4; y_diff++) {
Location[] expected = new Location[y_diff + 1];
for (int i = 0; i <= y_diff; i++) {
expected[y_diff - i] = new Location(x, y + i);
}
test(obj, x + "" + (y + y_diff) + "" + x + "" + y, expected);
}
}
}
}
@Test
public void testTreeRightDown() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int x_diff = 1; x + x_diff < 4; x_diff++) {
for (int y_diff = 1; y + y_diff < 4 && x_diff + y_diff < 4; y_diff++) {
Objective obj = new Objective("T" + (x + x_diff) + "" + y, 1);
Location[] expected = new Location[y_diff + x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
expected[i] = new Location(x + i, y);
}
for (int i = 1; i <= y_diff; i++) {
expected[i + x_diff] = new Location(x + x_diff, y + i);
}
test(obj, x + "" + y + "" + (x + x_diff) + "" + (y + y_diff), expected);
}
}
}
}
}
@Test
public void testTreeRightUp() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int x_diff = 1; x + x_diff < 4; x_diff++) {
for (int y_diff = 1; y + y_diff < 4 && x_diff + y_diff < 4; y_diff++) {
Objective obj = new Objective("T" + (x + x_diff) + "" + (y+y_diff), 1);
Location[] expected = new Location[y_diff + x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
expected[i] = new Location(x + i, y + y_diff);
}
for (int i = 0; i <= y_diff - 1; i++) {
expected[y_diff - i + x_diff] = new Location(x + x_diff, y + i);
}
test(obj, x + "" + (y + y_diff) + "" + (x + x_diff) + "" + y, expected);
}
}
}
}
}
@Test
public void testTreeLeftDown() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int x_diff = 1; x + x_diff < 4; x_diff++) {
for (int y_diff = 1; y + y_diff < 4 && x_diff + y_diff < 4; y_diff++) {
Objective obj = new Objective("T" + x + "" + y, 1);
Location[] expected = new Location[y_diff + x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
expected[x_diff-i] = new Location(x + i, y);
}
for (int i = 1; i <= y_diff; i++) {
expected[i + x_diff] = new Location(x, y + i);
}
test(obj, (x+x_diff) + "" + y + "" + x + "" + (y + y_diff), expected);
}
}
}
}
}
@Test
public void testTreeLeftUp() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 3; y++) {
for (int x_diff = 1; x + x_diff < 4; x_diff++) {
for (int y_diff = 1; y + y_diff < 4 && x_diff + y_diff < 4; y_diff++) {
Objective obj = new Objective("T" + x + "" + (y+y_diff), 1);
Location[] expected = new Location[y_diff + x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
expected[x_diff-i] = new Location(x + i, y+y_diff);
}
for (int i = 0; i <= y_diff - 1; i++) {
expected[y_diff - i + x_diff] = new Location(x, y + i);
}
test(obj, (x+x_diff) + "" + (y+y_diff) + "" + x + "" + y, expected);
}
}
}
}
}
}