src/test/java/de/uapcore/sudoku/SolverTest.java

Wed, 26 Aug 2020 19:09:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Aug 2020 19:09:07 +0200
changeset 25
569220009c54
parent 23
07b9adaed78e
permissions
-rw-r--r--

fixes wrong call of assertEquals()

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

mercurial