Kaynağa Gözat

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.

Frida Tveit 9 yıl önce
ebeveyn
işleme
daf17c0bef

+ 7
- 3
exercises/matrix/src/example/java/Matrix.java Dosyayı Görüntüle

1
+import java.util.regex.Pattern;
2
+
1
 public class Matrix {
3
 public class Matrix {
2
     private int[][] matrix;
4
     private int[][] matrix;
5
+    private static Pattern spacePattern = Pattern.compile(" ");
6
+    private static Pattern newlinePattern = Pattern.compile("\\n");
3
 
7
 
4
     public Matrix(String matrixAsString) {
8
     public Matrix(String matrixAsString) {
5
-        String[] rows = matrixAsString.split("\\n");
9
+        String[] rows = newlinePattern.split(matrixAsString);
6
         matrix = new int[rows.length][];
10
         matrix = new int[rows.length][];
7
         for (int i = 0; i < rows.length; i++) {
11
         for (int i = 0; i < rows.length; i++) {
8
-            String[] columnValues = rows[i].split(" ");
12
+            String[] columnValues = spacePattern.split(rows[i]);
9
             matrix[i] = new int[columnValues.length];
13
             matrix[i] = new int[columnValues.length];
10
             for (int j = 0; j < columnValues.length; j++) {
14
             for (int j = 0; j < columnValues.length; j++) {
11
-                matrix[i][j] = Integer.valueOf(columnValues[j]);
15
+                matrix[i][j] = Integer.parseInt(columnValues[j]);
12
             }
16
             }
13
         }
17
         }
14
     }
18
     }

+ 65
- 41
exercises/matrix/src/test/java/MatrixTest.java Dosyayı Görüntüle

18
         private Matrix matrix;
18
         private Matrix matrix;
19
         private int[] firstRow;
19
         private int[] firstRow;
20
 
20
 
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}\".")
22
         public static Collection<Object[]> data() {
22
         public static Collection<Object[]> data() {
23
             return Arrays.asList(new Object[][]{
23
             return Arrays.asList(new Object[][]{
24
                     {"0", new int[] {0}},
24
                     {"0", new int[] {0}},
25
                     {"0 1", new int[] {0, 1}},
25
                     {"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}}
29
             });
33
             });
30
         }
34
         }
31
 
35
 
32
         public GetFirstRowTest(String matrixAsString, int[] firstRow) {
36
         public GetFirstRowTest(String matrixAsString, int[] firstRow) {
33
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
37
+            this.matrix = new Matrix(matrixAsString);
34
             this.firstRow = firstRow;
38
             this.firstRow = firstRow;
35
         }
39
         }
36
 
40
 
37
         @Test
41
         @Test
38
         public void getFirstRowTest() {
42
         public void getFirstRowTest() {
39
-            assertArrayEquals(matrix.getRow(0), firstRow);
43
+            assertArrayEquals(firstRow, matrix.getRow(0));
40
         }
44
         }
41
     }
45
     }
42
 
46
 
47
+    @Ignore
43
     @RunWith(Parameterized.class)
48
     @RunWith(Parameterized.class)
44
     public static class GetLastRowTest {
49
     public static class GetLastRowTest {
45
         private Matrix matrix;
50
         private Matrix matrix;
46
         private int[] lastRow;
51
         private int[] lastRow;
47
 
52
 
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}\".")
49
         public static Collection<Object[]> data() {
54
         public static Collection<Object[]> data() {
50
             return Arrays.asList(new Object[][]{
55
             return Arrays.asList(new Object[][]{
51
                     {"0", new int[] {0}},
56
                     {"0", new int[] {0}},
52
                     {"0 1", new int[] {0, 1}},
57
                     {"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}}
56
             });
65
             });
57
         }
66
         }
58
 
67
 
59
         public GetLastRowTest(String matrixAsString, int[] lastRow) {
68
         public GetLastRowTest(String matrixAsString, int[] lastRow) {
60
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
69
+            this.matrix = new Matrix(matrixAsString);
61
             this.lastRow = lastRow;
70
             this.lastRow = lastRow;
62
         }
71
         }
63
 
72
 
64
-        @Ignore
65
         @Test
73
         @Test
66
         public void getLastRowTest() {
74
         public void getLastRowTest() {
67
-            assertArrayEquals(matrix.getRow(matrix.getRowsCount() - 1), lastRow);
75
+            assertArrayEquals(lastRow, matrix.getRow(matrix.getRowsCount() - 1));
68
         }
76
         }
69
     }
77
     }
70
 
78
 
79
+    @Ignore
71
     @RunWith(Parameterized.class)
80
     @RunWith(Parameterized.class)
72
     public static class GetFirstColumnTest {
81
     public static class GetFirstColumnTest {
73
         private Matrix matrix;
82
         private Matrix matrix;
74
         private int[] firstColumn;
83
         private int[] firstColumn;
75
 
84
 
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}\".")
77
         public static Collection<Object[]> data() {
86
         public static Collection<Object[]> data() {
78
             return Arrays.asList(new Object[][]{
87
             return Arrays.asList(new Object[][]{
79
                     {"0", new int[] {0}},
88
                     {"0", new int[] {0}},
80
                     {"0 1", new int[] {0}},
89
                     {"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}}
84
             });
97
             });
85
         }
98
         }
86
 
99
 
87
         public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
100
         public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
88
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
101
+            this.matrix = new Matrix(matrixAsString);
89
             this.firstColumn = firstColumn;
102
             this.firstColumn = firstColumn;
90
         }
