Board tests added

master
Elliott LE GUEHENNEC 2 years ago
parent 309d217751
commit 13d96fa74a

@ -0,0 +1,55 @@
import model.Board;
import model.Player;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Random;
import java.util.stream.Stream;
public class BoardTests {
public static Stream<Arguments> getSetBoxTestData() {
Random rng = RandomSingleton.get;
int col = rng.nextInt();
int row = rng.nextInt();
boolean shouldThrow = col >= 1 && col <= 3 && row >= 1 && row <= 3;
return Stream.of(
Arguments.of(1, 1, false),
Arguments.of(1, 3, false),
Arguments.of(0, 2, true),
Arguments.of(4, 2, true),
Arguments.of(2, 0, true),
Arguments.of(2, 4, true),
Arguments.of(col, row, shouldThrow)
);
}
@Test
public void constructorInitialisesValuesToNull(){
Board b = new Board();
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
Assertions.assertNull(b.getBox(i, j));
}
@ParameterizedTest
@MethodSource("getSetBoxTestData")
public void getSetBoxTests(Object[] args){
Board b = new Board();
Player p = Player.X;
int col = (int) args[0];
int row = (int) args[1];
boolean shouldThrow = (boolean) args[2];
if (shouldThrow){
Assertions.assertThrows(IndexOutOfBoundsException.class, ()-> b.setBox(col, row, p));
Assertions.assertThrows(IndexOutOfBoundsException.class, ()-> b.getBox(col, row));
}
else {
b.setBox(col, row, p);
Assertions.assertEquals(p, b.getBox(col, row));
}
}
}

@ -9,10 +9,8 @@ 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();
int val = RandomSingleton.get.nextInt();
boolean isValid = val >= 1 && val <= 9;
return Stream.of(
new Object[]{1, true},
@ -25,7 +23,7 @@ public class InputCheckerTests {
public static Stream<Object[]> boxOccupiedTestData() {
Board b = randomBoard();
int index = rng.nextInt(9) + 1;
int index = RandomSingleton.get.nextInt(9) + 1;
boolean isValid = true;
for (int i = 1; i <= 3; ++i)
@ -60,7 +58,7 @@ public class InputCheckerTests {
Board b = new Board();
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j) {
int rand = rng.nextInt(3);
int rand = RandomSingleton.get.nextInt(3);
Player p = switch (rand){
case 1 -> Player.X;
case 2 -> Player.O;

@ -0,0 +1,6 @@
import java.util.Random;
public class RandomSingleton {
public static Random get = new Random();
}

@ -12,7 +12,7 @@ import java.util.Random;
import java.util.stream.Stream;
public class WinCheckerTests {
private final static Random rng = new Random();
private final static Random rng = RandomSingleton.get;
public static Stream<Arguments> isGameOverTestData() {
Board randomBoard = new Board();

Loading…
Cancel
Save