tests redaction
All checks were successful
Linux arm64 / Build (push) Successful in 37s

This commit is contained in:
Janet-Doe
2025-02-02 11:36:07 +01:00
parent 990c830590
commit 4903fd567b
2 changed files with 95 additions and 71 deletions

View File

@@ -1,7 +1,6 @@
package sudoku; package sudoku;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File; import java.io.File;
import java.util.Random; import java.util.Random;
@@ -22,7 +21,7 @@ public class SudokuSerializerTest {
new RandomSolver().solve(sudoku); new RandomSolver().solve(sudoku);
JSONObject data = SudokuSerializer.serializeSudoku(sudoku); JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data); MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString())); assertEquals(data.toString(), SudokuSerializer.serializeSudoku(multiDoku).toString());
} }
void testSaveWithSize(int blockWidth, int blockHeight) { void testSaveWithSize(int blockWidth, int blockHeight) {
@@ -41,7 +40,7 @@ public class SudokuSerializerTest {
File fileToDelete = new File(savePath); File fileToDelete = new File(savePath);
fileToDelete.delete(); fileToDelete.delete();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); System.out.println(e.getMessage());
assert false; assert false;
} }
} }
@@ -52,7 +51,7 @@ public class SudokuSerializerTest {
JSONObject data = SudokuSerializer.serializeSudoku(sudoku); JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data); MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString())); assertEquals(data.toString(), SudokuSerializer.serializeSudoku(multiDoku).toString());
} }
@Test @Test
@@ -60,17 +59,17 @@ public class SudokuSerializerTest {
Random r = new Random(); Random r = new Random();
int testCount = 20; int testCount = 20;
for (int i = 0; i < testCount; i++) { for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(4) + 1; int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(4) + 1; int blockHeight = r.nextInt(10) + 1;
testSerializeWithSize(blockWidth, blockHeight); testSerializeWithSize(blockWidth, blockHeight);
} }
for (int i = 0; i < testCount; i++) { for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(4) + 1; int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(4) + 1; int blockHeight = r.nextInt(10) + 1;
testSaveWithSize(blockWidth, blockHeight); testSaveWithSize(blockWidth, blockHeight);
} }
for (int i = 0; i < testCount; i++) { for (int i = 0; i < testCount; i++) {
int size = r.nextInt(2) + 2; int size = r.nextInt(10) + 1;
testSerializeX(size); testSerializeX(size);
} }
} }

View File

