Tue, 28 Jul 2020 14:45:06 +0200
adds target/ to .hgignore
23 | 1 | package de.uapcore.sudoku; |
2 | ||
3 | import org.junit.jupiter.api.Test; | |
4 | ||
5 | import static org.junit.jupiter.api.Assertions.*; | |
6 | ||
7 | class SolverTest { | |
8 | ||
9 | private int[] createTestdata() { | |
10 | return new int[]{ | |
11 | 0,1,0,9,0,0,8,0,0, | |
12 | 0,0,0,0,0,8,0,0,4, | |
13 | 6,0,5,0,0,0,7,0,0, | |
14 | 0,9,0,0,6,0,0,0,8, | |
15 | 0,0,0,2,0,7,0,0,0, | |
16 | 8,0,0,0,3,0,0,6,0, | |
17 | 0,0,2,0,0,0,5,0,3, | |
18 | 1,0,0,4,0,0,0,0,0, | |
19 | 0,0,6,0,0,2,0,1,0}; | |
20 | } | |
21 | ||
22 | private int[] createTestdataSolved() { | |
23 | return new int[]{ | |
24 | 4,1,7,9,2,5,8,3,6, | |
25 | 3,2,9,6,7,8,1,5,4, | |
26 | 6,8,5,3,4,1,7,9,2, | |
27 | 2,9,1,5,6,4,3,7,8, | |
28 | 5,6,3,2,8,7,9,4,1, | |
29 | 8,7,4,1,3,9,2,6,5, | |
30 | 9,4,2,7,1,6,5,8,3, | |
31 | 1,5,8,4,9,3,6,2,7, | |
32 | 7,3,6,8,5,2,4,1,9}; | |
33 | } | |
34 | ||
35 | private Field createTestField(int[] testdata) { | |
36 | final var field = new Field(); | |
37 | for (int x = 0 ; x < 9 ; x++) { | |
38 | for (int y = 0 ; y < 9 ; y++) { | |
39 | field.setCellValue(x, y, testdata[x+9*y]); | |
40 | } | |
41 | } | |
42 | return field; | |
43 | } | |
44 | ||
45 | ||
46 | @Test | |
47 | void solveSuccess() { | |
48 | final var field = createTestField(createTestdata()); | |
49 | final var solvedField = createTestField(createTestdataSolved()); | |
50 | assertTrue(new Solver().solve(field)); | |
51 | for (int x = 0 ; x < 9 ; x++) { | |
52 | for (int y = 0 ; y < 9 ; y++) { | |
53 | assertEquals(solvedField.getCellValue(x, y), field.getCellValue(x, y)); | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | @Test | |
59 | void solvingSetsModifiedStates() { | |
60 | final var originalField = createTestField(createTestdata()); | |
61 | final var field = createTestField(createTestdata()); | |
62 | assertTrue(new Solver().solve(field)); | |
63 | for (int x = 0 ; x < 9 ; x++) { | |
64 | for (int y = 0 ; y < 9 ; y++) { | |
65 | if (originalField.isCellEmpty(x, y)) { | |
66 | assertTrue(field.isCellModified(x, y)); | |
67 | } else { | |
68 | assertFalse(field.isCellModified(x, y)); | |
69 | } | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
74 | @Test | |
75 | void checkSuccess() { | |
76 | final var field = createTestField(createTestdata()); | |
77 | assertTrue(new Solver().check(field)); | |
78 | assertFalse(field.isAnyCellModified()); | |
79 | } | |
80 | ||
81 | @Test | |
82 | void solveFail() { | |
83 | final var originalField = createTestField(createTestdata()); | |
84 | final var field = createTestField(createTestdata()); | |
85 | originalField.setCellValue(0, 3, 5); | |
86 | field.setCellValue(0, 3, 5); | |
87 | assertTrue(new Solver().check(field)); | |
88 | assertFalse(new Solver().solve(field)); | |
89 | for (int x = 0 ; x < 9 ; x++) { | |
90 | for (int y = 0 ; y < 9 ; y++) { | |
91 | if (originalField.isCellEmpty(x, y)) { | |
92 | assertTrue(field.isCellEmpty(x, y)); | |
93 | } else { | |
94 | assertFalse(field.isCellEmpty(x, y)); | |
95 | assertFalse(field.isCellModified(x, y)); | |
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | @Test | |
102 | void checkFail() { | |
103 | final var field = createTestField(createTestdata()); | |
104 | field.setCellValue(4, 6, 4); | |
105 | assertFalse(new Solver().check(field)); | |
106 | assertFalse(field.isAnyCellModified()); | |
107 | } | |
108 | } |