diff --git a/src/main/java/org/example/chaosgame/model/chaos/ChaosGameDescription.java b/src/main/java/org/example/chaosgame/model/chaos/ChaosGameDescription.java index 68c9afe57b852496e7aded2d64f94bd69591ba24..3e9af32d5eac7627b740fd5db56a0292389c328d 100644 --- a/src/main/java/org/example/chaosgame/model/chaos/ChaosGameDescription.java +++ b/src/main/java/org/example/chaosgame/model/chaos/ChaosGameDescription.java @@ -83,34 +83,14 @@ public class ChaosGameDescription { this.maxCoords = maxCoords; } - /** - * Equals method for ChaosGameDescription. - * Overrides the default equals method. - * Compares the minimum and maximum coordinates, the list of transformations, - * and the list of probabilities. - * - * @param o Object to compare - * @return true if the objects are equal, false otherwise - */ @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ChaosGameDescription that = (ChaosGameDescription) o; - return Objects.equals(minCoords, that.minCoords) - && Objects.equals(maxCoords, that.maxCoords) - && Objects.equals(transforms, that.transforms) - && Objects.equals(probabilities, that.probabilities); + return Objects.equals(minCoords, that.minCoords) && Objects.equals(maxCoords, that.maxCoords) && Objects.equals(transforms, that.transforms) && Objects.equals(probabilities, that.probabilities); } - /** - * Hashcode method for ChaosGameDescription. - * Overrides the default hashcode method. - * Use the minimum and maximum coordinates, the list of transformations, - * and the list of probabilities to generate the hashcode. - * - * @return the hashcode of the ChaosGameDescription object - */ @Override public int hashCode() { return Objects.hash(minCoords, maxCoords, transforms, probabilities); diff --git a/src/test/java/org/example/chaosgame/model/chaos/ChaosGameDescriptionTest.java b/src/test/java/org/example/chaosgame/model/chaos/ChaosGameDescriptionTest.java index fc302e0d4a632908158441fed34bb02237514e7e..3564dad37e3f07c4f6fdc0837d6f39891556b3e8 100644 --- a/src/test/java/org/example/chaosgame/model/chaos/ChaosGameDescriptionTest.java +++ b/src/test/java/org/example/chaosgame/model/chaos/ChaosGameDescriptionTest.java @@ -1,36 +1,107 @@ package org.example.chaosgame.model.chaos; -import org.junit.jupiter.api.Test; +import org.example.chaosgame.model.linalg.Matrix2x2; +import org.example.chaosgame.model.linalg.Vector2D; +import org.example.chaosgame.model.transformations.Transform2D; +import org.example.chaosgame.model.transformations.AffineTransform2D; +import org.junit.jupiter.api.*; + +import java.util.Arrays; +import java.util.List; +import java.util.Vector; import static org.junit.jupiter.api.Assertions.*; class ChaosGameDescriptionTest { + private ChaosGameDescription chaosGameDescription; + private Vector2D minCoords; + private Vector2D maxCoords; + private Matrix2x2 matrixA; + private Vector2D vectorPartA; + private Matrix2x2 matrixB; + private Vector2D vectorPartB; + private List<Transform2D> transforms; + private List<Integer> probabilities; + + @BeforeEach + void setUp() { + minCoords = new Vector2D(0, 0); + maxCoords = new Vector2D(1, 1); + + matrixA = new Matrix2x2(0.5, 0.0, 0.0, 0.5); + vectorPartA = new Vector2D(0.0, 0.0); + matrixB = new Matrix2x2(0.5, 0.0, 0.0, 0.5); + vectorPartB = new Vector2D(0.25, 0.50); + transforms = Arrays.asList(new AffineTransform2D(matrixA, vectorPartA), new AffineTransform2D(matrixB, vectorPartB)); + probabilities = Arrays.asList(50, 50); + chaosGameDescription = new ChaosGameDescription(minCoords, maxCoords, transforms, probabilities); + } + + @AfterEach + void tearDown() { + minCoords = null; + maxCoords = null; + transforms = null; + probabilities = null; + chaosGameDescription = null; + } + + @Test + void testGetMinCoords() { + assertEquals(minCoords, chaosGameDescription.getMinCoords()); + } + + @Test + void testGetMaxCoords() { + assertEquals(maxCoords, chaosGameDescription.getMaxCoords()); + } @Test - void getMinCoords() { + void testGetTransforms() { + assertEquals(transforms, chaosGameDescription.getTransforms()); } @Test - void getMaxCoords() { + void testGetProbabilities() { + assertEquals(probabilities, chaosGameDescription.getProbabilities()); } @Test - void setMinCoords() { + void testSetTransforms() { + Matrix2x2 newMatrix = new Matrix2x2(9.9, 9.9, 9.9, 9.9); + Vector2D newVector = new Vector2D(9.9, 9.9); + List<Transform2D> newTransforms = Arrays.asList(new AffineTransform2D(newMatrix, newVector)); + chaosGameDescription.setTransforms(newTransforms); + assertEquals(newTransforms, chaosGameDescription.getTransforms()); } @Test - void setMaxCoords() { + void testSetMinCoords() { + Vector2D newMinCoords = new Vector2D(-1, -1); + chaosGameDescription.setMinCoords(newMinCoords); + assertEquals(newMinCoords, chaosGameDescription.getMinCoords()); } @Test - void getTransforms() { + void testSetMaxCoords() { + Vector2D newMaxCoords = new Vector2D(2, 2); + chaosGameDescription.setMaxCoords(newMaxCoords); + assertEquals(newMaxCoords, chaosGameDescription.getMaxCoords()); } @Test - void setTransforms() { + void testEquals() { + ChaosGameDescription sameChaosGameDescription = new ChaosGameDescription(minCoords, maxCoords, transforms, probabilities); + assertTrue(chaosGameDescription.equals(sameChaosGameDescription)); + assertFalse(chaosGameDescription == null); + assertNotEquals(chaosGameDescription, new Object()); + ChaosGameDescription differentChaosGameDescription = new ChaosGameDescription(new Vector2D(1, 1), new Vector2D(2, 2), transforms, probabilities); + assertFalse(chaosGameDescription.equals(differentChaosGameDescription)); } @Test - void getProbabilities() { + void testHashCode() { + ChaosGameDescription sameChaosGameDescription = new ChaosGameDescription(minCoords, maxCoords, transforms, probabilities); + assertEquals(chaosGameDescription.hashCode(), sameChaosGameDescription.hashCode()); } -} \ No newline at end of file +} diff --git a/src/test/java/org/example/chaosgame/model/linalg/Vector2DTest.java b/src/test/java/org/example/chaosgame/model/linalg/Vector2DTest.java index 87c78e129e3a8a2afee7edd88c9531f4af1c9ff5..3f177bd90a19389a6cad105157b06ab297e08c5b 100644 --- a/src/test/java/org/example/chaosgame/model/linalg/Vector2DTest.java +++ b/src/test/java/org/example/chaosgame/model/linalg/Vector2DTest.java @@ -76,4 +76,74 @@ class Vector2DTest { assertNotEquals(2.0, w.getY()); } } + + @Nested + @DisplayName("Test scale and multiply") + class TestScaleAndMultiply { + @Test + @DisplayName("Test scale should work") + void scale() { + Vector2D w = vector.scale(2.0); + assertEquals(2.0, w.getX()); + assertEquals(4.0, w.getY()); + } + + @Test + @DisplayName("Test scale should not work") + void scaleFail() { + Vector2D w = vector.scale(2.0); + assertNotEquals(1.0, w.getX()); + assertNotEquals(2.0, w.getY()); + } + + @Test + @DisplayName("Test multiply should work") + void multiply() { + Vector2D w = vector.multiply(otherVector); + assertEquals(1.0, w.getX()); + assertEquals(2.0, w.getY()); + } + + @Test + @DisplayName("Test multiply should not work") + void multiplyFail() { + Vector2D w = vector.multiply(otherVector); + assertNotEquals(2.0, w.getX()); + assertNotEquals(4.0, w.getY()); + } + } + + @Nested + @DisplayName("Test divide and length") + class TestDivide { + @Test + @DisplayName("Test divide should work") + void divide() { + Vector2D w = vector.divide(otherVector); + assertEquals(1.0, w.getX()); + assertEquals(2.0, w.getY()); + } + + @Test + @DisplayName("Test divide should not work") + void divideFail() { + Vector2D w = vector.divide(otherVector); + assertNotEquals(2.0, w.getX()); + assertNotEquals(4.0, w.getY()); + } + + @Test + @DisplayName("Test length should work") + void length() { + double l = vector.lengthSq(); + assertEquals(2.23606797749979, l); + } + + @Test + @DisplayName("Test length should not work") + void lengthFail() { + double l = vector.lengthSq(); + assertNotEquals(2.0, l); + } + } } \ No newline at end of file