Files
mandy/tests/test_run_gpu_mandelbrot.py
ItsNotRexed 33905dc9dd Fix what the round-43 merges broke (pytest)
Merges that each passed alone but clash together:
- v_cmp_class_f64: the earlier cmp_class_f64 (685d16ed9) (operand-form checks, test_cmp_class_f64) and the later f64 branch
  in cmp_class both handled the opcode; f64 goes to cmp_class_f64 again, f16 stays in cmp_class.
- rdna2spirv CLI tests: spirv-as/spirv-val now run in the output folder with the staging file's bare name, so
  the test doubles resolve it against cwd; assemble_spirv decodes the output only when it is bytes. (The doubles
  had been writing .spirv-*.tmp files into the repository root.)
- ps_w64 fixtures (TEST-722): an 18th fixture (ps_w64_a_discard) landed; its killed lanes export nothing and
  keep the clear value.
- first_light: a run without an OBJ removes mesh.bin, and harness_env reads manifest["files"]; batch_glb's stats
  name failures and dropped textures; run_gpu_tests reports a frame never written as a missing readback
  (TEST-837).
- make_game TRANSLATION_SOURCES and runtime.yml's paths include spirv_opt.py and report_paths.py (and
  wave64_emulate_opt.py), which the translator imports.
Repo rules: stdout guard on six tools; encoding on two opens (gen_gdk_runtime_classes' helper is read_utf8);
newline on one write; anim_skeleton_match's joaat encodes like hashdict.joaat; test_feature_scan's new test is
listed with the others its globals() runner reaches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 22:07:41 -05:00

72 lines
2.9 KiB
Python

"""The GPU suite must judge the full Mandelbrot readback, without needing a device."""
from contextlib import redirect_stdout
import io
from pathlib import Path
import struct
import sys
import tempfile
from types import SimpleNamespace
import unittest
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
import run_gpu_tests as rgt
from check import mandelbrot_counts
def reference_frame(width: int) -> bytes:
return b"".join(struct.pack("<4I", 0, 0, 0, count) for count in mandelbrot_counts(width))
class MandelbrotReadbackTest(unittest.TestCase):
def run_frame(self, data: bytes | None) -> tuple[int, str]:
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
args = SimpleNamespace(filter="ps_mandelbrot.asm", build=directory, llvm_mc="unused")
def launch(cmd: list[str], **kwargs: object) -> tuple[int, str]:
if cmd[0] == sys.executable:
(work / "spirv" / "ps_mandelbrot.spv").touch()
elif Path(cmd[0]).stem == "vk_harness":
if data is not None:
Path(cmd[-1]).write_bytes(data)
else:
self.fail(f"unexpected command: {cmd}")
return 0, ""
output = io.StringIO()
with (patch.object(rgt, "assemble_to_s", side_effect=lambda _mc, _src, dst, _name: Path(dst).touch()),
patch.object(rgt, "spirv_as"),
patch.object(rgt, "run_cmd", side_effect=launch),
redirect_stdout(output)):
result = rgt.run_suite(args, directory)
return result, output.getvalue()
def test_missing_empty_short_or_partial_frames_fail_the_test(self) -> None:
for label, data in (
("missing", None), ("empty", b""), ("one complete pixel", reference_frame(1)),
("partial pixel", bytes(8)),
):
with self.subTest(label=label):
result, output = self.run_frame(data)
self.assertEqual(result, 1, output)
self.assertIn("FAIL ps_mandelbrot", output)
# a frame that was never written is reported as a missing readback (TEST-837) before its size is read
self.assertIn("missing readback" if data is None else "expected 4096 pixels", output)
def test_complete_frame_still_compares_the_iteration_counts(self) -> None:
correct = reference_frame(64)
result, output = self.run_frame(correct)
self.assertEqual(result, 0, output)
self.assertIn("4096/4096 iteration counts match", output)
wrong = bytearray(correct)
struct.pack_into("<I", wrong, 12, 65)
result, output = self.run_frame(bytes(wrong))
self.assertEqual(result, 1, output)
self.assertIn("1 of 4096 iteration counts differ", output)
if __name__ == "__main__":
unittest.main()