Skip to content

Commit daf17c0

Browse files
committed
Review markups. Changed assertion order, test names and position of Ignore in MatrixExercise. Changed Integer.valueOf to Integer.parseInt in Matrix. Changed String.split to using Pattern in Matrix.
1 parent 3a6995d commit daf17c0

2 files changed

Lines changed: 72 additions & 44 deletions

File tree

‎exercises/matrix/src/example/java/Matrix.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import java.util.regex.Pattern;
2+
13
public class Matrix {
24
private int[][] matrix;
5+
private static Pattern spacePattern = Pattern.compile(" ");
6+
private static Pattern newlinePattern = Pattern.compile("\\n");
37

48
public Matrix(String matrixAsString) {
5-
String[] rows = matrixAsString.split("\\n");
9+
String[] rows = newlinePattern.split(matrixAsString);
610
matrix = new int[rows.length][];
711
for (int i = 0; i < rows.length; i++) {
8-
String[] columnValues = rows[i].split(" ");
12+
String[] columnValues = spacePattern.split(rows[i]);
913
matrix[i] = new int[columnValues.length];
1014
for (int j = 0; j < columnValues.length; j++) {
11-
matrix[i][j] = Integer.valueOf(columnValues[j]);
15+
matrix[i][j] = Integer.parseInt(columnValues[j]);
1216
}
1317
}
1418
}

‎exercises/matrix/src/test/java/MatrixTest.java‎

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,166 +18,190 @@ public static class GetFirstRowTest {
1818
private Matrix matrix;
1919
private int[] firstRow;
2020

21-
@Parameterized.Parameters(name = "{index}: checking first row of matrix constructed with string \"{0}\".")
21+
@Parameterized.Parameters(name = "{index}: checking first row of matrix constructed with string \n\"{0}\".")
2222
public static Collection<Object[]> data() {
2323
return Arrays.asList(new Object[][]{
2424
{"0", new int[] {0}},
2525
{"0 1", new int[] {0, 1}},
26-
{"0\\n1", new int[] {0}},
27-
{"0 1\\n2 3", new int[] {0, 1}},
28-
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 1, 2}}
26+
{ "0\n"
27+
+ "1", new int[] {0}},
28+
{ "0 1\n"
29+
+ "2 3", new int[] {0, 1}},
30+
{ "0 1 2\n"
31+
+ "3 4 5\n"
32+
+ "6 7 8", new int[] {0, 1, 2}}
2933
});
3034
}
3135

3236
public GetFirstRowTest(String matrixAsString, int[] firstRow) {
33-
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
37+
this.matrix = new Matrix(matrixAsString);
3438
this.firstRow = firstRow;
3539
}
3640

3741
@Test
3842
public void getFirstRowTest() {
39-
assertArrayEquals(matrix.getRow(0), firstRow);
43+
assertArrayEquals(firstRow, matrix.getRow(0));
4044
}
4145
}
4246

47+
@Ignore
4348
@RunWith(Parameterized.class)
4449
public static class GetLastRowTest {
4550
private Matrix matrix;
4651
private int[] lastRow;
4752

48-
@Parameterized.Parameters(name = "{index}: checking last row of matrix constructed with string \"{0}\".")
53+
@Parameterized.Parameters(name = "{index}: checking last row of matrix constructed with string \n\"{0}\".")
4954
public static Collection<Object[]> data() {
5055
return Arrays.asList(new Object[][]{
5156
{"0", new int[] {0}},
5257
{"0 1", new int[] {0, 1}},
53-
{"0\\n1", new int[] {1}},
54-
{"0 1\\n2 3", new int[] {2, 3}},
55-
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {6, 7, 8}}
58+
{ "0\n"
59+
+ "1", new int[] {1}},
60+
{ "0 1\n"
61+
+ "2 3", new int[] {2, 3}},
62+
{ "0 1 2\n"
63+
+ "3 4 5\n"
64+
+ "6 7 8", new int[] {6, 7, 8}}
5665
});
5766
}
5867

5968
public GetLastRowTest(String matrixAsString, int[] lastRow) {
60-
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
69+
this.matrix = new Matrix(matrixAsString);
6170
this.lastRow = lastRow;
6271
}
6372

64-
@Ignore
6573
@Test
6674
public void getLastRowTest() {
67-
assertArrayEquals(matrix.getRow(matrix.getRowsCount() - 1), lastRow);
75+
assertArrayEquals(lastRow, matrix.getRow(matrix.getRowsCount() - 1));
6876
}
6977
}
7078

79+
@Ignore
7180
@RunWith(Parameterized.class)
7281
public static class GetFirstColumnTest {
7382
private Matrix matrix;
7483
private int[] firstColumn;
7584

76-
@Parameterized.Parameters(name = "{index}: checking first column of matrix constructed with string \"{0}\".")
85+
@Parameterized.Parameters(name = "{index}: checking first column of matrix constructed with string \n\"{0}\".")
7786
public static Collection<Object[]> data() {
7887
return Arrays.asList(new Object[][]{
7988
{"0", new int[] {0}},
8089
{"0 1", new int[] {0}},
81-
{"0\\n1", new int[] {0, 1}},
82-
{"0 1\\n2 3", new int[] {0, 2}},
83-
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 3, 6}}
90+
{ "0\n"
91+
+ "1", new int[] {0, 1}},
92+
{ "0 1\n"
93+
+ "2 3", new int[] {0, 2}},
94+
{ "0 1 2\n"
95+
+ "3 4 5\n"
96+
+ "6 7 8", new int[] {0, 3, 6}}
8497
});
8598
}
8699

