Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement simpleComparison() and create load tests for issue #119 and #174 #220

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ public ImageComparisonResult compareImages() {
.setRectangles(rectangles);
}

/**
* Compare two images and exclude drawing rectangles with the differences.
*
* @return the result comparisonState
*/
public ImageComparisonResult simpleComparison(){
// check that the images have the same size
if (isImageSizesNotEqual(expected, actual)) {
BufferedImage actualResized = ImageComparisonUtil.resize(actual, expected.getWidth(), expected.getHeight());
return ImageComparisonResult.defaultSizeMisMatchResult(expected, actual, getDifferencePercent(actualResized, expected));
}

if (isFirstDifferences()){
return ImageComparisonResult.defaultMisMatchResult(expected, actual, 0);
}else {
return ImageComparisonResult.defaultMatchResult(expected, actual);
}
}

/**
* Check images for equals their widths and heights.
*
Expand Down Expand Up @@ -301,6 +320,24 @@ private List<Rectangle> populateRectangles() {
return mergeRectangles(mergeRectangles(rectangles));
}

/**
* say if there exists two pixels equal or not.
*
* @return true, if first find two different pixels.
*/
private boolean isFirstDifferences() {
for (int y = 0; y < expected.getHeight(); y++) {
for (int x = 0; x < expected.getWidth(); x++) {
if (!excludedAreas.contains(new Point(x, y))) {
if (isDifferentPixels(expected.getRGB(x, y), actual.getRGB(x, y))) {
return true;
}
}
}
}
return false;
}

/**
* Say if provided {@param countOfDifferentPixels} is allowed for {@link ImageComparisonState#MATCH} state.
*
Expand Down
119 changes: 119 additions & 0 deletions src/test/java/com/github/romankh3/image/comparison/LoadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.github.romankh3.image.comparison;

import com.github.romankh3.image.comparison.model.ImageComparisonResult;
import com.github.romankh3.image.comparison.model.ImageComparisonState;
import java.awt.image.BufferedImage;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayName("Load tests for {@link simpleComparison} method")
public class LoadTest {
@Test
@DisplayName("should result MisMatch for two images")
public void simpleComparisonTest(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");

//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).simpleComparison();

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult.getImageComparisonState());
}


@Test
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
public void loadTest1(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");

long time1 = System.currentTimeMillis();
//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
long etime1 = System.currentTimeMillis();
//Time for compareImage method
long compareImageTime = etime1 - time1;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());

long time2 = System.currentTimeMillis();
//Create ImageComparison2 object and compare the images.
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
long etime2 = System.currentTimeMillis();
//Time for simpleComparison method
long simpleComparisonTime = etime2 - time2;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
//check the load differences between simpleComparison and compareImage pattern
assertTrue(simpleComparisonTime < compareImageTime);
}

@Test
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
public void loadTest2(){
//load images to be compared:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, follow general way for creating tests with BDD blocks:
//given
//when
//then

Copy link
Contributor Author

@Hannnnnnn404 Hannnnnnn404 Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that I should code tests following the BDD style? I‘m trying to modified my tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, follow general way for creating tests with BDD blocks: //given //when //then
Do you mean that I should code tests following the BDD style? And my logic of load tests is right.

BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#201.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#201.png");

long time1 = System.currentTimeMillis();
//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
long etime1 = System.currentTimeMillis();
//Time for compareImage method
long compareImageTime = etime1 - time1;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());

long time2 = System.currentTimeMillis();
//Create ImageComparison2 object and compare the images.
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
long etime2 = System.currentTimeMillis();
//Time for simpleComparison method
long simpleComparisonTime = etime2 - time2;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
//check the load differences between simpleComparison and compareImage pattern
assertTrue(simpleComparisonTime < compareImageTime);
}

@Test
@DisplayName("compare load differences between {@link simpleComparison} method and {@link compareImage} method")
public void loadTest3(){
//load images to be compared:
BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected#98.png");
BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual#98.png");

long time1 = System.currentTimeMillis();
//Create ImageComparison object and compare the images.
ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages();
long etime1 = System.currentTimeMillis();
//Time for compareImage method
long compareImageTime = etime1 - time1;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult1.getImageComparisonState());

long time2 = System.currentTimeMillis();
//Create ImageComparison2 object and compare the images.
ImageComparisonResult imageComparisonResult2 = new ImageComparison(expectedImage, actualImage).simpleComparison();
long etime2 = System.currentTimeMillis();
//Time for simpleComparison method
long simpleComparisonTime = etime2 - time2;

//Check the result
assertEquals(ImageComparisonState.MISMATCH, imageComparisonResult2.getImageComparisonState());
//check the load differences between simpleComparison and compareImage pattern
assertTrue(simpleComparisonTime < compareImageTime);
}

}