You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tictactoe/test/InputCheckerTests.java

94 lines
2.8 KiB

import model.Board;
import model.InputChecker;
import model.Player;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Random;
import java.util.stream.Stream;
public class InputCheckerTests {
static Random rng = new Random();
public static Stream<Object[]> validIndexTestData() {
int val = rng.nextInt();
boolean isValid = val >= 1 && val <= 9;
return Stream.of(
new Object[]{1, true},
new Object[]{9, true},
new Object[]{-3, false},
new Object[]{15, false},
new Object[]{val, isValid}
);
}
public static Stream<Object[]> boxOccupiedTestData() {
Board b = randomBoard();
int index = rng.nextInt(9) + 1;
boolean isValid = true;
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
if (b.getBox(i, j) != null)
isValid = false;
return Stream.of(
new Object[]{new Board(), 1, true},
new Object[]{fullBoard(), 1, false},
new Object[]{topLeftBoard(), 1, false},
new Object[]{topLeftBoard(), 3, true},
new Object[]{b, index, isValid}
);
}
private static Board fullBoard(){
Board b = new Board();
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
b.setBox(i, j, Player.X);
return b;
}
private static Board topLeftBoard(){
Board b = new Board();
b.setBox(1, 1, Player.X);
return b;
}
private static Board randomBoard(){
Board b = new Board();
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j) {
int rand = rng.nextInt(3);
Player p = switch (rand){
case 1 -> Player.X;
case 2 -> Player.O;
default -> null;
};
b.setBox(i, j, p);
}
return b;
}
@ParameterizedTest
@MethodSource("validIndexTestData")
public void validIndexTest(Object[] args){
Board b = new Board();
int val = (int) args[0];
boolean expected = (boolean) args[1];
boolean actual = new InputChecker().isInputValid(b, val);
Assertions.assertEquals(expected, actual);
}
@ParameterizedTest
@MethodSource("boxOccupiedTestData")
public void boxOccupiedTest(Object[] args){
Board b = (Board) args[0];
int input = (int) args[1];
boolean expected = (boolean) args[2];
boolean actual = new InputChecker().isInputValid(b, input);
Assertions.assertEquals(expected, actual);
}
}