116 lines
4.1 KiB
Java
116 lines
4.1 KiB
Java
package puzzle;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import puzzle.Export.Gridded;
|
|
import puzzle.Export.Placed;
|
|
import puzzle.Export.Rewards;
|
|
import puzzle.Export.PuzzleResult;
|
|
import puzzle.SwedishGenerator.FillResult;
|
|
import puzzle.SwedishGenerator.Grid;
|
|
import puzzle.SwedishGenerator.Lemma;
|
|
import puzzle.SwedishGenerator.Rng;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Paths;
|
|
import java.util.HashMap;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static puzzle.SwedishGenerator.*;
|
|
|
|
public class ExportFormatTest {
|
|
|
|
static final byte CLUE_UP = 0;
|
|
static final byte CLUE_RIGHT = 1;
|
|
static final byte CLUE_DOWN = 2;
|
|
static final byte CLUE_LEFT = 3;
|
|
|
|
@Test
|
|
void testExportFormatFromFilled() {
|
|
var swe = new SwedishGenerator(new Rng(0));
|
|
var grid = Grid.createEmpty();
|
|
|
|
// Place a RIGHT clue at (0,0)
|
|
grid.setClue(0, CLUE_RIGHT);
|
|
// This creates a slot starting at (0,1)
|
|
|
|
var clueMap = new HashMap<Integer, Long>();
|
|
// key = (cellIndex << 3) | (direction + 1)
|
|
var key = (0 << 3) | (CLUE_RIGHT + 1);
|
|
clueMap.put(key, Lemma.from("TEST"));
|
|
|
|
// Manually fill the grid letters for "TEST" at (0,1), (0,2), (0,3), (0,4)
|
|
grid.setLetter(Grid.offset(0, 1), (byte) 'T');
|
|
grid.setLetter(Grid.offset(0, 2), (byte) 'E');
|
|
grid.setLetter(Grid.offset(0, 3), (byte) 'S');
|
|
grid.setLetter(Grid.offset(0, 4), (byte) 'T');
|
|
// Terminate the slot at (0,5) with another digit to avoid it extending to MAX_WORD_LENGTH
|
|
grid.setClue(Grid.offset(0, 5), CLUE_UP);
|
|
|
|
var fillResult = new FillResult(true, new Gridded(grid), clueMap, new FillStats());
|
|
var puzzleResult = new PuzzleResult(swe, null, null, fillResult);
|
|
|
|
var rewards = new Rewards(10, 5, 1);
|
|
var exported = puzzleResult.exportFormatFromFilled(2, rewards);
|
|
|
|
assertNotNull(exported);
|
|
assertEquals(2, exported.difficulty());
|
|
assertEquals(rewards, exported.rewards());
|
|
|
|
// Check words
|
|
assertEquals(1, exported.words().length);
|
|
var w = exported.words()[0];
|
|
assertEquals("TEST", w.word());
|
|
assertEquals(Placed.HORIZONTAL, w.direction());
|
|
|
|
// The bounding box should include (0,0) for the arrow and (0,1)-(0,4) for the word.
|
|
// minR=0, maxR=0, minC=0, maxC=4
|
|
// startRow = 0 - minR = 0
|
|
// startCol = 1 - minC = 1
|
|
assertEquals(0, w.startRow());
|
|
assertEquals(1, w.startCol());
|
|
assertEquals(0, w.arrowRow());
|
|
assertEquals(0, w.arrowCol());
|
|
|
|
// Check gridv2
|
|
// It should be 1 row, containing "2TEST" -> but letters are mapped, digits are not explicitly in letterAt.
|
|
// Wait, look at exportFormatFromFilled logic:
|
|
// row.append(letterAt.getOrDefault(pack(r, c), '#'));
|
|
// letterAt only contains letters from placed words.
|
|
// arrow cells are NOT in letterAt unless they are also part of a word (unlikely).
|
|
// So (0,0) should be '#'
|
|
assertEquals(1, exported.grid().length);
|
|
assertEquals("#TEST", exported.grid()[0]);
|
|
}
|
|
|
|
@Test
|
|
void testExportFormatEmpty() {
|
|
var swe = new SwedishGenerator(new Rng(0));
|
|
var grid = Grid.createEmpty();
|
|
var fillResult = new FillResult(true, new Gridded(grid), new HashMap<>(), new FillStats());
|
|
var puzzleResult = new PuzzleResult(swe, null, null, fillResult);
|
|
|
|
var exported = puzzleResult.exportFormatFromFilled(1, new Rewards(0, 0, 0));
|
|
|
|
assertNotNull(exported);
|
|
assertEquals(0, exported.words().length);
|
|
// Should return full grid with '#'
|
|
assertEquals(R, exported.grid().length);
|
|
for (var row : exported.grid()) {
|
|
assertEquals(C, row.length());
|
|
assertTrue(row.matches("#+"));
|
|
}
|
|
}
|
|
@Test
|
|
void testIndex() {
|
|
var csv = Paths.get("nl_score_hints_v3.csv");
|
|
var idx = Paths.get("nl_score_hints_v3.idx");
|
|
|
|
try (var svc = new CsvIndexService(csv, idx)) {
|
|
System.out.println(svc.getLine(1319));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
}
|