From 13c3b093241f7536c13a4e192fdb991418995cd0 Mon Sep 17 00:00:00 2001 From: Kalpana Acharya <kachary3@myune.edu.au> Date: Tue, 6 Aug 2024 14:26:27 +1000 Subject: [PATCH] Add unit tests to detect assignment errors --- .../dotsandboxes/DotsAndBoxesGridTest.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java index 1946bed..72ff77f 100644 --- a/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java +++ b/src/test/java/dotsandboxes/DotsAndBoxesGridTest.java @@ -2,8 +2,6 @@ package dotsandboxes; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assumptions.*; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -27,5 +25,41 @@ public class DotsAndBoxesGridTest { assertTrue(true); } - // FIXME: You need to write tests for the two known bugs in the code. + // Test cases for known bugs + + @Test + public void testSquareCompletionBug() { + logger.info("Testing square completion logic"); + + // Create a new game grid + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(); + + // Draw lines to form a square + grid.drawLine(0, 0, 0, 1); // Top horizontal line + grid.drawLine(0, 0, 1, 0); // Left vertical line + grid.drawLine(0, 1, 1, 1); // Bottom horizontal line + grid.drawLine(1, 0, 1, 1); // Right vertical line + + // Test if the square at (0, 0) is complete + boolean isComplete = grid.isSquareComplete(0, 0); + + // The bug is that isSquareComplete always returns true + assertFalse(isComplete, "The square completion check should fail because the method is broken."); + } + + @Test + public void testDrawLineTwiceThrowsException() { + logger.info("Testing drawing the same line twice throws exception"); + + // Create a new game grid + DotsAndBoxesGrid grid = new DotsAndBoxesGrid(); + + // Draw a line once + grid.drawLine(0, 0, 0, 1); + + // Attempt to draw the same line again + assertThrows(IllegalStateException.class, () -> { + grid.drawLine(0, 0, 0, 1); + }, "Drawing the same line twice should throw IllegalStateException."); + } } -- GitLab