import pytest import aiohttp from aiohttp import web import mazegen paths = ('/static',) + tuple(f'/dynamic/{n:X}' for n in range(15)) checks = 'walls','exits','solvable' def assert_maze(m): """Checks for valid maze. Converts cells to integers to ease other checks. """ assert type(m) is list assert len(m) == 7 for row in m: assert type(row) is str assert len(row) == 7 for cell in row: assert cell in '0123456789abcdefABCDEF' return tuple(tuple(int(c,16) for c in row) for row in m) def assert_walls(m): """Checks that left walls match right walls and top match bottom""" for i in range(7): for j in range(6): assert bool(m[i][j] & 4) == bool(m[i][j+1] & 1) assert bool(m[j][i] & 2) == bool(m[j+1][i] & 8) def assert_exits(m, num): assert bool(m[3][0]&1) == bool(num&1) assert bool(m[6][3]&2) == bool(num&2) assert bool(m[3][6]&4) == bool(num&4) assert bool(m[0][3]&8) == bool(num&8) def find_path(m, p1, p2): visited = {p1} queue = [p1] while len(queue) > 0: p = queue.pop() y,x = p n = m[y][x] for nm,dx,dy in (1,-1,0), (2,0,+1), (4,+1,0), (8,0,-1): if x+dx < 0 or x+dx > 6 or y+dy < 0 or y+dy > 6: continue if not (n & nm): q = (y+dy,x+dx) if q == p2: return True if q not in visited: visited.add(q) queue.append(q) return False def assert_solvable(m, num): points = [(3,0),(6,3),(3,6),(0,3)] seg = [points[i] for i in range(4) if (num & (1< 10 @pytest.mark.parametrize('path', paths[:1]) async def test_static(aiohttp_client, path): num = 0 app = web.Application() app.add_routes(mazegen.routes) client = await aiohttp_client(app) answers = set() provided = { assert_maze(["988088c","1000004","1000004","0000000","1000004","1000004","3220226"]), # blank assert_maze(["98a0a8c","1494b04","1430e14","0280820","1e575d5","1e1c575","3a202a6"]), # CS 340 assert_maze(["9a8088c","5b02024","5b49494","0a02020","5b4d1e5","1e571e5","3a282a6"]) # Err 500 } for i in range(20): resp = await client.get(path) assert resp.status == 200 assert resp.content_type == 'application/json' maze = await resp.json() m = assert_maze(maze) assert m not in provided, "Don't use the mazes that we provided as examples" assert_walls(m) assert_exits(m, num) assert_solvable(m, num) answers.add(m) assert len(answers) == 1