Skip to content

Commit

Permalink
(#10) Added extra unit tests for createRect and paintEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jun 6, 2021
1 parent 71c5efa commit 318525f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/main/java/tech/fastj/graphics/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,6 @@ public static boolean pathEquals(Path2D path1, Path2D path2) {
return true;
}

/**
* Shorthand for checking equality between two {@link MultipleGradientPaint} objects.
*
* @param mGradientPaint1 The first {@code Paint} specified.
* @param mGradientPaint2 The second {@code Paint} specified.
* @return Whether the two {@code Paint}s are equal.
*/
private static boolean mGradientEquals(MultipleGradientPaint mGradientPaint1, MultipleGradientPaint mGradientPaint2) {
return mGradientPaint1.getTransparency() == mGradientPaint2.getTransparency()
&& mGradientPaint1.getTransform().equals(mGradientPaint2.getTransform())
&& mGradientPaint1.getColorSpace().equals(mGradientPaint2.getColorSpace())
&& mGradientPaint1.getCycleMethod().equals(mGradientPaint2.getCycleMethod())
&& Arrays.deepEquals(mGradientPaint1.getColors(), mGradientPaint2.getColors())
&& Arrays.equals(mGradientPaint1.getFractions(), mGradientPaint2.getFractions());
}

/**
* Checks for equality between two {@link Paint} objects as best as possible.
*
Expand Down Expand Up @@ -263,6 +247,22 @@ public static boolean paintEquals(Paint paint1, Paint paint2) {
return paint1.equals(paint2);
}

/**
* Shorthand for checking equality between two {@link MultipleGradientPaint} objects.
*
* @param mGradientPaint1 The first {@code Paint} specified.
* @param mGradientPaint2 The second {@code Paint} specified.
* @return Whether the two {@code Paint}s are equal.
*/
private static boolean mGradientEquals(MultipleGradientPaint mGradientPaint1, MultipleGradientPaint mGradientPaint2) {
return mGradientPaint1.getTransparency() == mGradientPaint2.getTransparency()
&& mGradientPaint1.getTransform().equals(mGradientPaint2.getTransform())
&& mGradientPaint1.getColorSpace().equals(mGradientPaint2.getColorSpace())
&& mGradientPaint1.getCycleMethod().equals(mGradientPaint2.getCycleMethod())
&& Arrays.deepEquals(mGradientPaint1.getColors(), mGradientPaint2.getColors())
&& Arrays.equals(mGradientPaint1.getFractions(), mGradientPaint2.getFractions());
}

/**
* Creates a {@code Pointf} array of 4 points, based on the specified x, y, width, and height floats.
* <p>
Expand Down Expand Up @@ -455,10 +455,7 @@ public static Pointf[] createBoxFromImage(BufferedImage source) {
*/
public static Rectangle2D.Float createRect(Pointf[] pts) {
if (pts.length != 4) {
FastJEngine.error(
CrashMessages.theGameCrashed("a rectangle creation error."),
new IllegalArgumentException("The length of the parameter point array must be 4.")
);
throw new IllegalArgumentException("The length of the parameter point array must be 4.");
}

return new Rectangle2D.Float(pts[0].x, pts[0].y, pts[1].x - pts[0].x, pts[3].y - pts[0].y);
Expand All @@ -484,7 +481,9 @@ public static Rectangle2D.Float createRectFromImage(BufferedImage source, Pointf
*/
public static Pointf centerOf(Pointf[] points) {
Pointf result = new Pointf();
for (Pointf p : points) result.add(p);
for (Pointf p : points) {
result.add(p);
}
return result.divide(points.length);
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -128,6 +129,15 @@ void tryComparePath2Ds_wherePathsAreNotEqual() {
);
}

@Test
void comparePaints_withNullPaintParameters() {
Paint paint1 = null;
Color paint2 = Color.red;

assertFalse(DrawUtil.paintEquals(paint1, paint2), "The two paints should not be equal.");
assertFalse(DrawUtil.paintEquals(paint2, paint1), "The two paints should not be equal.");
}

@Test
void checkGenerateBox_withFloatXAndY_andFloatSize() {
Pointf[] generatedResult = DrawUtil.createBox(5f, 5f, 35f);
Expand Down Expand Up @@ -243,6 +253,24 @@ void checkGenerateRectangleFloat_withHandwrittenPointfArray() {
assertEquals(expectedRect, actualRect, "The two rectangles should be equal.");
}

@Test
void tryGenerateRectangleFloat_withHandwrittenPointfArray_butArraySizeIsNotFour() {
float rectX = 0f;
float rectY = 5f;
float rectWidth = 25f;
float rectHeight = 30f;

Pointf[] points = {
new Pointf(rectX, rectY),
new Pointf(rectX + rectWidth, rectY),
new Pointf(rectX + rectWidth, rectY + rectHeight)
};

Throwable exception = assertThrows(IllegalArgumentException.class, () -> DrawUtil.createRect(points));
String expectedExceptionMessage = "The length of the parameter point array must be 4.";
assertEquals(expectedExceptionMessage, exception.getMessage(), "The thrown exception's message should match the expected exception message.");
}

@Test
void checkGenerateRectangleFloat_withPointfArrayFromGeneratedBox() {
float rectX = 0f;
Expand Down

0 comments on commit 318525f

Please sign in to comment.