mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
mode_diff: reject empty/unjudged runs, invalid counts, and malformed --ops; accept argv
This commit is contained in:
@ -1,11 +1,15 @@
|
||||
"""tools/mode_diff.py: the MODE.FP_ROUND reference against hand-worked f16 words, the generator, a short seeded run
|
||||
of each stage, and planted translator bugs that the run must catch. Standard library only; no GPU, no game files.
|
||||
"""
|
||||
import contextlib
|
||||
import io
|
||||
import os
|
||||
import random
|
||||
import sys
|
||||
import unittest
|
||||
from collections import Counter
|
||||
from fractions import Fraction
|
||||
from unittest import mock
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tools"))
|
||||
import mode_diff as d # noqa: E402
|
||||
@ -159,5 +163,69 @@ class PlantedBugs(unittest.TestCase):
|
||||
self.assertIn("with the MODE writes", failure[2])
|
||||
|
||||
|
||||
class CliGuards(unittest.TestCase):
|
||||
def _run_main(self, argv):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with mock.patch.object(sys, "argv", ["mode_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_invalid_counts_and_empty_runs_are_rejected(self):
|
||||
for argv, needle in (
|
||||
(["--no-sweep", "--programs", "-1", "--invariance", "0"], "--programs"),
|
||||
(["--no-sweep", "--programs", "0", "--invariance", "-1"], "--invariance"),
|
||||
(["--no-sweep", "--programs", "1", "--invariance", "0", "--max-ops", "0"], "--max-ops"),
|
||||
(["--no-sweep", "--programs", "1", "--invariance", "0", "--max-ops", "-2"], "--max-ops"),
|
||||
(["--no-sweep", "--programs", "0", "--invariance", "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_all_unjudged_run_exits_two(self):
|
||||
with mock.patch.object(d, "run_one", return_value=("unjudged", "synthetic unjudged")):
|
||||
rc, _, err = self._run_main(["--no-sweep", "--programs", "2", "--invariance", "0", "--ops", "v_add_f16"])
|
||||
self.assertEqual(rc, 2)
|
||||
self.assertIn("no judged program ran", err)
|
||||
|
||||
def test_ops_validation_and_deduplication(self):
|
||||
for bad_ops in ("", " ", ",", "v_add_f16,", ",v_add_f16"):
|
||||
with self.subTest(ops=bad_ops):
|
||||
rc, _, err = self._run_main(["--no-sweep", "--programs", "1", "--invariance", "0", "--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": Counter(), "unjudged": Counter(),
|
||||
"ops": Counter({"v_add_f16": 1}), "events": {}}, None
|
||||
|
||||
with mock.patch.object(d, "run_suite", side_effect=fake_suite):
|
||||
rc, _, _ = self._run_main(["--no-sweep", "--programs", "1", "--invariance", "0",
|
||||
"--ops", "v_add_f16, v_add_f16"])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(seen_ops, [["v_add_f16"]])
|
||||
|
||||
def test_suites_validate_bounds_and_main_accepts_argv(self):
|
||||
for fn in (d.run_suite, d.run_invariance):
|
||||
for bad_programs in (-1, True):
|
||||
with self.subTest(fn=fn.__name__, programs=bad_programs), self.assertRaises(ValueError):
|
||||
fn(bad_programs, 1)
|
||||
for bad_max_ops in (0, -1, True):
|
||||
with self.subTest(fn=fn.__name__, max_ops=bad_max_ops), self.assertRaises(ValueError):
|
||||
fn(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_add_f16", out.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -921,11 +921,19 @@ def run_sweep(seed, ops=None):
|
||||
|
||||
|
||||
def run_suite(programs, seed, max_ops=6, ops=None):
|
||||
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()
|
||||
return counts, _run(counts, suite_items(programs, seed, max_ops, ops), "rn")
|
||||
|
||||
|
||||
def run_invariance(programs, seed, max_ops=6):
|
||||
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()
|
||||
return counts, _run(counts, invariance_items(programs, seed, max_ops), "iv", invariance=True)
|
||||
|
||||
@ -1012,7 +1020,7 @@ def self_test():
|
||||
return ok
|
||||
|
||||
|
||||
def main():
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
|
||||
ap.add_argument("--programs", type=int, default=300, help="random programs after the sweep")
|
||||
ap.add_argument("--invariance", type=int, default=100, help="mode-invariance programs")
|
||||
@ -1022,7 +1030,7 @@ def main():
|
||||
ap.add_argument("--no-sweep", action="store_true", help="skip the sweep of every form under every MODE write")
|
||||
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 in FAMILY:
|
||||
print(f"{op:18s} {OP_FORMS[op]}")
|
||||
@ -1030,9 +1038,20 @@ def main():
|
||||
return 0
|
||||
if args.self_test:
|
||||
return 0 if self_test() else 1
|
||||
if args.programs < 0:
|
||||
ap.error("--programs must be nonnegative")
|
||||
if args.invariance < 0:
|
||||
ap.error("--invariance 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 and args.invariance == 0:
|
||||
ap.error("nothing to run: pass --programs >= 1, --invariance >= 1, or omit --no-sweep")
|
||||
ops = None
|
||||
if args.ops:
|
||||
ops = [o.strip() for o in args.ops.split(",") if o.strip()]
|
||||
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): {', '.join(bad)} (see --list-ops)", file=sys.stderr)
|
||||
@ -1077,6 +1096,9 @@ def main():
|
||||
if total["skip"]:
|
||||
print(f"SKIPPED {total['skip']} program(s): every generated form should translate (reasons above)")
|
||||
return 1
|
||||
if judged == 0:
|
||||
print("no judged program ran", file=sys.stderr)
|
||||
return 2
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user