103
         }
91
 
104
 
92
-        @Ignore
93
         @Test
105
         @Test
94
         public void getFirstColumnTest() {
106
         public void getFirstColumnTest() {
95
-            assertArrayEquals(matrix.getColumn(0), firstColumn);
107
+            assertArrayEquals(firstColumn, matrix.getColumn(0));
96
         }
108
         }
97
     }
109
     }
98
 
110
 
99
 
111
 
112
+    @Ignore
100
     @RunWith(Parameterized.class)
113
     @RunWith(Parameterized.class)
101
     public static class GetLastColumnTest {
114
     public static class GetLastColumnTest {
102
         private Matrix matrix;
115
         private Matrix matrix;
103
         private int[] lastColumn;
116
         private int[] lastColumn;
104
 
117
 
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}\".")
106
         public static Collection<Object[]> data() {
119
         public static Collection<Object[]> data() {
107
             return Arrays.asList(new Object[][]{
120
             return Arrays.asList(new Object[][]{
108
                     {"0", new int[] {0}},
121
                     {"0", new int[] {0}},
109
                     {"0 1", new int[] {1}},
122
                     {"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}}
113
             });
130
             });
114
         }
131
         }
115
 
132
 
116
         public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
133
         public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
117
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
134
+            this.matrix = new Matrix(matrixAsString);
118
             this.lastColumn = lastColumn;
135
             this.lastColumn = lastColumn;
119
         }
136
         }
120
 
137
 
121
-        @Ignore
122
         @Test
138
         @Test
123
         public void getLastColumnTest() {
139
         public void getLastColumnTest() {
124
-            assertArrayEquals(matrix.getColumn(matrix.getColumnsCount() - 1), lastColumn);
140
+            assertArrayEquals(lastColumn, matrix.getColumn(matrix.getColumnsCount() - 1));
125
         }
141
         }
126
     }
142
     }
127
 
143
 
144
+    @Ignore
128
     @RunWith(Parameterized.class)
145
     @RunWith(Parameterized.class)
129
     public static class CountColumnsTest {
146
     public static class CountColumnsTest {
130
         private Matrix matrix;
147
         private Matrix matrix;
131
         private int numberOfColumns;
148
         private int numberOfColumns;
132
 
149
 
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).")
134
         public static Collection<Object[]> data() {
151
         public static Collection<Object[]> data() {
135
             return Arrays.asList(new Object[][]{
152
             return Arrays.asList(new Object[][]{
136
                     {"0", 1},
153
                     {"0", 1},
137
                     {"0 1", 2},
154
                     {"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}
141
             });
162
             });
142
         }
163
         }
143
 
164
 
144
         public CountColumnsTest(String matrixAsString, int numberOfColumns) {
165
         public CountColumnsTest(String matrixAsString, int numberOfColumns) {
145
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
166
+            this.matrix = new Matrix(matrixAsString);
146
             this.numberOfColumns = numberOfColumns;
167
             this.numberOfColumns = numberOfColumns;
147
         }
168
         }
148
 
169
 
149
-        @Ignore
150
         @Test
170
         @Test
151
         public void countColumnsTest() {
171
         public void countColumnsTest() {
152
-            assertEquals(matrix.getColumnsCount(), numberOfColumns);
172
+            assertEquals(numberOfColumns, matrix.getColumnsCount());
153
         }
173
         }
154
     }
174
     }
155
 
175
 
176
+    @Ignore
156
     @RunWith(Parameterized.class)
177
     @RunWith(Parameterized.class)
157
     public static class CountRowsTest {
178
     public static class CountRowsTest {
158
         private Matrix matrix;
179
         private Matrix matrix;
159
         private int numberOfRows;
180
         private int numberOfRows;
160
 
181
 
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).")
162
         public static Collection<Object[]> data() {
183
         public static Collection<Object[]> data() {
163
             return Arrays.asList(new Object[][]{
184
             return Arrays.asList(new Object[][]{
164
                     {"0", 1},
185
                     {"0", 1},
165
                     {"0 1", 1},
186
                     {"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}
169
             });
194
             });
170
         }
195
         }
171
 
196
 
172
         public CountRowsTest(String matrixAsString, int numberOfRows) {
197
         public CountRowsTest(String matrixAsString, int numberOfRows) {
173
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
198
+            this.matrix = new Matrix(matrixAsString);
174
             this.numberOfRows = numberOfRows;
199
             this.numberOfRows = numberOfRows;
175
         }
200
         }
176
 
201
 
177
-        @Ignore
178
         @Test
202
         @Test
179
         public void countRowsTest() {
203
         public void countRowsTest() {
180
-            assertEquals(matrix.getRowsCount(), numberOfRows);
204
+            assertEquals(numberOfRows, matrix.getRowsCount());
181
         }
205
         }
182
     }
206
     }
183
     
207