From 13d96fa74aa4dfe7c0c03c2988881643eae72c09 Mon Sep 17 00:00:00 2001 From: elleguehen Date: Fri, 10 Feb 2023 13:43:33 +0100 Subject: [PATCH] Board tests added --- test/BoardTests.java | 55 +++++++++++++++++++++++++++++++++++++ test/InputCheckerTests.java | 8 ++---- test/RandomSingleton.java | 6 ++++ test/WinCheckerTests.java | 2 +- 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 test/BoardTests.java create mode 100644 test/RandomSingleton.java diff --git a/test/BoardTests.java b/test/BoardTests.java new file mode 100644 index 0000000..d7c6e76 --- /dev/null +++ b/test/BoardTests.java @@ -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 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)); + } + } +} diff --git a/test/InputCheckerTests.java b/test/InputCheckerTests.java index 5aaee88..df90e9e 100644 --- a/test/InputCheckerTests.java +++ b/test/InputCheckerTests.java @@ -9,10 +9,8 @@ import java.util.Random; import java.util.stream.Stream; public class InputCheckerTests { - static Random rng = new Random(); - public static Stream 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 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; diff --git a/test/RandomSingleton.java b/test/RandomSingleton.java new file mode 100644 index 0000000..d2b9839 --- /dev/null +++ b/test/RandomSingleton.java @@ -0,0 +1,6 @@ +import java.util.Random; + +public class RandomSingleton { + public static Random get = new Random(); + +} diff --git a/test/WinCheckerTests.java b/test/WinCheckerTests.java index 524f29e..e7242b2 100644 --- a/test/WinCheckerTests.java +++ b/test/WinCheckerTests.java @@ -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 isGameOverTestData() { Board randomBoard = new Board();