mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
cvt_diff: reject empty/zero-program runs, invalid counts, and malformed --ops; accept argv
This commit is contained in:
@ -5,6 +5,8 @@ differential runs. Standard library only; no GPU, no game files.
|
||||
The family is v_cvt_flr_i32_f32, v_cvt_rpi_i32_f32, v_cvt_norm_i16_f16, v_cvt_norm_u16_f16 (VOP1) and
|
||||
v_cvt_pknorm_i16_f32, v_cvt_pknorm_u16_f32, v_cvt_pknorm_i16_f16, v_cvt_pknorm_u16_f16 (VOP3).
|
||||
"""
|
||||
import contextlib
|
||||
import io
|
||||
import itertools
|
||||
import os
|
||||
import random
|
||||
@ -12,6 +14,7 @@ import re
|
||||
import sys
|
||||
import tempfile
|
||||
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"))
|
||||
@ -295,5 +298,65 @@ class Translated(unittest.TestCase):
|
||||
c.translate_listing = real
|
||||
|
||||
|
||||
class CliGuards(unittest.TestCase):
|
||||
def _run_main(self, argv):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with mock.patch.object(sys, "argv", ["cvt_diff.py", *argv]), \
|
||||
contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
try:
|
||||
rc = c.main()
|
||||
except SystemExit as exc:
|
||||
rc = exc.code
|
||||
return rc, out.getvalue(), err.getvalue()
|
||||
|
||||
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_zero_program_exhaustive_f32_run_exits_two(self):
|
||||
rc, _, err = self._run_main(["--no-sweep", "--programs", "0", "--exhaustive-f16", "--ops", "v_cvt_flr_i32_f32"])
|
||||
self.assertEqual(rc, 2)
|
||||
self.assertIn("no program ran", err)
|
||||
|
||||
def test_ops_validation_and_deduplication(self):
|
||||
for bad_ops in ("", " ", "v_cvt_flr_i32_f32,", ",v_cvt_flr_i32_f32"):
|
||||
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": c.Counter(), "ops": c.Counter({"v_cvt_flr_i32_f32": 1}),
|
||||
"events": c.Counter()}, None
|
||||
|
||||
with mock.patch.object(c, "run_suite", side_effect=fake_suite):
|
||||
rc, _, _ = self._run_main(["--no-sweep", "--programs", "1", "--ops", "v_cvt_flr_i32_f32, v_cvt_flr_i32_f32"])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(seen_ops, [["v_cvt_flr_i32_f32"]])
|
||||
|
||||
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):
|
||||
c.run_suite(bad_programs, 1)
|
||||
for bad_max_ops in (0, -1, True):
|
||||
with self.subTest(max_ops=bad_max_ops), self.assertRaises(ValueError):
|
||||
c.run_suite(1, 1, max_ops=bad_max_ops)
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out):
|
||||
self.assertEqual(c.main(["--list-ops"]), 0)
|
||||
self.assertIn("v_cvt_flr_i32_f32", out.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -724,6 +724,10 @@ def run_exhaustive(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}")
|
||||
@ -819,7 +823,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")
|
||||
@ -830,7 +834,7 @@ def main():
|
||||
ap.add_argument("--exhaustive-f16", action="store_true", help="also run every f16 input through the f16 ops")
|
||||
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():
|
||||
@ -839,11 +843,23 @@ def main():
|
||||
if args.self_test:
|
||||
return 0 if self_test() else 1
|
||||
|
||||
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 not args.exhaustive_f16 and args.programs == 0:
|
||||
ap.error("nothing to run: pass --programs >= 1, --exhaustive-f16, or omit --no-sweep")
|
||||
|
||||
ops = None
|
||||
if args.ops is not None:
|
||||
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")
|
||||
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()
|
||||
@ -883,6 +899,9 @@ def main():
|
||||
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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user