test_miniproject.py 1.42 KB
from collections import Counter, defaultdict
import random

import pytest
from src.floodfill import *

NUM_ITERS: int = 100000


@pytest.mark.parametrize('n, m', [(1, 1), (5, 5), (5, 10), (10, 5)])
def test_generate_random_grid(n: int, m: int):
    random.seed(1000)
    d: defaultdict[tuple[int, int], Counter[str]] = defaultdict(Counter)
    for _ in range(NUM_ITERS):
        grid: list[list[str]] = generate_random_grid(n, m)
        assert n == len(grid)
        for i in range(n):
            assert m == len(grid[i])

        for i in range(n):
            for j in range(m):
                d[(i, j)][grid[i][j]] += 1

    for x in d:
        assert pytest.approx(
            1/2, 0.01) == d[x][' '] / NUM_ITERS  # type: ignore
        assert pytest.approx(
            1/2, 0.01) == d[x]['W'] / NUM_ITERS  # type: ignore


@pytest.mark.parametrize('n, m', [(1, 1), (5, 5), (5, 10), (10, 5), (10, 10)])
def test_all_coordinates(n: int, m: int):
    assert set([(x, y) for x in range(n) for y in range(m)]) == set(
        all_coordinates(generate_random_grid(n, m)))


def test_find_random_empty() -> None:
    grid: list[list[str]] = [[' ', 'X', ' ', ' '], ['B', 'B', ' ', 'B']]
    d: Counter[tuple[int, int]] = Counter()
    for _ in range(NUM_ITERS):
        d[find_random_empty(grid)] += 1
    for x, y in d:
        assert grid[x][y] == ' '
        assert pytest.approx(  # type: ignore
            1/4, 0.01) == d[(x, y)] / NUM_ITERS