-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMiniTester.java
More file actions
374 lines (321 loc) · 12.4 KB
/
MiniTester.java
File metadata and controls
374 lines (321 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import java.io.BufferedReader;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Scanner;
public class MiniTester {
private static Game loadSudoku(String in) {
Scanner sc = new Scanner(new BufferedReader (new StringReader(in)));
int rows = sc.nextInt();
int columns = sc.nextInt();
int[][] board = new int[rows][columns];
//Reading the board
for (int i=0; i<board.length; i++){
for (int j=0; j<board[i].length; j++){
String value = sc.next();
if (value.equals("-")) {
board[i][j] = -1;
} else {
try {
board[i][j] = Integer.valueOf(value);
}catch(Exception e) {
System.out.println("Ups, something went wrong");
}
}
}
}
int regions = sc.nextInt();
Game game = new Game();
game.sudoku = game.new Board(rows, columns, regions);
game.sudoku.setValues(board);
for (int i=0; i< regions;i++) {
int num_cells = sc.nextInt();
Game.Region new_region = game.new Region(num_cells);
for (int j=0; j< num_cells; j++) {
String cell = sc.next();
String value1 = cell.substring(cell.indexOf("(") + 1, cell.indexOf(","));
String value2 = cell.substring(cell.indexOf(",") + 1, cell.indexOf(")"));
Game.Cell new_cell = game.new Cell(Integer.valueOf(value1)-1,Integer.valueOf(value2)-1);
new_region.setCell(j, new_cell);
}
game.sudoku.setRegion(i, new_region);
}
sc.close();
return game;
}
private static void testSudoku1() {
System.out.println("----------TEST OUTPUT OF SUDOKU 1----------\n");
String in = "3 5\n"
+ "- - - - -\n"
+ "- - - - -\n"
+ "4 - - - 1\n"
+ "5\n"
+ "1 (1,1)\n"
+ "2 (1,2) (1,3)\n"
+ "5 (2,1) (2,2) (3,1) (3,2) (3,3)\n"
+ "4 (2,3) (2,4) (1,4) (1,5)\n"
+ "3 (3,4) (3,5) (2,5)";
int[][] expected = {{1,2,1,2,1},{3,5,3,4,3},{4,2,1,2,1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 1.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 1 for the solver method!"+ "\n");
}
private static void testSudoku2() {
System.out.println("----------TEST OUTPUT OF SUDOKU 2----------\n");
String in = "7 7\n"
+ "- - - - - - -\n"
+ "4 - - - - 5 -\n"
+ "- - - - - - -\n"
+ "- - - 3 - - -\n"
+ "- - - - - - -\n"
+ "- - - - - - -\n"
+ "- - - - - - -\n"
+ "14\n"
+ "5 (1,1) (2,1) (3,1) (2,2) (2,3)\n"
+ "4 (2,4) (1,2) (1,3) (1,4)\n"
+ "2 (1,5) (1,6)\n"
+ "1 (1,7)\n"
+ "4 (4,1) (5,1) (6,1) (5,2)\n"
+ "1 (7,1)\n"
+ "4 (3,2) (4,2) (4,3) (5,3)\n"
+ "3 (7,3) (7,2) (6,2)\n"
+ "4 (6,3) (6,4) (7,4) (7,5)\n"
+ "6 (2,5) (3,5) (4,5) (3,6) (2,6) (2,7)\n"
+ "4 (3,7) (4,7) (4,6) (5,6)\n"
+ "1 (5,7)\n"
+ "5 (7,7) (7,6) (6,7) (6,6) (6,5)\n"
+ "5 (3,3) (3,4) (4,4) (5,4) (5,5)";
int[][] expected = {{3,1,4,2,1,2,1},{4,2,5,3,6,5,4},{1,3,1,4,2,3,1},{2,4,2,3,1,4,2},{1,3,1,5,2,3,1},{4,2,4,3,4,5,2},{1,3,1,2,1,3,1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 2.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 2 for the solver method!"+ "\n");
}
private static void testSudoku3() {
System.out.println("----------TEST OUTPUT OF SUDOKU 3----------\n");
String in = "1 1\n"
+ "-\n"
+ "1\n"
+ "1 (1,1)";
int[][] expected = {{1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 3.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 3 for the solver method!"+ "\n");
}
private static void testSudoku4() {
System.out.println("----------TEST OUTPUT OF SUDOKU 4----------\n");
String in = "1 3\n"
+ "- - -\n"
+ "2\n"
+ "1 (1,1)\n"
+ "2 (1,2) (1,3)";
int[][] expected = {{1,2,1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 4.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 4 for the solver method!"+ "\n");
}
private static void testSudoku5() {
System.out.println("----------TEST OUTPUT OF SUDOKU 5----------\n");
String in = "1 3\n"
+ "- - -\n"
+ "2\n"
+ "1 (1,3)\n"
+ "2 (1,2) (1,1)";
int[][] expected = {{1,2,1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 5.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 5 for the solver method!"+ "\n");
}
private static void testSudoku6() {
System.out.println("----------TEST OUTPUT OF SUDOKU 6----------\n");
String in = "3 1\n"
+ "-\n"
+ "-\n"
+ "-\n"
+ "2\n"
+ "1 (1,1)\n"
+ "2 (2,1) (3,1)";
int[][] expected = {{1},{2},{1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 6.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 6 for the solver method!"+ "\n");
}
private static void testSudoku7() {
System.out.println("----------TEST OUTPUT OF SUDOKU 7----------\n");
String in = "3 1\n"
+ "-\n"
+ "-\n"
+ "-\n"
+ "2\n"
+ "1 (3,1)\n"
+ "2 (2,1) (1,1)";
int[][] expected = {{1},{2},{1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 7.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 7 for the solver method!"+ "\n");
}
private static void testSudoku8() {
System.out.println("----------TEST OUTPUT OF SUDOKU 8----------\n");
String in = "7 7\n"
+ "2 - - - 2 - 1\n"
+ "- - - 4 - - -\n"
+ "4 - - - - - -\n"
+ "- 3 - - - 3 -\n"
+ "- - - - - - 4\n"
+ "- - - 2 - - -\n"
+ "1 - 3 - - - 3\n"
+ "10\n"
+ "5 (1,1) (2,1) (2,2) (3,1) (3,2)\n"
+ "5 (1,2) (1,3) (1,4) (2,4) (2,3)\n"
+ "5 (1,6) (1,5) (2,6) (1,7) (2,5)\n"
+ "4 (3,3) (4,5) (3,5) (3,4)\n"
+ "5 (4,7) (4,6) (3,6) (3,7) (2,7)\n"
+ "5 (4,1) (4,2) (5,1) (5,2) (6,1)\n"
+ "5 (4,3) (4,4) (5,4) (5,3) (5,5)\n"
+ "5 (5,6) (5,7) (6,6) (6,7) (7,7)\n"
+ "5 (7,1) (7,2) (7,3) (6,3) (6,2)\n"
+ "5 (6,4) (6,5) (7,4) (7,5) (7,6)";
int[][] expected = {{2,5,1,3,2,4,1},{1,3,2,4,5,3,5},{4,5,1,3,2,1,4},{2,3,2,5,4,3,2},{4,1,4,3,1,5,4},{5,2,5,2,4,2,1},{1,4,3,1,3,5,3}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 8.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 8 for the solver method!"+ "\n");
}
private static void testSudoku9() {
System.out.println("----------TEST OUTPUT OF SUDOKU 9----------\n");
String in = "7 7\n"
+ "- - - - - - 1\n"
+ "- 5 - - - - -\n"
+ "- - 3 - - - -\n"
+ "- - - - - - -\n"
+ "- - - - 2 - -\n"
+ "- - - - - 5 -\n"
+ "4 - - - - - -\n"
+ "11\n"
+ "4 (4,1) (1,1) (3,1) (2,1)\n"
+ "5 (1,2) (2,2) (3,2) (2,3) (2,4)\n"
+ "5 (1,3) (1,4) (1,5) (1,6) (2,5)\n"
+ "3 (3,7) (2,7) (1,7)\n"
+ "5 (2,6) (3,6) (3,4) (3,5) (4,6)\n"
+ "5 (3,3) (4,3) (4,4) (4,5) (5,3)\n"
+ "5 (6,2) (6,1) (5,2) (5,1) (4,2)\n"
+ "4 (7,1) (7,2) (6,3) (7,3)\n"
+ "5 (7,4) (6,4) (5,4) (6,6) (6,5)\n"
+ "5 (5,7) (5,5) (4,7) (6,7) (5,6)\n"
+ "3 (7,6) (7,5) (7,7)";
int[][] expected = {{2,3,2,5,1,4,1},{4,5,1,4,3,5,3},{3,2,3,2,1,4,2},{1,4,1,4,5,3,5},{2,3,2,3,2,4,1},{1,5,1,4,1,5,3},{4,2,3,2,3,2,1}};
Game game = loadSudoku(in);
int[][] result = game.solver();
if (!Arrays.deepEquals(result, expected))
System.err.println("Failed Sudoku 9.\nOutput: " + Arrays.deepToString(result) + "\nExpected: " + Arrays.deepToString(expected) + "\n");
else
System.out.println("Passed Sudoku test 9 for the solver method!"+ "\n");
}
private static void setPenalizationArray(int[] arr) {
try {
Class<?> clazz = Midterm.class;
Object cc = clazz.newInstance();
Field f1 = cc.getClass().getDeclaredField("penalization");
f1.setAccessible(true);
f1.set(cc, arr);
} catch(Exception e) {
e.printStackTrace();
System.err.println("Failed to change penalization variable in Midterm class!");
}
}
private static void testMidterm1() {
System.out.println("----------TEST OUTPUT OF MIDTERM 1----------\n");
int[] penalization = {216,190,171,59,62};
setPenalizationArray(penalization);
int expected = 501;
int answer = Midterm.lost_marks(penalization);
if (expected != answer)
System.err.println("Failed Midterm test 1.\nOutput: " + answer + "\nExpected: " + expected + "\n");
else
System.out.println("Passed Midterm test 1 for the lost_marks method!"+ "\n");
}
private static void testMidterm2() {
System.out.println("----------TEST OUTPUT OF MIDTERM 2----------\n");
int[] penalization = {111,179,64,78,219,97,160,40};
setPenalizationArray(penalization);
int expected = 535;
int answer = Midterm.lost_marks(penalization);
if (expected != answer)
System.err.println("Failed Midterm test 2.\nOutput: " + answer + "\nExpected: " + expected + "\n");
else
System.out.println("Passed Midterm test 2 for the lost_marks method!"+ "\n");
}
private static void testMidterm3() {
System.out.println("----------TEST OUTPUT OF MIDTERM 3----------\n");
int[] penalization = {84,235,135,229,154,185,4,114,86,106};
setPenalizationArray(penalization);
int expected = 745;
int answer = Midterm.lost_marks(penalization);
if (expected != answer)
System.err.println("Failed Midterm test 3.\nOutput: " + answer + "\nExpected: " + expected + "\n");
else
System.out.println("Passed Midterm test 3 for the lost_marks method!"+ "\n");
}
private static void testMidterm4() {
System.out.println("----------TEST OUTPUT OF MIDTERM 4----------\n");
int[] penalization = {98,180,141,12,161,202,259,214,150,163,60,202,148,137,40,150,15,83,205,115,21,78,166,224,81};
setPenalizationArray(penalization);
int expected = 1056;
int answer = Midterm.lost_marks(penalization);
if (expected != answer)
System.err.println("Failed Midterm test 4.\nOutput: " + answer + "\nExpected: " + expected + "\n");
else
System.out.println("Passed Midterm test 4 for the lost_marks method!"+ "\n");
}
private static void testMidterm5() {
System.out.println("----------TEST OUTPUT OF MIDTERM 5----------\n");
int[] penalization = {86,181,218,223,104,29,234,236,33,118,1,130,181,212,153,51,101,17,238,105,131,36,249,263,61,132,132,42,64,267,96,35,208,229,253,160,69,96,3,100,73,254,112,12,1,268,220,1,98,119};
setPenalizationArray(penalization);
int expected = 1205;
int answer = Midterm.lost_marks(penalization);
if (expected != answer)
System.err.println("Failed Midterm test 5.\nOutput: " + answer + "\nExpected: " + expected + "\n");
else
System.out.println("Passed Midterm test 5 for the lost_marks method!"+ "\n");
}
public static void main(String[] args) {
testSudoku1();
testSudoku2();
testSudoku3();
testSudoku4();
testSudoku5();
testSudoku6();
testSudoku7();
testSudoku8();
testSudoku9();
System.out.println("---------------------------------------------------------------------------------\n");
testMidterm1();
testMidterm2();
testMidterm3();
testMidterm4();
testMidterm5();
System.out.println("---------------------------------------------------------------------------------\n");
}
}