CS计算机代考程序代写 Java junit package comp1110.ass1;

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 CheckLeashTest {

private void test(Objective obj, String placement, int leashLength, boolean expected) {
WalkTheDog game = new WalkTheDog(obj);
boolean output = game.checkLeash(placement, leashLength);
assertEquals(expected, output, “Expected checkLeash to return ” + expected +
” for objective state ‘” + obj.getInitialState() +
“‘, placement string ‘” + placement +
“‘, and leash length ” + leashLength +
” but got ” + output + “.”);
}

@Test
public void testSimple() {
Objective obj = new Objective(“”, 1);

test(obj, “”, 0, false);
test(obj, “1”, 0, false);
for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { test(obj, i + "" + j, 0, true); } } } @Test public void testHorizontalLeash() { Objective empty_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 <= 3; x_diff++) { String placement1 = x + "" + y + "" + (x + x_diff) + "" + y; String placement2 = (x + x_diff) + "" + y + "" + x + "" + y; for (int tree_x = 0; tree_x < 3; tree_x++) { for (int tree_y = 0; tree_y < 3; tree_y++) { if (!(tree_y == y && (tree_x == x || tree_x == x + x_diff))) { Objective tree_obj = new Objective("T" + tree_x + "" + tree_y, 1); for (int i = 0; i < 4; i++) { boolean expected = i == x_diff; test(empty_obj, placement1, i, expected); test(empty_obj, placement2, i, expected); test(tree_obj, placement1, i, expected); test(tree_obj, placement2, i, expected); } } } } } } } } @Test public void testVerticalLeash() { Objective empty_obj = new Objective("", 1); for (int y = 0; y < 3; y++) { for (int x = 0; x < 4; x++) { for (int y_diff = 1; y_diff + y <= 3; y_diff++) { String placement1 = x+""+y+""+x + "" + (y+y_diff); String placement2 = x+""+(y+y_diff)+""+x+""+y; for (int tree_x = 0; tree_x < 3; tree_x++) { for (int tree_y = 0; tree_y < 3; tree_y++){ if (!(tree_x == x && (tree_y == y || tree_y == y + y_diff))) { Objective tree_obj = new Objective("T"+tree_x + ""+tree_y, 1); for (int i = 0; i < 4; i ++) { boolean expected = i == y_diff; test(empty_obj, placement1, i, expected); test(empty_obj, placement2, i, expected); test(tree_obj, placement1, i, expected); test(tree_obj, placement2, i, expected); } } } } } } } } @Test public void testAroundTree() { for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { for (int x_diff = 1; x_diff + x <= 3; x_diff++) { for (int y_diff = 1; x_diff + y_diff <= 3 && y + y_diff <=3; y_diff++) { String placement1 = x + "" + y + "" + (x + x_diff) + "" + (y+y_diff); String placement2 = (x + x_diff) + "" + (y+y_diff) + "" + x + "" + y; String placement3 = (x + x_diff) + "" + y + "" + x + "" + (y+y_diff); String placement4 = x + "" + (y+y_diff) + "" + (x + x_diff) + "" + y; // Invalid, no tree Objective no_tree = new Objective("",1); test(no_tree, placement1, x_diff+y_diff, false); test(no_tree, placement2, x_diff+y_diff, false); // Tree at all 4 corners Objective obj1 = new Objective("T"+x +""+(y+y_diff),1); Objective obj2 = new Objective("T"+(x+x_diff) +""+y,1); Objective obj3 = new Objective("T"+x +""+y,1); Objective obj4 = new Objective("T"+(x+x_diff) +""+(y+y_diff),1); for (int i = 0; i < 4; i++) { boolean expected = i == x_diff + y_diff; for (String placement : new String[]{placement1, placement2}) { test(obj1, placement, i, expected); test(obj2, placement, i, expected); test(obj3, placement, i, false); test(obj4, placement, i, false); } for (String placement : new String[]{placement3, placement4}) { test(obj1, placement, i, false); test(obj2, placement, i, false); test(obj3, placement, i, expected); test(obj4, placement, i, expected); } } } } } } } }