87100
public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
88-
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
101+
this.matrix = new Matrix(matrixAsString);
89102
this.firstColumn = firstColumn;
90103
}
91104

92-
@Ignore
93105
@Test
94106
public void getFirstColumnTest() {
95-
assertArrayEquals(matrix.getColumn(0), firstColumn);
107+
assertArrayEquals(firstColumn, matrix.getColumn(0));
96108
}
97109
}
98110

99111

112+
@Ignore
100113
@RunWith(Parameterized.class)
101114
public static class GetLastColumnTest {
102115
private Matrix matrix;
103116
private int[] lastColumn;
104117

105-
@Parameterized.Parameters(name = "{index}: checking last column of matrix constructed with string \"{0}\".")
118+
@Parameterized.Parameters(name = "{index}: checking last column of matrix constructed with string \n\"{0}\".")
106119
public static Collection<Object[]> data() {
107120
return Arrays.asList(new Object[][]{
108121
{"0", new int[] {0}},
109122
{"0 1", new int[] {1}},
110-
{"0\\n1", new int[] {0, 1}},
111-
{"0 1\\n2 3", new int[] {1, 3}},
112-
{"0 1 2\\n3 4 5\\n6 7 8", new int[] {2, 5, 8}}
123+
{ "0\n"
124+
+ "1", new int[] {0, 1}},
125+
{ "0 1\n"
126+
+ "2 3", new int[] {1, 3}},
127+
{ "0 1 2\n"
128+
+ "3 4 5\n"
129+
+ "6 7 8", new int[] {2, 5, 8}}
113130
});
114131
}
115132

116133
public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
117-
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
134+
this.matrix = new Matrix(matrixAsString);
118135
this.lastColumn = lastColumn;
119136
}
120137

121-
@Ignore
122138
@Test
123139
public void getLastColumnTest() {
124-
assertArrayEquals(matrix.getColumn(matrix.getColumnsCount() - 1), lastColumn);
140+
assertArrayEquals(lastColumn, matrix.getColumn(matrix.getColumnsCount() - 1));
125141
}
126142
}
127143

144+
@Ignore
128145
@RunWith(Parameterized.class)
129146
public static class CountColumnsTest {
130147
private Matrix matrix;
131148
private int numberOfColumns;
132149

133-
@Parameterized.Parameters(name = "{index}: expected matrix constructed with string \"{0}\" to have {1} column(s).")
150+
@Parameterized.Parameters(name = "{index}: expected matrix constructed with string \n\"{0}\" to have {1} column(s).")
134151
public static Collection<Object[]> data() {
135152
return Arrays.asList(new Object[][]{
136153
{"0", 1},
137154
{"0 1", 2},
138-
{"0\\n1", 1},
139-
{"0 1\\n2 3", 2},
140-
{"0 1 2\\n3 4 5\\n6 7 8", 3}
155+
{ "0\n"
156+
+ "1", 1},
157+
{ "0 1\n"
158+
+ "2 3", 2},
159+
{ "0 1 2\n"
160+
+ "3 4 5\n"
161+
+ "6 7 8", 3}
141162
});
142163
}
143164

144165
public CountColumnsTest(String matrixAsString, int numberOfColumns) {
145-
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
166+
this.matrix = new Matrix(matrixAsString);
146167
this.numberOfColumns = numberOfColumns;
147168
}
148169

149-
@Ignore
150170
@Test
151171
public void countColumnsTest() {
152-
assertEquals(matrix.getColumnsCount(), numberOfColumns);
172+
assertEquals(numberOfColumns, matrix.getColumnsCount());
153173
}
154174
}
155175

176+
@Ignore
156177
@RunWith(Parameterized.class)
157178
public static class CountRowsTest {
158179
private Matrix matrix;
159180
private int numberOfRows;
160181

161-
@Parameterized.Parameters(name = "{index}: expected matrix constructed with string \"{0}\" to have {1} row(s).")
182+
@Parameterized.Parameters(name = "{index}: expected matrix constructed with string \n\"{0}\" to have {1} row(s).")
162183
public static Collection<Object[]> data() {
163184
return Arrays.asList(new Object[][]{
164185
{"0", 1},
165186
{"0 1", 1},
166-
{"0\\n1", 2},
167-
{"0 1\\n2 3", 2},
168-
{"0 1 2\\n3 4 5\\n6 7 8", 3}
187+
{ "0\n"
188+
+ "1", 2},
189+
{ "0 1\n"
190+
+ "2 3", 2},
191+
{ "0 1 2\n"
192+
+ "3 4 5\n"
193+
+ "6 7 8", 3}
169194
});
170195
}
171196

172197
public CountRowsTest(String matrixAsString, int numberOfRows) {
173-
this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
198+
this.matrix = new Matrix(matrixAsString);
174199
this.numberOfRows = numberOfRows;
175200
}
176201

177-
@Ignore
178202
@Test
179203
public void countRowsTest() {
180-
assertEquals(matrix.getRowsCount(), numberOfRows);
204+
assertEquals(numberOfRows, matrix.getRowsCount());
181205
}
182206
}
183207

0 commit comments

Comments
 (0)