diff --git a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java index 0a563809fd9adf994bd09cf71a0ad54ea83c69d2..f8854ca44b1a9b45532ce932485cefc6c563003a 100644 --- a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java +++ b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandlerTest.java @@ -1,5 +1,133 @@ package edu.ntnu.stud.chaosgame.game; + +import edu.ntnu.stud.chaosgame.model.data.Complex; +import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; +import edu.ntnu.stud.chaosgame.model.data.Vector2D; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; +import edu.ntnu.stud.chaosgame.model.transformations.JuliaTransform; +import edu.ntnu.stud.chaosgame.model.transformations.Transform2D; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + class ChaosGameFileHandlerTest { - + + @Test + void testReadFromFile() throws IOException { + String fileContent = """ + Affine2D + 0.0, 0.0 + 1.0, 1.0 + 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 + 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 + 0.5, 1.0, 1.0, 0.5, 3.0, 1.0 + """; + + Path tempFile = Files.createTempFile("test", ".txt"); + Files.writeString(tempFile, fileContent); + ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); + ChaosGameDescription description = fileHandler.readFromFile(tempFile.toString()); + + Vector2D minCoords = description.getMinCoords(); + Vector2D maxCoords = description.getMaxCoords(); + + assertEquals(0.0, minCoords.getX0()); + assertEquals(0.0, minCoords.getX1()); + assertEquals(1.0, maxCoords.getX0()); + assertEquals(1.0, maxCoords.getX1()); + + for (var transform : description.getTransforms()) { + assertEquals(AffineTransform2D.class, transform.getClass()); + AffineTransform2D affine = (AffineTransform2D) transform; + Matrix2x2 matrix = affine.getMatrix(); + Vector2D vector = affine.getVector(); + + assertEquals(0.5, matrix.getA00()); + assertEquals(1.0, matrix.getA01()); + assertEquals(1.0, matrix.getA10()); + assertEquals(0.5, matrix.getA11()); + assertEquals(3.0, vector.getX0()); + assertEquals(1.0, vector.getX1()); + } + } + + @Test + void testAffine2DWriteToFile() throws IOException { + // Create a ChaosGameDescription with three transforms + ChaosGameDescription description = getChaosGameDescription(); + + // Write to a temporary file + Path tempFile = Files.createTempFile("test", ".txt"); + ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); + fileHandler.writeToFile(description, tempFile.toString()); + + // Read the file and verify its contents + List<String> lines = Files.readAllLines(tempFile); + assertEquals("Affine2D", lines.get(0)); + assertEquals("0.0, 0.0", lines.get(1)); + assertEquals("1.0, 1.0", lines.get(2)); + assertEquals("0.5, 1.0, 1.0, 0.5, 1.0, 1.0", lines.get(3)); + assertEquals("0.5, 0.0, 0.0, 0.5, 0.0, 0.0", lines.get(4)); + assertEquals("1.0, 0.2, 0.2, 1.0, 0.5, 0.5", lines.get(5)); + } + + private static ChaosGameDescription getChaosGameDescription() { + Vector2D minCoords = new Vector2D(0.0, 0.0); + Vector2D maxCoords = new Vector2D(1.0, 1.0); + Matrix2x2 matrix1 = new Matrix2x2(0.5, 1.0, 1.0, 0.5); + Matrix2x2 matrix2 = new Matrix2x2(0.5, 0.0, 0.0, 0.5); + Matrix2x2 matrix3 = new Matrix2x2(1.0, 0.2, 0.2, 1.0); + List<Transform2D> transforms = getTransform2DS(matrix1, matrix2, matrix3); + return new ChaosGameDescription(minCoords, maxCoords, transforms); + } + + private static List<Transform2D> getTransform2DS(Matrix2x2 matrix1, Matrix2x2 matrix2, Matrix2x2 matrix3) { + Vector2D vector1 = new Vector2D(1.0, 1.0); + Vector2D vector2 = new Vector2D(0.0, 0.0); + Vector2D vector3 = new Vector2D(0.5, 0.5); + Transform2D transform1 = new AffineTransform2D(matrix1, vector1); + Transform2D transform2 = new AffineTransform2D(matrix2, vector2); + Transform2D transform3 = new AffineTransform2D(matrix3, vector3); + List<Transform2D> transforms = new ArrayList<>(); + transforms.add(transform1); + transforms.add(transform2); + transforms.add(transform3); + return transforms; + } + + @Test + void testJuliaWriteToFile() throws IOException { + // Create a ChaosGameDescription + Vector2D minCoords = new Vector2D(0.0, 0.0); + Vector2D maxCoords = new Vector2D(1.0, 1.0); + Complex complex = new Complex(0.5,0.5); + Transform2D transform = new JuliaTransform(complex, 1); + List<Transform2D> transforms = Collections.singletonList(transform); + ChaosGameDescription description = new ChaosGameDescription(minCoords, maxCoords, transforms); + + // Write to a temporary file + Path tempFile = Files.createTempFile("test", ".txt"); + ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); + fileHandler.writeToFile(description, tempFile.toString()); + + // Read the file and verify its contents + List<String> lines = Files.readAllLines(tempFile); + assertEquals("Julia", lines.get(0)); + assertEquals("0.0, 0.0", lines.get(1)); + assertEquals("1.0, 1.0", lines.get(2)); + assertEquals("0.5, 0.5", lines.get(3)); + } } \ No newline at end of file