Преглед на файлове

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 години
родител
ревизия
daf17c0bef
променени са 2 файла, в които са добавени 72 реда и са изтрити 44 реда
  1. 7
    3
      exercises/matrix/src/example/java/Matrix.java
  2. 65
    41
      exercises/matrix/src/test/java/MatrixTest.java

+ 7
- 3
exercises/matrix/src/example/java/Matrix.java Целия файл

@@ -1,14 +1,18 @@
1
+import java.util.regex.Pattern;
2
+
1 3
 public class Matrix {
2 4
     private int[][] matrix;
5
+    private static Pattern spacePattern = Pattern.compile(" ");
6
+    private static Pattern newlinePattern = Pattern.compile("\\n");
3 7
 
4 8
     public Matrix(String matrixAsString) {
5
-        String[] rows = matrixAsString.split("\\n");
9
+        String[] rows = newlinePattern.split(matrixAsString);
6 10
         matrix = new int[rows.length][];
7 11
         for (int i = 0; i < rows.length; i++) {
8
-            String[] columnValues = rows[i].split(" ");
12
+            String[] columnValues = spacePattern.split(rows[i]);
9 13
             matrix[i] = new int[columnValues.length];
10 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 Целия файл

@@ -18,166 +18,190 @@ public class MatrixTest {
18 18
         private Matrix matrix;
19 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 22
         public static Collection<Object[]> data() {
23 23
             return Arrays.asList(new Object[][]{
24 24
                     {"0", new int[] {0}},
25 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 36
         public GetFirstRowTest(String matrixAsString, int[] firstRow) {
33
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
37
+            this.matrix = new Matrix(matrixAsString);
34 38
             this.firstRow = firstRow;
35 39
         }
36 40
 
37 41
         @Test
38 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 48
     @RunWith(Parameterized.class)
44 49
     public static class GetLastRowTest {
45 50
         private Matrix matrix;
46 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 54
         public static Collection<Object[]> data() {
50 55
             return Arrays.asList(new Object[][]{
51 56
                     {"0", new int[] {0}},
52 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 68
         public GetLastRowTest(String matrixAsString, int[] lastRow) {
60
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
69
+            this.matrix = new Matrix(matrixAsString);
61 70
             this.lastRow = lastRow;
62 71
         }
63 72
 
64
-        @Ignore
65 73
         @Test
66 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 80
     @RunWith(Parameterized.class)
72 81
     public static class GetFirstColumnTest {
73 82
         private Matrix matrix;
74 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 86
         public static Collection<Object[]> data() {
78 87
             return Arrays.asList(new Object[][]{
79 88
                     {"0", new int[] {0}},
80 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 100
         public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
88
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
101
+            this.matrix = new Matrix(matrixAsString);
89 102
             this.firstColumn = firstColumn;
90 103
         }
91 104
 
92
-        @Ignore
93 105
         @Test
94 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 113
     @RunWith(Parameterized.class)
101 114
     public static class GetLastColumnTest {
102 115
         private Matrix matrix;
103 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 119
         public static Collection<Object[]> data() {
107 120
             return Arrays.asList(new Object[][]{
108 121
                     {"0", new int[] {0}},
109 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 133
         public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
117
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
134
+            this.matrix = new Matrix(matrixAsString);
118 135
             this.lastColumn = lastColumn;
119 136
         }
120 137
 
121
-        @Ignore
122 138
         @Test
123 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 145
     @RunWith(Parameterized.class)
129 146
     public static class CountColumnsTest {
130 147
         private Matrix matrix;
131 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 151
         public static Collection<Object[]> data() {
135 152
             return Arrays.asList(new Object[][]{
136 153
                     {"0", 1},
137 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 165
         public CountColumnsTest(String matrixAsString, int numberOfColumns) {
145
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
166
+            this.matrix = new Matrix(matrixAsString);
146 167
             this.numberOfColumns = numberOfColumns;
147 168
         }
148 169
 
149
-        @Ignore
150 170
         @Test
151 171
         public void countColumnsTest() {
152
-            assertEquals(matrix.getColumnsCount(), numberOfColumns);
172
+            assertEquals(numberOfColumns, matrix.getColumnsCount());
153 173
         }
154 174
     }
155 175
 
176
+    @Ignore
156 177
     @RunWith(Parameterized.class)
157 178
     public static class CountRowsTest {
158 179
         private Matrix matrix;
159 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 183
         public static Collection<Object[]> data() {
163 184
             return Arrays.asList(new Object[][]{
164 185
                     {"0", 1},
165 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 197
         public CountRowsTest(String matrixAsString, int numberOfRows) {
173
-            this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
198
+            this.matrix = new Matrix(matrixAsString);
174 199
             this.numberOfRows = numberOfRows;
175 200
         }
176 201
 
177
-        @Ignore
178 202
         @Test
179 203
         public void countRowsTest() {
180
-            assertEquals(matrix.getRowsCount(), numberOfRows);
204
+            assertEquals(numberOfRows, matrix.getRowsCount());
181 205
         }
182 206
     }
183 207