From a3fe99cce408a267d8c47a18a462d2e8beb9f692 Mon Sep 17 00:00:00 2001 From: Chantelle Perreau <cperreau@myune.edu.au> Date: Sun, 11 Jul 2021 16:15:39 +1000 Subject: [PATCH] Tests added for known bugs --- .../dotsandboxes/DotsAndBoxesGridTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946bed..4bf9fcf 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Assumptions.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.Random; + public class DotsAndBoxesGridTest { /* * Because Test classes are classes, they can have fields, and can have static fields. @@ -28,4 +30,54 @@ public class DotsAndBoxesGridTest { } // FIXME: You need to write tests for the two known bugs in the code. + + @Test + public void testBoxComplete() { + logger.info("Test to show that game identifies when box is complete"); + Random rand= new Random(); + int width = rand.nextInt(100)+3; + int height = rand.nextInt(100)+3; + DotsAndBoxesGrid testGrid = new DotsAndBoxesGrid(width, height, 2); + int x = rand.nextInt(testGrid.width-1); + int y = rand.nextInt(testGrid.height-1); + int a = rand.nextInt(testGrid.width-1); + int b = rand.nextInt(testGrid.height-1); + int c = rand.nextInt(testGrid.width-1); + int d = rand.nextInt(testGrid.height-1); + // A complete square which should assert to True + testGrid.drawHorizontal(x, y, 1); + testGrid.drawHorizontal(x, y+1, 1); + testGrid.drawVertical(x, y, 1); + testGrid.drawVertical(x+1, y, 2); + // An incomplete square which should assert to False + testGrid.drawHorizontal(a, b, 2); + testGrid.drawVertical(a+1, b, 1); + testGrid.drawVertical(a, b, 1); + assertTrue(testGrid.boxComplete(x, y)); + assertFalse(testGrid.boxComplete(a, b)); + // An unattempted square which should assert to False + assertFalse(testGrid.boxComplete(c, d)); + } + + @Test + public void testLineAlreadyDrawn() { + logger.info("Test to make sure exception is thrown when playing tries to draw an line that has already been drawn"); + DotsAndBoxesGrid testGrid = new DotsAndBoxesGrid(15, 8, 2); + Random rand= new Random(); + int x = rand.nextInt(testGrid.width-1); + int y = rand.nextInt(testGrid.height-1); + int a = rand.nextInt(testGrid.width-1); + int b = rand.nextInt(testGrid.height-1); + testGrid.drawHorizontal(x, y, 1); + testGrid.drawVertical(a, b, 2); + assertThrows(IllegalStateException.class, () -> testGrid.drawHorizontal(x,y, 1), "There " + + "is already a line drawn here."); + assertThrows(IllegalStateException.class, () -> testGrid.drawHorizontal(x,y, 2), "There " + + "is already a line drawn here."); + assertThrows(IllegalStateException.class, () -> testGrid.drawVertical(a,b, 1), "There " + + "is already a line drawn here."); + assertThrows(IllegalStateException.class, () -> testGrid.drawVertical(a,b, 2), "There " + + "is already a line drawn here."); + + } } -- GitLab