diff --git a/src/main/java/edu/ntnu/idatt2003/model/Matrix2x2.java b/src/main/java/edu/ntnu/idatt2003/model/Matrix2x2.java index 7fafa8bf627362f5bb617e9fbc45ceff750175f5..139a41fe61be2de8a28ac0dc357c646e3cbde9f8 100644 --- a/src/main/java/edu/ntnu/idatt2003/model/Matrix2x2.java +++ b/src/main/java/edu/ntnu/idatt2003/model/Matrix2x2.java @@ -51,6 +51,6 @@ public class Matrix2x2 implements Serializable { */ @Override public String toString() { - return String.format(Locale.ENGLISH, "%f, %f, %f, %f", a00, a01, a10, a11); + return String.format(Locale.ENGLISH, "%.2f, %.2f, %.2f, %.2f", a00, a01, a10, a11); } } diff --git a/src/test/java/edu/ntnu/idatt2003/model/AffineTransform2dTest.java b/src/test/java/edu/ntnu/idatt2003/model/AffineTransform2dTest.java index f27d0666db3aec3688bbf4f362eb50e66e560693..3df27a3b2f05a1b1d320c3ce870d8b8d757db13a 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/AffineTransform2dTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/AffineTransform2dTest.java @@ -1,21 +1,24 @@ package edu.ntnu.idatt2003.model; import static org.junit.jupiter.api.Assertions.assertEquals; - -import edu.ntnu.idatt2003.model.AffineTransform2D; -import edu.ntnu.idatt2003.model.Matrix2x2; -import edu.ntnu.idatt2003.model.Vector2d; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +/** + * Test class for AffineTransform2D, covering positive test cases. + */ public class AffineTransform2dTest { private static Matrix2x2 a1; private static Vector2d v1; private static Vector2d b1; + /** + * Sets up the test environment before all tests. + * Initializes the Matrix2x2 and Vector2d objects. + */ @BeforeAll static void setUp() { a1 = new Matrix2x2(2, 2, 4, 5); @@ -23,9 +26,17 @@ public class AffineTransform2dTest { b1 = new Vector2d(3, 4); } + /** + * Positive test cases for AffineTransform2D. + */ @Nested @DisplayName("Positive tests") public class PositiveTests { + + /** + * Tests the transform method of AffineTransform2D. + * Verifies that the transformation is correctly applied. + */ @Test @DisplayName("transformTestAffine") void testTransform() { @@ -34,12 +45,6 @@ public class AffineTransform2dTest { assertEquals(9, v3.getX0()); assertEquals(18, v3.getX1()); - } } - - - - - } diff --git a/src/test/java/edu/ntnu/idatt2003/model/ChaosCanvasTest.java b/src/test/java/edu/ntnu/idatt2003/model/ChaosCanvasTest.java index 7eca7dac0b9eb68947859f0bcb6807f31a22e3f8..66ef43105a2aa603ab497a4a303244bf4fb1299b 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ChaosCanvasTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ChaosCanvasTest.java @@ -1,66 +1,168 @@ package edu.ntnu.idatt2003.model; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +/** + * Test class for ChaosCanvas, covering both positive and negative test cases. + */ public class ChaosCanvasTest { - private static ChaosCanvas canvas; - private static final int WIDTH = 10; - private static final int HEIGHT = 10; + private ChaosCanvas chaosCanvas; @BeforeEach public void setUp() { Vector2d minCoords = new Vector2d(0, 0); - Vector2d maxCoords = new Vector2d(100, 100); // Example coordinates - canvas = new ChaosCanvas(WIDTH, HEIGHT, minCoords, maxCoords); + Vector2d maxCoords = new Vector2d(10, 10); + chaosCanvas = new ChaosCanvas(10, 10, minCoords, maxCoords); } - @Test - public void testGetPixel() { - assertEquals(0, canvas.getPixel(new Vector2d(0, 0))); - assertEquals(0, canvas.getPixel(new Vector2d(5, 5))); - assertEquals(0, canvas.getPixel(new Vector2d(90, 50))); - } + /** + * Positive test cases for ChaosCanvas. + */ + @Nested + @DisplayName("Positive Tests") + class PositiveTests { - @Test - public void testPutPixel() { - Vector2d point1 = new Vector2d(50, 50); - Vector2d point2 = new Vector2d(5, 5); - Vector2d point3 = new Vector2d(75, 30); - canvas.putPixel(point1); - canvas.putPixel(point2); - canvas.putPixel(point3); - assertEquals(1, canvas.getPixel(point1)); - assertEquals(1, canvas.getPixel(point2)); - assertEquals(1, canvas.getPixel(point3)); - } + /** + * Tests the constructor of ChaosCanvas. + */ + @Test + @DisplayName("Constructor Test") + public void testConstructor() { + assertNotNull(chaosCanvas); + assertEquals(10, chaosCanvas.getWidth()); + assertEquals(10, chaosCanvas.getHeight()); + assertNotNull(chaosCanvas.getCanvasArray()); + } + + /** + * Tests getting a pixel within bounds. + */ + @Test + @DisplayName("Get Pixel Test") + public void testGetPixel() { + Vector2d point = new Vector2d(5, 5); + assertEquals(0, chaosCanvas.getPixel(point)); + + chaosCanvas.putPixel(point); + assertEquals(1, chaosCanvas.getPixel(point)); + } + + /** + * Tests putting a pixel within bounds. + */ + @Test + @DisplayName("Put Pixel Test") + public void testPutPixel() { + Vector2d point = new Vector2d(5, 5); + chaosCanvas.putPixel(point); + assertEquals(1, chaosCanvas.getPixel(point)); + } + + /** + * Tests getting the canvas array. + */ + @Test + @DisplayName("Get Canvas Array Test") + public void testGetCanvasArray() { + int[][] canvasArray = chaosCanvas.getCanvasArray(); + assertNotNull(canvasArray); + assertEquals(10, canvasArray.length); + assertEquals(10, canvasArray[0].length); + } + + /** + * Tests clearing the canvas. + */ + @Test + @DisplayName("Clear Canvas Test") + public void testClear() { + Vector2d point = new Vector2d(5, 5); + chaosCanvas.putPixel(point); + assertEquals(1, chaosCanvas.getPixel(point)); - @Test - public void testGetCanvasArray() { - int[][] canvasArray = canvas.getCanvasArray(); - assertEquals(WIDTH, canvasArray.length); - assertEquals(HEIGHT, canvasArray[0].length); + chaosCanvas.clear(); + assertEquals(0, chaosCanvas.getPixel(point)); + } + + /** + * Tests getting the height of the canvas. + */ + @Test + @DisplayName("Get Height Test") + public void testGetHeight() { + assertEquals(10, chaosCanvas.getHeight()); + } + + /** + * Tests getting the width of the canvas. + */ + @Test + @DisplayName("Get Width Test") + public void testGetWidth() { + assertEquals(10, chaosCanvas.getWidth()); + } + + /** + * Tests transforming coordinates to indices. + */ + @Test + @DisplayName("Transform Coords to Indices Test") + public void testTransformCoordsToIndicesIntegration() { + Vector2d point = new Vector2d(5, 5); + chaosCanvas.putPixel(point); + assertEquals(1, chaosCanvas.getPixel(point)); + + Vector2d minPoint = new Vector2d(0, 0); + chaosCanvas.putPixel(minPoint); + assertEquals(1, chaosCanvas.getPixel(minPoint)); + + Vector2d maxPoint = new Vector2d(10, 10); + chaosCanvas.putPixel(maxPoint); + assertEquals(1, chaosCanvas.getPixel(maxPoint)); + } + + /** + * Tests displaying the canvas. + */ + @Test + @DisplayName("Show Canvas Test") + public void testShowCanvas() { + chaosCanvas.showCanvas(); + } } - @Test - public void testClear() { - // Put some pixels first - Vector2d point1 = new Vector2d(10, 10); - Vector2d point2 = new Vector2d(20, 20); - canvas.putPixel(point1); - canvas.putPixel(point2); - - // Clear the canvas - canvas.clear(); - - // Check if all pixels are cleared (set to 0) - for (int i = 0; i < WIDTH; i++) { - for (int j = 0; j < HEIGHT; j++) { - assertEquals(0, canvas.getPixel(new Vector2d(i, j))); - } + /** + * Negative test cases for ChaosCanvas. + */ + @Nested + @DisplayName("Negative Tests") + class NegativeTests { + + /** + * Tests getting a pixel out of bounds. + */ + @Test + @DisplayName("Get Pixel Out of Bounds Test") + public void testGetPixelOutOfBounds() { + Vector2d outOfBoundsPoint = new Vector2d(20, 20); + assertEquals(0, chaosCanvas.getPixel(outOfBoundsPoint)); + } + + /** + * Tests putting a pixel out of bounds. + */ + @Test + @DisplayName("Put Pixel Out of Bounds Test") + public void testPutPixelOutOfBounds() { + Vector2d outOfBoundsPoint = new Vector2d(20, 20); + chaosCanvas.putPixel(outOfBoundsPoint); + assertEquals(0, chaosCanvas.getPixel(outOfBoundsPoint)); } } } \ No newline at end of file diff --git a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionFactoryTest.java b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionFactoryTest.java index 3a1288ece0daf994fddd3a544224b602c0543462..bf9590aaf1f1cffa8d76641d55fc6199c311bf93 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionFactoryTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionFactoryTest.java @@ -1,36 +1,108 @@ package edu.ntnu.idatt2003.model; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test class for ChaosGameDescriptionFactory, covering both positive and negative test cases. + */ public class ChaosGameDescriptionFactoryTest { - private ChaosGameDescription sierpinskiDescription; - private ChaosGameDescription barnsleyFernDescription; - private ChaosGameDescription juliaDescription; + /** + * Positive test cases for ChaosGameDescriptionFactory. + */ + @Nested + @DisplayName("Positive Tests") + class PositiveTests { - @BeforeEach - public void setup() { - sierpinskiDescription = ChaosGameDescriptionFactory.get(ChaosGameDescriptionFactory.descriptionTypeEnum.SIERPINSKI_TRIANGLE); - barnsleyFernDescription = ChaosGameDescriptionFactory.get(ChaosGameDescriptionFactory.descriptionTypeEnum.BARNSLEY_FERN); - juliaDescription = ChaosGameDescriptionFactory.get(ChaosGameDescriptionFactory.descriptionTypeEnum.JULIA); - } + /** + * Tests getting the Sierpinski Triangle description. + */ + @Test + @DisplayName("Get Sierpinski Triangle Test") + public void testGetSierpinskiTriangle() { + ChaosGameDescription description = ChaosGameDescriptionFactory.get(ChaosGameDescriptionFactory.descriptionTypeEnum.SIERPINSKI_TRIANGLE); - @Test - public void testSierpinskiTriangleDescription() { - assertNotNull(sierpinskiDescription.toString()); - } + assertNotNull(description); + assertEquals(new Vector2d(0, 0).toString(), description.getMinCoords().toString()); + assertEquals(new Vector2d(1, 1).toString(), description.getMaxCoords().toString()); + + List<Transform2D> transforms = description.getTransform(); + assertEquals(3, transforms.size()); + + assertInstanceOf(AffineTransform2D.class, transforms.get(0)); + assertInstanceOf(AffineTransform2D.class, transforms.get(1)); + assertInstanceOf(AffineTransform2D.class, transforms.get(2)); + } + + /** + * Tests getting the Barnsley Fern description. + */ + @Test + @DisplayName("Get Barnsley Fern Test") + public void testGetBarnsleyFern() { + ChaosGameDescription description = ChaosGameDescriptionFactory.get(ChaosGameDescriptionFactory.descriptionTypeEnum.BARNSLEY_FERN); + + assertNotNull(description); + assertEquals(new Vector2d(-2.5, 0).toString(), description.getMinCoords().toString()); + assertEquals(new Vector2d(2.5, 10).toString(), description.getMaxCoords().toString()); + + List<Transform2D> transforms = description.getTransform(); + assertEquals(4, transforms.size()); - @Test - public void testBarnsleyFernDescription() { - assertNotNull(barnsleyFernDescription); + assertInstanceOf(AffineTransform2D.class, transforms.get(0)); + assertInstanceOf(AffineTransform2D.class, transforms.get(1)); + assertInstanceOf(AffineTransform2D.class, transforms.get(2)); + assertInstanceOf(AffineTransform2D.class, transforms.get(3)); + } + + /** + * Tests getting the Julia transformation description. + */ + @Test + @DisplayName("Get Julia Transformation Test") + public void testGetJuliaTransformation() { + ChaosGameDescription description = ChaosGameDescriptionFactory.get(ChaosGameDescriptionFactory.descriptionTypeEnum.JULIA); + + assertNotNull(description); + assertEquals(new Vector2d(-1.6, -1).toString(), description.getMinCoords().toString()); + assertEquals(new Vector2d(1.6, 1).toString(), description.getMaxCoords().toString()); + + List<Transform2D> transforms = description.getTransform(); + assertEquals(2, transforms.size()); + + assertInstanceOf(JuliaTransform.class, transforms.get(0)); + assertInstanceOf(JuliaTransform.class, transforms.get(1)); + } } - @Test - public void testJuliaDescription() { - assertNotNull(juliaDescription); + /** + * Negative test cases for ChaosGameDescriptionFactory. + */ + @Nested + @DisplayName("Negative Tests") + class NegativeTests { + + /** + * Tests getting a custom transformation file that does not exist. + */ + @Test + @DisplayName("Get Custom Transformation File Not Found Test") + public void testGetCustomTransformationFileNotFound() { + Exception exception = assertThrows(RuntimeException.class, () -> + ChaosGameDescriptionFactory.getCustom("non_existent_transformation") + ); + + String expectedMessage = "File src/main/resources/transformations/non_existent_transformation.txt not found."; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } } } @@ -38,4 +110,3 @@ public class ChaosGameDescriptionFactoryTest { - diff --git a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionTest.java b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionTest.java index f8cc09c864750e0d0ba223f503c62b4b36d1b80b..41cd920ac3f82e45f349f109de8b37699dcef961 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameDescriptionTest.java @@ -9,22 +9,39 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; +/** + * Test class for ChaosGameDescription. + */ + class ChaosGameDescriptionTest { - private static ChaosGameDescription chaosGameDescription; private static List<Transform2D> transforms; + private static ChaosGameDescription chaosGameDescription; + private static ChaosGameDescription chaosGameDescriptionJulia; + private static List<Transform2D> juliaTransforms; + /** + * Sets up the test environment before all tests. + * Initializes the list of transformations and the ChaosGameDescription object. + */ @BeforeAll static void setUp() { transforms = new ArrayList<>(); - transforms.add(new JuliaTransform(new Complex(1, 2), 1)); transforms.add(new AffineTransform2D(new Matrix2x2(1, 2, 3, 4), new Vector2d(3, 4))); chaosGameDescription = new ChaosGameDescription(new Vector2d(1, 2), new Vector2d(3, 4), transforms); - } + juliaTransforms = new ArrayList<>(); + juliaTransforms.add(new JuliaTransform(new Complex(1, 2), 1)); + + chaosGameDescriptionJulia = new ChaosGameDescription(new Vector2d(1, 2), new Vector2d(3, 4), juliaTransforms); + } + /** + * Tests the getMinCoords() method of ChaosGameDescription. + * Verifies that the minimum coordinates are correctly retrieved. + */ @Test @DisplayName("Test getMinCoords()") void testGetMinCoords() { @@ -32,6 +49,10 @@ class ChaosGameDescriptionTest { assertEquals(2, chaosGameDescription.getMinCoords().getX1()); } + /** + * Tests the getMaxCoords() method of ChaosGameDescription. + * Verifies that the maximum coordinates are correctly retrieved. + */ @Test @DisplayName("Test getMaxCoords()") void testGetMaxCoords() { @@ -39,9 +60,34 @@ class ChaosGameDescriptionTest { assertEquals(4, chaosGameDescription.getMaxCoords().getX1()); } + /** + * Tests the getTransform() method of ChaosGameDescription. + * Verifies that the list of transformations is correctly retrieved. + */ @Test @DisplayName("Test getTransform()") void testGetTransform() { assertEquals(transforms, chaosGameDescription.getTransform()); } + + /** + * Tests the toString() method of ChaosGameDescription. + * Verifies the string representation of the ChaosGameDescription for both AffineTransform2D and JuliaTransform cases. + */ + @Test + @DisplayName("Test toString() with AffineTransform2D and JuliaTransform") + void testToString() { + String expectedOutputAffine = "Affine2D\n" + + "1.0, 2.0\n" + + "3.0, 4.0\n" + + "1.00, 2.00, 3.00, 4.00, 3.0, 4.0\n"; + + String expectedOutputJulia = "Julia\n" + + "1.0, 2.0\n" + + "3.0, 4.0\n" + + "1.0, 2.0\n"; + + assertEquals(expectedOutputAffine, chaosGameDescription.toString()); + assertEquals(expectedOutputJulia, chaosGameDescriptionJulia.toString()); + } } \ No newline at end of file diff --git a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameFileHandlerTest.java b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameFileHandlerTest.java index 2921dc91ecd18a1fc2c2dccd14ca7c3c47686072..6768c48edef0697b7ecf377d2f29577d1c543f0d 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameFileHandlerTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameFileHandlerTest.java @@ -9,6 +9,9 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; +/** + * Test class for ChaosGameFileHandler, covering both positive and negative test cases. + */ class ChaosGameFileHandlerTest { private static ChaosGameFileHandler fileHandler; private static String affine2dTextFilePath; @@ -16,38 +19,54 @@ class ChaosGameFileHandlerTest { private static ChaosGameDescription affine2DDescription; private static ChaosGameDescription juliaDescription; + /** + * Sets up the test environment before all tests. + * Initializes the ChaosGameFileHandler, file paths, and ChaosGameDescription objects. + */ @BeforeAll static void setUp() { fileHandler = new ChaosGameFileHandler(); affine2dTextFilePath = "src/test/resources/Affine2DExample.txt"; List<Transform2D> affine2DTransform = List.of( - new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2d(0, 0)), - new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2d(0.25, 0.5)), - new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2d(0.5, 0)) + new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2d(0, 0)), + new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2d(0.25, 0.5)), + new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2d(0.5, 0)) ); affine2DDescription = new ChaosGameDescription( - new Vector2d(0, 0), new Vector2d(1, 1), affine2DTransform); + new Vector2d(0, 0), new Vector2d(1, 1), affine2DTransform); juliaTextFilePath = "src/test/resources/JuliaExample.txt"; juliaDescription = new ChaosGameDescription( - new Vector2d(-1.6, -1), new Vector2d(1.6, 1), - List.of(new JuliaTransform(new Complex(-0.74543, 0.11301), 1) - ,(new JuliaTransform(new Complex(-0.74543, 0.11301), 1)))); + new Vector2d(-1.6, -1), new Vector2d(1.6, 1), + List.of(new JuliaTransform(new Complex(-0.74543, 0.11301), 1), + new JuliaTransform(new Complex(-0.74543, 0.11301), 1))); } + + /** + * Cleans up the test environment after each test. + * Deletes any output files created during the tests. + */ @AfterEach void cleanUp() { try { - Files.deleteIfExists(Paths.get(("src/test/resources/Affine2DExampleOutput.txt"))); - Files.deleteIfExists(Paths.get(("src/test/resources/JuliaExampleOutput.txt"))); - } catch (Exception e) { - fail(e.getMessage()); + Files.deleteIfExists(Paths.get("src/test/resources/Affine2DExampleOutput.txt")); + Files.deleteIfExists(Paths.get("src/test/resources/JuliaExampleOutput.txt")); + } catch (Exception e) { + fail(e.getMessage()); } } + /** + * Positive test cases for ChaosGameFileHandler. + */ @Nested @DisplayName("Positive tests") class PositiveTests { + + /** + * Tests reading from a file with Affine2D transformations. + */ @Test - @DisplayName("Test readFromfile with Affine2DTransform") + @DisplayName("Test readFromFile with Affine2DTransform") void testReadFromFileWithAffine2D() { try { assertEquals(affine2DDescription.toString(), fileHandler.readFromFile(affine2dTextFilePath).toString()); @@ -56,8 +75,11 @@ class ChaosGameFileHandlerTest { } } + /** + * Tests reading from a file with Julia transformations. + */ @Test - @DisplayName("Test readFromfile with JuliaTransform") + @DisplayName("Test readFromFile with JuliaTransform") void testReadFromFileWithJulia() { try { assertEquals(juliaDescription.toString(), fileHandler.readFromFile(juliaTextFilePath).toString()); @@ -65,6 +87,10 @@ class ChaosGameFileHandlerTest { fail(e.getMessage()); } } + + /** + * Tests writing to a file with Affine2D transformations and then reading from it. + */ @Test @DisplayName("Test writeToFile with Affine2DTransform") void testWriteToFileWithAffine2D() { @@ -75,6 +101,10 @@ class ChaosGameFileHandlerTest { fail(e.getMessage()); } } + + /** + * Tests writing to a file with Julia transformations and then reading from it. + */ @Test @DisplayName("Test writeToFile with JuliaTransform") void testWriteToFileWithJulia() { @@ -87,9 +117,17 @@ class ChaosGameFileHandlerTest { } } + /** + * Negative test cases for ChaosGameFileHandler. + */ @Nested @DisplayName("Negative tests") class NegativeTests { + + /** + * Tests reading from a non-existing file. + * Verifies that a FileNotFoundException is thrown. + */ @Test @DisplayName("Test readChaosGameDescription with non-existing file") void testReadChaosGameDescriptionWithNonExistingFile() { diff --git a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java index 1e15d590294601fd256016016a212217c5ce2b71..f55d88a52b49641af2cbef808f1026e0d85e6054 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ChaosGameTest.java @@ -1,38 +1,215 @@ package edu.ntnu.idatt2003.model; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.*; -import java.util.List; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; +import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test class for ChaosGame, covering both positive and negative test cases. + */ public class ChaosGameTest { - private static ChaosGame cg; + private ChaosGame chaosGame; + private ChaosGameDescription description; + private TestObserver observer; + + /** + * Sets up the test environment before each test. + * Initializes the ChaosGame with a description and registers an observer. + */ + @BeforeEach + public void setUp() { - @BeforeAll - public static void setup() { + AffineTransform2D transform1 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(1, 1)); + AffineTransform2D transform2 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(-1, -1)); - Vector2d minCoords = new Vector2d(0, 0); - Vector2d maxCoords = new Vector2d(100, 100); - Transform2D transform = new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), - new Vector2d(0, 0)); - ChaosGameDescription description = new ChaosGameDescription(minCoords, maxCoords, - List.of(transform)); + description = new ChaosGameDescription(new Vector2d(0, 0), new Vector2d(10, 10), Arrays.asList(transform1, transform2)); - cg = new ChaosGame(description, 100, 100); - cg.runStepsAndUpdateTotal(100); + chaosGame = new ChaosGame(description, 10, 10); + + observer = new TestObserver(); + chaosGame.registerObserver(observer); } + /** + * Positive test cases for ChaosGame. + */ @Nested - @DisplayName("Positive tests") - public class PositiveTests { + @DisplayName("Positive Tests") + class PositiveTests { + + /** + * Tests the constructor of ChaosGame. + * Verifies that the ChaosGame object is properly initialized. + */ + @Test + @DisplayName("Constructor Test") + public void testConstructor() { + assertNotNull(chaosGame); + assertEquals(10, chaosGame.getCanvas().getWidth()); + assertEquals(10, chaosGame.getCanvas().getHeight()); + } + + /** + * Tests the getCanvas() method of ChaosGame. + * Verifies that the canvas is correctly retrieved. + */ + @Test + @DisplayName("Get Canvas Test") + public void testGetCanvas() { + assertNotNull(chaosGame.getCanvas()); + } + + /** + * Tests the getTotalSteps() method of ChaosGame. + * Verifies that the total steps are initially zero. + */ + @Test + @DisplayName("Get Total Steps Test") + public void testGetTotalSteps() { + assertEquals(0, chaosGame.getTotalSteps()); + } + + /** + * Tests running steps and updating the total steps. + * Verifies that the total steps are updated correctly. + */ + @Test + @DisplayName("Run Steps and Update Total Test") + public void testRunStepsAndUpdateTotal() { + chaosGame.runStepsAndUpdateTotal(5); + assertEquals(5, chaosGame.getTotalSteps()); + } + /** + * Tests running steps without updating the total steps. + * Verifies that the total steps remain unchanged. + */ @Test - public void testChaosGameInstanceNotNull() { - assertNotNull(cg, "ChaosGame instance should not be null"); + @DisplayName("Run Steps without Updating Total Test") + public void testRunStepsWithoutUpdatingTotal() { + chaosGame.runStepsWithoutUpdatingTotal(5); + assertEquals(0, chaosGame.getTotalSteps()); + } + + /** + * Tests changing the transformation of ChaosGame. + * Verifies that the new description is correctly set. + */ + @Test + @DisplayName("Change Transformation Test") + public void testChangeTransformation() { + AffineTransform2D newTransform1 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(2, 2)); + AffineTransform2D newTransform2 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(-2, -2)); + ChaosGameDescription newDescription = new ChaosGameDescription(new Vector2d(0, 0), new Vector2d(10, 10), Arrays.asList(newTransform1, newTransform2)); + + chaosGame.changeCustomTransformation(newDescription); + + assertEquals(newDescription, chaosGame.getDescription()); + } + + /** + * Tests changing to a custom transformation of ChaosGame. + * Verifies that the custom description is correctly set. + */ + @Test + @DisplayName("Change Custom Transformation Test") + public void testChangeCustomTransformation() { + AffineTransform2D customTransform1 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(3, 3)); + AffineTransform2D customTransform2 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(-3, -3)); + ChaosGameDescription customDescription = new ChaosGameDescription(new Vector2d(0, 0), new Vector2d(10, 10), Arrays.asList(customTransform1, customTransform2)); + + chaosGame.changeCustomTransformation(customDescription); + + assertEquals(customDescription, chaosGame.getDescription()); + } + + /** + * Tests setting a new description in ChaosGame. + * Verifies that the new description is correctly set. + */ + @Test + @DisplayName("Set Description Test") + public void testSetDescription() { + AffineTransform2D newTransform1 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(4, 4)); + AffineTransform2D newTransform2 = new AffineTransform2D(new Matrix2x2(1, 0, 0, 1), new Vector2d(-4, -4)); + ChaosGameDescription newDescription = new ChaosGameDescription(new Vector2d(0, 0), new Vector2d(10, 10), Arrays.asList(newTransform1, newTransform2)); + + chaosGame.setDescription(newDescription); + + assertEquals(newDescription, chaosGame.getDescription()); + } + + /** + * Tests the getDescription() method of ChaosGame. + * Verifies that the description is correctly retrieved. + */ + @Test + @DisplayName("Get Description Test") + public void testGetDescription() { + assertEquals(description, chaosGame.getDescription()); + } + + /** + * Tests registering an observer in ChaosGame. + * Verifies that the observer is registered and notified. + */ + @Test + @DisplayName("Register Observer Test") + public void testRegisterObserver() { + TestObserver newObserver = new TestObserver(); + chaosGame.registerObserver(newObserver); + chaosGame.notifyObservers(); + assertTrue(newObserver.isUpdated()); + } + + /** + * Tests notifying observers in ChaosGame. + * Verifies that the observers are notified. + */ + @Test + @DisplayName("Notify Observers Test") + public void testNotifyObservers() { + chaosGame.notifyObservers(); + assertTrue(observer.isUpdated()); + } + } + + /** + * Negative test cases for ChaosGame. + */ + @Nested + @DisplayName("Negative Tests") + class NegativeTests { + + /** + * Tests removing an observer from ChaosGame. + * Verifies that the observer is not notified after removal. + */ + @Test + @DisplayName("Remove Observer Test") + public void testRemoveObserver() { + chaosGame.removeObserver(observer); + chaosGame.notifyObservers(); + assertFalse(observer.isUpdated()); + } + } + + /** + * A simple implementation of ChaosGameObserver for testing purposes. + */ + private static class TestObserver implements ChaosGameObserver { + private boolean updated = false; + + @Override + public void update() { + updated = true; + } + + public boolean isUpdated() { + return updated; } } -} +} \ No newline at end of file diff --git a/src/test/java/edu/ntnu/idatt2003/model/ComplexTest.java b/src/test/java/edu/ntnu/idatt2003/model/ComplexTest.java index 2b3df832d6ede67abd3238bbebc819ab3f6a588b..6e3d1f0b6a4a22f5137925877f2e714697e598de 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/ComplexTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/ComplexTest.java @@ -8,30 +8,52 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +/** + * Test class for Complex, covering both positive test cases. + */ class ComplexTest { private static Complex v1; + /** + * Sets up the test environment before all tests. + * Initializes the Complex object. + */ @BeforeAll static void setUp() { v1 = new Complex(1, 2); } + /** + * Positive test cases for Complex. + */ @Nested @DisplayName("Positive tests") class PositiveTests { + /** + * Tests the getX0() method of Complex. + * Verifies that the real part (X0) is correctly retrieved. + */ @Test @DisplayName("Test getX0") void testGetX0() { assertEquals(1, v1.getX0()); } + /** + * Tests the getX1() method of Complex. + * Verifies that the imaginary part (X1) is correctly retrieved. + */ @Test @DisplayName("Test getX1") void testGetX1() { assertEquals(2, v1.getX1()); } + /** + * Tests the add() method of Complex. + * Verifies that two Complex numbers are correctly added. + */ @Test @DisplayName("Test add") void testAdd() { @@ -41,6 +63,10 @@ class ComplexTest { assertEquals(6, v3.getX1()); } + /** + * Tests the subtract() method of Complex. + * Verifies that two Complex numbers are correctly subtracted. + */ @Test @DisplayName("Test subtract") void testSubtract() { @@ -50,6 +76,10 @@ class ComplexTest { assertEquals(-2, v3.getX1()); } + /** + * Tests the sqrt() method of Complex. + * Verifies that the square root of a Complex number is correctly calculated. + */ @Test @DisplayName("Test sqrt") void testSqrt() { diff --git a/src/test/java/edu/ntnu/idatt2003/model/JuliaTransformTest.java b/src/test/java/edu/ntnu/idatt2003/model/JuliaTransformTest.java index c0406a15a689a2fb31943dbceba02ce98fb625fd..daeef8881399369343d237506b6ad42c5a7e110a 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/JuliaTransformTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/JuliaTransformTest.java @@ -6,12 +6,21 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; - - +/** + * Test class for JuliaTransform, covering positive test cases. + */ class JuliaTransformTest { + /** + * Positive test cases for JuliaTransform. + */ @Nested @DisplayName("Positive tests") class PositiveTests { + + /** + * Tests the transform method of JuliaTransform with a positive sign. + * Verifies that the transformation is correctly applied. + */ @Test @DisplayName("Test transform with positive sign") void testTransform() { @@ -22,6 +31,10 @@ class JuliaTransformTest { assertEquals(0.644, v3.getX1(), 0.001); } + /** + * Tests the transform method of JuliaTransform with a negative sign. + * Verifies that the transformation is correctly applied. + */ @Test @DisplayName("Test transform with negative sign") void testTransform2() { diff --git a/src/test/java/edu/ntnu/idatt2003/model/Matrix2x2Test.java b/src/test/java/edu/ntnu/idatt2003/model/Matrix2x2Test.java index ee542fbbd95f9dab964c6b1be29265189a86b4c1..85e65c2e9e2014bed9444869c2f7ab37ad2392b2 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/Matrix2x2Test.java +++ b/src/test/java/edu/ntnu/idatt2003/model/Matrix2x2Test.java @@ -2,24 +2,35 @@ package edu.ntnu.idatt2003.model; import static org.junit.jupiter.api.Assertions.assertEquals; -import edu.ntnu.idatt2003.model.Matrix2x2; -import edu.ntnu.idatt2003.model.Vector2d; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +/** + * Test class for Matrix2x2, covering both positive test cases. + */ public class Matrix2x2Test { private static Matrix2x2 m1; + /** + * Sets up the test environment before all tests. + * Initializes the Matrix2x2 object. + */ @BeforeAll static void setUp() { m1 = new Matrix2x2(2,2,4,5); } - + /** + * Positive test cases for Matrix2x2. + */ @Nested @DisplayName("Positive tests") class positiveTests { + /** + * Tests the multiply method of Matrix2x2. + * Verifies that the matrix multiplication is correctly applied to a vector. + */ @Test @DisplayName("Test multiply") void testMultiply() { diff --git a/src/test/java/edu/ntnu/idatt2003/model/Vector2dTest.java b/src/test/java/edu/ntnu/idatt2003/model/Vector2dTest.java index 32968b8f3530a2dfe7e44cc129d39933071bd777..96117ce98532b195560287640dab8240eae465da 100644 --- a/src/test/java/edu/ntnu/idatt2003/model/Vector2dTest.java +++ b/src/test/java/edu/ntnu/idatt2003/model/Vector2dTest.java @@ -1,6 +1,5 @@ package edu.ntnu.idatt2003.model; -import edu.ntnu.idatt2003.model.Vector2d; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -8,15 +7,22 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; - +/** + * Test class for Vector2d, covering both positive test cases. + */ public class Vector2dTest { private static Vector2d v1; - + /** + * Sets up the test environment before all tests. + * Initializes the Vector2d object. + */ @BeforeAll static void setUp() { v1 = new Vector2d(1, 2); } - + /** + * Positive test cases for Vector2d. + */ @Nested @DisplayName("Positive tests") class positiveTests { @@ -25,13 +31,19 @@ public class Vector2dTest { void testGetX0() { assertEquals(1, v1.getX0()); } - + /** + * Tests the getX0() method of Vector2d. + * Verifies that the X0 component is correctly retrieved. + */ @Test @DisplayName("Test getX1") void testGetX1() { assertEquals(2, v1.getX1()); } - + /** + * Tests the getX1() method of Vector2d. + * Verifies that the X1 component is correctly retrieved. + */ @Test @DisplayName("Test add") void testAdd() { @@ -40,7 +52,10 @@ public class Vector2dTest { assertEquals(4, v3.getX0()); assertEquals(6, v3.getX1()); } - + /** + * Tests the add() method of Vector2d. + * Verifies that two Vector2d objects are correctly added. + */ @Test @DisplayName("Test subtract") void testSubtract() {