From 0863720aba3dd12cdfaa340d2bec450187fdba0a Mon Sep 17 00:00:00 2001 From: lucasstarsz Date: Mon, 12 Apr 2021 10:13:35 -0400 Subject: [PATCH] (#3) Added unit tests for the Model2D class New Additions: - Added `toString()` overrides for `Polygon2D` and `Model2D` Other Changes: - Renamed `Model2D#getObjects()` to `Model2D#getPolygons()` --- .../lucasstarsz/fastj/graphics/DrawUtil.java | 8 +- .../fastj/graphics/shapes/Model2D.java | 13 ++- .../fastj/graphics/shapes/Polygon2D.java | 13 +++ .../graphics/shapes/Model2DTests.java | 108 +++++++++++++++++- .../graphics/shapes/Polygon2DTests.java | 2 +- 5 files changed, 134 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/lucasstarsz/fastj/graphics/DrawUtil.java b/src/main/java/io/github/lucasstarsz/fastj/graphics/DrawUtil.java index 591d02f1..76c8439f 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/graphics/DrawUtil.java +++ b/src/main/java/io/github/lucasstarsz/fastj/graphics/DrawUtil.java @@ -138,10 +138,10 @@ public static void writeToPSDF(String destPath, Model2D model) { StringBuilder fileContents = new StringBuilder(); // write object count - fileContents.append("amt ").append(model.getObjects().length).append(sep); + fileContents.append("amt ").append(model.getPolygons().length).append(sep); - for (int i = 0; i < model.getObjects().length; i++) { - Polygon2D obj = model.getObjects()[i]; + for (int i = 0; i < model.getPolygons().length; i++) { + Polygon2D obj = model.getPolygons()[i]; Color c = obj.getColor(); // Write obj color, fill, show @@ -164,7 +164,7 @@ public static void writeToPSDF(String destPath, Model2D model) { } // if there are more objects after this object, then add a new line. - if (i != model.getObjects().length - 1) fileContents.append(sep); + if (i != model.getPolygons().length - 1) fileContents.append(sep); } Files.writeString(Paths.get(destPath), fileContents, StandardCharsets.UTF_8); diff --git a/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Model2D.java b/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Model2D.java index 6fb03a6a..12ecb7db 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Model2D.java +++ b/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Model2D.java @@ -105,7 +105,7 @@ public Model2D(Polygon2D[] polygonArray, Pointf location, float rotVal, Pointf s * * @return The array of {@code Polygon2D}s. */ - public Polygon2D[] getObjects() { + public Polygon2D[] getPolygons() { return polyArr; } @@ -263,4 +263,15 @@ public int hashCode() { result = 31 * result + Arrays.hashCode(polyArr); return result; } + + @Override + public String toString() { + return "Model2D{" + + "polyArr=" + Arrays.toString(polyArr) + + ", collisionObject=" + collisionObject + + ", rotation=" + rotation + + ", scale=" + scale + + ", translation=" + translation + + '}'; + } } diff --git a/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Polygon2D.java b/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Polygon2D.java index 410c13c5..920545d3 100644 --- a/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Polygon2D.java +++ b/src/main/java/io/github/lucasstarsz/fastj/graphics/shapes/Polygon2D.java @@ -336,4 +336,17 @@ public int hashCode() { result = 31 * result + Arrays.hashCode(points); return result; } + + @Override + public String toString() { + return "Polygon2D{" + + "renderPath=" + renderPath + + ", points=" + Arrays.toString(points) + + ", color=" + color + + ", paintFilled=" + paintFilled + + ", rotation=" + rotation + + ", scale=" + scale + + ", translation=" + translation + + '}'; + } } diff --git a/src/test/java/unittest/testcases/graphics/shapes/Model2DTests.java b/src/test/java/unittest/testcases/graphics/shapes/Model2DTests.java index 301ffd9d..29306336 100644 --- a/src/test/java/unittest/testcases/graphics/shapes/Model2DTests.java +++ b/src/test/java/unittest/testcases/graphics/shapes/Model2DTests.java @@ -25,7 +25,7 @@ public void checkModel2DCreation_withPolygon2DArrayParam() { Model2D model2D = new Model2D(polygons); - assertArrayEquals(polygons, model2D.getObjects(), "The created model's Polygon2D array should match the original Polygon2D array."); + assertArrayEquals(polygons, model2D.getPolygons(), "The created model's Polygon2D array should match the original Polygon2D array."); assertEquals(Model2D.DefaultShow, model2D.shouldRender(), "The created model's 'show' option should match the default show option."); assertEquals(GameObject.defaultTranslation, model2D.getTranslation(), "The created model's translation should match the default translation."); assertEquals(GameObject.defaultRotation, model2D.getRotation(), "The created model's rotation should match the default rotation."); @@ -46,7 +46,7 @@ public void checkModel2DCreation_withPolygon2DArrayParam_andRandomlyGeneratedSho Model2D model2D = new Model2D(polygons, shouldRender); - assertArrayEquals(polygons, model2D.getObjects(), "The created model's Polygon2D array should match the original Polygon2D array."); + assertArrayEquals(polygons, model2D.getPolygons(), "The created model's Polygon2D array should match the original Polygon2D array."); assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the default show option."); assertEquals(GameObject.defaultTranslation, model2D.getTranslation(), "The created model's translation should match the default translation."); assertEquals(GameObject.defaultRotation, model2D.getRotation(), "The created model's rotation should match the default rotation."); @@ -70,7 +70,7 @@ public void checkModel2DCreation_withPolygon2DArrayParam_andRandomlyGeneratedSho Model2D model2D = new Model2D(polygons, randomTranslation, randomRotation, randomScale, shouldRender); - assertArrayEquals(polygons, model2D.getObjects(), "The created model's Polygon2D array should match the original Polygon2D array."); + assertArrayEquals(polygons, model2D.getPolygons(), "The created model's Polygon2D array should match the original Polygon2D array."); assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the default show option."); assertEquals(randomTranslation, model2D.getTranslation(), "The created model's translation should match the default translation."); assertEquals(randomRotation, model2D.getRotation(), "The created model's rotation should match the default rotation."); @@ -98,10 +98,110 @@ public void checkModel2DCreation_withPolygon2DArrayParam_andRandomlyGeneratedSho .setScale(randomScale) .setShouldRender(shouldRender); - assertArrayEquals(polygons, model2D.getObjects(), "The created model's Polygon2D array should match the original Polygon2D array."); + assertArrayEquals(polygons, model2D.getPolygons(), "The created model's Polygon2D array should match the original Polygon2D array."); assertEquals(shouldRender, model2D.shouldRender(), "The created model's 'show' option should match the default show option."); assertEquals(randomTranslation, model2D.getTranslation(), "The created model's translation should match the default translation."); assertEquals(randomRotation, model2D.getRotation(), "The created model's rotation should match the default rotation."); assertEquals(randomScale, model2D.getScale(), "The created model's scaling should match the default scale."); } + + @Test + public void checkModel2DBoundsCreation_shouldMatchExpected() { + Pointf[] square1 = DrawUtil.createBox(Pointf.origin, 50f); + Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.origin, 25f), 50f); + + Polygon2D[] polygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + Pointf[] expectedBounds = { + square1[0].copy(), + new Pointf(square2[1].x, square1[1].y), + square2[2].copy(), + new Pointf(square1[3].x, square2[3].y) + }; + + Pointf[] actualBounds = new Model2D(polygons).getBounds(); + + assertArrayEquals(expectedBounds, actualBounds, "The actual bounds generated by the Model2D should match the expected bounds."); + } + + @Test + public void checkModel2DTranslation_shouldMatchExpected() { + Pointf[] square1 = DrawUtil.createBox(Pointf.origin, 50f); + Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.origin, 25f), 50f); + Pointf randomTranslation = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f)); + + Polygon2D[] expectedPolygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + for (Polygon2D polygon2D : expectedPolygons) { + polygon2D.translate(randomTranslation); + } + + Polygon2D[] actualPolygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + Model2D model2D = new Model2D(actualPolygons); + model2D.translate(randomTranslation); + + assertArrayEquals(expectedPolygons, model2D.getPolygons(), "The array of actual translated Polygon2Ds should match the expected Polygon2Ds."); + } + + @Test + public void checkModel2DRotation_aroundOrigin_shouldMatchExpected() { + Pointf[] square1 = DrawUtil.createBox(Pointf.origin, 50f); + Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.origin, 25f), 50f); + float randomRotation = Maths.random(-50f, 50f); + + Polygon2D[] expectedPolygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + for (Polygon2D polygon2D : expectedPolygons) { + polygon2D.rotate(randomRotation, Pointf.origin); + } + + Polygon2D[] actualPolygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + Model2D model2D = new Model2D(actualPolygons); + model2D.rotate(randomRotation, Pointf.origin); + + assertArrayEquals(expectedPolygons, model2D.getPolygons(), "The array of actual rotated Polygon2Ds should match the expected Polygon2Ds."); + } + + @Test + public void checkModel2DScaling_atOrigin_shouldMatchExpected() { + Pointf[] square1 = DrawUtil.createBox(Pointf.origin, 50f); + Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.origin, 25f), 50f); + Pointf randomScaling = new Pointf(Maths.random(-50f, 50f), Maths.random(-50f, 50f)); + + Polygon2D[] expectedPolygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + for (Polygon2D polygon2D : expectedPolygons) { + polygon2D.scale(randomScaling, Pointf.origin); + } + + Polygon2D[] actualPolygons = { + new Polygon2D(square1), + new Polygon2D(square2) + }; + + Model2D model2D = new Model2D(actualPolygons); + model2D.scale(randomScaling, Pointf.origin); + + assertArrayEquals(expectedPolygons, model2D.getPolygons(), "The array of actual scaled Polygon2Ds should match the expected Polygon2Ds."); + } } diff --git a/src/test/java/unittest/testcases/graphics/shapes/Polygon2DTests.java b/src/test/java/unittest/testcases/graphics/shapes/Polygon2DTests.java index fc5dd251..13f1316c 100644 --- a/src/test/java/unittest/testcases/graphics/shapes/Polygon2DTests.java +++ b/src/test/java/unittest/testcases/graphics/shapes/Polygon2DTests.java @@ -177,7 +177,7 @@ public void checkPolygon2DTranslation_shouldMatchExpected() { } @Test - public void checkPolygon2DRotation_shouldMatchExpected() { + public void checkPolygon2DRotation_aroundOrigin_shouldMatchExpected() { Pointf[] originalPoints = DrawUtil.createBox(Pointf.origin, 5f); float randomRotationInDegrees = Maths.random(0f, 1f); float randomRotationInRadians = (float) Math.toRadians(randomRotationInDegrees);