@@ -9,23 +9,50 @@ import org.junit.jupiter.api.Test;
import gui.constants.Symbols; import gui.constants.Symbols;
import sudoku.io.SudokuPrinter; import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer; import sudoku.io.SudokuSerializer;
import sudoku.structure.Cell; import sudoku.structure.*;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
import sudoku.structure.SudokuFactory;
class SolverTest { class SolverTest {
private int ns = Cell.NOSYMBOL;
@Test void testSize2(String solverToUse){
void solveTest() { MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS); MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS); Sudoku test = mdTest.getSubGrid(0);
Sudoku result = mdResult.getSubGrid(0);
List<Integer> immutableCells = List.of(
ns, 2, 3, ns,
0, ns, ns, ns,
ns, ns, ns, 3,
ns, 0, 1, ns);
assert (test.setImmutableCellsSymbol(immutableCells));
List<Integer> correctCells = List.of(
1, 2, 3, 0,
0, 3, 2, 1,
2, 1, 0, 3,
3, 0, 1, 2);
assert(result.setCellsSymbol(correctCells));
assert(result.isSolved());
Sudoku sudokuToTest = dokuToTest.getSubGrid(0); switch (solverToUse){
Sudoku sudokuResult = dokuResult.getSubGrid(0); case "human": new HumanSolver().solve(mdTest); break;
case "mixed": new MixedSolver().solve(mdTest); break;
default: new RandomSolver().solve(mdTest); break;
}
assert (mdTest.isSolved());
for (Cell cell : test.getCells()) {
cell.setImmutable();
}
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
SudokuSerializer.serializeSudoku(mdResult).toString());
}
int ns = Cell.NOSYMBOL; void testSize3(String solverToUse){
List<Integer> immutableCells = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1, MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
Sudoku test = mdTest.getSubGrid(0);
Sudoku result = mdResult.getSubGrid(0);
List<Integer> immutableCells = List.of(
ns, ns, 0, ns, ns, 2, 8, ns, 1,
ns, 3, ns, ns, 5, 6, 7, ns, ns, ns, 3, ns, ns, 5, 6, 7, ns, ns,
ns, ns, ns, 8, ns, 7, ns, ns, 6, ns, ns, ns, 8, ns, 7, ns, ns, 6,
0, ns, 1, ns, ns, ns, ns, ns, ns, 0, ns, 1, ns, ns, ns, ns, ns, ns,
@@ -34,13 +61,9 @@ class SolverTest {
ns, ns, 6, ns, ns, 8, ns, 7, 5, ns, ns, 6, ns, ns, 8, ns, 7, 5,
8, 0, ns, 7, ns, 5, 2, ns, 3, 8, 0, ns, 7, ns, 5, 2, ns, 3,
5, ns, ns, ns, 3, 1, 0, ns, ns); 5, ns, ns, ns, 3, 1, 0, ns, ns);
assert (test.setImmutableCellsSymbol(immutableCells));
assert (sudokuToTest.setImmutableCellsSymbol(immutableCells)); List<Integer> correctCells = List.of(
7, 6, 0, 3, 4, 2, 8, 5, 1,
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Numbers);
List<Integer> correctCells = List.of(7, 6, 0, 3, 4, 2, 8, 5, 1,
2, 3, 8, 1, 5, 6, 7, 0, 4, 2, 3, 8, 1, 5, 6, 7, 0, 4,
1, 4, 5, 8, 0, 7, 3, 2, 6, 1, 4, 5, 8, 0, 7, 3, 2, 6,
0, 2, 1, 6, 8, 3, 5, 4, 7, 0, 2, 1, 6, 8, 3, 5, 4, 7,
@@ -49,56 +72,58 @@ class SolverTest {
3, 1, 6, 0, 2, 8, 4, 7, 5, 3, 1, 6, 0, 2, 8, 4, 7, 5,
8, 0, 4, 7, 6, 5, 2, 1, 3, 8, 0, 4, 7, 6, 5, 2, 1, 3,
5, 7, 2, 4, 3, 1, 0, 6, 8); 5, 7, 2, 4, 3, 1, 0, 6, 8);
assert(result.setCellsSymbol(correctCells));
assert(result.isSolved());
sudokuResult.setCellsSymbol(correctCells); switch (solverToUse){
case "human": new HumanSolver().solve(mdTest); break;
System.out.println("\n****************************Doku Control\n"); case "mixed": new MixedSolver().solve(mdTest); break;
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3, Symbols.Russian); default: new RandomSolver().solve(mdTest); break;
}
assert (dokuResult.isSolved()); assert (mdTest.isSolved());
for (Cell cell : test.getCells()) {
new RandomSolver().solve(dokuToTest);
System.out.println("\n****************************\nDoku solved");
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Emojis);
assert (dokuToTest.isSolved());
for (Cell cell : sudokuToTest.getCells()) {
cell.setImmutable(); cell.setImmutable();
} }
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
SudokuSerializer.serializeSudoku(mdResult).toString());
}
for (Cell cell : sudokuResult.getCells()) { private void testMDSize3(String solverToUse){
MultiDoku mdTest = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
try {
SudokuFactory.fillDoku(mdTest, Difficulty.Easy);
} catch (Exception e) {
assert(false);
}
MultiDoku result = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
assert(result.isSolved());
switch (solverToUse){
case "human": new HumanSolver().solve(mdTest); break;
case "mixed": new MixedSolver().solve(mdTest); break;
default: new RandomSolver().solve(mdTest); break;
}
assert (mdTest.isSolved());
for (Cell cell : mdTest.getCells()) {
cell.setImmutable(); cell.setImmutable();
} }
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
SudokuSerializer.serializeSudoku(result).toString());
}
assertEquals(SudokuSerializer.serializeSudoku(dokuResult).toString(), @Test
SudokuSerializer.serializeSudoku(dokuToTest).toString()); void solveTest() {
String h = "human";
String m = "mixed";
String r = "random";
MultiDoku dokuToTest2 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS); testSize2(h);
Sudoku sudokuToTest2 = dokuToTest2.getSubGrid(0); testSize3(h);
testMDSize3(h);
List<Integer> immutableCells2 = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1, testSize2(m);
1, 3, ns, ns, 5, 6, 7, ns, ns, testSize3(m);
ns, ns, ns, 8, ns, 7, ns, ns, 6, testMDSize3(m);
0, ns, 1, ns, ns, ns, ns, ns, ns, testSize2(r);
4, 8, 7, 5, 1, ns, 6, ns, ns, testSize3(r);
6, ns, 3, 2, ns, ns, ns, 8, 0, testMDSize3(r);
ns, ns, 6, ns, ns, 8, ns, 7, 5,
8, 0, ns, 7, ns, 5, 2, ns, 3,
5, ns, ns, ns, 3, 1, 0, ns, ns);
sudokuToTest2.setImmutableCellsSymbol(immutableCells2);
boolean isSolved = new RandomSolver().solve(dokuToTest2);
assert (!isSolved);
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
new RandomSolver().solve(dokuToTest3);
//SudokuPrinter.printRectangleSudoku(dokuToTest3.getSubGrid(0), 3, 3);
SudokuPrinter.printMultiDoku(dokuToTest3, 3, 3, Symbols.Letters);
} }
} }