From 5d4dbb6c4391ab91a140a12b2c8c30cace92a3ff Mon Sep 17 00:00:00 2001 From: lucasstarsz Date: Sat, 3 Jul 2021 18:10:09 -0400 Subject: [PATCH] (#10, #32) Added outline stroke generation unit test 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) --- .../tech/fastj/graphics/util/DrawUtil.java | 2 +- .../graphics/util/DrawUtilTests.java | 51 +++++-- .../util/gradients/GradientsTests.java | 130 ++++++++++++------ 3 files changed, 129 insertions(+), 54 deletions(-) diff --git a/src/main/java/tech/fastj/graphics/util/DrawUtil.java b/src/main/java/tech/fastj/graphics/util/DrawUtil.java index 0b5ee2fe..9ae7383e 100644 --- a/src/main/java/tech/fastj/graphics/util/DrawUtil.java +++ b/src/main/java/tech/fastj/graphics/util/DrawUtil.java @@ -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 ); } } diff --git a/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java b/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java index 2563166b..a81ec7aa 100644 --- a/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java +++ b/src/test/java/unittest/testcases/graphics/util/DrawUtilTests.java @@ -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; @@ -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; @@ -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)); } @@ -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)); } @@ -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)); + } } diff --git a/src/test/java/unittest/testcases/graphics/util/gradients/GradientsTests.java b/src/test/java/unittest/testcases/graphics/util/gradients/GradientsTests.java index 338bc52a..a9e51697 100644 --- a/src/test/java/unittest/testcases/graphics/util/gradients/GradientsTests.java +++ b/src/test/java/unittest/testcases/graphics/util/gradients/GradientsTests.java @@ -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() { @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } }