diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index 73321af..734d0f1 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -17,6 +17,14 @@ import java.util.function.Predicate; @SuppressWarnings("ALL") public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) { + class Data { + + static byte[] EXAMPLE = new byte[0]; + } + public SwedishGenerator { + Data.EXAMPLE = new byte[SIZE]; + Arrays.fill(Data.EXAMPLE, (byte) '#'); + } public SwedishGenerator(int W, int H) { this(W, H, W * H, Math.min(W, H)); } public SwedishGenerator() { this(9, 8); } @@ -91,7 +99,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) { int getOffset(int r, int c) { return r * W + c; } boolean isLettercell(int r, int c) { - return isLetterCell(getCharAt(r,c)); + return isLetterCell(getCharAt(r, c)); } char getCharAt(int r, int c) { return (char) (g[getOffset(r, c)] & 0xFF); @@ -113,9 +121,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) { } // ---------------- Grid helpers ---------------- Grid makeEmptyGrid() { - var g = new byte[H * W]; - Arrays.fill(g, (byte) '#'); - return new Grid(g, H, W); + return new Grid(Data.EXAMPLE.clone(), H, W); } String gridToString(Grid g) { @@ -670,9 +676,9 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) { var n = 0; for (var i = 0; i < s.len; i++) { - int r = s.rs[i], c = s.cs[i]; - char cur = grid.getCharAt(r, c); - var ch = w.charAt(i); + int r = s.rs[i], c = s.cs[i]; + char cur = grid.getCharAt(r, c); + var ch = w.charAt(i); if (cur == '#') { urs[n] = r; ucs[n] = c; diff --git a/src/test/java/puzzle/MainTest.java b/src/test/java/puzzle/MainTest.java index 6d7a7a8..60ff6ca 100644 --- a/src/test/java/puzzle/MainTest.java +++ b/src/test/java/puzzle/MainTest.java @@ -1,6 +1,9 @@ package puzzle; +import org.junit.jupiter.api.Assertions; +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; @@ -9,7 +12,81 @@ import static puzzle.Main.indentLines; public class MainTest { public static void main(String[] args) { - new MainTest().testAttempt(); + MainTest t = new MainTest(); + t.testGridBasics(); + t.testGridDeepCopy(); + t.testGridOffset(); + t.testMini(); + t.testAttempt(); + } + + @Test + public void testGridBasics() { + var grid = new Grid(new byte[3 * 4], 3, 4); + // Initialize with # + for (int r = 0; r < 3; r++) { + for (int c = 0; c < 4; c++) { + grid.setCharAt(r, c, '#'); + } + } + + // Test set/get + grid.setCharAt(0, 0, 'A'); + grid.setCharAt(1, 2, '5'); + grid.setCharAt(2, 3, 'Z'); + + Assertions.assertEquals('A', grid.getCharAt(0, 0)); + Assertions.assertEquals('5', grid.getCharAt(1, 2)); + Assertions.assertEquals('Z', grid.getCharAt(2, 3)); + Assertions.assertEquals('#', grid.getCharAt(1, 1)); + + // Test isLetterAt + Assertions.assertTrue(grid.isLetterAt(0, 0)); + Assertions.assertFalse(grid.isLetterAt(1, 2)); + Assertions.assertTrue(grid.isLetterAt(2, 3)); + Assertions.assertFalse(grid.isLetterAt(1, 1)); + + // Test isDigitAt + Assertions.assertFalse(grid.isDigitAt(0, 0)); + Assertions.assertTrue(grid.isDigitAt(1, 2)); + Assertions.assertFalse(grid.isDigitAt(2, 3)); + Assertions.assertFalse(grid.isDigitAt(1, 1)); + + // Test isLettercell + Assertions.assertTrue(grid.isLettercell(0, 0)); // 'A' is letter + Assertions.assertFalse(grid.isLettercell(1, 2)); // '5' is digit + Assertions.assertTrue(grid.isLettercell(1, 1)); // '#' is lettercell + } + + @Test + public void testGridDeepCopy() { + var grid = new Grid(new byte[2 * 2], 2, 2); + grid.setCharAt(0, 0, 'A'); + grid.setCharAt(0, 1, 'B'); + grid.setCharAt(1, 0, 'C'); + grid.setCharAt(1, 1, 'D'); + + var copy = grid.deepCopyGrid(); + Assertions.assertEquals('A', copy.getCharAt(0, 0)); + + copy.setCharAt(0, 0, 'X'); + Assertions.assertEquals('X', copy.getCharAt(0, 0)); + Assertions.assertEquals('A', grid.getCharAt(0, 0)); // Original should be unchanged + } + + @Test + public void testGridOffset() { + var grid = new Grid(new byte[10 * 10], 10, 10); + Assertions.assertEquals(0, grid.getOffset(0, 0)); + Assertions.assertEquals(11, grid.getOffset(1, 1)); + Assertions.assertEquals(99, grid.getOffset(9, 9)); + } + + @Test + public void testMini() { + var grid = new Grid(new byte[3 * 3], 3, 3); + grid.setCharAt(1, 1, '1'); + Assertions.assertTrue(grid.isDigitAt(1, 1)); } public void testAttempt() {