​a_maze_ing /
tests / test_mazegen.py
159 commits
07-06-2026
297 lines 9.9 kB
  1import pytest
  2from mazegen import MazeGenerator, MazegenError
  3from mazegen.mazegen import N, E, S, W, DX, DY, SOLID_CELL
  4
  5SEEDS = list(range(50))
  6ALGOS = ["dfs", "prim"]
  7DIRS = {"N": N, "E": E, "S": S, "W": W}
  8
  9
 10def _gen(
 11    width: int = 30,
 12    height: int = 20,
 13    perfect: bool = False,
 14    seed: int = 0,
 15    algorithm: str = "prim",
 16    braid: float = 0.0,
 17) -> MazeGenerator:
 18    g = MazeGenerator(
 19        width,
 20        height,
 21        (0, 0),
 22        (width - 1, height - 1),
 23        perfect=perfect,
 24        algorithm=algorithm,
 25        braid=braid,
 26    )
 27    g.generate(seed)
 28    g.solve()
 29    return g
 30
 31
 32@pytest.mark.parametrize("algorithm", ALGOS)
 33@pytest.mark.parametrize("perfect", [True, False])
 34@pytest.mark.parametrize("seed", SEEDS)
 35def test_reproducible(perfect: bool, seed: int, algorithm: str) -> None:
 36    a = _gen(perfect=perfect, seed=seed, algorithm=algorithm)
 37    b = _gen(perfect=perfect, seed=seed, algorithm=algorithm)
 38    assert a.grid == b.grid
 39    assert a.path == b.path
 40
 41
 42@pytest.mark.parametrize("algorithm", ALGOS)
 43@pytest.mark.parametrize("perfect", [True, False])
 44@pytest.mark.parametrize("seed", SEEDS)
 45def test_wall_coherence(seed: int, perfect: bool, algorithm: str) -> None:
 46    g = _gen(perfect=perfect, seed=seed, algorithm=algorithm)
 47    for y in range(g.height):
 48        for x in range(g.width):
 49            cell = g.grid[y][x]
 50            if x + 1 < g.width:
 51                assert bool(cell & E) == bool(g.grid[y][x + 1] & W)
 52            if y + 1 < g.height:
 53                assert bool(cell & S) == bool(g.grid[y + 1][x] & N)
 54
 55
 56@pytest.mark.parametrize("algorithm", ALGOS)
 57@pytest.mark.parametrize("seed", SEEDS)
 58def test_borders_closed(seed: int, algorithm: str) -> None:
 59    g = _gen(seed=seed, algorithm=algorithm)
 60    for x in range(g.width):
 61        assert g.grid[0][x] & N
 62        assert g.grid[g.height - 1][x] & S
 63    for y in range(g.height):
 64        assert g.grid[y][0] & W
 65        assert g.grid[y][g.width - 1] & E
 66
 67
 68@pytest.mark.parametrize("algorithm", ALGOS)
 69@pytest.mark.parametrize("perfect", [True, False])
 70@pytest.mark.parametrize("seed", SEEDS)
 71def test_connectivity(perfect: bool, seed: int, algorithm: str) -> None:
 72    g = _gen(perfect=perfect, seed=seed, algorithm=algorithm)
 73    seen = {g.entry}
 74    stack = [g.entry]
 75    while stack:
 76        x, y = stack.pop()
 77        for d in (N, E, S, W):
 78            if g.grid[y][x] & d:
 79                continue
 80            nxt = (x + DX[d], y + DY[d])
 81            if nxt not in seen:
 82                seen.add(nxt)
 83                stack.append(nxt)
 84    expected = g.width * g.height - len(g.pattern_cells)
 85    assert len(seen) == expected
 86
 87
 88@pytest.mark.parametrize("algorithm", ALGOS)
 89@pytest.mark.parametrize("perfect", [True, False])
 90@pytest.mark.parametrize("seed", SEEDS)
 91def test_pattern_cells_sealed(
 92    seed: int, perfect: bool, algorithm: str
 93) -> None:
 94    g = _gen(perfect=perfect, seed=seed, algorithm=algorithm)
 95    for x, y in g.pattern_cells:
 96        assert g.grid[y][x] == SOLID_CELL
 97
 98
 99@pytest.mark.parametrize("algorithm", ALGOS)
