valid tests save and getFromSave sudoku
All checks were successful
Linux arm64 / Build (push) Successful in 42s

This commit is contained in:
Janet-Doe
2025-01-29 10:55:35 +01:00
parent b8553428ee
commit aa86e9b956
3 changed files with 284 additions and 10027 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,9 @@
package sudoku.io;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@@ -101,26 +104,68 @@ public class SudokuSerializer {
return jsonRoot;
}
public static void saveMultiDoku(final MultiDoku doku) {
/**
* Save a serialized MultiDoku in a JSON file.
* @param doku MultiDoku, MultiDoku to save.
* @return int, number of the save.
*/
public static int saveMultiDoku(final MultiDoku doku) {
JSONObject jsonRoot = serializeSudoku(doku);
File f = new File("save", "save.json");
InputStream is = null;
int i = 1;
int i = 0;
while (f.exists()) {
String newName = "save-" + i + ".json";
String newName = "save-" + ++i + ".json";
f = new File("save", newName);
i++;
}
try (FileWriter file = new FileWriter(f)) {
file.write(jsonRoot.toString(3));
//file.flush();
} catch (IOException e) {
e.fillInStackTrace();
}
return i;
}
/**
* Get a MultiDoku from a pre-existing json save file.
* @param numberSave int, number of the save file to open.
* @return MultiDoku, MultoDoku contained in the file.
* @throws Exception when the given save file does not exist.
*/
public static MultiDoku getSavedMultiDoku(int numberSave) throws Exception {
String fileName;
if (numberSave < 1) {
fileName = "save.json";
} else {
fileName = "save-" + numberSave + ".json";
}
File f = new File("save", fileName);
String fileContent;
if (!f.exists()) {
throw new Exception("This save does not exist.");
} else {
fileContent = new String(Files.readAllBytes(Paths.get("save/" + fileName)));
/*
try {
FileReader file = new FileReader(f);
char[] rawFileContent = {};
int length = 1000;
while (file.read(rawFileContent, 0, length) != -1) {
rawFileContent = new char[]{};
length = length * 10;
}
fileContent = new String(rawFileContent);
} catch (IOException e) {
throw new Exception("Error reading file.");
}
*/
return deserializeSudoku(fileContent);
}
}
public static MultiDoku deserializeSudoku(final String json) {

View File

@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
import java.util.Random;
@@ -17,20 +18,41 @@ public class SudokuSerializerTest {
void testSerializeWithSize(int blockWidth, int blockHeight) {
var sudoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
SudokuSerializer.saveMultiDoku(sudoku);
//MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
//assertEquals(data, SudokuSerializer.serializeSudoku(multiDoku));
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assert(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
}
void testSaveWithSize(int blockWidth, int blockHeight) {
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
int saveNumber = SudokuSerializer.saveMultiDoku(doku);
MultiDoku otherDoku = null;
try {
otherDoku = SudokuSerializer.getSavedMultiDoku(saveNumber);
assert (otherDoku != null);
} catch (Exception e) {
e.printStackTrace();
assert false;
}
assert(doku.equals(otherDoku));
}
@Test
void testSerialize() {
int testCount = 5;
Random r = new Random();
int testCount = 5;
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(20) + 1;
int blockHeight = r.nextInt(20) + 1;
testSerializeWithSize(blockWidth, blockHeight);
}
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(20) + 1;
int blockHeight = r.nextInt(20) + 1;
testSaveWithSize(blockWidth, blockHeight);
}
}
}