RewriteRowAndColumnsWithZeros { public void rewrite(int[][] matrix) { validateInput(matrix); Set<Integer> rowsToSkip = new HashSet<Integer>(); Set<Integer> columnsToSkip = new HashSet<Integer>(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (shouldPaintRowAndColumn(matrix, i, j, rowsToSkip, columnsToSkip)) { rewriteRow(matrix, i, rowsToSkip); rewriteColumn(matrix, j, columnsToSkip); } } } }  void rewrite(int[][] matrix);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullMatrix() { rewriteMatrix.rewrite(null); }
@Test public void shouldRewritePositionsEvenWithAOneDimensionMatrix() { int[][] matrix = { { 1, 0 }, { } }; rewriteMatrix.rewrite(matrix); int[][] expectedMatrix = { { 0, 0 }, { } }; assertArrayEquals(expectedMatrix, matrix); }
@Test public void shouldNotRewritePositionsWithNonZeroElements() { int[][] matrix = { { 1, 1 }, { 1, 1 } }; rewriteMatrix.rewrite(matrix); int[][] expectedMatrix = { { 1, 1 }, { 1, 1 } }; assertArrayEquals(expectedMatrix, matrix); }
@Test public void hsouldRewriteJustCenterRowAndCoulmnWithZeros() { int[][] matrix = { { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 } }; rewriteMatrix.rewrite(matrix); int[][] expectedMatrix = { { 1, 0, 1 }, { 0, 0, 0 }, { 1, 0, 1 } }; assertArrayEquals(expectedMatrix, matrix); }
@Test public void shouldRewriteRowAndColumnsWithZeros() { int[][] matrix = { { 1, 1, 0 }, { 1, 0, 1 }, { 1, 1, 1 } }; rewriteMatrix.rewrite(matrix); int[][] expectedMatrix = { { 0, 0, 0 }, { 0, 0, 0 }, { 1, 0, 0 } }; assertArrayEquals(expectedMatrix, matrix); }