FloodFill { public void apply(int[][] picture, int colorToReplace, int colorToPaint, int x, int y) { validatePicture(picture); int currentColor = getValueAt(picture, x, y); if (currentColor == colorToReplace) { picture[x][y] = colorToPaint; apply(picture, colorToReplace, colorToPaint, x + 1, y); apply(picture, colorToReplace, colorToPaint, x - 1, y); apply(picture, colorToReplace, colorToPaint, x, y + 1); apply(picture, colorToReplace, colorToPaint, x, y - 1); } }  void apply(int[][] picture, int colorToReplace, int colorToPaint, int x, int y);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullInstances() { floodFill.apply(null, 0, 1, 0, 0); }
@Test public void shouldPaintSelectedZoneUsingIndicatedColor() { int[][] picture = { { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1 }, { 1, 1, 1, 1 } }; int colorToReplace = 0; int colorToPaint = 1; int x = 1; int y = 1; floodFill.apply(picture, colorToReplace, colorToPaint, x, y); int[][] expectedResult = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } }; assertArrayEquals(expectedResult, picture); }
@Test public void shouldNotModifyIfStartPaintingInOnePixelWithTheColorToPaint() { int[][] picture = { { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1 }, { 1, 1, 1, 1 } }; int colorToReplace = 0; int colorToPaint = 1; int x = 0; int y = 0; floodFill.apply(picture, colorToReplace, colorToPaint, x, y); int[][] expectedResult = { { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1 }, { 1, 1, 1, 1 } }; assertArrayEquals(expectedResult, picture); }