Skip to content

Commit

Permalink
(#10, #32) Added outline stroke generation unit test
Browse files Browse the repository at this point in the history
Bug Fixes:
- Fixed issue with occasionally generating outline strokes with miter limit values less than 1.0

Other Changes:
- Added `Assertions#assertDoesNotThrow` to all random generation unit tests (resolves sonarcloud code smells)
  • Loading branch information
lucasstarsz committed Jul 3, 2021
1 parent 23f47eb commit 5d4dbb6
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/main/java/tech/fastj/graphics/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public static BasicStroke randomOutlineStroke() {
Maths.random(0.0f, 32.0f),
randomCap,
randomJoin,
randomJoin == BasicStroke.JOIN_MITER ? Maths.random(Maths.FloatPrecision, 64.0f) : 0.0f
randomJoin == BasicStroke.JOIN_MITER ? Maths.random(1.0f, 64.0f) : 0.0f
);
}
}
51 changes: 42 additions & 9 deletions src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.util.DrawUtil;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
Expand All @@ -17,6 +18,7 @@
import org.junit.jupiter.api.TestMethodOrder;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -393,9 +395,14 @@ void checkGenerateColors_shouldNotFail() {
int generatedColorCount = 255;
Color[] generatedRGBColors = new Color[generatedColorCount];

for (int i = 0; i < generatedColorCount; i++) {
generatedRGBColors[i] = DrawUtil.randomColor();
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedColorCount; i++) {
generatedRGBColors[i] = DrawUtil.randomColor();
}
},
"Errors should not be produced while generating random RGB colors."
);

System.out.println("Generated RGB colors: " + Arrays.toString(generatedRGBColors));
}

Expand All @@ -404,9 +411,14 @@ void checkGenerateColorsWithRandomAlpha_shouldNotFail() {
int generatedColorCount = 255;
Color[] generatedRGBAColors = new Color[generatedColorCount];

for (int i = 0; i < generatedColorCount; i++) {
generatedRGBAColors[i] = DrawUtil.randomColorWithAlpha();
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedColorCount; i++) {
generatedRGBAColors[i] = DrawUtil.randomColorWithAlpha();
}
},
"Errors should not be produced while generating random RGBA colors."
);

System.out.println("Generated RGBA colors: " + Arrays.toString(generatedRGBAColors));
}

Expand All @@ -415,9 +427,30 @@ void checkGenerateFonts_shouldNotFail() {
int generatedFontCount = 255;
Font[] generatedFonts = new Font[generatedFontCount];

for (int i = 0; i < generatedFontCount; i++) {
generatedFonts[i] = DrawUtil.randomFont();
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedFontCount; i++) {
generatedFonts[i] = DrawUtil.randomFont();
}
},
"Errors should not be produced while generating random fonts."
);

System.out.println("Generated fonts: " + Arrays.toString(generatedFonts));
}

@Test
void checkGenerateOutlineStrokes_shouldNotFail() {
int generatedOutlineStrokeCount = 255;
BasicStroke[] generatedOutlineStrokes = new BasicStroke[generatedOutlineStrokeCount];

assertDoesNotThrow(() -> {
for (int i = 0; i < generatedOutlineStrokeCount; i++) {
generatedOutlineStrokes[i] = DrawUtil.randomOutlineStroke();
}
},
"Errors should not be produced while generating random outline strokes."
);

System.out.println("Generated outline strokes: " + Arrays.toString(generatedOutlineStrokes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.junit.jupiter.api.Test;
import unittest.mock.graphics.MockBoundariesDrawable;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

class GradientsTests {

private static float randomFloat() {
Expand Down Expand Up @@ -40,13 +42,18 @@ void checkGenerateLinearGradientPaints_usingDrawableAndRandomBoundaries_shouldNo
LinearGradientPaint[] generatedLinearGradients = new LinearGradientPaint[generatedGradientCount];
Drawable mockBoundariesDrawable = new MockBoundariesDrawable();

for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradient(
mockBoundariesDrawable,
randomBeginningBoundary(),
randomEndingBoundary()
);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradient(
mockBoundariesDrawable,
randomBeginningBoundary(),
randomEndingBoundary()
);
}
},
"Errors should not be produced while generating random linear gradients (using a drawable with random boundaries)."
);

System.out.println("Generated linear gradients: " + Arrays.toString(generatedLinearGradients));
}

Expand All @@ -56,13 +63,18 @@ void checkGenerateLinearGradientPaintsWithRandomAlpha_usingDrawableAndRandomBoun
LinearGradientPaint[] generatedLinearGradients = new LinearGradientPaint[generatedGradientCount];
Drawable mockBoundariesDrawable = new MockBoundariesDrawable();

for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradientWithAlpha(
mockBoundariesDrawable,
randomBeginningBoundary(),
randomEndingBoundary()
);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradientWithAlpha(
mockBoundariesDrawable,
randomBeginningBoundary(),
randomEndingBoundary()
);
}
},
"Errors should not be produced while generating random linear gradients with random alpha (using a drawable with random boundaries)."
);

