fix: serialize

This commit is contained in:
2025-01-30 00:51:22 +01:00
parent d4beaec8a8
commit 03f577828b
11 changed files with 208 additions and 404 deletions

View File

@@ -1,5 +1,8 @@
package sudoku;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.util.Random;
@@ -7,6 +10,7 @@ import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import sudoku.io.SudokuSerializer;
import sudoku.solver.Solver;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
@@ -15,19 +19,23 @@ public class SudokuSerializerTest {
void testSerializeWithSize(int blockWidth, int blockHeight) {
var sudoku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
Solver.solveRandom(sudoku, new Random());
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assert (data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
}
void testSaveWithSize(int blockWidth, int blockHeight) {
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
Solver.solveRandom(doku, new Random());
String savePath = SudokuSerializer.saveMultiDoku(doku);
MultiDoku otherDoku = null;
try {
otherDoku = SudokuFactory.fromfile(savePath);
assert (otherDoku != null);
assertEquals(SudokuSerializer.serializeSudoku(doku).toString(), SudokuSerializer.serializeSudoku(otherDoku).toString());
// clean file after test
File fileToDelete = new File(savePath);
fileToDelete.delete();
@@ -35,23 +43,35 @@ public class SudokuSerializerTest {
e.printStackTrace();
assert false;
}
assert (doku.equals(otherDoku));
}
void testSerializeX(int size) {
var sudoku = SudokuFactory.createBasicXShapedMultidoku(size, SudokuFactory.DEFAULT_CONSTRAINTS);
Solver.solveRandom(sudoku, new Random());
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
}
@Test
void testSerialize() {
Random r = new Random();
int testCount = 5;
int testCount = 20;
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(10) + 1;
int blockWidth = r.nextInt(4) + 1;
int blockHeight = r.nextInt(4) + 1;
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(4) + 1;
int blockHeight = r.nextInt(4) + 1;
testSaveWithSize(blockWidth, blockHeight);
}
for (int i = 0; i < testCount; i++) {
int size = r.nextInt(2) + 2;
testSerializeX(size);
}
}
}