"""Unit tests for pure Galaxian sprite and bullet descriptor decoding.""" import pytest from galemu.video import ( BULLET_RAM_SIZE, BULLET_WHITE, BULLET_YELLOW, SPRITE_COUNT, SPRITE_RAM_SIZE, decode_bullet_descriptors, decode_sprite_descriptors, ) def test_sprite_descriptors_decode_coordinates_bits_colors_and_pixels() -> None: sprite_ram = bytearray(SPRITE_RAM_SIZE) sprites = _blank_sprites() sprites[0x3F] = _sprite_with_pixel(15, 15, 3) offset = 6 * 4 sprite_ram[offset : offset + 4] = bytes((0xC0, 0xFF, 0xFD, 0x20)) descriptors = decode_sprite_descriptors(bytes(sprite_ram), tuple(sprites)) assert len(descriptors) == 8 assert descriptors[6].slot == 6 assert descriptors[6].code == 0x3F assert (descriptors[6].x, descriptors[6].y) == (33, 48) assert descriptors[6].color_group == 5 assert descriptors[6].flip_x is True assert descriptors[6].flip_y is True assert descriptors[6].pixels[15][15] == 3 def test_first_three_sprite_slots_apply_the_hardware_scanline_offset() -> None: sprite_ram = bytearray(SPRITE_RAM_SIZE) sprite_ram[0] = 0xC0 descriptors = decode_sprite_descriptors(bytes(sprite_ram), tuple(_blank_sprites())) assert descriptors[0].y == 49 assert descriptors[3].y == 240 def test_bullet_descriptors_use_native_raster_geometry_and_fixed_colors() -> None: bullet_ram = bytearray(BULLET_RAM_SIZE) bullet_ram[1] = 0xD0 bullet_ram[3] = 0x10 bullet_ram[7 * 4 + 1] = 0x02 bullet_ram[7 * 4 + 3] = 0xFE descriptors = decode_bullet_descriptors(bytes(bullet_ram)) assert len(descriptors) == 4 shell = next(descriptor for descriptor in descriptors if descriptor.slot == 0) assert shell.kind == "shell" assert (shell.x, shell.y) == (235, 48) assert shell.color == BULLET_WHITE assert shell.pixels == ((1, 1, 1, 1),) missile = next(descriptor for descriptor in descriptors if descriptor.kind == "missile") assert (missile.x, missile.y) == (-3, 253) assert missile.color == BULLET_YELLOW def test_bullet_descriptors_keep_only_the_last_shell_matching_each_scanline() -> None: bullet_ram = bytearray(BULLET_RAM_SIZE) bullet_ram[1] = 0xD0 bullet_ram[3] = 0x10 bullet_ram[4 + 1] = 0xD0 bullet_ram[4 + 3] = 0x20 descriptors = decode_bullet_descriptors(bytes(bullet_ram)) shell = next(descriptor for descriptor in descriptors if descriptor.kind == "shell") assert (shell.slot, shell.x, shell.y) == (1, 219, 48) @pytest.mark.parametrize("size", [0, SPRITE_RAM_SIZE - 1, SPRITE_RAM_SIZE + 1]) def test_sprite_descriptors_require_complete_sprite_ram(size: int) -> None: with pytest.raises(ValueError, match="expected a 32-byte sprite RAM"): decode_sprite_descriptors(bytes(size), tuple(_blank_sprites())) @pytest.mark.parametrize("size", [0, BULLET_RAM_SIZE - 1, BULLET_RAM_SIZE + 1]) def test_bullet_descriptors_require_complete_bullet_ram(size: int) -> None: with pytest.raises(ValueError, match="expected a 32-byte bullet RAM"): decode_bullet_descriptors(bytes(size)) def test_sprite_descriptors_reject_an_incomplete_decoded_sprite_table() -> None: with pytest.raises(ValueError, match="expected 64 decoded sprites"): decode_sprite_descriptors(bytes(SPRITE_RAM_SIZE), ()) def test_sprite_descriptors_reject_invalid_decoded_sprite_pixels() -> None: sprites = _blank_sprites() sprites[0] = ((4,) * 16,) * 16 with pytest.raises(ValueError, match="decoded sprites must contain"): decode_sprite_descriptors(bytes(SPRITE_RAM_SIZE), tuple(sprites)) def _blank_sprites() -> list[tuple[tuple[int, ...], ...]]: sprite = tuple((0,) * 16 for _ in range(16)) return [sprite] * SPRITE_COUNT def _sprite_with_pixel(x: int, y: int, pixel: int) -> tuple[tuple[int, ...], ...]: rows = [[0] * 16 for _ in range(16)] rows[y][x] = pixel return tuple(tuple(row) for row in rows)