update tests to check tolerance args and expecing nan in numpy arrays

This commit is contained in:
abrammer 2018-07-29 22:47:38 -04:00
parent bf127a63b2
commit 330640eb96
1 changed files with 41 additions and 14 deletions

View File

@ -344,24 +344,51 @@ class TestApprox(object):
def test_numpy_tolerance_args(self): def test_numpy_tolerance_args(self):
""" """
quick check that numpy rel/abs args are handled correctly Check that numpy rel/abs args are handled correctly
for comparison against an np.array for comparison against an np.array
- 3.6.4 would approx both actual / expected if np.array Check both sides of the operator, hopefully it doesn't impact things.
regardless of which value was passed to approx() Test all permutations of where the approx and np.array() can show up
Means tolerance could be calculated against bad test return
""" """
np = pytest.importorskip("numpy") np = pytest.importorskip("numpy")
expected = 100 expected = 100.
actual = 99 actual = 99.
assert actual != pytest.approx(expected, abs=0.1, rel=0) abs_diff = expected - actual
assert np.array(actual) != pytest.approx(expected, abs=0.1, rel=0) rel_diff = (expected - actual) / expected
assert actual != pytest.approx(np.array(expected), abs=0.1, rel=0)
assert np.array(actual) != pytest.approx(np.array(expected), abs=0.1, rel=0)
assert actual == pytest.approx(expected, abs=0, rel=0.01) tests = [
assert np.array(actual) == pytest.approx(expected, abs=0, rel=0.01) (eq, abs_diff, 0),
assert actual == pytest.approx(np.array(expected), abs=0, rel=0.01) (eq, 0, rel_diff),
assert np.array(actual) == pytest.approx(np.array(expected), abs=0, rel=0.01) (ne, 0, rel_diff / 2.), # rel diff fail
(ne, abs_diff / 2., 0), # abs diff fail
]
for op, _abs, _rel in tests:
assert op(np.array(actual), approx(expected, abs=_abs, rel=_rel)) # a, b
assert op(approx(expected, abs=_abs, rel=_rel), np.array(actual)) # b, a
assert op(actual, approx(np.array(expected), abs=_abs, rel=_rel)) # a, b
assert op(approx(np.array(expected), abs=_abs, rel=_rel), actual) # b, a
assert op(np.array(actual), approx(np.array(expected), abs=_abs, rel=_rel))
assert op(approx(np.array(expected), abs=_abs, rel=_rel), np.array(actual))
def test_numpy_expecting_nan(self):
np = pytest.importorskip("numpy")
examples = [
(eq, nan, nan),
(eq, -nan, -nan),
(eq, nan, -nan),
(ne, 0.0, nan),
(ne, inf, nan),
]
for op, a, x in examples:
# Nothing is equal to NaN by default.
assert np.array(a) != approx(x)
assert a != approx(np.array(x))
# If ``nan_ok=True``, then NaN is equal to NaN.
assert op(np.array(a), approx(x, nan_ok=True))
assert op(a, approx(np.array(x), nan_ok=True))
def test_numpy_array_wrong_shape(self): def test_numpy_array_wrong_shape(self):
np = pytest.importorskip("numpy") np = pytest.importorskip("numpy")