Skip to content

Commit

Permalink
Add tests for Grid and Board
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-Guo committed May 16, 2024
1 parent a50873a commit f0f6812
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build-backend = "setuptools.build_meta"
[tool.ruff]
line-length = 96
indent-width = 4
exclude = ["royal_game/_exceptions.py", "royal_game/__init__.py"]
exclude = ["royal_game/_exceptions.py", "royal_game/__init__.py", "royal_game/tests/*.py"]

[tool.ruff.format]
quote-style = "double"
Expand Down
47 changes: 23 additions & 24 deletions royal_game/modules/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,42 @@ class Board:

def __init__(self, seed: int = 122138132480) -> None:
self.board: dict[str, Grid] = {}
white_total = 0
black_total = 0

# initialize the white row
offset = 0
for name, is_rosette in white_iter():
status_bit = (seed & (0b1 << offset)) >> offset
offset += 1

if status_bit:
white_total += 1

# simplified expression since GridStatus(1) indicates white
self.board[name] = Grid(name, is_rosette, GridStatus(status_bit))

# initialize the black row
for name, is_rosette in black_iter():
status_bit = (seed & (0b1 << offset)) >> offset
offset += 1

if status_bit:
black_total += 1

self.board[name] = Grid(
name, is_rosette, GridStatus(2) if status_bit else GridStatus(0)
)

# initialize public row
for name, is_rosette in public_iter():
status_bit = (seed & (0b11 << offset)) >> offset

if status_bit == 1:
white_total += 1
elif status_bit == 2:
black_total += 1

offset += 2
self.board[name] = Grid(name, is_rosette, GridStatus(status_bit))

Expand All @@ -73,7 +88,14 @@ def __init__(self, seed: int = 122138132480) -> None:
offset += 3
self.board[name] = StartEndGrid(num_pieces, name)

self.verify_board()
white_total += self.board["WS"].num_pieces + self.board["WE"].num_pieces
black_total += self.board["BS"].num_pieces + self.board["BE"].num_pieces

if white_total != 7:
raise InvalidNumberofPieces("white", white_total)

if black_total != 7:
raise InvalidNumberofPieces("black", black_total)

def __repr__(self):
fmt = ""
Expand Down Expand Up @@ -114,29 +136,6 @@ def __int__(self) -> int:

return board_int

def verify_board(self) -> bool:
"""Check if the board has the correct number of total pieces."""
white_total = 0
black_total = 0

for name, _ in chain(white_iter(), public_iter()):
if self.board[name].status is GridStatus.white:
white_total += 1

white_total += self.board["WS"].num_pieces + self.board["WE"].num_pieces

if white_total != 7:
raise InvalidNumberofPieces("white", white_total)

for name, _ in chain(black_iter(), public_iter()):
if self.board[name].status is GridStatus.black:
black_total += 1

black_total += self.board["BS"].num_pieces + self.board["BE"].num_pieces

if black_total != 7:
raise InvalidNumberofPieces("black", black_total)

def is_end_state(self):
"""
Check if the board is at an end state.
Expand Down
33 changes: 33 additions & 0 deletions royal_game/tests/test_board.py
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
"""Test board logic and representation."""

import pytest

from royal_game._exceptions import InvalidNumberofPieces
from royal_game.modules.board import Board
from royal_game.modules.grid_status import GridStatus


def test_reject_invalid_board():
with pytest.raises(InvalidNumberofPieces, match=r"(white|black).*0"):
_ = Board(0)
with pytest.raises(InvalidNumberofPieces, match=r"white.*8"):
_ = Board(481040535615)
with pytest.raises(InvalidNumberofPieces, match=r"black.*6"):
_ = Board(155055118456)
with pytest.raises(InvalidNumberofPieces, match=r"black.*8"):
_ = Board(292865706639)


def test_init():
board = Board(174518804524)
assert board.board["W4"].status == GridStatus.white
assert board.board["8"].status == GridStatus.empty
assert board.board["B14"].status == GridStatus.empty
assert int(board.board["WS"]) == 2
assert int(board.board["BE"]) == 1


def test_check_endstate():
assert Board(966988398624).is_end_state()
assert not Board(829549445216).is_end_state()
assert Board(599282155520).is_end_state()
assert not Board(597403107328).is_end_state()
37 changes: 37 additions & 0 deletions royal_game/tests/test_grid.py
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
"""Test grid logic and representation."""

import pytest

from royal_game._constants import black_piece, rosette, white_piece
from royal_game._exceptions import InvalidNumPieces
from royal_game.modules.grid import Grid, StartEndGrid
from royal_game.modules.grid_status import GridStatus


def test_reject_invalid_grid():
with pytest.raises(InvalidNumPieces) as e:
_ = StartEndGrid(num_pieces=-1, name="")

with pytest.raises(InvalidNumPieces) as e:
_ = StartEndGrid(num_pieces=10, name="")


def test_int_conversion():
assert int(Grid("", False, GridStatus.empty)) == 0
assert int(StartEndGrid(5, "")) == 5


def test_repr():
test_end = StartEndGrid(3, "end")
assert str(test_end) == " \n 3 \n \n"

test_grid = Grid("test", False, GridStatus.empty)
assert str(test_grid) == "---\n| |\n---\n"

test_grid.is_rosette = True
assert str(test_grid) == f"---\n|{rosette}|\n---\n"

test_grid.status = GridStatus.white
assert str(test_grid) == f"---\n|{white_piece}|\n---\n"

test_grid.status = GridStatus.black
assert str(test_grid) == f"---\n|{black_piece}|\n---\n"

0 comments on commit f0f6812

Please sign in to comment.