Skip to content

Commit 330640e

Browse files
author
abrammer
committed
update tests to check tolerance args and expecing nan in numpy arrays
1 parent bf127a6 commit 330640e

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

testing/python/approx.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,51 @@ def test_numpy_array(self):
344344

345345
def test_numpy_tolerance_args(self):
346346
"""
347-
quick check that numpy rel/abs args are handled correctly
347+
Check that numpy rel/abs args are handled correctly
348348
for comparison against an np.array
349-
- 3.6.4 would approx both actual / expected if np.array
350-
regardless of which value was passed to approx()
351-
Means tolerance could be calculated against bad test return
349+
Check both sides of the operator, hopefully it doesn't impact things.
350+
Test all permutations of where the approx and np.array() can show up
352351
"""
353352
np = pytest.importorskip("numpy")
354-
expected = 100
355-
actual = 99
356-
assert actual != pytest.approx(expected, abs=0.1, rel=0)
357-
assert np.array(actual) != pytest.approx(expected, abs=0.1, rel=0)
358-
assert actual != pytest.approx(np.array(expected), abs=0.1, rel=0)
359-
assert np.array(actual) != pytest.approx(np.array(expected), abs=0.1, rel=0)
360-
361-
assert actual == pytest.approx(expected, abs=0, rel=0.01)
362-
assert np.array(actual) == pytest.approx(expected, abs=0, rel=0.01)
363-
assert actual == pytest.approx(np.array(expected), abs=0, rel=0.01)
364-
assert np.array(actual) == pytest.approx(np.array(expected), abs=0, rel=0.01)
353+
expected = 100.
354+
actual = 99.
355+
abs_diff = expected - actual
356+
rel_diff = (expected - actual) / expected
357+
358+
tests = [
359+
(eq, abs_diff, 0),
360+
(eq, 0, rel_diff),
361+
(ne, 0, rel_diff / 2.), # rel diff fail
362+
(ne, abs_diff / 2., 0), # abs diff fail
363+
]
364+
365+
for op, _abs, _rel in tests:
366+
assert op(np.array(actual), approx(expected, abs=_abs, rel=_rel)) # a, b
367+
assert op(approx(expected, abs=_abs, rel=_rel), np.array(actual)) # b, a
368+
369+
assert op(actual, approx(np.array(expected), abs=_abs, rel=_rel)) # a, b
370+
assert op(approx(np.array(expected), abs=_abs, rel=_rel), actual) # b, a
371+
372+
assert op(np.array(actual), approx(np.array(expected), abs=_abs, rel=_rel))
373+
assert op(approx(np.array(expected), abs=_abs, rel=_rel), np.array(actual))
374+
375+
def test_numpy_expecting_nan(self):
376+
np = pytest.importorskip("numpy")
377+
examples = [
378+
(eq, nan, nan),
379+
(eq, -nan, -nan),
380+
(eq, nan, -nan),
381+
(ne, 0.0, nan),
382+
(ne, inf, nan),
383+
]
384+
for op, a, x in examples:
385+
# Nothing is equal to NaN by default.
386+
assert np.array(a) != approx(x)
387+
assert a != approx(np.array(x))
388+
389+
# If ``nan_ok=True``, then NaN is equal to NaN.
390+
assert op(np.array(a), approx(x, nan_ok=True))
391+
assert op(a, approx(np.array(x), nan_ok=True))
365392

366393
def test_numpy_array_wrong_shape(self):
367394
np = pytest.importorskip("numpy")

0 commit comments

Comments
 (0)