summaryrefslogtreecommitdiff
path: root/plugins/code-modernization/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/code-modernization/scripts')
-rw-r--r--plugins/code-modernization/scripts/compare.py153
-rw-r--r--plugins/code-modernization/scripts/tests/test_compare_tolerance.py106
2 files changed, 239 insertions, 20 deletions
diff --git a/plugins/code-modernization/scripts/compare.py b/plugins/code-modernization/scripts/compare.py
index 5bd0c74a..ca3c2469 100644
--- a/plugins/code-modernization/scripts/compare.py
+++ b/plugins/code-modernization/scripts/compare.py
@@ -23,6 +23,16 @@ position of a difference and a variable-length match still compares equal. A cas
differs different; the first difference is recorded (a person may approve it: differs-approved)
missing a file is absent, unreadable or outside the cases folder. Never a pass.
+Numbers that a run legitimately prints a little differently (floating-point results from another
+compiler or math library) can be compared within a declared tolerance, per case or for all cases:
+
+ "tolerance": {"rel": 1e-9, "abs": 0, "why": "last-digit rounding of the exp() in the interest formula"}
+
+The reason is required and is recorded. Only numbers written with a decimal point or an exponent
+may differ, and only within the tolerance; every other byte, and every integer, must match exactly.
+The case is `same`, with the largest relative difference recorded; anything outside the tolerance
+is `differs`. A relative tolerance above 1% is refused: that is a different result, not rounding.
+
Writes EQUIVALENCE.json (default: next to the cases file). Exit 0 only when at least one case
executed, none is missing or differs (approved differences are allowed), the self-check did not
fail and some compared output was not empty; 1 otherwise; 2 for unusable input.
@@ -32,6 +42,7 @@ import bisect
import datetime
import hashlib
import json
+import math
import os
import re
import sys
@@ -39,6 +50,8 @@ import tempfile
MARK = b"\x00<MASK>\x00"
MAX_BYTES = 256 * 1024 * 1024
+MAX_TOLERANT_BYTES = 32 * 1024 * 1024
+NUMBER = re.compile(rb"[-+]?(?:\d+\.\d*|\.\d+|\d+)(?:[eE][-+]?\d+)?")
class InputError(Exception):
@@ -72,6 +85,74 @@ def parse_masks(specs, cid):
return masks
+def parse_tolerance(spec, cid):
+ """{"rel": 1e-9, "abs": 0, "why": "..."} -> a validated dict, or None when there is none."""
+ if spec is None:
+ return None
+ if not isinstance(spec, dict):
+ raise InputError("case %s: 'tolerance' must be an object with rel, abs and why" % cid)
+ try:
+ rel, ab = float(spec.get("rel", 0)), float(spec.get("abs", 0))
+ except (TypeError, ValueError):
+ raise InputError("case %s: the tolerance's rel and abs must be numbers" % cid)
+ if not (math.isfinite(rel) and math.isfinite(ab)) or rel < 0 or ab < 0 or (rel == 0 and ab == 0):
+ raise InputError("case %s: give a positive rel or abs tolerance" % cid)
+ if rel > 0.01:
+ raise InputError("case %s: a relative tolerance above 1%% is a different result, not rounding" % cid)
+ why = clip(spec.get("why"), 300).strip()
+ if not why:
+ raise InputError("case %s: a tolerance needs a 'why' saying what legitimately differs" % cid)
+ return {"rel": rel, "abs": ab, "why": why}
+
+
+def split_numbers(data):
+ """-> [(is_number, bytes, offset)] covering `data` end to end."""
+ parts, pos = [], 0
+ for m in NUMBER.finditer(data):
+ if m.start() > pos:
+ parts.append((False, data[pos:m.start()], pos))
+ parts.append((True, m.group(), m.start()))
+ pos = m.end()
+ if pos < len(data):
+ parts.append((False, data[pos:], pos))
+ return parts
+
+
+def is_float(token):
+ return any(c in token for c in b".eE")
+
+
+def within_tolerance(a, b, tol):
+ """Compare two masked outputs number by number. -> (True, stats) or (False, offset of the first real difference)."""
+ if len(a) > MAX_TOLERANT_BYTES or len(b) > MAX_TOLERANT_BYTES:
+ return False, first_diff(a, b) or 0
+ ta, tb = split_numbers(a), split_numbers(b)
+ if len(ta) != len(tb):
+ return False, first_diff(a, b) or 0
+ differing, worst = 0, 0.0
+ for (na, xa, sa), (nb, xb, _) in zip(ta, tb):
+ if xa == xb:
+ continue
+ if na != nb:
+ return False, sa
+ if not na:
+ return False, sa + (first_diff(xa, xb) or 0)
+ if not (is_float(xa) and is_float(xb)):
+ return False, sa # an integer must match exactly
+ try:
+ x, y = float(xa), float(xb)
+ except ValueError:
+ return False, sa
+ if not (math.isfinite(x) and math.isfinite(y)):
+ return False, sa
+ scale = max(abs(x), abs(y))
+ if abs(x - y) > max(tol["abs"], tol["rel"] * scale):
+ return False, sa
+ differing += 1
+ worst = max(worst, abs(x - y) / scale if scale else 0.0)
+ return True, {"differing": differing, "maxRelativeDifference": worst}
+
+
def mask_spans(data, masks):
"""Merged (start, end) spans of `data` that the masks hide."""
spans, text = [], None
@@ -170,10 +251,11 @@ def rel_to(path, folder):
return path
-def judge(case, index, base, out_dir, allow_outside):
+def judge(case, index, base, out_dir, allow_outside, default_tolerance=None):
"""-> (case record, legacy bytes or None, masks)."""
cid = clip(str(case.get("id") or "C%02d" % (index + 1)), 80)
masks = parse_masks(case.get("mask"), cid)
+ tol = parse_tolerance(case["tolerance"] if "tolerance" in case else default_tolerance, cid)
rec = {"id": cid, "title": clip(case.get("title"), 300), "verdict": "missing", "reason": "",
"legacyPath": "", "newPath": "", "legacySha256": "", "newSha256": "",
"masked": [m[2] for m in masks], "approvedDifference": clip(case.get("approvedDifference"), 500),
@@ -187,7 +269,7 @@ def judge(case, index, base, out_dir, allow_outside):
problems.append("The %s output was not compared: %s (%s)." % (side, why, clip(str(given), 200)))
if problems:
rec["reason"] = " ".join(problems)
- return rec, None, masks
+ return rec, None, masks, tol
legacy, new = data["legacy"], data["new"]
rec["legacySha256"], rec["newSha256"] = hashlib.sha256(legacy).hexdigest(), hashlib.sha256(new).hexdigest()
a, cuts, hid_a = apply_masks(legacy, masks)
@@ -200,7 +282,7 @@ def judge(case, index, base, out_dir, allow_outside):
rec["reason"] = ("Both outputs are empty, so nothing was compared." if rec["empty"] else
"Identical after masking: %d of %d bytes were hidden by %d mask(s)." % (hid_a, len(legacy), len(masks)) if masks else
"Identical, %d bytes." % len(legacy))
- return rec, legacy, masks
+ return rec, legacy, masks, tol
seen, seen_at = context(a, at)
got, _ = context(b, at)
line = a.count(b"\n", 0, at) + 1
@@ -211,22 +293,39 @@ def judge(case, index, base, out_dir, allow_outside):
"legacy" if at >= len(a) else "new", where, "new" if at >= len(a) else "legacy")
else:
rec["reason"] = "The outputs first differ at %s." % where
+ if tol:
+ rec["tolerance"] = dict(tol)
+ ok, info = within_tolerance(a, b, tol)
+ if ok:
+ rec["verdict"] = "same"
+ rec["withinTolerance"] = info
+ rec["reason"] = ("Identical apart from %d number(s) that differ within the declared tolerance "
+ "(relative %g, absolute %g; largest relative difference %.3g). Declared reason: %s"
+ % (info["differing"], tol["rel"], tol["abs"], info["maxRelativeDifference"], clip(tol["why"], 200)))
+ return rec, legacy, masks, tol
+ line = a.count(b"\n", 0, info) + 1
+ rec["reason"] = "A number or text differs beyond the declared tolerance at line %d, byte %d." % (line, original_offset(cuts, info))
if rec["approvedDifference"].strip():
rec["verdict"] = "differs-approved"
rec["reason"] += " A person approved this difference: %s" % clip(rec["approvedDifference"], 200)
else:
rec["verdict"] = "differs"
- return rec, legacy, masks
+ return rec, legacy, masks, tol
+
+
+def differs_under(changed, data, masks, tol):
+ """Would the comparator report `changed` as different from `data`?"""
+ a, b = apply_masks(data, masks)[0], apply_masks(changed, masks)[0]
+ return a != b and not (tol and within_tolerance(a, b, tol)[0])
def self_check(items):
"""Prove the comparator can fail: change one unmasked byte of each legacy output; it must differ."""
tested = 0
- for cid, data, masks in items:
+ for cid, data, masks, tol in items:
if not data:
continue
tested += 1
- base, _, _ = apply_masks(data, masks)
free, pos = [], 0
for s, e in mask_spans(data, masks) + [(len(data), len(data))]:
if s > pos:
@@ -235,17 +334,32 @@ def self_check(items):
if not free:
return False, "case %s: the masks hide every byte, so no difference could ever be seen" % cid
biggest = max(free, key=lambda f: f[1] - f[0])
- for p in (free[0][0], free[-1][1] - 1, (biggest[0] + biggest[1]) // 2):
- changed = data[:p] + bytes([data[p] ^ 1]) + data[p + 1:]
- if apply_masks(changed, masks)[0] != base:
- break
- else:
- return False, "case %s: a one-byte change to the legacy output was not reported as different" % cid
+ flip = lambda p: data[:p] + bytes([data[p] ^ 1]) + data[p + 1:]
+ spots = [free[0][0], free[-1][1] - 1, (biggest[0] + biggest[1]) // 2]
+ caught = any(differs_under(flip(p), data, masks, tol) for p in spots)
+ if tol and caught:
+ # A tolerance must never hide a change to the numbers it covers: flipping the leading digit of a tolerated
+ # float (one that is not masked) has to be reported, or the tolerance is too loose to prove anything.
+ floats = [m.start() for m in list(NUMBER.finditer(data[:1 << 20]))[:200]
+ if is_float(m.group()) and any(a <= m.start() < b for a, b in free)]
+ caught = not floats or any(differs_under(flip(p), data, masks, tol) for p in floats)
+ if not caught:
+ return False, "case %s: a one-byte change to the legacy output was not reported as different%s" % (
+ cid, " (the tolerance may be too loose)" if tol else "")
if not tested:
return None, "not applicable: no executed case had bytes to test"
return True, "each legacy output was compared with a copy changed by one byte and reported as different"
+def totals(records, executed, count):
+ t = {"cases": len(records), "executed": executed, "same": count("same"), "differs": count("differs"),
+ "differsApproved": count("differs-approved"), "missing": count("missing")}
+ tolerant = sum(1 for r in records if r.get("withinTolerance"))
+ if tolerant: # additive: absent unless a declared tolerance was used
+ t["sameWithinTolerance"] = tolerant
+ return t
+
+
def run(cases_path, out_path=None, allow_outside=False):
"""-> the EQUIVALENCE record. Raises InputError for a cases file that cannot be used."""
try:
@@ -261,21 +375,19 @@ def run(cases_path, out_path=None, allow_outside=False):
for i, case in enumerate(spec["cases"]):
if not isinstance(case, dict):
raise InputError("case %d is not an object" % (i + 1))
- rec, data, masks = judge(case, i, base, out_dir, allow_outside)
+ rec, data, masks, tol = judge(case, i, base, out_dir, allow_outside, spec.get("tolerance"))
if rec["id"] in ids:
raise InputError("duplicate case id %r" % rec["id"])
ids.add(rec["id"])
records.append(rec)
if data is not None:
- items.append((rec["id"], data, masks))
+ items.append((rec["id"], data, masks, tol))
passed, detail = self_check(items)
count = lambda v: sum(1 for r in records if r["verdict"] == v)
ends = lambda key: {k: clip(spec.get(key, {}).get(k), 300) for k in ("label", "command")} if isinstance(spec.get(key), dict) else {"label": "", "command": ""}
return {"system": clip(spec.get("system"), 200), "generated": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"legacy": ends("legacy"), "new": ends("new"), "selfCheck": {"passed": passed, "detail": detail},
- "totals": {"cases": len(records), "executed": len(items), "same": count("same"), "differs": count("differs"),
- "differsApproved": count("differs-approved"), "missing": count("missing")},
- "cases": records}
+ "totals": totals(records, len(items), count), "cases": records}
def verdict_of(result):
@@ -336,12 +448,13 @@ def main(argv=None):
if not args.quiet:
t, plain = result["totals"], lambda s: re.sub(r"[\x00-\x1f\x7f-\x9f]", "?", s)
print("equivalence cases executed: %d" % t["executed"])
- print("same %d | differs %d | differs but approved %d | missing %d | of %d cases" %
- (t["same"], t["differs"], t["differsApproved"], t["missing"], t["cases"]))
+ print("same %d%s | differs %d | differs but approved %d | missing %d | of %d cases" %
+ (t["same"], " (%d within a declared tolerance)" % t["sameWithinTolerance"] if t.get("sameWithinTolerance") else "",
+ t["differs"], t["differsApproved"], t["missing"], t["cases"]))
sc = result["selfCheck"]
print("self-check: %s - %s" % ({True: "passed", False: "FAILED", None: "not applicable"}[sc["passed"]], plain(sc["detail"])))
for r in result["cases"]:
- if r["verdict"] != "same":
+ if r["verdict"] != "same" or r.get("withinTolerance"):
print(" %s %s: %s" % (plain(r["id"]), r["verdict"], plain(r["reason"])[:200]))
print(("NOT PROVEN: %s" % problem) if problem else "PROVEN: %d case(s) executed, none differ" % t["executed"])
print("wrote %s" % out)
diff --git a/plugins/code-modernization/scripts/tests/test_compare_tolerance.py b/plugins/code-modernization/scripts/tests/test_compare_tolerance.py
new file mode 100644
index 00000000..5b0841f5
--- /dev/null
+++ b/plugins/code-modernization/scripts/tests/test_compare_tolerance.py
@@ -0,0 +1,106 @@
+"""Tests for the numeric tolerance in scripts/compare.py.
+
+Run: python3 -m unittest discover -s plugins/code-modernization/scripts/tests
+"""
+import json
+import os
+import shutil
+import sys
+import tempfile
+import unittest
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+PLUGIN = os.path.dirname(os.path.dirname(HERE))
+sys.path.insert(0, os.path.join(PLUGIN, "scripts"))
+import compare as cmp # noqa: E402
+
+TOL = {"rel": 1e-9, "abs": 0, "why": "last-digit rounding of exp() between libm versions"}
+
+
+class ToleranceTests(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, self.tmp, True)
+
+ def case(self, name, legacy, new, **extra):
+ for side, data in (("legacy", legacy), ("new", new)):
+ os.makedirs(os.path.join(self.tmp, side), exist_ok=True)
+ with open(os.path.join(self.tmp, side, name), "wb") as fh:
+ fh.write(data)
+ return {"id": name, "legacy": "legacy/" + name, "new": "new/" + name, **extra}
+
+ def run_cases(self, cases, **top):
+ path = os.path.join(self.tmp, "cases.json")
+ with open(path, "w") as fh:
+ json.dump({"system": "s", "cases": cases, **top}, fh)
+ return cmp.run(path, os.path.join(self.tmp, "EQUIVALENCE.json"))
+
+ def test_a_tiny_float_difference_is_same_within_the_declared_tolerance_and_says_so(self):
+ res = self.run_cases([self.case("a", b"total 1234.5678901234\nrate 0.05\n", b"total 1234.5678901239\nrate 0.05\n", tolerance=TOL)])
+ rec = res["cases"][0]
+ self.assertEqual(rec["verdict"], "same")
+ self.assertEqual(rec["withinTolerance"]["differing"], 1)
+ self.assertLess(rec["withinTolerance"]["maxRelativeDifference"], 1e-9)
+ self.assertIn("declared tolerance", rec["reason"])
+ self.assertIn("libm", rec["reason"])
+ self.assertEqual(res["totals"]["sameWithinTolerance"], 1)
+ self.assertIsNone(cmp.verdict_of(res))
+ self.assertTrue(res["selfCheck"]["passed"], res["selfCheck"])
+
+ def test_without_a_tolerance_the_same_outputs_differ_and_no_new_key_appears(self):
+ res = self.run_cases([self.case("a", b"1234.5678901234\n", b"1234.5678901239\n")])
+ self.assertEqual(res["cases"][0]["verdict"], "differs")
+ self.assertNotIn("sameWithinTolerance", res["totals"])
+
+ def test_a_difference_outside_the_tolerance_differs(self):
+ res = self.run_cases([self.case("a", b"total 100.0\n", b"total 100.5\n", tolerance=TOL)])
+ rec = res["cases"][0]
+ self.assertEqual(rec["verdict"], "differs")
+ self.assertIn("beyond the declared tolerance", rec["reason"])
+ self.assertIn("NOT PROVEN" if cmp.verdict_of(res) else "", "NOT PROVEN")
+
+ def test_integers_and_text_must_match_exactly_even_with_a_tolerance(self):
+ loose = {"rel": 0.01, "abs": 0, "why": "test"}
+ for legacy, new in ((b"count 1000000\n", b"count 1000001\n"), (b"name alpha 1.5\n", b"name alphb 1.5\n"),
+ (b"1.5 2.5\n", b"1.5\n"), (b"x 10\n", b"x 10.0\n")):
+ res = self.run_cases([self.case("a", legacy, new, tolerance=loose)])
+ self.assertEqual(res["cases"][0]["verdict"], "differs", (legacy, new))
+
+ def test_exponent_forms_compare_by_value(self):
+ res = self.run_cases([self.case("a", b"v 1.5e3\n", b"v 1500.0000000000002\n", tolerance=TOL)])
+ self.assertEqual(res["cases"][0]["verdict"], "same")
+
+ def test_a_tolerance_needs_a_reason_and_a_sane_size(self):
+ legacy = self.case("a", b"1.0\n", b"1.0\n")
+ for bad in ({"rel": 1e-9}, {"rel": 1e-9, "why": " "}, {"rel": 0.5, "why": "x"}, {"why": "x"}, {"rel": -1, "why": "x"},
+ {"rel": "big", "why": "x"}, {"rel": float("nan"), "why": "x"}, "1e-9"):
+ with self.assertRaises(cmp.InputError, msg=repr(bad)):
+ self.run_cases([dict(legacy, tolerance=bad)])
+
+ def test_a_default_tolerance_applies_to_every_case_and_a_case_can_switch_it_off(self):
+ a = self.case("a", b"1.00000000000\n", b"1.00000000001\n")
+ b = self.case("b", b"1.00000000000\n", b"1.00000000001\n", tolerance=None)
+ res = self.run_cases([a, b], tolerance=TOL)
+ self.assertEqual([r["verdict"] for r in res["cases"]], ["same", "differs"])
+
+ def test_the_self_check_fails_when_the_tolerance_would_hide_any_change(self):
+ # every byte is a tolerated float digit and the tolerance is 1%: a one-byte change to a low digit is invisible,
+ # but the leading-digit change must still be caught, so the check passes here and the outputs still compare
+ res = self.run_cases([self.case("a", b"5.5\n", b"5.5\n", tolerance={"abs": 100, "why": "absurdly loose"})])
+ self.assertFalse(res["selfCheck"]["passed"])
+ self.assertIn("tolerance may be too loose", res["selfCheck"]["detail"])
+ self.assertIn("self-check failed", cmp.verdict_of(res))
+
+ def test_masks_and_tolerance_work_together(self):
+ res = self.run_cases([self.case("a", b"at 2026-09-24 10:00:00 value 2.0000000001\n", b"at 2027-01-01 11:11:11 value 2.0000000002\n",
+ mask=[{"regex": r"\d{4}-\d\d-\d\d \d\d:\d\d:\d\d", "why": "run time"}], tolerance=TOL)])
+ self.assertEqual(res["cases"][0]["verdict"], "same")
+
+ def test_large_outputs_are_not_tolerance_compared(self):
+ big = b"1.0 " * (cmp.MAX_TOLERANT_BYTES // 4 + 10)
+ rec = self.run_cases([self.case("a", big, big[:-3] + b"2.0", tolerance=TOL)])["cases"][0]
+ self.assertEqual(rec["verdict"], "differs")
+
+
+if __name__ == "__main__":
+ unittest.main()