redo
This commit is contained in:
@@ -7,6 +7,7 @@ import anno.LemmaGen;
|
||||
import lombok.val;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import precomp.Neighbors9x8;
|
||||
import precomp.Neighbors9x8.rci;
|
||||
import puzzle.DictJavaGeneratorMulti.DictEntryDTO.IntListDTO;
|
||||
import puzzle.Export.Signa;
|
||||
@@ -37,6 +38,9 @@ import static puzzle.LemmaData.INER;
|
||||
import static puzzle.LemmaData.INEREN;
|
||||
import static puzzle.LemmaData.INERENA;
|
||||
import static puzzle.LemmaData.INERENAE;
|
||||
import static puzzle.LemmaData.WORD_A;
|
||||
import static puzzle.LemmaData.WORD_C;
|
||||
import static puzzle.LemmaData.WORD_X;
|
||||
import static puzzle.SwedishGenerator.*;
|
||||
@GenerateNeighbors(@GenerateNeighbor(C = 4, R = 3, packageName = "precomp", className = "Neighbors4x3", MIN_LEN = 2))
|
||||
@LemmaGen(
|
||||
@@ -52,10 +56,7 @@ import static puzzle.SwedishGenerator.*;
|
||||
)
|
||||
public class SwedishGeneratorTest {
|
||||
|
||||
public static final long WORD_A = Lemma.from("A");
|
||||
public static final long WORD_C = Lemma.from("C");
|
||||
public static final long WORD_X = Lemma.from("X");
|
||||
static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); }
|
||||
static Grid createEmpty9x8() { return new Grid(new byte[Neighbors9x8.SIZE], X, X); }
|
||||
record Context(long[] bitset) {
|
||||
|
||||
public Context() { this(new long[2500]); }
|
||||
@@ -86,26 +87,10 @@ public class SwedishGeneratorTest {
|
||||
INERENA,
|
||||
INERENAE };
|
||||
|
||||
static final byte LETTER_A = ((byte) 'A') & 31;
|
||||
static final byte LETTER_P = ((byte) 'P') & 31;
|
||||
static final byte LETTER_L = ((byte) 'L') & 31;
|
||||
static final byte LETTER_B = ((byte) 'B') & 31;
|
||||
static final byte LETTER_C = ((byte) 'C') & 31;
|
||||
static final byte LETTER_E = ((byte) 'E') & 31;
|
||||
static final byte LETTER_I = ((byte) 'I') & 31;
|
||||
static final byte LETTER_N = ((byte) 'N') & 31;
|
||||
static final byte LETTER_X = ((byte) 'X') & 31;
|
||||
static final byte LETTER_R = ((byte) 'R') & 31;
|
||||
static final byte LETTER_Z = ((byte) 'Z') & 31;
|
||||
static final byte CLUE_DOWN = 0;
|
||||
static final byte CLUE_RIGHT = 1;
|
||||
static final byte CLUE_UP = 2;
|
||||
static final byte CLUE_LEFT = 3;
|
||||
|
||||
@Test
|
||||
void testPatternForSlotAllLetters() {
|
||||
var grid = new Puzzle(Signa.of(r0c0d1));
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3), 0L, ABC);
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c1.or(r0c2).or(r0c3).lo(), X, ABC);
|
||||
val map = grid.collect(Collectors.toMap(Lettrix::index, Lettrix::letter));
|
||||
assertEquals(LETTER_A, map.get(OFF_0_1));
|
||||
assertEquals(LETTER_B, map.get(OFF_0_2));
|
||||
@@ -114,60 +99,32 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testPatternForSlotMixed() {
|
||||
var grid = createEmpty();
|
||||
GridBuilder.placeWord(grid, grid.g, r0c0d1.slotKey, 1L << OFF_0_0, 0, WORD_A);
|
||||
GridBuilder.placeWord(grid, grid.g, r0c0d1.slotKey, 1L << OFF_2_0, 0, WORD_C);
|
||||
var key = Slot.packSlotKey(OFF_1_0, CLUE_RIGHT);
|
||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, key, 7L, 0L);
|
||||
var grid = createEmpty9x8();
|
||||
GridBuilder.placeWord(grid, grid.g, r0c0d1.slotKey, r0c0.mask, X, WORD_A);
|
||||
GridBuilder.placeWord(grid, grid.g, r0c0d1.slotKey, r2c0.mask, X, WORD_C);
|
||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, 7L, X);
|
||||
assertEquals(14081L, pattern);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPatternForSlotAllDashes() {
|
||||
var grid = createEmpty();
|
||||
var key = Slot.packSlotKey(1 << Slot.BIT_FOR_DIR, CLUE_RIGHT);
|
||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, key, 7L, 0L);
|
||||
assertEquals(0L, pattern);
|
||||
var grid = createEmpty9x8();
|
||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, 7L, X);
|
||||
assertEquals(X, pattern);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPatternForSlotSingleLetter() {
|
||||
var grid = createEmpty();
|
||||
//Slot.packSlotKey(0, CLUE_RIGHT)
|
||||
GridBuilder.placeWord(grid, grid.g, r0c0d1.slotKey, 1L << OFF_0_0, 0, WORD_A);
|
||||
var key = Slot.packSlotKey(1, CLUE_RIGHT);
|
||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, key, 7L, 0L);
|
||||
var grid = createEmpty9x8();
|
||||
GridBuilder.placeWord(grid, grid.g, r0c0d1.slotKey, r0c0.mask, X, WORD_A);
|
||||
var pattern = patternForSlot(grid.lo, grid.hi, grid.g, 7L, X);
|
||||
assertEquals(1L, pattern);
|
||||
}
|
||||
@Test
|
||||
void testRng() {
|
||||
var rng = new Rng(123);
|
||||
var val1 = rng.nextU32();
|
||||
var val2 = rng.nextU32();
|
||||
assertNotEquals(val1, val2);
|
||||
|
||||
var rng2 = new Rng(123);
|
||||
assertEquals(val1, rng2.nextU32());
|
||||
|
||||
for (var i = 0; i < 100; i++) {
|
||||
var r = rng.randomClueDir();
|
||||
assertTrue(r >= 0 && r <= 5);
|
||||
var f = rng.nextFloat();
|
||||
assertTrue(f >= 0.0 && f <= 1.0);
|
||||
}
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGrid() {
|
||||
var grid = new Puzzle(Clues.createEmpty());
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, 1L << OFF_0_0, 0, WORD_A);
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c0d0.mask, X, WORD_A);
|
||||
val arr = grid.collect(Collectors.toMap(Lettrix::index, Lettrix::letter));
|
||||
assertEquals(1, arr.size());
|
||||
assertEquals(LETTER_A, arr.get(OFF_0_0));
|
||||
@@ -210,25 +167,10 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testSlot() {
|
||||
System.out.println("[DEBUG_LOG] Slot.BIT_FOR_DIR = " + Slot.BIT_FOR_DIR);
|
||||
// key = (r << 8) | (c << 4) | d
|
||||
var offset = OFF_2_3;
|
||||
System.out.println("[DEBUG_LOG] Grid.offset(2, 3) = " + offset);
|
||||
var key = Slot.packSlotKey(offset, CLUE_DOWN);
|
||||
System.out.println("[DEBUG_LOG] key = " + key);
|
||||
long lo = 0;
|
||||
// pos 0: (2, 5)
|
||||
lo |= 1L << OFF_2_5;
|
||||
// pos 1: (3, 5)
|
||||
lo |= 1L << OFF_3_5;
|
||||
// pos 2: (4, 5)
|
||||
lo |= 1L << OFF_4_5;
|
||||
|
||||
System.out.println("[DEBUG_LOG] s.dir() = " + Slot.dir(key));
|
||||
assertEquals(OFF_2_3, Slot.clueIndex(key));
|
||||
assertEquals(CLUE_DOWN, Slot.dir(key));
|
||||
assertFalse(Slot.horiz(key));
|
||||
var cells = Puzzle.cellWalk((byte) key, lo, 0L).mapToObj(i -> Masker.IT[i]).toArray(rci[]::new);
|
||||
assertEquals(OFF_2_3, Slot.clueIndex(r2c3d0.slotKey));
|
||||
assertEquals(CLUE_DOWN0, Slot.dir(r2c3d0.slotKey));
|
||||
assertFalse(Slot.horiz(r2c3d0.slotKey));
|
||||
var cells = Puzzle.cellWalk((byte) r2c3d0.slotKey, r2c5.or(r3c5).or(r4c5).lo(), 0L).mapToObj(i -> Masker.IT[i]).toArray(rci[]::new);
|
||||
assertEquals(2, cells[0].r());
|
||||
assertEquals(3, cells[1].r());
|
||||
assertEquals(4, cells[2].r());
|
||||
@@ -236,8 +178,8 @@ public class SwedishGeneratorTest {
|
||||
assertEquals(5, cells[1].c());
|
||||
assertEquals(5, cells[2].c());
|
||||
|
||||
assertTrue(Slot.horiz(CLUE_RIGHT)); // right
|
||||
assertFalse(Slot.horiz(CLUE_DOWN)); // down
|
||||
assertTrue(Slot.horiz(CLUE_RIGHT1)); // right
|
||||
assertFalse(Slot.horiz(CLUE_DOWN0)); // down
|
||||
}
|
||||
|
||||
static long packPattern(String s) {
|
||||
@@ -267,13 +209,13 @@ public class SwedishGeneratorTest {
|
||||
void testForEachSlotAndExtractSlots() {
|
||||
// This should detect a slot starting at 0,1 with length 2 (0,1 and 0,2)
|
||||
var dict = DictJavaGeneratorMulti.Dicts.makeDict(WORDS2);
|
||||
var slots = Masker.extractSlots(Signa.of(r0c0d1).c(), dict.index());
|
||||
var slots = Masker.extractSlots(Signa.of(r0c0d1).c(), dict.index(), dict.reversed());
|
||||
assertEquals(1, slots.length);
|
||||
var s = slots[0];
|
||||
|
||||
assertTrue(Slot.length(s.lo(), s.hi()) >= 2);
|
||||
assertEquals(OFF_0_0, Slot.clueIndex(s.key()));
|
||||
assertEquals(CLUE_RIGHT, Slot.dir(s.key()));
|
||||
assertEquals(CLUE_RIGHT1, Slot.dir(s.key()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -292,33 +234,20 @@ public class SwedishGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testGeneticAlgorithmComponents() {
|
||||
var rng = new Rng(42);
|
||||
var gen = new Masker(rng, new int[Masker.STACK_SIZE], Masker.Clues.createEmpty());
|
||||
|
||||
var c1 = new Signa(gen.randomMask(18));
|
||||
var gen = new Masker(new Rng(42), new int[Masker.STACK_SIZE], Masker.Clues.createEmpty());
|
||||
var c1 = new Signa(gen.randomMask(18));
|
||||
assertNotNull(c1);
|
||||
|
||||
var g2 = new Signa(gen.mutate(c1.deepCopyGrid().c()));
|
||||
assertNotNull(g2);
|
||||
assertNotSame(c1.c(), g2.c());
|
||||
|
||||
assertNotNull(gen.crossover(c1.c(), g2.c()));
|
||||
|
||||
var g4 = gen.hillclimb(c1.c(), 18, 10);
|
||||
assertNotNull(g4);
|
||||
assertNotNull(gen.hillclimb(c1.c(), 18, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPlaceWord() {
|
||||
var grid = new Puzzle(Clues.createEmpty());
|
||||
// Slot at OFF_0_0 length 3, horizontal (right)
|
||||
var key = Slot.packSlotKey(0, CLUE_RIGHT);
|
||||
var lo = (1L << OFF_0_0) | (1L << OFF_0_1) | (1L << OFF_0_2);
|
||||
val hi = 0L;
|
||||
var w1 = ABC;
|
||||
|
||||
// 1. Successful placement in empty grid
|
||||
assertTrue(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
|
||||
assertTrue(GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c0.or(r0c1).or(r0c2).lo(), X, ABC));
|
||||
|
||||
var map = grid.collect(Collectors.toMap(Lettrix::index, Lettrix::letter));
|
||||
assertEquals(3, map.size());
|
||||
@@ -327,9 +256,9 @@ public class SwedishGeneratorTest {
|
||||
assertEquals(LETTER_C, map.get(OFF_0_2));
|
||||
|
||||
// 2. Successful placement with partial overlap (same characters)
|
||||
assertTrue(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
|
||||
assertTrue(GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c0.or(r0c1).or(r0c2).lo(), X, ABC));
|
||||
// 3. Conflict: place "ABD" where "ABC" is
|
||||
assertFalse(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, ABD));
|
||||
assertFalse(GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c0.or(r0c1).or(r0c2).lo(), X, ABD));
|
||||
// Verify grid is unchanged (still "ABC")
|
||||
map = grid.collect(Collectors.toMap(Lettrix::index, Lettrix::letter));
|
||||
assertEquals(3, map.size());
|
||||
@@ -339,8 +268,8 @@ public class SwedishGeneratorTest {
|
||||
|
||||
// 4. Partial placement then conflict (rollback)
|
||||
grid = new Puzzle(Clues.createEmpty());
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_0_2, 0, WORD_X); // Conflict at the end
|
||||
assertFalse(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
|
||||
GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, 1L << OFF_0_2, 0, WORD_X); // Conflict at the end
|
||||
assertFalse(GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c0.or(r0c1).or(r0c2).lo(), X, ABC));
|
||||
map = grid.stream().collect(Collectors.toMap(Lettrix::index, Lettrix::letter));
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(LETTER_X, map.get(OFF_0_2));
|
||||
@@ -350,12 +279,9 @@ public class SwedishGeneratorTest {
|
||||
void testBacktrackingHelpers() {
|
||||
var grid = new Puzzle(Clues.createEmpty());
|
||||
// Slot at 0,1 length 2
|
||||
var key = Slot.packSlotKey(0, CLUE_RIGHT);
|
||||
var lo = (1L << OFF_0_1) | (1L << OFF_0_2);
|
||||
var w = AZ;
|
||||
val low = grid.grid().lo;
|
||||
val top = grid.grid().hi;
|
||||
var placed = GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, 0L, w);
|
||||
var placed = GridBuilder.placeWord(grid.grid(), grid.grid().g, r0c0d1.slotKey, r0c1.or(r0c2).lo(), X, AZ);
|
||||
assertTrue(placed);
|
||||
|
||||
var map = grid.collect(Collectors.toMap(Lettrix::index, Lettrix::letter));
|
||||
@@ -374,16 +300,16 @@ public class SwedishGeneratorTest {
|
||||
@Test
|
||||
void testInnerWorkings() {
|
||||
// 1. Test Slot.increasing
|
||||
assertFalse(Slotinfo.increasing(CLUE_LEFT)); // Left
|
||||
assertTrue(Slotinfo.increasing(CLUE_RIGHT)); // Right
|
||||
assertTrue(Slotinfo.increasing(CLUE_DOWN)); // Down
|
||||
assertFalse(Slotinfo.increasing(CLUE_UP)); // Up
|
||||
assertFalse(Slotinfo.increasing(CLUE_LEFT3)); // Left
|
||||
assertTrue(Slotinfo.increasing(CLUE_RIGHT1)); // Right
|
||||
assertTrue(Slotinfo.increasing(CLUE_DOWN0)); // Down
|
||||
assertFalse(Slotinfo.increasing(CLUE_UP2)); // Up
|
||||
|
||||
assertTrue(Slotinfo.increasing(Slot.packSlotKey(0, CLUE_RIGHT)));
|
||||
assertFalse(Slotinfo.increasing(Slot.packSlotKey(0, CLUE_LEFT)));
|
||||
assertTrue(Slotinfo.increasing(r0c0d1.slotKey));
|
||||
assertFalse(Slotinfo.increasing(r0c0d3.slotKey));
|
||||
|
||||
// 2. Test slotScore
|
||||
val counts = new byte[SIZE];
|
||||
val counts = new byte[Neighbors9x8.SIZE];
|
||||
counts[1] = 2;
|
||||
counts[2] = 3;
|
||||
var dict = DictJavaGeneratorMulti.Dicts.makeDict(WORDS);
|
||||
@@ -415,4 +341,29 @@ public class SwedishGeneratorTest {
|
||||
var fitOne = gen.maskFitness(grid, 18);
|
||||
assertTrue(fitOne < fitEmpty);
|
||||
}
|
||||
@Test
|
||||
void testRng() {
|
||||
var rng = new Rng(123);
|
||||
var val1 = rng.nextU32();
|
||||
var val2 = rng.nextU32();
|
||||
assertNotEquals(val1, val2);
|
||||
|
||||
var rng2 = new Rng(123);
|
||||
assertEquals(val1, rng2.nextU32());
|
||||
|
||||
for (var i = 0; i < 100; i++) {
|
||||
var r = rng.randomClueDir();
|
||||
assertTrue(r >= 0 && r <= 5);
|
||||
var f = rng.nextFloat();
|
||||
assertTrue(f >= 0.0 && f <= 1.0);
|
||||
}
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user