Gather data

This commit is contained in:
mike
2026-01-08 00:37:54 +01:00
parent efe70fb121
commit 2680e70418
6 changed files with 105 additions and 133 deletions

View File

@@ -1,12 +1,18 @@
package puzzle;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import puzzle.SwedishGenerator.Dict;
import puzzle.SwedishGenerator.Grid;
import puzzle.SwedishGenerator.Lemma;
import puzzle.SwedishGenerator.PuzzleResult;
import puzzle.SwedishGenerator.Rng;
import puzzle.SwedishGenerator.Slot;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static puzzle.Main.indentLines;
import static puzzle.SwedishGenerator.C_DASH;
@@ -19,10 +25,68 @@ public class MainTest {
t.testMini();
t.testAttempt();
}
@Test
void testExtractSlots() {
var generator = new SwedishGenerator();
var grid = SwedishGenerator.makeEmptyGrid();
// Set up digits on the grid to create slots.
// '2' (right) at (0,0) -> slot at (0,1), (0,2)
grid.setCharAt(0, 0, '2');
grid.setCharAt(0, 1, 'A');
grid.setCharAt(0, 2, 'B');
var slots = generator.extractSlots(grid);
assertEquals(1, slots.size());
var s = slots.get(0);
assertEquals(8, s.len());
assertEquals(0, s.r(0));
assertEquals(1, s.c(0));
assertEquals(0, s.r(1));
assertEquals(2, s.c(1));
}
@Test
void testStaticSlotMethods() {
// Test static r and c extraction
// packedRs: 1 at index 0, 2 at index 1
var packedRs = (1L << 0) | (2L << 4);
assertEquals(1, Slot.r(packedRs, 0));
assertEquals(2, Slot.r(packedRs, 1));
// Test static horiz
// dir 2 (right) is horizontal
assertTrue(Slot.horiz(2));
// dir 3 (down) is vertical
assertFalse(Slot.horiz(3));
}
@Test
void testForEachSlot() {
var generator = new SwedishGenerator();
var grid = SwedishGenerator.makeEmptyGrid();
grid.setCharAt(0, 0, '2'); // right
var count = new AtomicInteger(0);
generator.forEachSlot(grid, (key, rs, cs, len) -> {
count.incrementAndGet();
assertEquals(8, len);
assertEquals(0, Slot.r(rs, 0));
assertEquals(1, Slot.c(cs, 0));
});
assertEquals(1, count.get());
}
@Test
public void testHoriz() {
assertTrue(new Slot(2, 0L, 0L, 1).horiz());
assertTrue(new Slot(4, 0L, 0L, 1).horiz());
assertFalse(new Slot(1, 0L, 3L, 1).horiz());
assertFalse(new Slot(3, 0L, 3L, 1).horiz());
assertFalse(new Slot(5, 0L, 3L, 1).horiz());
}
@Test
public void testGridBasics() {
var grid = new Grid(new byte[3 * 4], 4);
var grid = SwedishGenerator.makeEmptyGrid();
// Initialize with #
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 4; c++) {
@@ -61,7 +125,7 @@ public class MainTest {
@Test
public void testGridDeepCopy() {
var grid = new Grid(new byte[2 * 2], 2);
var grid = SwedishGenerator.makeEmptyGrid();
grid.setCharAt(0, 0, 'A');
grid.setCharAt(0, 1, 'B');
grid.setCharAt(1, 0, 'C');
@@ -77,11 +141,12 @@ public class MainTest {
@Test
public void testMini() {
var grid = new Grid(new byte[3 * 3], 3);
var grid = SwedishGenerator.makeEmptyGrid();
grid.setCharAt(1, 1, '1');
Assertions.assertTrue(grid.isDigitAt(1, 1));
}
@Test
@Disabled
public void testAttempt() {
// Arrange
var opts = new Main.Opts();
@@ -94,8 +159,6 @@ public class MainTest {
opts.threads = 1;
opts.tries = 1;
opts.verbose = true;
opts.W = 3;
opts.H = 3;
// We need a small dictionary for testing
// Instead of loading from file, we might want a way to create a mock Dict

View File

@@ -1,18 +0,0 @@
package puzzle;
import org.junit.jupiter.api.Test;
import puzzle.SwedishGenerator.Slot;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SlotTest {
@Test
public void testHoriz() {
assertTrue(new Slot(2, 0L, 0L ,1).horiz());
assertTrue(new Slot(4, 0L, 0L ,1).horiz());
assertFalse(new Slot(1, 0L, 3L ,1).horiz());
assertFalse(new Slot(3, 0L, 3L ,1).horiz());
assertFalse(new Slot(5, 0L, 3L ,1).horiz());
}
}

View File

@@ -1,63 +0,0 @@
package puzzle;
import org.junit.jupiter.api.Test;
import puzzle.SwedishGenerator.Grid;
import puzzle.SwedishGenerator.Slot;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SwedishGeneratorTest {
@Test
void testExtractSlots() {
SwedishGenerator generator = new SwedishGenerator(3, 3);
Grid grid = generator.makeEmptyGrid();
// Set up digits on the grid to create slots.
// '2' (right) at (0,0) -> slot at (0,1), (0,2)
grid.setCharAt(0, 0, '2');
grid.setCharAt(0, 1, 'A');
grid.setCharAt(0, 2, 'B');
ArrayList<Slot> slots = generator.extractSlots(grid);
assertEquals(1, slots.size());
Slot s = slots.get(0);
assertEquals(2, s.len());
assertEquals(0, s.r(0));
assertEquals(1, s.c(0));
assertEquals(0, s.r(1));
assertEquals(2, s.c(1));
}
@Test
void testStaticSlotMethods() {
// Test static r and c extraction
// packedRs: 1 at index 0, 2 at index 1
long packedRs = (1L << 0) | (2L << 4);
assertEquals(1, Slot.r(packedRs, 0));
assertEquals(2, Slot.r(packedRs, 1));
// Test static horiz
// dir 2 (right) is horizontal
assertTrue(Slot.horiz(2));
// dir 3 (down) is vertical
assertTrue(!Slot.horiz(3));
}
@Test
void testForEachSlot() {
SwedishGenerator generator = new SwedishGenerator(3, 3);
Grid grid = generator.makeEmptyGrid();
grid.setCharAt(0, 0, '2'); // right
java.util.concurrent.atomic.AtomicInteger count = new java.util.concurrent.atomic.AtomicInteger(0);
generator.forEachSlot(grid, (key, rs, cs, len) -> {
count.incrementAndGet();
assertEquals(2, len);
assertEquals(0, Slot.r(rs, 0));
assertEquals(1, Slot.c(cs, 0));
});
assertEquals(1, count.get());
}
}