System.out.println("Generated linear gradients: " + Arrays.toString(generatedLinearGradients));
}

Expand All @@ -73,12 +85,17 @@ void checkGenerateLinearGradientPaints_usingRandomStartingAndEndingPointfs_shoul
Pointf randomStartingPosition = randomPointf();
Pointf randomEndingPosition = randomPointf();

for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradient(
randomStartingPosition,
randomEndingPosition
);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradient(
randomStartingPosition,
randomEndingPosition
);
}
},
"Errors should not be produced while generating random linear gradients (using random starting and ending points)."
);

System.out.println("Generated linear gradients: " + Arrays.toString(generatedLinearGradients));
}

Expand All @@ -89,12 +106,17 @@ void checkGenerateLinearGradientPaintsWithRandomAlpha_usingRandomStartingAndEndi
Pointf randomStartingPosition = randomPointf();
Pointf randomEndingPosition = randomPointf();

for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradientWithAlpha(
randomStartingPosition,
randomEndingPosition
);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedLinearGradients[i] = Gradients.randomLinearGradientWithAlpha(
randomStartingPosition,
randomEndingPosition
);
}
},
"Errors should not be produced while generating random linear gradients with random alpha (using random starting and ending points)."
);

System.out.println("Generated linear gradients: " + Arrays.toString(generatedLinearGradients));
}

Expand All @@ -104,9 +126,14 @@ void checkGenerateRadialGradientPaints_usingDrawable_shouldNotFail() {
RadialGradientPaint[] generatedRadialGradients = new RadialGradientPaint[generatedGradientCount];
Drawable mockBoundariesDrawable = new MockBoundariesDrawable();

for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradient(mockBoundariesDrawable);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradient(mockBoundariesDrawable);
}
},
"Errors should not be produced while generating random radial gradients (using a drawable)."
);

System.out.println("Generated radial gradients: " + Arrays.toString(generatedRadialGradients));
}

Expand All @@ -116,9 +143,14 @@ void checkGenerateRadialGradientPaintsWithRandomAlpha_usingDrawable_shouldNotFai
RadialGradientPaint[] generatedRadialGradients = new RadialGradientPaint[generatedGradientCount];
Drawable mockBoundariesDrawable = new MockBoundariesDrawable();

for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradientWithAlpha(mockBoundariesDrawable);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradientWithAlpha(mockBoundariesDrawable);
}
},
"Errors should not be produced while generating random radial gradients with random alpha (using a drawable)."
);

System.out.println("Generated radial gradients: " + Arrays.toString(generatedRadialGradients));
}

Expand All @@ -129,12 +161,17 @@ void checkGenerateRadialGradientPaints_usingRandomPointfCenterAndRandomFloatRadi
Pointf randomCenterpoint = randomPointf();
float randomRadius = randomFloat();

for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradient(
randomCenterpoint,
randomRadius
);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradient(
randomCenterpoint,
randomRadius
);
}
},
"Errors should not be produced while generating random radial gradients (using a random center and radius)."
);

System.out.println("Generated radial gradients: " + Arrays.toString(generatedRadialGradients));
}

Expand All @@ -145,12 +182,17 @@ void checkGenerateRadialGradientPaintsWithRandomAlpha_usingRandomPointfCenterAnd
Pointf randomCenterpoint = randomPointf();
float randomRadius = randomFloat();

for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradientWithAlpha(
randomCenterpoint,
randomRadius
);
}
assertDoesNotThrow(() -> {
for (int i = 0; i < generatedGradientCount; i++) {
generatedRadialGradients[i] = Gradients.randomRadialGradientWithAlpha(
randomCenterpoint,
randomRadius
);
}
},
"Errors should not be produced while generating random radial gradients with alpha (using a random center and radius)."
);

System.out.println("Generated radial gradients: " + Arrays.toString(generatedRadialGradients));
}
}

0 comments on commit 5d4dbb6

Please sign in to comment.