From 5bebf32bc83d94769f272e47aa65ec95fe05541b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?H=C3=A5vard=20Daleng?=
 <142524365+MrMarHVD@users.noreply.github.com>
Date: Wed, 3 Apr 2024 11:54:04 +0200
Subject: [PATCH] Created ChaosCanvasTest and added some getters to
 ChaosCanvas.

---
 .../chaosgame/model/game/ChaosCanvas.java     | 52 ++++++++---
 .../stud/chaosgame/game/ChaosCanvasTest.java  | 90 +++++++++++++++++++
 2 files changed, 130 insertions(+), 12 deletions(-)
 create mode 100644 src/test/java/edu/ntnu/stud/chaosgame/game/ChaosCanvasTest.java

diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java
index 5710a8c..da65438 100644
--- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java
+++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java
@@ -56,15 +56,12 @@ public class ChaosCanvas {
 
     this.canvas = new int[height][width];
 
-    // Define the affine transformation that maps coordinates to indices in the canvas.
-    /*this.transformCoordsToIndices = new AffineTransform2D(
-        new Matrix2x2(0, (height - 1) / (minCoords.getX1() - maxCoords.getX1()), (width - 1) / (maxCoords.getX0() - minCoords.getX0()), 0),
-        new Vector2D((((height - 1) * maxCoords.getX1()) / (maxCoords.getX1() - minCoords.getX1())),
-        ((width - 1) * minCoords.getX0()) / (minCoords.getX0() - maxCoords.getX0()))
-    );*/
+    // Convert the coordinates to indices in the canvas
     this.transformCoordsToIndices = new AffineTransform2D(
-        new Matrix2x2(0, (height-1) / (minCoords.getX1() - maxCoords.getX1()), (width-1) / (maxCoords.getX0() - minCoords.getX0()), 0),
-        new Vector2D(((height-1)* maxCoords.getX1()) / (maxCoords.getX1() - minCoords.getX1()), (width-1)* minCoords.getX0() / (minCoords.getX0() - maxCoords.getX0())));
+        new Matrix2x2(0, (height-1) / (minCoords.getX1() - maxCoords.getX1()),
+            (width-1) / (maxCoords.getX0() - minCoords.getX0()), 0),
+        new Vector2D(((height-1)* maxCoords.getX1()) / (maxCoords.getX1() - minCoords.getX1()),
+            (width-1)* minCoords.getX0() / (minCoords.getX0() - maxCoords.getX0())));
   }
 
   /**
@@ -96,13 +93,36 @@ public class ChaosCanvas {
   }
 
   /**
-   * Get the canvas array.
-   * @return the canvas array
+   * Get the width and height of the canvas in the form of an array where the first
+   * index stores the width and the second stores the height.
+   *
+   * @return an array of two numbers containing both the width and height.
    */
-  public int[][] getCanvasArray() {
-    return canvas;
+  public int[] getSize() {
+    return new int[]{this.width, this.height};
   }
 
+  /**
+   * Get the vector storing the minimum coordinates.
+   *
+   * @return the vector.
+   */
+  public Vector2D getMinCoords() {return this.minCoords; }
+
+  /**
+   * Get the vector storing the maximum coordinates.
+   *
+   * @return the vector.
+   */
+  public Vector2D getMaxCoords() { return this.maxCoords; }
+
+  /**
+   * Get the affine transform representing the conversion from coordinates to indices.
+   *
+   * @return the transform.
+   */
+  public AffineTransform2D getTransformCoordsToIndices() { return this.transformCoordsToIndices; }
+
   /**
    * Clear the canvas of all content.
    */
@@ -114,6 +134,14 @@ public class ChaosCanvas {
     }
   }
 
+  /**
+   * Get the array defining the size of the canvas.
+   * @return the array.
+   */
+  public int[][] getCanvas() {
+    return this.canvas;
+  }
+
   /**
    * Prints the current state of the canvas to the console.
    * Populated points are represented by 'x', unpopulated points by ' '.
diff --git a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosCanvasTest.java b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosCanvasTest.java
new file mode 100644
index 0000000..8211aec
--- /dev/null
+++ b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosCanvasTest.java
@@ -0,0 +1,90 @@
+package edu.ntnu.stud.chaosgame.game;
+import edu.ntnu.stud.chaosgame.controller.ChaosGameObserver;
+import edu.ntnu.stud.chaosgame.model.data.Vector2D;
+import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class ChaosCanvasTest {
+
+  private ChaosCanvas canvas;
+
+  /**
+   * Instantiate a canvas before each test
+   */
+  @BeforeEach
+  private void setUp() {
+    Vector2D minCoords = new Vector2D(0, 0);
+    Vector2D maxCoords = new Vector2D(1, 1);
+    this.canvas = new ChaosCanvas(100, 100, minCoords, maxCoords);
+  }
+
+  /**
+   * Test whether creating the canvas worked properly, and simultanteously test whether
+   * the getters work as expected.
+   */
+  @Test
+  void shouldCreateCanvas() {
+    ChaosCanvas testCanvas = new ChaosCanvas(100, 100,
+        new Vector2D(0,0), new Vector2D(1,1));
+
+    Assertions.assertEquals(testCanvas.getSize()[0], 100);
+    Assertions.assertEquals(testCanvas.getSize()[1], 100);
+    Assertions.assertEquals(testCanvas.getMinCoords().getX0(), 0);
+    Assertions.assertEquals(testCanvas.getMinCoords().getX1(), 0);
+    Assertions.assertEquals(testCanvas.getMaxCoords().getX0(), 1);
+    Assertions.assertEquals(testCanvas.getMaxCoords().getX1(), 1);
+    //Assertions.assertEquals(testCanvas.getTransformCoordsToIndices(), );
+  }
+
+  /**
+   * Test whether an arbitrary point on the canvas is obtained correctly and hence
+   * equal to 0.
+   */
+  @Test
+  void shouldGetPixel() {
+    Assertions.assertEquals(this.canvas.getPixel(new Vector2D(0.5, 0.5)), 0);
+  }
+
+  /**
+   * Test whether putting a new pixel in an arbitrary location on the canvas works
+   *  as expected.
+   */
+  @Test
+  void shouldPutPixel() {
+    Vector2D point = new Vector2D(0.2, 0.3);
+    this.canvas.putPixel(point);
+
+    // Test whether new point was added
+    Assertions.assertEquals(this.canvas.getPixel(point), 1);
+
+    // Ensure another, arbitrary point was not also added
+    Assertions.assertEquals(this.canvas.getPixel(new Vector2D(0.1, 0.4)), 0);
+  }
+
+  /**
+   * Test whether clearing the canvas works as expected.
+   */
+  @Test
+  void shouldClearCanvas() {
+
+    // Put pixels throughout a part of the canvas.
+    for (int i = 1; i < 101; i++) {
+      this.canvas.putPixel(new Vector2D( 1.0 / i, 1.0 / i));
+
+      // Check that the of the points where a pixel was added, are not equal to 0.
+      Assertions.assertNotEquals(0, this.canvas.getPixel(new Vector2D(1.0 / i, 1.0 / i)));
+    }
+
+    // Clear the canvas
+    this.canvas.clearCanvas();
+
+    // Ensure that each point where a pixel was added, is now equal to 0.
+    for (int i = 1; i < 101; i++) {
+      Assertions.assertEquals(this.canvas.getPixel(new Vector2D(1.0 / i, 1.0 / i)), 0);
+    }
+  }
+
+
+}
-- 
GitLab