Просмотр исходного кода

update matrix tests (#1297)

* update matrix tests

* updated matrix tests

* updated matrix
jssander 8 лет назад
Родитель
Сommit
de71a98ff4

+ 0
- 8
exercises/matrix/.meta/src/reference/java/Matrix.java Просмотреть файл

@@ -28,12 +28,4 @@ class Matrix {
28 28
         }
29 29
         return column;
30 30
     }
31
-
32
-    int getRowsCount() {
33
-        return matrix.length;
34
-    }
35
-
36
-    int getColumnsCount() {
37
-        return matrix[0].length;
38
-    }
39 31
 }

+ 2
- 0
exercises/matrix/.meta/version Просмотреть файл

@@ -0,0 +1,2 @@
1
+1.0.0
2
+

+ 95
- 193
exercises/matrix/src/test/java/MatrixTest.java Просмотреть файл

@@ -10,198 +10,100 @@ import java.util.Collection;
10 10
 import static org.junit.Assert.assertArrayEquals;
11 11
 import static org.junit.Assert.assertEquals;
12 12
 
13
-@RunWith(Enclosed.class)
14 13
 public class MatrixTest {
15
-
16
-    @RunWith(Parameterized.class)
17
-    public static class CountRowsTest {
18
-        private Matrix matrix;
19
-        private int numberOfRows;
20
-
21
-        @Parameterized.Parameters(name = "{index}: expected matrix constructed with string \n\"{0}\" to have {1} row(s).")
22
-        public static Collection<Object[]> data() {
23
-            return Arrays.asList(new Object[][]{
24
-                    {"0", 1},
25
-                    {"0 1", 1},
26
-                    {  "0\n"
27
-                     + "1", 2},
28
-                    {  "0 1\n"
29
-                     + "2 3", 2},
30
-                    {  "0 1 2\n"
31
-                     + "3 4 5\n"
32
-                     + "6 7 8", 3}
33
-            });
34
-        }
35
-
36
-        public CountRowsTest(String matrixAsString, int numberOfRows) {
37
-            this.matrix = new Matrix(matrixAsString);
38
-            this.numberOfRows = numberOfRows;
39
-        }
40
-
41
-        @Test
42
-        public void countRowsTest() {
43
-            assertEquals(numberOfRows, matrix.getRowsCount());
44
-        }
45
-    }
46
-
47
-    @Ignore("Remove to run test")
48
-    @RunWith(Parameterized.class)
49
-    public static class CountColumnsTest {
50
-        private Matrix matrix;
51
-        private int numberOfColumns;
52
-
53
-        @Parameterized.Parameters(name = "{index}: expected matrix constructed with string \n\"{0}\" to have {1} column(s).")
54
-        public static Collection<Object[]> data() {
55
-            return Arrays.asList(new Object[][]{
56
-                    {"0", 1},
57
-                    {"0 1", 2},
58
-                    {  "0\n"
59
-                     + "1", 1},
60
-                    {  "0 1\n"
61
-                     + "2 3", 2},
62
-                    {  "0 1 2\n"
63
-                     + "3 4 5\n"
64
-                     + "6 7 8", 3}
65
-            });
66
-        }
67
-
68
-        public CountColumnsTest(String matrixAsString, int numberOfColumns) {
69
-            this.matrix = new Matrix(matrixAsString);
70
-            this.numberOfColumns = numberOfColumns;
71
-        }
72
-
73
-        @Test
74
-        public void countColumnsTest() {
75
-            assertEquals(numberOfColumns, matrix.getColumnsCount());
76
-        }
77
-    }
78
-
79
-    @Ignore("Remove to run test")
80
-    @RunWith(Parameterized.class)
81
-    public static class GetFirstRowTest {
82
-        private Matrix matrix;
83
-        private int[] firstRow;
84
-
85
-        @Parameterized.Parameters(name = "{index}: checking first row of matrix constructed with string \n\"{0}\".")
86
-        public static Collection<Object[]> data() {
87
-            return Arrays.asList(new Object[][]{
88
-                    {"0", new int[] {0}},
89
-                    {"0 1", new int[] {0, 1}},
90
-                    {  "0\n"
91
-                     + "1", new int[] {0}},
92
-                    {  "0 1\n"
93
-                     + "2 3", new int[] {0, 1}},
94
-                    {  "0 1 2\n"
95
-                     + "3 4 5\n"
96
-                     + "6 7 8", new int[] {0, 1, 2}}
97
-            });
98
-        }
99
-
100
-        public GetFirstRowTest(String matrixAsString, int[] firstRow) {
101
-            this.matrix = new Matrix(matrixAsString);
102
-            this.firstRow = firstRow;
103
-        }
104
-
105
-        @Test
106
-        public void getFirstRowTest() {
107
-            assertArrayEquals(firstRow, matrix.getRow(0));
108
-        }
109
-    }
110
-
111
-    @Ignore("Remove to run test")
112
-    @RunWith(Parameterized.class)
113
-    public static class GetLastRowTest {
114
-        private Matrix matrix;
115
-        private int[] lastRow;
116
-
117
-        @Parameterized.Parameters(name = "{index}: checking last row of matrix constructed with string \n\"{0}\".")
118
-        public static Collection<Object[]> data() {
119
-            return Arrays.asList(new Object[][]{
120
-                    {"0", new int[] {0}},
121
-                    {"0 1", new int[] {0, 1}},
122
-                    {  "0\n"
123
-                     + "1", new int[] {1}},
124
-                    {  "0 1\n"
125
-                     + "2 3", new int[] {2, 3}},
126
-                    {  "0 1 2\n"
127
-                     + "3 4 5\n"
128
-                     + "6 7 8", new int[] {6, 7, 8}}
129
-            });
130
-        }
131
-
132
-        public GetLastRowTest(String matrixAsString, int[] lastRow) {
133
-            this.matrix = new Matrix(matrixAsString);
134
-            this.lastRow = lastRow;
135
-        }
136
-
137
-        @Test
138
-        public void getLastRowTest() {
139
-            assertArrayEquals(lastRow, matrix.getRow(matrix.getRowsCount() - 1));
140
-        }
141
-    }
142
-
143
-    @Ignore("Remove to run test")
144
-    @RunWith(Parameterized.class)
145
-    public static class GetFirstColumnTest {
146
-        private Matrix matrix;
147
-        private int[] firstColumn;
148
-
149
-        @Parameterized.Parameters(name = "{index}: checking first column of matrix constructed with string \n\"{0}\".")
150
-        public static Collection<Object[]> data() {
151
-            return Arrays.asList(new Object[][]{
152
-                    {"0", new int[] {0}},
153
-                    {"0 1", new int[] {0}},
154
-                    {  "0\n"
155
-                     + "1", new int[] {0, 1}},
156
-                    {  "0 1\n"
157
-                     + "2 3", new int[] {0, 2}},
158
-                    {  "0 1 2\n"
159
-                     + "3 4 5\n"
160
-                     + "6 7 8", new int[] {0, 3, 6}}
161
-            });
162
-        }
163
-
164
-        public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
165
-            this.matrix = new Matrix(matrixAsString);
166
-            this.firstColumn = firstColumn;
167
-        }
168
-
169
-        @Test
170
-        public void getFirstColumnTest() {
171
-            assertArrayEquals(firstColumn, matrix.getColumn(0));
172
-        }
173
-    }
174
-
175
-
176
-    @Ignore("Remove to run test")
177
-    @RunWith(Parameterized.class)
178
-    public static class GetLastColumnTest {
179
-        private Matrix matrix;
180
-        private int[] lastColumn;
181
-
182
-        @Parameterized.Parameters(name = "{index}: checking last column of matrix constructed with string \n\"{0}\".")
183
-        public static Collection<Object[]> data() {
184
-            return Arrays.asList(new Object[][]{
185
-                    {"0", new int[] {0}},
186
-                    {"0 1", new int[] {1}},
187
-                    {  "0\n"
188
-                     + "1", new int[] {0, 1}},
189
-                    {  "0 1\n"
190
-                     + "2 3", new int[] {1, 3}},
191
-                    {  "0 1 2\n"
192
-                     + "3 4 5\n"
193
-                     + "6 7 8", new int[] {2, 5, 8}}
194
-            });
195
-        }
196
-
197
-        public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
198
-            this.matrix = new Matrix(matrixAsString);
199
-            this.lastColumn = lastColumn;
200
-        }
201
-
202
-        @Test
203
-        public void getLastColumnTest() {
204
-            assertArrayEquals(lastColumn, matrix.getColumn(matrix.getColumnsCount() - 1));
205
-        }
206
-    }
14
+	
15
+	@Test
16
+	public void extractRowFromOneNumberMatrixTest() {
17
+		String matrixAsString = "1";
18
+		int rowIndex = 0;
19
+		int[] expectedRow = {1};
20
+		
21
+		Matrix matrix = new Matrix(matrixAsString);
22
+		
23
+		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
24
+	}
25
+	
26
+	@Ignore("Remove to run test")
27
+	@Test
28
+	public void extractRowFromMatrixTest() {
29
+		String matrixAsString = "1 2\n3 4";
30
+		int rowIndex = 1;
31
+		int[] expectedRow = {3, 4};
32
+		
33
+		Matrix matrix = new Matrix(matrixAsString);
34
+		
35
+		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
36
+	}
37
+	
38
+	@Ignore("Remove to run test")
39
+	@Test
40
+	public void extractRowFromDiffWidthsMatrixTest() {
41
+		String matrixAsString = "1 2\n10 20";
42
+		int rowIndex = 1;
43
+		int[] expectedRow = {10, 20};
44
+		
45
+		Matrix matrix = new Matrix(matrixAsString);
46
+		
47
+		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
48
+	}
49
+	
50
+	@Ignore("Remove to run test")
51
+	@Test
52
+	public void extractRowFromNonSquareMatrixTest() {
53
+		String matrixAsString = "1 2 3\n4 5 6\n7 8 9\n8 7 6";
54
+		int rowIndex = 2;
55
+		int[] expectedRow = {7, 8, 9};
56
+		
57
+		Matrix matrix = new Matrix(matrixAsString);
58
+		
59
+		assertArrayEquals(expectedRow, matrix.getRow(rowIndex));
60
+	}
61
+	
62
+	@Ignore("Remove to run test")
63
+	@Test
64
+	public void extractColumnFromOneNumberMatrixTest() {
65
+		String matrixAsString = "1";
66
+		int columnIndex = 0;
67
+		int[] expectedColumn = {1};
68
+		
69
+		Matrix matrix = new Matrix(matrixAsString);
70
+		
71
+		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
72
+	}
73
+	
74
+	@Ignore("Remove to run test")
75
+	@Test
76
+	public void extractColumnMatrixTest() {
77
+		String matrixAsString = "1 2 3\n4 5 6\n7 8 9";
78
+		int columnIndex = 2;
79
+		int[] expectedColumn = {3, 6, 9};
80
+		
81
+		Matrix matrix = new Matrix(matrixAsString);
82
+		
83
+		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
84
+	}
85
+	
86
+	@Ignore("Remove to run test")
87
+	@Test
88
+	public void extractColumnFromNonSquareMatrixTest() {
89
+		String matrixAsString = "1 2 3\n4 5 6\n7 8 9\n8 7 6";
90
+		int columnIndex = 2;
91
+		int[] expectedColumn = {3, 6, 9, 6};
92
+		
93
+		Matrix matrix = new Matrix(matrixAsString);
94
+		
95
+		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
96
+	}
97
+	
98
+	@Ignore("Remove to run test")
99
+	@Test
100
+	public void extractColumnFromDiffWidthsMatrixTest() {
101
+		String matrixAsString = "89 1903 3\n18 3 1\n9 4 800";
102
+		int columnIndex = 1;
103
+		int[] expectedColumn = {1903, 3, 4};
104
+		
105
+		Matrix matrix = new Matrix(matrixAsString);
106
+		
107
+		assertArrayEquals(expectedColumn, matrix.getColumn(columnIndex));
108
+	}
207 109
 }