f16misc_diff: reject a zero or negative --max-ops and --programs

A zero --programs ran the sweep and nothing else with exit 0, and a zero --max-ops reached random.randint(1, 0)'s ValueError traceback. Both are now usage errors: --max-ops and --programs must be at least 1, checked right after parsing and before any work, and argparse exits 2 with the message. main() takes argv so the tests can drive it.
This commit is contained in:
xel1uz
2026-11-25 07:06:06 +01:00
parent 42537e7b6f
commit 1ded3b4d8e
2 changed files with 62 additions and 7 deletions

View File

@ -22,7 +22,7 @@ import re
import sys
import unittest
from collections import Counter
from contextlib import redirect_stdout
from contextlib import redirect_stderr, redirect_stdout
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
@ -42,6 +42,22 @@ def base_op(line):
return re.sub(r"_e(32|64)$", "", line.split()[0])
def run_main(*argv):
"""(return code, stdout, stderr) of main() with these arguments."""
out, err = io.StringIO(), io.StringIO()
real = sys.argv
try:
sys.argv = ["f16misc_diff.py"] + [str(a) for a in argv]
with redirect_stdout(out), redirect_stderr(err):
try:
rc = f.main()
except SystemExit as e:
rc = e.code
finally:
sys.argv = real
return rc, out.getvalue(), err.getvalue()
def measured(rows):
"""Run the hardware-oracle rows (text, v1, v2, v3, word in v4) of the family through the reference.
Returns (Counter of ok / unjudged reasons, the mismatching rows)."""
@ -350,5 +366,37 @@ class Translated(unittest.TestCase):
f.translate_listing = real
class CommandLine(unittest.TestCase):
"""A zero or negative --programs / --max-ops is a usage error (exit 2), checked before any work."""
def assert_rejected(self, argv, message):
rc, out, err = run_main(*argv)
self.assertEqual(rc, 2, (argv, out, err))
self.assertIn(message, err)
self.assertEqual(out, "") # nothing ran: a zero --programs used to print sweep stats only
self.assertTrue(err.startswith("usage: "), err)
def test_max_ops_zero_is_rejected(self):
self.assert_rejected(["--programs", 1, "--no-sweep", "--max-ops", 0], "--max-ops must be at least 1")
def test_max_ops_negative_is_rejected(self):
self.assert_rejected(["--programs", 1, "--no-sweep", "--max-ops", -3], "--max-ops must be at least 1")
def test_programs_zero_is_rejected(self):
self.assert_rejected(["--programs", 0, "--no-sweep"], "--programs must be at least 1")
def test_programs_negative_is_rejected(self):
self.assert_rejected(["--programs", -1, "--no-sweep"], "--programs must be at least 1")
def test_validation_comes_before_list_ops(self):
self.assert_rejected(["--list-ops", "--programs", 0], "--programs must be at least 1")
def test_the_smallest_valid_run_passes(self):
rc, out, err = run_main("--programs", 1, "--no-sweep", "--max-ops", 1, "--ops", "v_ldexp_f16")
self.assertEqual(rc, 0, (out, err))
self.assertIn("f16 misc family: 1/1 judged programs match", out)
self.assertEqual(err, "")
if __name__ == "__main__":
unittest.main()

View File

@ -67,11 +67,12 @@ Stages:
exponents in turn).
Exit code 0 when every judged program matches, 1 on a mismatch (the first one is printed with its listing) or a
translator skip (every generated form is one the translator takes), 2 on a harness error.
translator skip (every generated form is one the translator takes), 2 on a usage error or a harness error.
--programs and --max-ops must be at least 1 (checked before any work).
Usage:
python -I tools/f16misc_diff.py [--programs N] [--seed S] [--max-ops N] [--ops v_ldexp_f16,v_fma_mix_f32]
python -I tools/f16misc_diff.py --exhaustive-f16 --programs 0 --no-sweep
python -I tools/f16misc_diff.py --exhaustive-f16 --programs 1 --no-sweep
python -I tools/f16misc_diff.py --list-ops
python -I tools/f16misc_diff.py --self-test
"""
@ -1189,7 +1190,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("--seed", type=int, default=1)
@ -1199,7 +1200,14 @@ def main():
ap.add_argument("--exhaustive-f16", action="store_true", help="also run every f16 input through frexp/ldexp")
ap.add_argument("--list-ops", action="store_true")
ap.add_argument("--self-test", action="store_true")
args = ap.parse_args()
if argv is None:
argv = sys.argv[1:]
args = ap.parse_args(argv)
# Before any work: a zero --programs silently did nothing and a zero --max-ops reached randint(1, 0)'s traceback.
if args.max_ops < 1:
ap.error("--max-ops must be at least 1")
if args.programs < 1:
ap.error("--programs must be at least 1")
if args.list_ops:
for op in FAMILY:
print(f"{op:22s} {OP_FORMS[op]}")
@ -1218,8 +1226,7 @@ def main():
stages += [("sweep", lambda: run_sweep(args.seed, ops)), ("edge grid", lambda: run_grid(ops))]
if args.exhaustive_f16:
stages.append(("exhaustive f16", lambda: run_exhaustive(ops)))
if args.programs > 0:
stages.append(("random", lambda: run_suite(args.programs, args.seed, args.max_ops, ops)))
stages.append(("random", lambda: run_suite(args.programs, args.seed, args.max_ops, ops)))
total = {"pass": 0, "mismatch": 0, "skip": 0, "unjudged": Counter(), "ops": Counter(), "events": {}}
first = None
try: