Skip to content

Commit

Permalink
(#3) Added unit tests for the Model2D class
Browse files Browse the repository at this point in the history
New Additions:
- Added `toString()` overrides for `Polygon2D` and `Model2D`

Other Changes:
- Renamed `Model2D#getObjects()` to `Model2D#getPolygons()`
  • Loading branch information
lucasstarsz committed Apr 12, 2021
1 parent c414cae commit 0863720
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
'}';
}
}
108 changes: 104 additions & 4 deletions src/test/java/unittest/testcases/graphics/shapes/Model2DTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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.");
Expand All @@ -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.");
Expand Down Expand Up @@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0863720

Please sign in to comment.