From 803575d2a7af7e358f55c77ca46ee24ad5cdc30e Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 06:58:24 +0100 Subject: [PATCH] dot_diff: fail on skipped programs, reject invalid counts/empty --ops, and accept argv --- tests/test_dot_diff.py | 70 ++++++++++++++++++++++++++++++++++++++++++ tools/dot_diff.py | 50 ++++++++++++++++++++++-------- 2 files changed, 107 insertions(+), 13 deletions(-) diff --git a/tests/test_dot_diff.py b/tests/test_dot_diff.py index 0d6692c36..30d8f75eb 100644 --- a/tests/test_dot_diff.py +++ b/tests/test_dot_diff.py @@ -5,11 +5,14 @@ library only; no GPU, no game files. The family is v_dot2_i32_i16, v_dot2_u32_u16, v_dot4_i32_i8, v_dot4_u32_u8, v_dot8_i32_i4, v_dot8_u32_u4 (VOP3P) and the accumulating v_dot4c_i32_i8, v_dot2c_i32_i16, v_dot2c_u32_u16, v_dot4c_u32_u8, v_dot8c_i32_i4, v_dot8c_u32_u4. """ +import contextlib +import io import os import random import re import sys import unittest +from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) @@ -180,5 +183,72 @@ class Translated(unittest.TestCase): d.translate_listing = real +class CliGuards(unittest.TestCase): + def _run_main(self, argv): + out, err = io.StringIO(), io.StringIO() + with mock.patch.object(sys, "argv", ["dot_diff.py", *argv]), \ + contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + try: + rc = d.main() + except SystemExit as exc: + rc = exc.code + return rc, out.getvalue(), err.getvalue() + + def test_skipped_programs_exit_nonzero(self): + with mock.patch.object(d, "run_one", return_value=("skip", "translator unsupported: synthetic")): + rc, out, _ = self._run_main(["--no-sweep", "--programs", "2", "--ops", "v_dot2_i32_i16"]) + self.assertEqual(rc, 1) + self.assertIn("SKIPPED 2 program(s)", out) + + def test_invalid_counts_and_empty_runs_are_rejected(self): + for argv, needle in ( + (["--no-sweep", "--programs", "-1"], "--programs"), + (["--no-sweep", "--programs", "1", "--max-ops", "0"], "--max-ops"), + (["--no-sweep", "--programs", "1", "--max-ops", "-2"], "--max-ops"), + (["--no-sweep", "--programs", "0"], "nothing to run"), + ): + with self.subTest(argv=argv): + rc, _, err = self._run_main(argv) + self.assertEqual(rc, 2) + self.assertIn(needle, err) + + def test_ops_validation_and_deduplication(self): + for bad_ops in ("", " ", "v_dot2_i32_i16,", ",v_dot2_i32_i16"): + with self.subTest(ops=bad_ops): + rc, _, err = self._run_main(["--no-sweep", "--programs", "1", "--ops", bad_ops]) + self.assertEqual(rc, 2) + self.assertIn("--ops", err) + + seen_ops = [] + + def fake_suite(programs, seed, max_ops=6, ops=None): + seen_ops.append(ops) + return {"pass": 1, "mismatch": 0, "skip": d.Counter(), "ops": d.Counter({"v_dot2_i32_i16": 1}), + "events": d.Counter()}, None + + with mock.patch.object(d, "run_suite", side_effect=fake_suite): + rc, _, _ = self._run_main(["--no-sweep", "--programs", "1", "--ops", "v_dot2_i32_i16, v_dot2_i32_i16"]) + self.assertEqual(rc, 0) + self.assertEqual(seen_ops, [["v_dot2_i32_i16"]]) + + seen_ops.clear() + with mock.patch.object(d, "run_suite", side_effect=fake_suite): + rc, _, _ = self._run_main(["--no-sweep", "--programs", "1", "--ops", " gfx103x "]) + self.assertEqual(rc, 0) + self.assertEqual(seen_ops, [list(d.GFX103X_OPS)]) + + def test_run_suite_validates_bounds_and_main_accepts_argv(self): + for bad_programs in (-1, True): + with self.subTest(programs=bad_programs), self.assertRaises(ValueError): + d.run_suite(bad_programs, 1) + for bad_max_ops in (0, -1, True): + with self.subTest(max_ops=bad_max_ops), self.assertRaises(ValueError): + d.run_suite(1, 1, max_ops=bad_max_ops) + out = io.StringIO() + with contextlib.redirect_stdout(out): + self.assertEqual(d.main(["--list-ops"]), 0) + self.assertIn("v_dot2_i32_i16", out.getvalue()) + + if __name__ == "__main__": unittest.main() diff --git a/tools/dot_diff.py b/tools/dot_diff.py index 442689b34..8147338c8 100644 --- a/tools/dot_diff.py +++ b/tools/dot_diff.py @@ -523,6 +523,10 @@ def run_grid(ops=None): def run_suite(programs, seed, max_ops=6, ops=None): """Random seeded programs. Returns (counts, failure).""" + if type(programs) is not int or programs < 0: + raise ValueError("programs must be a nonnegative integer") + if type(max_ops) is not int or max_ops < 1: + raise ValueError("max_ops must be at least 1") counts = _new_counts() failure = None rng = random.Random(f"suite:{seed}") @@ -619,7 +623,7 @@ def self_test(): return ok -def main(): +def main(argv=None): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0], formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--programs", type=int, default=300, help="random programs after the sweep") @@ -630,7 +634,7 @@ def main(): ap.add_argument("--no-sweep", action="store_true", help="skip the source-kind sweep and the edge grid") ap.add_argument("--list-ops", action="store_true") ap.add_argument("--self-test", action="store_true") - args = ap.parse_args() + args = ap.parse_args(argv) if args.list_ops: for op, form in OP_FORMS.items(): @@ -640,30 +644,44 @@ def main(): if args.self_test: return 0 if self_test() else 1 - if args.ops == "gfx103x": - ops = list(GFX103X_OPS) - else: - ops = [o.strip() for o in args.ops.split(",")] if args.ops else None - bad = [o for o in (ops or []) if o not in FAMILY] - if bad: - print(f"unknown op(s) {bad}; --list-ops shows the family", file=sys.stderr) - return 2 + if args.programs < 0: + ap.error("--programs must be nonnegative") + if args.max_ops < 1: + ap.error("--max-ops must be at least 1") + if args.no_sweep and args.programs == 0: + ap.error("nothing to run: pass --programs >= 1 or omit --no-sweep") - total_pass = total_run = total_mismatch = 0 + ops = None + if args.ops is not None: + if args.ops.strip() == "gfx103x": + ops = list(GFX103X_OPS) + else: + tokens = [o.strip() for o in args.ops.split(",")] + if not tokens or any(not o for o in tokens): + ap.error("--ops must be a comma-separated list of opcodes (or 'gfx103x')") + ops = list(dict.fromkeys(tokens)) + bad = [o for o in ops if o not in FAMILY] + if bad: + print(f"unknown op(s) {bad}; --list-ops shows the family", file=sys.stderr) + return 2 + + total_pass = total_run = total_mismatch = total_skip = 0 per_op, events = Counter(), Counter() failure = None try: for label, enabled, runner in ( ("sweep", not args.no_sweep, lambda: run_sweep(args.seed, ops)), ("edge grid", not args.no_sweep, lambda: run_grid(ops)), - ("random", True, lambda: run_suite(args.programs, args.seed, args.max_ops, ops)), + ("random", args.programs > 0, lambda: run_suite(args.programs, args.seed, args.max_ops, ops)), ): if not enabled: continue counts, first = runner() - ran = counts["pass"] + counts["mismatch"] + sum(counts["skip"].values()) + skips = sum(counts["skip"].values()) + ran = counts["pass"] + counts["mismatch"] + skips total_pass += counts["pass"] total_mismatch += counts["mismatch"] + total_skip += skips total_run += ran per_op.update(counts["ops"]) events.update(counts["events"]) @@ -681,6 +699,12 @@ def main(): if failure: _report(failure) return 1 + if total_skip: + print(f"SKIPPED {total_skip} program(s): every generated form should translate (reasons above)") + return 1 + if total_run == 0: + print("no program ran", file=sys.stderr) + return 2 return 0