fix: apply numeric tolerance to nested coordinates (#3)
This commit is contained in:
1 parent
dd3f461d80
commit
d9109dda1d
1 file changed
+18
-6
@@ -12,6 +12,23 @@ SECTIONS = {'JUNCTIONS':'junction', 'OUTFALLS':'outfall', 'STORAGE':'storage',
|
||||
'CONDUITS':'conduit', 'PUMPS':'pump', 'WEIRS':'weir'}
|
||||
|
||||
|
||||
def values_match(actual, expected, tolerance):
|
||||
"""Apply the same numeric contract at every nesting level."""
|
||||
if isinstance(expected, bool):
|
||||
return isinstance(actual, bool) and actual == expected
|
||||
if isinstance(expected, (float, int)):
|
||||
return (isinstance(actual, (float, int)) and not isinstance(actual, bool)
|
||||
and math.isfinite(actual) and math.isfinite(expected)
|
||||
and abs(actual - expected) <= tolerance)
|
||||
if isinstance(expected, list):
|
||||
return (isinstance(actual, list) and len(actual) == len(expected)
|
||||
and all(values_match(a, e, tolerance) for a, e in zip(actual, expected)))
|
||||
if isinstance(expected, dict):
|
||||
return (isinstance(actual, dict) and actual.keys() == expected.keys()
|
||||
and all(values_match(actual[k], v, tolerance) for k, v in expected.items()))
|
||||
return type(actual) is type(expected) and actual == expected
|
||||
|
||||
|
||||
def parse_inp(path):
|
||||
raw = Path(path).read_bytes()
|
||||
for encoding in ('utf-8-sig', 'cp949'):
|
||||
@@ -44,12 +61,7 @@ def validate(inp, network, tolerance=1e-6):
|
||||
def compare(entity, field, actual, expected):
|
||||
nonlocal checks
|
||||
checks += 1
|
||||
if isinstance(expected, (float, int)) and not isinstance(expected, bool):
|
||||
ok = (isinstance(actual, (float, int)) and not isinstance(actual, bool)
|
||||
and math.isfinite(actual) and abs(actual - expected) <= tolerance)
|
||||
else:
|
||||
ok = actual == expected
|
||||
if not ok:
|
||||
if not values_match(actual, expected, tolerance):
|
||||
errors.append({'id': entity, 'field': field, 'actual': actual, 'expected': expected})
|
||||
|
||||
def number(tokens, index, default=None):
|
||||
|
||||
Reference in new issue
Block a user