This commit is contained in:
@@ -18,7 +18,8 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
|
||||
|
||||
implementation 'org.json:json:20250107'
|
||||
|
||||
|
||||
@@ -192,6 +192,26 @@ public class MultiDoku {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (!(other instanceof MultiDoku))
|
||||
return false;
|
||||
MultiDoku otherDoku = (MultiDoku) other;
|
||||
if (this.getNbSubGrids() != otherDoku.getNbSubGrids())
|
||||
return false;
|
||||
for (int i = 0; i < this.getNbSubGrids(); i++) {
|
||||
Sudoku sudoku = this.getSubGrid(i);
|
||||
Sudoku otherSudoku = otherDoku.getSubGrid(i);
|
||||
if (sudoku.getSize() != otherSudoku.getSize())
|
||||
return false;
|
||||
for (int j = 0; j < sudoku.getSize() * sudoku.getSize(); j++) {
|
||||
if (sudoku.getCell(i).getSymbolIndex() != otherSudoku.getCell(i).getSymbolIndex())
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public MultiDoku clone() {
|
||||
// TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||
|
||||
@@ -58,18 +58,20 @@ public class SudokuSerializerTest {
|
||||
void testSerialize() {
|
||||
Random r = new Random();
|
||||
int testCount = 20;
|
||||
int minSize = 2;
|
||||
int maxSize = 3;
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int blockWidth = r.nextInt(10) + 1;
|
||||
int blockHeight = r.nextInt(10) + 1;
|
||||
int blockWidth = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||
int blockHeight = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||
testSerializeWithSize(blockWidth, blockHeight);
|
||||
}
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int blockWidth = r.nextInt(10) + 1;
|
||||
int blockHeight = r.nextInt(10) + 1;
|
||||
int blockWidth = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||
int blockHeight = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||
testSaveWithSize(blockWidth, blockHeight);
|
||||
}
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int size = r.nextInt(10) + 1;
|
||||
int size = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||
testSerializeX(size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import gui.constants.Symbols;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.*;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Difficulty;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
class SolverTest {
|
||||
private int ns = Cell.NOSYMBOL;
|
||||
@@ -19,13 +24,13 @@ class SolverTest {
|
||||
private static MixedSolver m;
|
||||
|
||||
@BeforeAll
|
||||
public static void initializeSolvers(){
|
||||
public static void initializeSolvers() {
|
||||
h = new HumanSolver();
|
||||
r = new RandomSolver();
|
||||
m = new MixedSolver();
|
||||
}
|
||||
|
||||
private void testSize2(Solver solver){
|
||||
private void testSize2(Solver solver) {
|
||||
MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
Sudoku test = mdTest.getSubGrid(0);
|
||||
@@ -35,25 +40,23 @@ class SolverTest {
|
||||
0, ns, ns, ns,
|
||||
ns, ns, ns, 3,
|
||||
ns, 0, 1, ns);
|
||||
assert (test.setImmutableCellsSymbol(immutableCells));
|
||||
assertTrue(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());
|
||||
|
||||
1, 2, 3, 0,
|
||||
0, 3, 2, 1,
|
||||
2, 1, 0, 3,
|
||||
3, 0, 1, 2);
|
||||
assertTrue(result.setCellsSymbol(correctCells));
|
||||
assertTrue(result.isSolved());
|
||||
|
||||
assertNotEquals(mdResult, mdTest);
|
||||
solver.solve(mdTest);
|
||||
assert (mdTest.isSolved());
|
||||
for (Cell cell : test.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||
SudokuSerializer.serializeSudoku(mdResult).toString());
|
||||
assertTrue(mdTest.isSolved());
|
||||
|
||||
assertEquals(mdTest, mdResult);
|
||||
}
|
||||
|
||||
private void testSize3(Solver solver){
|
||||
private void testSize3(Solver solver) {
|
||||
MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
Sudoku test = mdTest.getSubGrid(0);
|
||||
@@ -79,41 +82,38 @@ class SolverTest {
|
||||
3, 1, 6, 0, 2, 8, 4, 7, 5,
|
||||
8, 0, 4, 7, 6, 5, 2, 1, 3,
|
||||
5, 7, 2, 4, 3, 1, 0, 6, 8);
|
||||
assert(result.setCellsSymbol(correctCells));
|
||||
assert(result.isSolved());
|
||||
assert (result.setCellsSymbol(correctCells));
|
||||
assert (result.isSolved());
|
||||
|
||||
assertNotEquals(mdResult, mdTest);
|
||||
solver.solve(mdTest);
|
||||
assert (mdTest.isSolved());
|
||||
for (Cell cell : test.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||
SudokuSerializer.serializeSudoku(mdResult).toString());
|
||||
assertEquals(mdTest, mdResult);
|
||||
}
|
||||
|
||||
private void testMDSize3(Solver solver){
|
||||
private void testMDSize3(Solver solver) {
|
||||
MultiDoku mdTest = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
try {
|
||||
SudokuFactory.fillDoku(mdTest, Difficulty.Easy);
|
||||
} catch (Exception e) {
|
||||
assert(false);
|
||||
assert (false);
|
||||
}
|
||||
MultiDoku result = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||
assert(result.isSolved());
|
||||
solver.solve(mdTest);
|
||||
assert (mdTest.isSolved());
|
||||
for (Cell cell : mdTest.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||
SudokuSerializer.serializeSudoku(result).toString());
|
||||
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||
assertFalse(mdTest.isSolved());
|
||||
assertFalse(mdResult.isSolved());
|
||||
assertTrue(solver.solve(mdTest));
|
||||
assertTrue(mdTest.isSolved());
|
||||
assertFalse(mdResult.isSolved());
|
||||
assertNotEquals(mdTest, mdResult);
|
||||
solver.solve(mdResult);
|
||||
assertEquals(mdTest, mdResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void solveTest() {
|
||||
initializeSolvers();
|
||||
testSize2(h);
|
||||
testSize3(h);
|
||||
testMDSize3(h);
|
||||
testSize2(m);
|
||||
testSize3(m);
|
||||
testMDSize3(m);
|
||||
|
||||
Reference in New Issue
Block a user