100@pytest.mark.parametrize("perfect", [True, False])
101@pytest.mark.parametrize("seed", SEEDS)
102def test_path_walks_entry_to_exit(
103    perfect: bool, seed: int, algorithm: str
104) -> None:
105    g = _gen(perfect=perfect, seed=seed, algorithm=algorithm)
106    x, y = g.entry
107    for step in g.path:
108        d = DIRS[step]
109        assert not (g.grid[y][x] & d)
110        x, y = x + DX[d], y + DY[d]
111    assert (x, y) == g.exit_
112
113
114@pytest.mark.parametrize("algorithm", ALGOS)
115@pytest.mark.parametrize("seed", SEEDS)
116def test_perfect_is_spanning_tree(seed: int, algorithm: str) -> None:
117    g = _gen(perfect=True, seed=seed, algorithm=algorithm)
118    edges = 0
119    for y in range(g.height):
120        for x in range(g.width):
121            if (x, y) in g.pattern_cells:
122                continue
123            for d in (E, S):
124                nx, ny = x + DX[d], y + DY[d]
125                if (nx, ny) in g.pattern_cells:
126                    continue
127                if 0 <= nx < g.width and 0 <= ny < g.height:
128                    if not g.grid[y][x] & d:
129                        edges += 1
130    cells = g.width * g.height - len(g.pattern_cells)
131    assert edges == cells - 1
132
133
134def test_too_small_raises() -> None:
135    with pytest.raises(MazegenError, match="too small"):
136        MazeGenerator.pattern_cells_for(8, 6)
137    g = MazeGenerator(8, 6, (0, 0), (7, 5), perfect=True)
138    with pytest.raises(MazegenError, match="too small"):
139        g.generate(0)
140
141
142def test_entry_on_pattern_raises() -> None:
143    # (1,1) is a glyph cell for 9x7 (sx=1, sy=1)
144    g = MazeGenerator(9, 7, (1, 1), (8, 6), perfect=True)
145    with pytest.raises(MazegenError, match="ENTRY/EXIT"):
146        g.generate(0)
147
148
149def test_glyph_false_skips_pattern() -> None:
150    g = MazeGenerator(8, 6, (0, 0), (7, 5), perfect=True, glyph=False)
151    g.generate(0)
152    assert g.pattern_cells == set()
153
154
155def test_unknown_algorithm_raises() -> None:
156    with pytest.raises(MazegenError, match="Unknown algorithm"):
157        MazeGenerator(9, 7, (0, 0), (8, 6), perfect=True, algorithm="bfs")
158
159
160def test_to_output_lines() -> None:
161    g = _gen(seed=7)
162    lines = list(g.to_output_lines())
163    assert lines[: g.height] == list(g.to_hex_lines())
164    assert lines[g.height] == ""
165    ex, ey = g.entry
166    assert lines[g.height + 1] == f"{ex},{ey}"
167    xx, xy = g.exit_
168    assert lines[g.height + 2] == f"{xx},{xy}"
169    assert lines[g.height + 3] == g.path
170    assert len(lines) == g.height + 4
171
172
173def test_seed_round_trip() -> None:
174    g1 = _gen(seed=99)
175    g2 = MazeGenerator(
176        30, 20, (0, 0), (29, 19), perfect=False, algorithm="prim", braid=0.0
177    )
178    g2.generate(99)
179    g2.solve()
180    assert g1.grid == g2.grid
181    assert g1.path == g2.path
182
183
184# --- braid tests ---
185
186
187@pytest.mark.parametrize("algorithm", ALGOS)
188@pytest.mark.parametrize("seed", SEEDS)
189def test_braid_reproducible(seed: int, algorithm: str) -> None:
190    a = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=0.5)
191    b = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=0.5)
192    assert a.grid == b.grid
193    assert a.path == b.path
194
195
196@pytest.mark.parametrize("algorithm", ALGOS)
197@pytest.mark.parametrize("seed", SEEDS)
198def test_braid_no_3x3_open(seed: int, algorithm: str) -> None:
199    g = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=1.0)
200    for by in range(g.height - 2):
201        for bx in range(g.width - 2):
202            fully_open = True
203            for cy in range(by, by + 3):
204                for cx in range(bx, bx + 3):
205                    cell = g.grid[cy][cx]
206                    if cx < bx + 2 and cell & E:
207                        fully_open = False
208                    if cx > bx and cell & W:
209                        fully_open = False
210                    if cy < by + 2 and cell & S:
211                        fully_open = False
212                    if cy > by and cell & N:
213                        fully_open = False
214            assert not fully_open, (
215                f"3x3 open block at ({bx},{by}), seed={seed}"
216            )
217
218
219@pytest.mark.parametrize("algorithm", ALGOS)
220@pytest.mark.parametrize("seed", SEEDS)
221def test_braid_preserves_borders(seed: int, algorithm: str) -> None:
222    g = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=1.0)
223    for x in range(g.width):
224        assert g.grid[0][x] & N
225        assert g.grid[g.height - 1][x] & S
226    for y in range(g.height):
227        assert g.grid[y][0] & W
228        assert g.grid[y][g.width - 1] & E
229
230
231@pytest.mark.parametrize("algorithm", ALGOS)
232@pytest.mark.parametrize("seed", SEEDS)
233def test_braid_preserves_pattern_cells(seed: int, algorithm: str) -> None:
234    g = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=1.0)
235    for x, y in g.pattern_cells:
236        assert g.grid[y][x] == SOLID_CELL
237
238
239@pytest.mark.parametrize("algorithm", ALGOS)
240@pytest.mark.parametrize("seed", SEEDS)
241def test_braid_reduces_dead_ends(seed: int, algorithm: str) -> None:
242    def count_dead_ends(g: MazeGenerator) -> int:
243        return sum(
244            1
245            for y in range(g.height)
246            for x in range(g.width)
247            if (x, y) not in g.pattern_cells
248            and (x, y) != g.entry
249            and (x, y) != g.exit_
250            and (g.grid[y][x] & SOLID_CELL).bit_count() == 3
251        )
252
253    g_perfect = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=0.0)
254    g_braid = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=1.0)
255    assert count_dead_ends(g_braid) < count_dead_ends(g_perfect)
256
257
258@pytest.mark.parametrize("algorithm", ALGOS)
259@pytest.mark.parametrize("seed", SEEDS)
260def test_braid_connectivity(seed: int, algorithm: str) -> None:
261    g = _gen(perfect=False, seed=seed, algorithm=algorithm, braid=1.0)
262    seen = {g.entry}
263    stack = [g.entry]
264    while stack:
265        x, y = stack.pop()
266        for d in (N, E, S, W):
267            if g.grid[y][x] & d:
268                continue
269            nxt = (x + DX[d], y + DY[d])
270            if nxt not in seen:
271                seen.add(nxt)
272                stack.append(nxt)
273    expected = g.width * g.height - len(g.pattern_cells)
274    assert len(seen) == expected
275
276
277def test_braid_invalid_rate_raises() -> None:
278    with pytest.raises(MazegenError, match="braid must be in"):
279        MazeGenerator(9, 7, (0, 0), (8, 6), perfect=False, braid=1.5)
280
281
282def test_perfect_true_ignores_braid() -> None:
283    g = _gen(perfect=True, seed=0, braid=1.0)
284    edges = 0
285    for y in range(g.height):
286        for x in range(g.width):
287            if (x, y) in g.pattern_cells:
288                continue
289            for d in (E, S):
290                nx, ny = x + DX[d], y + DY[d]
291                if (nx, ny) in g.pattern_cells:
292                    continue
293                if 0 <= nx < g.width and 0 <= ny < g.height:
294                    if not g.grid[y][x] & d:
295                        edges += 1
296    cells = g.width * g.height - len(g.pattern_cells)
297    assert edges == cells - 1