From 743c10c285d444492f49e3a2f446bbe19b9e0c1d Mon Sep 17 00:00:00 2001 From: arnaudlabourel <arnaud.labourel@univ-amu.fr> Date: Tue, 5 Oct 2021 10:36:30 +0200 Subject: [PATCH] Ajout tests --- build.gradle | 8 ++-- src/main/java/ByteGrayColor.java | 4 -- src/main/java/DecreaseGrayLevels.java | 10 +++-- src/main/java/Display.java | 4 +- src/test/java/ByteGrayColorTest.java | 45 +++++++++++++++++++---- src/test/java/DecreaseGrayLevelsTest.java | 13 +++++++ src/test/java/MatrixGrayImageTest.java | 32 ++++++++++------ 7 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 src/test/java/DecreaseGrayLevelsTest.java diff --git a/build.gradle b/build.gradle index cef5ac5..4ae9191 100644 --- a/build.gradle +++ b/build.gradle @@ -13,9 +13,9 @@ repositories { } dependencies { - testImplementation('org.junit.jupiter:junit-jupiter-api:5.7.2', - 'org.hamcrest:hamcrest-library:2.2', 'net.obvj:junit-utils:1.3.1') - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' + testImplementation('org.junit.jupiter:junit-jupiter-api:5.8.1', + 'org.assertj:assertj-core:3.21.0') + testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.8.1') } test { @@ -24,4 +24,4 @@ test { application { mainClassName = "Main" -} \ No newline at end of file +} diff --git a/src/main/java/ByteGrayColor.java b/src/main/java/ByteGrayColor.java index bf54f4e..8d53f23 100644 --- a/src/main/java/ByteGrayColor.java +++ b/src/main/java/ByteGrayColor.java @@ -13,10 +13,6 @@ public class ByteGrayColor implements GrayColor { private final int grayLevel; - public ByteGrayColor(){ - this.grayLevel = MINIMUM_GRAY_LEVEL; - } - public ByteGrayColor(int grayLevel) { this.grayLevel = grayLevel; } diff --git a/src/main/java/DecreaseGrayLevels.java b/src/main/java/DecreaseGrayLevels.java index cae2894..0c0494b 100755 --- a/src/main/java/DecreaseGrayLevels.java +++ b/src/main/java/DecreaseGrayLevels.java @@ -19,10 +19,14 @@ public class DecreaseGrayLevels implements Transform { } private void modifyPixel(GrayImage image, int x, int y) { - int numberOfIntervals = nbGrayLevels - 1; - double sizeOfIntervals = 1. / (double) numberOfIntervals; double luminosity = image.getPixelGrayColor(x, y).getLuminosity(); - double newLuminosity = Math.floor(luminosity * numberOfIntervals) * sizeOfIntervals; + double newLuminosity = getDecreaseGrayLevelsLuminosity(luminosity); image.setPixel(new ByteGrayColor(newLuminosity), x, y); } + + double getDecreaseGrayLevelsLuminosity(double luminosity) { + int numberOfIntervals = nbGrayLevels - 1; + double sizeOfIntervals = 1. / (double) numberOfIntervals; + return Math.floor(luminosity * numberOfIntervals) * sizeOfIntervals; + } } diff --git a/src/main/java/Display.java b/src/main/java/Display.java index cdf672f..03bd1b0 100644 --- a/src/main/java/Display.java +++ b/src/main/java/Display.java @@ -21,8 +21,8 @@ public class Display implements Initializable { this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm"); - Transform transform = new CompositeTransform(new Transform[] {new DecreaseGrayLevels(8), new Outline(0.05), new Invert()}); - + //Transform transform = new CompositeTransform(new Transform[] {new DecreaseGrayLevels(8), new Outline(0.05), new Invert()}); + Transform transform = new Outline(0.025); transform.applyTo(image); image.writeIntoPGMFormat("/Users/arnaudlabourel/luminy.pgm"); render(); diff --git a/src/test/java/ByteGrayColorTest.java b/src/test/java/ByteGrayColorTest.java index ddca387..ce60ed8 100644 --- a/src/test/java/ByteGrayColorTest.java +++ b/src/test/java/ByteGrayColorTest.java @@ -1,23 +1,54 @@ import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.closeTo; -import static org.hamcrest.Matchers.is; +import static org.assertj.core.api.Assertions.*; public class ByteGrayColorTest { @Test public void testGetLuminosity_whenColorCreatedWithGrayLevel(){ ByteGrayColor black = new ByteGrayColor(0); ByteGrayColor white = new ByteGrayColor(255); - assertThat(black.getLuminosity(), is(closeTo(0.,.0001))); - assertThat(white.getLuminosity(), is(closeTo(1.,.0001))); + assertThat(black.getLuminosity()).isCloseTo(0., within(.01)); + assertThat(white.getLuminosity()).isCloseTo(1., within(.01)); } @Test public void testGetLuminosity_whenColorCreatedWithLuminosity(){ ByteGrayColor color1 = new ByteGrayColor(.25); ByteGrayColor color2 = new ByteGrayColor(.75); - assertThat(color1.getLuminosity(), is(closeTo(.25,.01))); - assertThat(color2.getLuminosity(), is(closeTo(.75,.01))); + assertThat(color1.getLuminosity()).isCloseTo(.25, within(.01)); + assertThat(color2.getLuminosity()).isCloseTo(.75, within(.01)); + } + + @Test + public void testCompareTo_whenColorsCreatedWithGrayLevel(){ + ByteGrayColor color1 = new ByteGrayColor(100); + ByteGrayColor color2 = new ByteGrayColor(100); + ByteGrayColor color3 = new ByteGrayColor(150); + assertThat(color1.compareTo(color3)).isNegative(); + assertThat(color3.compareTo(color1)).isPositive(); + assertThat(color1.compareTo(color3)).isEqualTo(-(color3.compareTo(color1))); + assertThat(color1.compareTo(color2)).isZero(); + } + + @Test + public void testCompareTo_whenColorsCreatedWithLuminosity(){ + ByteGrayColor color1 = new ByteGrayColor(0.20); + ByteGrayColor color2 = new ByteGrayColor(0.20); + ByteGrayColor color3 = new ByteGrayColor(0.60); + assertThat(color1.compareTo(color3)).isNegative(); + assertThat(color3.compareTo(color1)).isPositive(); + assertThat(color1.compareTo(color3)).isEqualTo(-(color3.compareTo(color1))); + assertThat(color1.compareTo(color2)).isZero(); + } + + @Test + public void testCompareTo_whenColorsCreatedWithLuminosityAndGrayLevel(){ + ByteGrayColor color1 = new ByteGrayColor(0.); + ByteGrayColor color2 = new ByteGrayColor(0); + ByteGrayColor color3 = new ByteGrayColor(100); + assertThat(color1.compareTo(color3)).isNegative(); + assertThat(color3.compareTo(color1)).isPositive(); + assertThat(color1.compareTo(color3)).isEqualTo(-(color3.compareTo(color1))); + assertThat(color1.compareTo(color2)).isZero(); } } diff --git a/src/test/java/DecreaseGrayLevelsTest.java b/src/test/java/DecreaseGrayLevelsTest.java new file mode 100644 index 0000000..5ae652b --- /dev/null +++ b/src/test/java/DecreaseGrayLevelsTest.java @@ -0,0 +1,13 @@ +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class DecreaseGrayLevelsTest { + @Test + void testGetDecreaseGrayLevelsLuminosity(){ + DecreaseGrayLevels decreaseGrayLevels = new DecreaseGrayLevels(5); + assertThat(decreaseGrayLevels.getDecreaseGrayLevelsLuminosity(0.125)).isCloseTo(0, within(.01)); + assertThat(decreaseGrayLevels.getDecreaseGrayLevelsLuminosity(0.625)).isCloseTo(0.5, within(.01)); + assertThat(decreaseGrayLevels.getDecreaseGrayLevelsLuminosity(1.)).isCloseTo(1., within(.01)); + } +} diff --git a/src/test/java/MatrixGrayImageTest.java b/src/test/java/MatrixGrayImageTest.java index 0664e25..72e6100 100644 --- a/src/test/java/MatrixGrayImageTest.java +++ b/src/test/java/MatrixGrayImageTest.java @@ -1,23 +1,31 @@ import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; - +import static org.assertj.core.api.Assertions.*; class MatrixGrayImageTest { @Test - void getWidth() { - assertThat(new MatrixGrayImage(0,0).getWidth(), is(equalTo(0))); - assertThat(new MatrixGrayImage(10,20).getWidth(), is(equalTo(10))); - assertThat(new MatrixGrayImage(400,300).getWidth(), is(equalTo(400))); + void testGetWidth() { + assertThat(new MatrixGrayImage(0,0).getWidth()).isEqualTo(0); + assertThat(new MatrixGrayImage(10,20).getWidth()).isEqualTo(10); + assertThat(new MatrixGrayImage(400,300).getWidth()).isEqualTo(400); + } + + @Test + void testGetHeight() { + assertThat(new MatrixGrayImage(0,0).getHeight()).isEqualTo(0); + assertThat(new MatrixGrayImage(10,20).getHeight()).isEqualTo(20); + assertThat(new MatrixGrayImage(400,300).getHeight()).isEqualTo(300); } @Test - void getHeight() { - assertThat(new MatrixGrayImage(0,0).getHeight(), is(equalTo(0))); - assertThat(new MatrixGrayImage(10,20).getHeight(), is(equalTo(20))); - assertThat(new MatrixGrayImage(400,300).getHeight(), is(equalTo(300))); + void testGetPixel_whenPixelHasBeenSet() { + GrayColor grey1 = new ByteGrayColor(0.2); + GrayColor grey2 = new ByteGrayColor(0.8); + MatrixGrayImage image = new MatrixGrayImage(10, 10); + image.setPixel(grey1, 1, 1); + assertThat(image.getPixelGrayColor(1,1)).isEqualTo(grey1); + image.setPixel(grey2, 3, 9); + assertThat(image.getPixelGrayColor(3,9)).isEqualTo(grey2); } } \ No newline at end of file -- GitLab