diff --git a/tests/test_buffer_format_fuzz_cli.py b/tests/test_buffer_format_fuzz_cli.py new file mode 100644 index 0000000000..3c92181b38 --- /dev/null +++ b/tests/test_buffer_format_fuzz_cli.py @@ -0,0 +1,50 @@ +"""CPU-only tests for tools/buffer_format_fuzz.py's command line: an explicit --formats is checked before the GPU. + + python -I tests/test_buffer_format_fuzz_cli.py + +A typo in --formats used to be found only after runner.open_oracle(), so a machine without the oracle answered +"hardware oracle unavailable" (77) and one with it raised SystemExit (1). It is a usage error (2) before the oracle. +""" +import importlib.util +import os +import subprocess +import sys +import unittest + +REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +TOOL = os.path.join(REPO, "tools", "buffer_format_fuzz.py") + + +def run(*args): + return subprocess.run([sys.executable, "-I", TOOL, *args], capture_output=True, text=True, check=False) + + +def load_tool(): + spec = importlib.util.spec_from_file_location("buffer_format_fuzz", TOOL) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +class BufferFormatFuzzFormatsTests(unittest.TestCase): + def test_unknown_format_is_a_usage_error_before_the_oracle(self): + proc = run("--oracle", "hw", "--formats", "8_8_8_8_UNORM_typo") + self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr) + self.assertIn("unknown format '8_8_8_8_UNORM_typo'", proc.stderr) + self.assertNotIn("hardware oracle", proc.stdout + proc.stderr) + + def test_a_code_outside_the_table_is_refused_too(self): + proc = run("--oracle", "hw", "--formats", "0,8_8_8_8_UNORM") + self.assertEqual(proc.returncode, 2, proc.stdout + proc.stderr) + self.assertIn("unknown format '0'", proc.stderr) + + def test_known_names_and_codes_map_to_the_table(self): + tool = load_tool() + pairs = tool.select_formats("8_8_8_8_UNORM,56") + self.assertEqual([n for _, n in pairs], ["8_8_8_8_UNORM", tool.GFX10_NAMES[56]]) + with self.assertRaises(ValueError): + tool.select_formats("not_a_format") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/buffer_format_fuzz.py b/tools/buffer_format_fuzz.py index 24d3cec29f..ad408c5abb 100644 --- a/tools/buffer_format_fuzz.py +++ b/tools/buffer_format_fuzz.py @@ -18,7 +18,7 @@ python -I tools/buffer_format_fuzz.py --self-test python -I tools/buffer_format_fuzz.py --oracle hw [--mode load|store|all] [--path mubuf|mtbuf|both] [--formats 56,36,...] [--seed S] [--negative-control] -Exit code: 0 all match, 1 a mismatch, 2 harness error, 77 no hardware oracle. Nothing here reads game files. +Exit code: 0 all match, 1 a mismatch, 2 a usage or harness error, 77 no hardware oracle. Nothing here reads game files. """ import argparse import json @@ -507,7 +507,7 @@ def select_formats(spec, family="gfx11"): tok = tok.strip() code = int(tok) if tok.isdigit() else codes.get(tok) if code not in GFX10_NAMES: - raise SystemExit(f"unknown format {tok!r}; give a GFX10 code (1..77) or name such as 8_8_8_8_UNORM") + raise ValueError(f"unknown format {tok!r}; give a GFX10 code (1..77) or name such as 8_8_8_8_UNORM") out.append((code, GFX10_NAMES[code])) return out @@ -606,6 +606,14 @@ def main(): return self_test() if a.oracle != "hw": ap.error("--oracle hw or --self-test") + # An explicit --formats needs no GPU to be parsed: a typo is a usage error here, before the oracle is opened + # (it used to be reported only after it, or masked by "hardware oracle unavailable" without one). + explicit = None + if a.formats: + try: + explicit = select_formats(a.formats) + except ValueError as e: + ap.error(str(e)) from hw_oracle import runner, asm try: oracle = runner.open_oracle(arch=a.hw_arch) @@ -613,7 +621,7 @@ def main(): print(f"hardware oracle unavailable: {e}") return 77 family = asm.family(oracle.arch) - fmts = select_formats(a.formats, family) + fmts = explicit if explicit is not None else select_formats("", family) if family == "gfx12": print("GFX12 numbers its formats again; not built here") oracle.close()