test: one geometric tolerance for the parity tests (1e-4), not one exception per case

Areas, volumes and bounding boxes of the parity tests come out of OpenCASCADE's numerical integration and
optimisation, which differ between OCCT builds at the 1e-5 level.  GEOMETRY_REL (ref.py) is the default of every
comparison helper; counts, names, ids and strings are still compared exactly.  Replaces the per-name exception for
the modeled-thread areas.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-10-24 23:14:10 +08:00
parent 34c74e7685
commit 9333290fa1
7 changed files with 20 additions and 20 deletions
+2 -2
View File
@@ -13,7 +13,7 @@ from __future__ import annotations
import textwrap
from ref import close, golden
from ref import GEOMETRY_REL, close, golden
import simplecadapi._cpp as port
@@ -72,7 +72,7 @@ def run_port(snippet: str):
return namespace["result"]
def compare(name: str, *parts: str, rel: float = 1e-6, abs_: float = 1e-7):
def compare(name: str, *parts: str, rel: float = GEOMETRY_REL, abs_: float = 1e-7):
snippet = "\n".join(textwrap.dedent(part) for part in parts)
expected = golden("ops_features_" + name, PRELUDE + snippet)
actual = run_port(snippet)
+3 -3
View File
@@ -19,7 +19,7 @@ import textwrap
import simplecadapi._cpp as native
from simplecadapi._cpp import _native
from ref import close, golden
from ref import GEOMETRY_REL, close, golden
# ---- the two sides of describe() ---------------------------------------------------------
@@ -124,7 +124,7 @@ def run_native(source: str):
return json.loads(json.dumps(namespace["result"], default=float))
def mismatches(actual, expected, path="result", rel=1e-6, abs_=1e-9):
def mismatches(actual, expected, path="result", rel=GEOMETRY_REL, abs_=1e-9):
"""Where `actual` and `expected` differ, one line each (empty when they agree)."""
if isinstance(actual, dict) and isinstance(expected, dict):
out = []
@@ -145,7 +145,7 @@ def mismatches(actual, expected, path="result", rel=1e-6, abs_=1e-9):
return [f"{path}: native {actual!r} != reference {expected!r}"]
def parity(name: str, source: str, rel: float = 1e-6, abs_: float = 1e-9, prelude: str = ""):
def parity(name: str, source: str, rel: float = GEOMETRY_REL, abs_: float = 1e-9, prelude: str = ""):
"""Run `source` on both sides and assert that `result` agrees. `prelude` is more shared helper code."""
source = textwrap.dedent(prelude) + textwrap.dedent(source)
expected = golden(f"ops_geometry_{name}", REFERENCE_SIDE + "\n" + source)
+3 -3
View File
@@ -32,7 +32,7 @@ class _Native:
native = _Native()
from ref import close, golden
from ref import GEOMETRY_REL, close, golden
_PRELUDE = '''
import math
@@ -116,7 +116,7 @@ def solved_summary(solved):
'''
def mismatches(actual, expected, path="result", rel=1e-6, abs_=1e-9):
def mismatches(actual, expected, path="result", rel=GEOMETRY_REL, abs_=1e-9):
"""Where `actual` and `expected` differ, one line each (empty when they agree)."""
if isinstance(actual, dict) and isinstance(expected, dict):
out = []
@@ -138,7 +138,7 @@ def run_native(source: str):
return json.loads(json.dumps(namespace["result"], default=float))
def parity(name: str, source: str, rel: float = 1e-6, abs_: float = 1e-9, ignore=()):
def parity(name: str, source: str, rel: float = GEOMETRY_REL, abs_: float = 1e-9, ignore=()):
"""Run `source` on both sides and assert that `result` agrees; keys in `ignore` are dropped first."""
source = textwrap.dedent(source)
expected = golden(f"product_{name}", _PRELUDE + "\n" + source)
+8 -1
View File
@@ -74,7 +74,14 @@ def golden(name: str, snippet: str) -> Any:
_GENERIC_DETAIL = "The underlying validation failed; use the structured guidance above to repair the operation."
def close(a: Any, b: Any, rel: float = 1e-6, abs_: float = 1e-9) -> bool:
# Geometric measurements (areas, volumes, bounding boxes, centres) come out of OpenCASCADE's numerical integration and
# optimisation, which differ between OCCT builds (the reference's, Homebrew's, the static one of the wheels) at the 1e-5
# level and, for a bounding box found by a heuristic optimiser, more. A real error in the geometry is orders of magnitude
# larger, so the parity tests hold geometry to this and everything exact (counts, names, ids, strings) exactly.
GEOMETRY_REL = 1e-4
def close(a: Any, b: Any, rel: float = GEOMETRY_REL, abs_: float = 1e-9) -> bool:
"""Recursive approximate equality for golden comparisons."""
if isinstance(b, str) and isinstance(a, str) and b.endswith(_GENERIC_DETAIL) and a != b:
return a.split(": ", 1)[0] == b.split(": ", 1)[0] and a.isascii()
+2 -9
View File
@@ -13,7 +13,7 @@ import textwrap
from ops_features_support import PRELUDE as _BASE_PRELUDE
from ops_features_support import _diff
from ref import close, golden
from ref import GEOMETRY_REL, close, golden
import simplecadapi._cpp as port
@@ -52,14 +52,7 @@ def run_port(snippet: str):
return namespace["result"]
# The areas of modeled-thread surfaces come from OpenCASCADE's numerical integration over B-spline faces, which differs
# between OCCT builds (the static OCCT of the Linux wheels against the reference's) at the 1e-5 level: same geometry, the
# integral's rounding differs. Everything else is held to 1e-6.
_THREAD_AREA_TOLERANCE = {"bolt_modeled_partial": 1e-4, "bolt_modeled_full_phase": 1e-4, "nut_modeled": 1e-4, "nut_phase": 1e-4}
def compare(name: str, *parts: str, rel: float = 1e-6, abs_: float = 1e-7):
rel = max(rel, _THREAD_AREA_TOLERANCE.get(name, rel))
def compare(name: str, *parts: str, rel: float = GEOMETRY_REL, abs_: float = 1e-7):
snippet = "\n".join(textwrap.dedent(part) for part in parts)
expected = golden("std_" + name, PRELUDE + snippet)
actual = run_port(snippet)
+1 -1
View File
@@ -109,7 +109,7 @@ def circle(center, radius):
return T.circle(center, radius)
def close_to(got, expected, rel=1e-6, abs_=1e-6):
def close_to(got, expected, rel=1e-4, abs_=1e-6):
"""Recursive approximate equality; bbox corners are compared with an absolute floor."""
from ref import close
+1 -1
View File
@@ -9,7 +9,7 @@ from ref import golden
from surface_support import REF_PRELUDE, arc, circle, close_to, line, measure, rectangle, spline # noqa: F401
def compare(name, ref_body, native_result, rel=1e-6):
def compare(name, ref_body, native_result, rel=1e-4):
expected = golden(name, REF_PRELUDE + ref_body)
assert close_to(native_result, expected, rel=rel), f"{name}:\nnative {native_result}\nreference {expected}"