This commit is contained in:
@@ -18,7 +18,8 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// 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'
|
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() {
|
public MultiDoku clone() {
|
||||||
// TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
// TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
||||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||||
|
|||||||
@@ -58,18 +58,20 @@ public class SudokuSerializerTest {
|
|||||||
void testSerialize() {
|
void testSerialize() {
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
int testCount = 20;
|
int testCount = 20;
|
||||||
|
int minSize = 2;
|
||||||
|
int maxSize = 3;
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int blockWidth = r.nextInt(10) + 1;
|
int blockWidth = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||||
int blockHeight = r.nextInt(10) + 1;
|
int blockHeight = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||||
testSerializeWithSize(blockWidth, blockHeight);
|
testSerializeWithSize(blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int blockWidth = r.nextInt(10) + 1;
|
int blockWidth = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||||
int blockHeight = r.nextInt(10) + 1;
|
int blockHeight = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||||
testSaveWithSize(blockWidth, blockHeight);
|
testSaveWithSize(blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int size = r.nextInt(10) + 1;
|
int size = r.nextInt(maxSize - minSize + 1) + minSize;
|
||||||
testSerializeX(size);
|
testSerializeX(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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 java.util.List;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import gui.constants.Symbols;
|
|
||||||
import sudoku.io.SudokuPrinter;
|
|
||||||
import sudoku.io.SudokuSerializer;
|
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 {
|
class SolverTest {
|
||||||
private int ns = Cell.NOSYMBOL;
|
private int ns = Cell.NOSYMBOL;
|
||||||
@@ -35,22 +40,20 @@ class SolverTest {
|
|||||||
0, ns, ns, ns,
|
0, ns, ns, ns,
|
||||||
ns, ns, ns, 3,
|
ns, ns, ns, 3,
|
||||||
ns, 0, 1, ns);
|
ns, 0, 1, ns);
|
||||||
assert (test.setImmutableCellsSymbol(immutableCells));
|
assertTrue(test.setImmutableCellsSymbol(immutableCells));
|
||||||
List<Integer> correctCells = List.of(
|
List<Integer> correctCells = List.of(
|
||||||
1, 2, 3, 0,
|
1, 2, 3, 0,
|
||||||
0, 3, 2, 1,
|
0, 3, 2, 1,
|
||||||
2, 1, 0, 3,
|
2, 1, 0, 3,
|
||||||
3, 0, 1, 2);
|
3, 0, 1, 2);
|
||||||
assert(result.setCellsSymbol(correctCells));
|
assertTrue(result.setCellsSymbol(correctCells));
|
||||||
assert(result.isSolved());
|
assertTrue(result.isSolved());
|
||||||
|
|
||||||
|
assertNotEquals(mdResult, mdTest);
|
||||||
solver.solve(mdTest);
|
solver.solve(mdTest);
|
||||||
assert (mdTest.isSolved());
|
assertTrue(mdTest.isSolved());
|
||||||
for (Cell cell : test.getCells()) {
|
|
||||||
cell.setImmutable();
|
assertEquals(mdTest, mdResult);
|
||||||
}
|
|
||||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
|
||||||
SudokuSerializer.serializeSudoku(mdResult).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testSize3(Solver solver) {
|
private void testSize3(Solver solver) {
|
||||||
@@ -82,13 +85,10 @@ class SolverTest {
|
|||||||
assert (result.setCellsSymbol(correctCells));
|
assert (result.setCellsSymbol(correctCells));
|
||||||
assert (result.isSolved());
|
assert (result.isSolved());
|
||||||
|
|
||||||
|
assertNotEquals(mdResult, mdTest);
|
||||||
solver.solve(mdTest);
|
solver.solve(mdTest);
|
||||||
assert (mdTest.isSolved());
|
assert (mdTest.isSolved());
|
||||||
for (Cell cell : test.getCells()) {
|
assertEquals(mdTest, mdResult);
|
||||||
cell.setImmutable();
|
|
||||||
}
|
|
||||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
|
||||||
SudokuSerializer.serializeSudoku(mdResult).toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testMDSize3(Solver solver) {
|
private void testMDSize3(Solver solver) {
|
||||||
@@ -98,22 +98,22 @@ class SolverTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assert (false);
|
assert (false);
|
||||||
}
|
}
|
||||||
MultiDoku result = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||||
assert(result.isSolved());
|
assertFalse(mdTest.isSolved());
|
||||||
solver.solve(mdTest);
|
assertFalse(mdResult.isSolved());
|
||||||
assert (mdTest.isSolved());
|
assertTrue(solver.solve(mdTest));
|
||||||
for (Cell cell : mdTest.getCells()) {
|
assertTrue(mdTest.isSolved());
|
||||||
cell.setImmutable();
|
assertFalse(mdResult.isSolved());
|
||||||
}
|
assertNotEquals(mdTest, mdResult);
|
||||||
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
solver.solve(mdResult);
|
||||||
SudokuSerializer.serializeSudoku(result).toString());
|
assertEquals(mdTest, mdResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void solveTest() {
|
void solveTest() {
|
||||||
|
initializeSolvers();
|
||||||
testSize2(h);
|
testSize2(h);
|
||||||
testSize3(h);
|
testSize3(h);
|
||||||
testMDSize3(h);
|
|
||||||
testSize2(m);
|
testSize2(m);
|
||||||
testSize3(m);
|
testSize3(m);
|
||||||
testMDSize3(m);
|
testMDSize3(m);
|
||||||
|
|||||||
Reference in New Issue
Block a user