introduce bitloops
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package puzzle;
|
||||
|
||||
import lombok.val;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import puzzle.Export.Gridded;
|
||||
import puzzle.Export.Placed;
|
||||
@@ -27,11 +28,15 @@ public class ExportFormatTest {
|
||||
@Test
|
||||
void testExportFormatFromFilled() {
|
||||
var swe = new SwedishGenerator(new Rng(0), new int[STACK_SIZE]);
|
||||
var grid = Grid.createEmpty();
|
||||
|
||||
val clues = Clues.createEmpty();
|
||||
// Place a RIGHT clue at (0,0)
|
||||
grid.setClue(0, CLUE_RIGHT);
|
||||
clues.setClue(0, CLUE_RIGHT);
|
||||
// This creates a slot starting at (0,1)
|
||||
// Terminate the slot at (0,5) with another digit to avoid it extending to MAX_WORD_LENGTH
|
||||
clues.setClue(Grid.offset(0, 5), CLUE_LEFT);
|
||||
var grid = clues.toGrid();
|
||||
|
||||
|
||||
var clueMap = new long[300];
|
||||
// key = (cellIndex << 2) | (direction)
|
||||
@@ -43,8 +48,7 @@ public class ExportFormatTest {
|
||||
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_LEFT);
|
||||
|
||||
|
||||
var fillResult = new FillResult(true, new Gridded(grid), clueMap, new FillStats(0, 0, 0, 0));
|
||||
var puzzleResult = new PuzzleResult(swe, null, null, fillResult);
|
||||
|
||||
@@ -33,11 +33,10 @@ public class MainTest {
|
||||
|
||||
@Test
|
||||
void testExtractSlots() {
|
||||
var grid = Grid.createEmpty();
|
||||
|
||||
// Set up digits on the grid to create slots.
|
||||
// CLUE_RIGHT at OFF_0_0 -> slot at (0,1), (0,2)
|
||||
grid.setClue(OFF_0_0, CLUE_RIGHT);
|
||||
var clues = Clues.createEmpty();
|
||||
clues.setClue(OFF_0_0, CLUE_RIGHT);
|
||||
var grid = clues.toGrid();
|
||||
grid.setLetter(OFF_0_1, LETTER_A);
|
||||
grid.setLetter(OFF_0_2, LETTER_B);
|
||||
|
||||
@@ -63,8 +62,9 @@ public class MainTest {
|
||||
|
||||
@Test
|
||||
void testForEachSlot() {
|
||||
var grid = Grid.createEmpty();
|
||||
grid.setClue(OFF_0_0, CLUE_RIGHT); // right
|
||||
var clues = Clues.createEmpty();
|
||||
clues.setClue(OFF_0_0, CLUE_RIGHT);
|
||||
var grid = clues.toGrid();
|
||||
|
||||
var count = new AtomicInteger(0);
|
||||
grid.forEachSlot((key, lo, hi) -> {
|
||||
@@ -84,11 +84,12 @@ public class MainTest {
|
||||
}
|
||||
@Test
|
||||
public void testGridBasics() {
|
||||
var grid = Grid.createEmpty();
|
||||
var clues = Clues.createEmpty();
|
||||
clues.setClue(OFF_1_2, CLUE_UP);
|
||||
var grid = clues.toGrid();
|
||||
|
||||
// Test set/get
|
||||
grid.setLetter(OFF_0_0, LETTER_A);
|
||||
grid.setClue(OFF_1_2, CLUE_UP);
|
||||
grid.setLetter(OFF_2_3, LETTER_Z);
|
||||
|
||||
Assertions.assertEquals(LETTER_A, grid.byteAt(OFF_0_0));
|
||||
@@ -114,28 +115,27 @@ public class MainTest {
|
||||
Assertions.assertTrue(grid.isClue(OFF_1_2)); // digit
|
||||
Assertions.assertTrue(grid.notClue(OFF_1_1)); // '#' is lettercell
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGridDeepCopy() {
|
||||
var grid = Grid.createEmpty();
|
||||
grid.setLetter(Grid.offset(0, 0), (byte) 'A');
|
||||
grid.setLetter(Grid.offset(0, 1), (byte) 'B');
|
||||
grid.setLetter(Grid.offset(1, 0), (byte) 'C');
|
||||
grid.setLetter(Grid.offset(1, 1), (byte) 'D');
|
||||
public void testCluesDeepCopy() {
|
||||
var grid = Clues.createEmpty();
|
||||
grid.setClue(Grid.offset(0, 0), (byte) 1);
|
||||
grid.setClue(Grid.offset(0, 1), (byte) 2);
|
||||
grid.setClue(Grid.offset(1, 0), (byte) 3);
|
||||
grid.setClue(Grid.offset(1, 1), (byte) 0);
|
||||
|
||||
var copy = grid.deepCopyGrid();
|
||||
Assertions.assertEquals((byte) 'A', copy.byteAt(0));
|
||||
Assertions.assertEquals((byte) 1, copy.digitAt(0));
|
||||
|
||||
copy.setLetter(0, (byte) 'X');
|
||||
Assertions.assertEquals((byte) 'X', copy.byteAt(0));
|
||||
Assertions.assertEquals((byte) 'A', grid.byteAt(0)); // Original should be unchanged
|
||||
copy.setClue(0, (byte) 3);
|
||||
Assertions.assertEquals((byte) 3, copy.digitAt(0));
|
||||
Assertions.assertEquals((byte) 1, grid.digitAt(0)); // Original should be unchanged
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMini() {
|
||||
var grid = Grid.createEmpty();
|
||||
val idx = OFF_1_1;
|
||||
grid.setClue(idx, CLUE_LEFT);
|
||||
var clues = Clues.createEmpty();
|
||||
clues.setClue(idx, CLUE_LEFT);
|
||||
var grid = clues.toGrid();
|
||||
Assertions.assertTrue(grid.isClue(idx));
|
||||
}
|
||||
@Test
|
||||
@@ -179,8 +179,8 @@ public class MainTest {
|
||||
Assertions.assertEquals(12348, foundSeed, "Found seed changed");
|
||||
Assertions.assertEquals(18, res.filled().wordCount(), "Number of assigned words changed");
|
||||
Assertions.assertEquals("SLEDE", Lemma.asWord(res.filled().clueMap()[282]));
|
||||
Assertions.assertEquals(74732156493031040L, res.filled().grid().grid().lo);
|
||||
Assertions.assertEquals(193L, res.filled().grid().grid().hi);
|
||||
Assertions.assertEquals(74732156493031040L, res.filled().grid().grid().lo());
|
||||
Assertions.assertEquals(193L, res.filled().grid().grid().hi());
|
||||
}
|
||||
boolean isLetter(byte b) { return (b & 64) != 0; }
|
||||
@Test
|
||||
|
||||
@@ -91,21 +91,6 @@ public class SwedishGeneratorTest {
|
||||
void testGrid() {
|
||||
var grid = Grid.createEmpty();
|
||||
grid.setLetter(OFF_0_0, LETTER_A);
|
||||
grid.setClue(OFF_0_1, CLUE_LEFT);
|
||||
|
||||
assertEquals('A', grid.byteAt(OFF_0_0));
|
||||
assertEquals(CLUE_LEFT, grid.digitAt(OFF_0_1));
|
||||
assertTrue(grid.notClue(OFF_0_0));
|
||||
assertFalse(grid.isClue(OFF_0_0));
|
||||
assertTrue(grid.isClue(OFF_0_1));
|
||||
assertFalse(grid.notClue(OFF_0_1));
|
||||
assertTrue(grid.notClue(OFF_0_0));
|
||||
assertFalse(grid.notClue(OFF_0_1));
|
||||
|
||||
var copy = grid.deepCopyGrid();
|
||||
assertEquals('A', copy.byteAt(OFF_0_0));
|
||||
copy.setLetter(OFF_0_0, LETTER_B);
|
||||
assertEquals('B', copy.byteAt(OFF_0_0));
|
||||
assertEquals('A', grid.byteAt(OFF_0_0));
|
||||
}
|
||||
|
||||
@@ -267,11 +252,11 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testForEachSlotAndExtractSlots() {
|
||||
var grid = Grid.createEmpty();
|
||||
// 3x3 grid (Config.PUZZLE_ROWS/COLS are 3 in test env)
|
||||
// Set CLUE_RIGHT at OFF_0_0
|
||||
grid.setClue(OFF_0_0, CLUE_RIGHT);
|
||||
// This should detect a slot starting at 0,1 with length 2 (0,1 and 0,2)
|
||||
var clues = Clues.createEmpty();
|
||||
clues.setClue(OFF_0_0, CLUE_RIGHT);
|
||||
var grid = clues.toGrid();
|
||||
|
||||
|
||||
var slots = extractSlots(grid);
|
||||
assertEquals(1, slots.length);
|
||||
@@ -430,20 +415,8 @@ public class SwedishGeneratorTest {
|
||||
// Empty grid: huge penalty
|
||||
var fitEmpty = gen.maskFitness(grid);
|
||||
assertTrue(fitEmpty >= 1_000_000_000L);
|
||||
|
||||
// Grid with one short slot: still high penalty but less than empty
|
||||
grid.setClue(0, D_BYTE_2); // Right from 0,0. Len 2 if 3x3.
|
||||
var fitOne = gen.maskFitness(grid);
|
||||
assertTrue(fitOne < fitEmpty);
|
||||
|
||||
// Test penalty for TARGET_CLUES
|
||||
// TARGET_CLUES = SIZE >>> 2. For 3x3 it is 9 >>> 2 = 2.
|
||||
// If we have 1 clue, |1 - 2| * 16000 = 16000 penalty.
|
||||
// If we add another clue at a distant position.
|
||||
var grid2 = Grid.createEmpty();
|
||||
grid2.setClue(0, D_BYTE_2);
|
||||
grid2.setClue(Grid.offset(2, 0), D_BYTE_2);
|
||||
// Now clueCount = 2, penalty should be 0 from target clues.
|
||||
// But they might have other penalties (short slots, etc.)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user