Skip to content

Commit

Permalink
(#112) testing quake2.Visibility.as_bytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
snake-biscuits committed Jul 1, 2023
1 parent 00afa6a commit 6bf48d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion bsp_tool/branches/id_software/quake2.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Visibility:
# -- [b == "1" for b in f"{int.from_bytes(x, 'big'):b}"[::-1]]

def __init__(self, pvs_table: List[List[bool]] = tuple(), pas_table: List[List[bool]] = tuple()):
assert len(pvs_table) == len(pas_table)
self.pvs = pvs_table
self.pas = pas_table

Expand Down Expand Up @@ -278,7 +279,7 @@ def run_length_encode(data: bytes) -> bytes:
return bytes(out)

def as_bytes(self) -> bytes:
# TODO: test
# NOTE: should be a byte-for-byte match, indices into lump should be correct
# TODO: reduce pvs & pas to a set of each unique flag sequence
# -- then index that fixed list of pvs/pas flags for extra compression
assert len(self.pvs) == len(self.pas)
Expand Down
38 changes: 27 additions & 11 deletions tests/branches/id_software/test_quake2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@
# TODO: verify lumps that index other lumps are in bounds


@pytest.mark.parametrize("bsp", bsps.values(), ids=bsps.keys())
def test_visibility(bsp: IdTechBsp):
num_clusters = len({leaf.cluster for leaf in bsp.LEAVES if leaf.cluster != -1})
assert len(bsp.VISIBILITY.pvs) == num_clusters
assert len(bsp.VISIBILITY.pas) == num_clusters
max_value = 2 ** num_clusters - 1 # all clusters visible (fast vis)
for pvs in bsp.VISIBILITY.pvs:
assert int.from_bytes(pvs, "little") <= max_value
for pas in bsp.VISIBILITY.pas:
assert int.from_bytes(pas, "little") <= max_value
# is it little endian? what about bit endian?
class TestVisibility:
@pytest.mark.parametrize("bsp", bsps.values(), ids=bsps.keys())
def test_parser(self, bsp: IdTechBsp):
num_clusters = len({leaf.cluster for leaf in bsp.LEAVES if leaf.cluster != -1})
assert len(bsp.VISIBILITY.pvs) == num_clusters
assert len(bsp.VISIBILITY.pas) == num_clusters
max_value = 2 ** num_clusters - 1 # all clusters visible (fast vis)
for pvs in bsp.VISIBILITY.pvs:
assert int.from_bytes(pvs, "little") <= max_value
for pas in bsp.VISIBILITY.pas:
assert int.from_bytes(pas, "little") <= max_value
# is it little endian? what about bit endian?

# TODO: test_run_length_encode
# TODO: test_run_length_decode

@pytest.mark.parametrize("bsp", bsps.values(), ids=bsps.keys())
def test_as_bytes(self, bsp: IdTechBsp):
# content-aware diff
double_parsed = quake2.Visibility.from_bytes(bsp.VISIBILITY.as_bytes())
assert bsp.VISIBILITY.pvs == double_parsed.pvs
assert bsp.VISIBILITY.pas == double_parsed.pas
# byte-for-byte check
header = bsp.headers["VISIBILITY"]
bsp.file.seek(header.offset)
raw_lump = bsp.file.read(header.length)
assert bsp.VISIBILITY.as_bytes() == raw_lump, "not byte-for-byte"


# @pytest.mark.parametrize("bsp", bsps.values(), ids=bsps.keys())
Expand Down

0 comments on commit 6bf48d6

Please sign in to comment.