mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-31 08:47:02 +08:00
Add tests, remove print
& main
methods in BoundaryFill.java
(#5640)
This commit is contained in:
@ -52,48 +52,4 @@ public final class BoundaryFill {
|
||||
boundaryFill(image, xCoordinate - 1, yCoordinate - 1, newColor, boundaryColor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will print the 2D image matrix
|
||||
*
|
||||
* @param image The image to be printed on the console
|
||||
*/
|
||||
public static void printImageArray(int[][] image) {
|
||||
for (int i = 0; i < image.length; i++) {
|
||||
for (int j = 0; j < image[0].length; j++) {
|
||||
System.out.print(image[i][j] + " ");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
// Input 2D image matrix
|
||||
int[][] image = {
|
||||
{0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 3, 3, 3, 3, 0, 0},
|
||||
{0, 3, 0, 0, 3, 0, 0},
|
||||
{0, 3, 0, 0, 3, 3, 3},
|
||||
{0, 3, 3, 3, 0, 0, 3},
|
||||
{0, 0, 0, 3, 0, 0, 3},
|
||||
{0, 0, 0, 3, 3, 3, 3},
|
||||
};
|
||||
|
||||
boundaryFill(image, 2, 2, 5, 3);
|
||||
|
||||
/* Output ==>
|
||||
* 0 0 0 0 0 0 0
|
||||
0 3 3 3 3 0 0
|
||||
0 3 5 5 3 0 0
|
||||
0 3 5 5 3 3 3
|
||||
0 3 3 3 5 5 3
|
||||
0 0 0 3 5 5 3
|
||||
0 0 0 3 3 3 3
|
||||
* */
|
||||
|
||||
// print 2D image matrix
|
||||
printImageArray(image);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,66 @@
|
||||
package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BoundaryFillTest {
|
||||
|
||||
private int[][] image;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
image = new int[][] {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 0, 0, 3, 0, 0}, {0, 3, 0, 0, 3, 3, 3}, {0, 3, 3, 3, 0, 0, 3}, {0, 0, 0, 3, 0, 0, 3}, {0, 0, 0, 3, 3, 3, 3}};
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPixel() {
|
||||
assertEquals(3, BoundaryFill.getPixel(image, 1, 1));
|
||||
assertEquals(0, BoundaryFill.getPixel(image, 2, 2));
|
||||
assertEquals(3, BoundaryFill.getPixel(image, 4, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPutPixel() {
|
||||
BoundaryFill.putPixel(image, 2, 2, 5);
|
||||
assertEquals(5, BoundaryFill.getPixel(image, 2, 2));
|
||||
|
||||
BoundaryFill.putPixel(image, 0, 0, 7);
|
||||
assertEquals(7, BoundaryFill.getPixel(image, 0, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBoundaryFill() {
|
||||
BoundaryFill.boundaryFill(image, 2, 2, 5, 3);
|
||||
|
||||
int[][] expectedImage = {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 5, 5, 3, 0, 0}, {0, 3, 5, 5, 3, 3, 3}, {0, 3, 3, 3, 5, 5, 3}, {0, 0, 0, 3, 5, 5, 3}, {0, 0, 0, 3, 3, 3, 3}};
|
||||
|
||||
for (int i = 0; i < image.length; i++) {
|
||||
assertArrayEquals(expectedImage[i], image[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBoundaryFillEdgeCase() {
|
||||
BoundaryFill.boundaryFill(image, 1, 1, 3, 3);
|
||||
|
||||
int[][] expectedImage = {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 0, 0, 3, 0, 0}, {0, 3, 0, 0, 3, 3, 3}, {0, 3, 3, 3, 0, 0, 3}, {0, 0, 0, 3, 0, 0, 3}, {0, 0, 0, 3, 3, 3, 3}};
|
||||
|
||||
for (int i = 0; i < image.length; i++) {
|
||||
assertArrayEquals(expectedImage[i], image[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBoundaryFillInvalidCoordinates() {
|
||||
BoundaryFill.boundaryFill(image, -1, -1, 5, 3);
|
||||
|
||||
int[][] expectedImage = {{0, 0, 0, 0, 0, 0, 0}, {0, 3, 3, 3, 3, 0, 0}, {0, 3, 0, 0, 3, 0, 0}, {0, 3, 0, 0, 3, 3, 3}, {0, 3, 3, 3, 0, 0, 3}, {0, 0, 0, 3, 0, 0, 3}, {0, 0, 0, 3, 3, 3, 3}};
|
||||
|
||||
for (int i = 0; i < image.length; i++) {
|
||||
assertArrayEquals(expectedImage[i], image[i]);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user