增加环绕侦察场景适配
This commit is contained in:
@@ -36,6 +36,9 @@ from numpy.testing import (
|
||||
|
||||
class _GenericTest:
|
||||
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def _test_equal(self, a, b):
|
||||
self._assert_func(a, b)
|
||||
|
||||
@@ -82,8 +85,8 @@ class _GenericTest:
|
||||
|
||||
class TestArrayEqual(_GenericTest):
|
||||
|
||||
def setup_method(self):
|
||||
self._assert_func = assert_array_equal
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
assert_array_equal(*args, **kwargs)
|
||||
|
||||
def test_generic_rank1(self):
|
||||
"""Test rank 1 array for all dtypes."""
|
||||
@@ -197,6 +200,40 @@ class TestArrayEqual(_GenericTest):
|
||||
self._test_equal(a, b)
|
||||
self._test_equal(b, a)
|
||||
|
||||
# Also provides test cases for gh-11121
|
||||
def test_masked_scalar(self):
|
||||
# Test masked scalar vs. plain/masked scalar
|
||||
for a_val, b_val, b_masked in itertools.product(
|
||||
[3., np.nan, np.inf],
|
||||
[3., 4., np.nan, np.inf, -np.inf],
|
||||
[False, True],
|
||||
):
|
||||
a = np.ma.MaskedArray(a_val, mask=True)
|
||||
b = np.ma.MaskedArray(b_val, mask=True) if b_masked else np.array(b_val)
|
||||
self._test_equal(a, b)
|
||||
self._test_equal(b, a)
|
||||
|
||||
# Test masked scalar vs. plain array
|
||||
for a_val, b_val in itertools.product(
|
||||
[3., np.nan, -np.inf],
|
||||
itertools.product([3., 4., np.nan, np.inf, -np.inf], repeat=2),
|
||||
):
|
||||
a = np.ma.MaskedArray(a_val, mask=True)
|
||||
b = np.array(b_val)
|
||||
self._test_equal(a, b)
|
||||
self._test_equal(b, a)
|
||||
|
||||
# Test masked scalar vs. masked array
|
||||
for a_val, b_val, b_mask in itertools.product(
|
||||
[3., np.nan, np.inf],
|
||||
itertools.product([3., 4., np.nan, np.inf, -np.inf], repeat=2),
|
||||
itertools.product([False, True], repeat=2),
|
||||
):
|
||||
a = np.ma.MaskedArray(a_val, mask=True)
|
||||
b = np.ma.MaskedArray(b_val, mask=b_mask)
|
||||
self._test_equal(a, b)
|
||||
self._test_equal(b, a)
|
||||
|
||||
def test_subclass_that_overrides_eq(self):
|
||||
# While we cannot guarantee testing functions will always work for
|
||||
# subclasses, the tests should ideally rely only on subclasses having
|
||||
@@ -265,6 +302,8 @@ class TestArrayEqual(_GenericTest):
|
||||
b = np.array([34986, 545676, 439655, 0])
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 4 (25%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [3]: 563766 (ACTUAL), 0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 563766\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -272,6 +311,9 @@ class TestArrayEqual(_GenericTest):
|
||||
|
||||
a = np.array([34986, 545676, 439655.2, 563766])
|
||||
expected_msg = ('Mismatched elements: 2 / 4 (50%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [2]: 439655.2 (ACTUAL), 439655 (DESIRED)\n'
|
||||
' [3]: 563766.0 (ACTUAL), 0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'563766.\n'
|
||||
'Max relative difference among violations: '
|
||||
@@ -350,8 +392,8 @@ class TestBuildErrorMessage:
|
||||
|
||||
class TestEqual(TestArrayEqual):
|
||||
|
||||
def setup_method(self):
|
||||
self._assert_func = assert_equal
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
assert_equal(*args, **kwargs)
|
||||
|
||||
def test_nan_items(self):
|
||||
self._assert_func(np.nan, np.nan)
|
||||
@@ -445,8 +487,8 @@ class TestEqual(TestArrayEqual):
|
||||
|
||||
class TestArrayAlmostEqual(_GenericTest):
|
||||
|
||||
def setup_method(self):
|
||||
self._assert_func = assert_array_almost_equal
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
assert_array_almost_equal(*args, **kwargs)
|
||||
|
||||
def test_closeness(self):
|
||||
# Note that in the course of time we ended up with
|
||||
@@ -466,6 +508,8 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
self._assert_func([1.499999], [0.0], decimal=0)
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 1 (100%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [0]: 1.5 (ACTUAL), 0.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.5\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -474,12 +518,16 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
a = [1.4999999, 0.00003]
|
||||
b = [1.49999991, 0]
|
||||
expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: 3e-05 (ACTUAL), 0.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 3.e-05\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
self._assert_func(a, b, decimal=7)
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: 0.0 (ACTUAL), 3e-05 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 3.e-05\n'
|
||||
'Max relative difference among violations: 1.')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -493,6 +541,8 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
self._assert_func(x, y, decimal=4)
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 1 (100%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [0]: 1234.2222 (ACTUAL), 1234.2223 (DESIRED)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'1.e-04\n'
|
||||
'Max relative difference among violations: '
|
||||
@@ -504,6 +554,9 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
a = [5498.42354, 849.54345, 0.00]
|
||||
b = 5498.42354
|
||||
expected_msg = ('Mismatched elements: 2 / 3 (66.7%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [1]: 849.54345 (ACTUAL), 5498.42354 (DESIRED)\n'
|
||||
' [2]: 0.0 (ACTUAL), 5498.42354 (DESIRED)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'5498.42354\n'
|
||||
'Max relative difference among violations: 1.')
|
||||
@@ -511,6 +564,9 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
self._assert_func(a, b, decimal=9)
|
||||
|
||||
expected_msg = ('Mismatched elements: 2 / 3 (66.7%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [1]: 5498.42354 (ACTUAL), 849.54345 (DESIRED)\n'
|
||||
' [2]: 5498.42354 (ACTUAL), 0.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'5498.42354\n'
|
||||
'Max relative difference among violations: 5.4722099')
|
||||
@@ -519,6 +575,8 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
|
||||
a = [5498.42354, 0.00]
|
||||
expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: 5498.42354 (ACTUAL), 0.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'5498.42354\n'
|
||||
'Max relative difference among violations: inf')
|
||||
@@ -527,6 +585,8 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
|
||||
b = 0
|
||||
expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [0]: 5498.42354 (ACTUAL), 0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'5498.42354\n'
|
||||
'Max relative difference among violations: inf')
|
||||
@@ -555,6 +615,18 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
assert_raises(AssertionError,
|
||||
lambda: self._assert_func(a, b))
|
||||
|
||||
def test_complex_inf(self):
|
||||
a = np.array([np.inf + 1.j, 2. + 1.j, 3. + 1.j])
|
||||
b = a.copy()
|
||||
self._assert_func(a, b)
|
||||
b[1] = 3. + 1.j
|
||||
expected_msg = ('Mismatched elements: 1 / 3 (33.3%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: (2+1j) (ACTUAL), (3+1j) (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.\n')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
self._assert_func(a, b)
|
||||
|
||||
def test_subclass(self):
|
||||
a = np.array([[1., 2.], [3., 4.]])
|
||||
b = np.ma.masked_array([[1., 2.], [0., 4.]],
|
||||
@@ -603,6 +675,8 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
all(z)
|
||||
b = np.array([1., 202]).view(MyArray)
|
||||
expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: 2.0 (ACTUAL), 202.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 200.\n'
|
||||
'Max relative difference among violations: 0.99009')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -629,8 +703,8 @@ class TestArrayAlmostEqual(_GenericTest):
|
||||
|
||||
class TestAlmostEqual(_GenericTest):
|
||||
|
||||
def setup_method(self):
|
||||
self._assert_func = assert_almost_equal
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
assert_almost_equal(*args, **kwargs)
|
||||
|
||||
def test_closeness(self):
|
||||
# Note that in the course of time we ended up with
|
||||
@@ -693,6 +767,10 @@ class TestAlmostEqual(_GenericTest):
|
||||
|
||||
# Test with a different amount of decimal digits
|
||||
expected_msg = ('Mismatched elements: 3 / 3 (100%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [0]: 1.00000000001 (ACTUAL), 1.00000000002 (DESIRED)\n'
|
||||
' [1]: 2.00000000002 (ACTUAL), 2.00000000003 (DESIRED)\n'
|
||||
' [2]: 3.00003 (ACTUAL), 3.00004 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.e-05\n'
|
||||
'Max relative difference among violations: '
|
||||
'3.33328889e-06\n'
|
||||
@@ -708,6 +786,8 @@ class TestAlmostEqual(_GenericTest):
|
||||
# differs. Note that we only check for the formatting of the arrays
|
||||
# themselves.
|
||||
expected_msg = ('Mismatched elements: 1 / 3 (33.3%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [2]: 3.00003 (ACTUAL), 3.00004 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.e-05\n'
|
||||
'Max relative difference among violations: '
|
||||
'3.33328889e-06\n'
|
||||
@@ -720,6 +800,8 @@ class TestAlmostEqual(_GenericTest):
|
||||
x = np.array([np.inf, 0])
|
||||
y = np.array([np.inf, 1])
|
||||
expected_msg = ('Mismatched elements: 1 / 2 (50%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: 0.0 (ACTUAL), 1.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.\n'
|
||||
'Max relative difference among violations: 1.\n'
|
||||
' ACTUAL: array([inf, 0.])\n'
|
||||
@@ -731,6 +813,9 @@ class TestAlmostEqual(_GenericTest):
|
||||
x = np.array([1, 2])
|
||||
y = np.array([0, 0])
|
||||
expected_msg = ('Mismatched elements: 2 / 2 (100%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [0]: 1 (ACTUAL), 0 (DESIRED)\n'
|
||||
' [1]: 2 (ACTUAL), 0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 2\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -742,6 +827,12 @@ class TestAlmostEqual(_GenericTest):
|
||||
x = 2
|
||||
y = np.ones(20)
|
||||
expected_msg = ('Mismatched elements: 20 / 20 (100%)\n'
|
||||
'First 5 mismatches are at indices:\n'
|
||||
' [0]: 2 (ACTUAL), 1.0 (DESIRED)\n'
|
||||
' [1]: 2 (ACTUAL), 1.0 (DESIRED)\n'
|
||||
' [2]: 2 (ACTUAL), 1.0 (DESIRED)\n'
|
||||
' [3]: 2 (ACTUAL), 1.0 (DESIRED)\n'
|
||||
' [4]: 2 (ACTUAL), 1.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.\n'
|
||||
'Max relative difference among violations: 1.')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -750,6 +841,12 @@ class TestAlmostEqual(_GenericTest):
|
||||
y = 2
|
||||
x = np.ones(20)
|
||||
expected_msg = ('Mismatched elements: 20 / 20 (100%)\n'
|
||||
'First 5 mismatches are at indices:\n'
|
||||
' [0]: 1.0 (ACTUAL), 2 (DESIRED)\n'
|
||||
' [1]: 1.0 (ACTUAL), 2 (DESIRED)\n'
|
||||
' [2]: 1.0 (ACTUAL), 2 (DESIRED)\n'
|
||||
' [3]: 1.0 (ACTUAL), 2 (DESIRED)\n'
|
||||
' [4]: 1.0 (ACTUAL), 2 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1.\n'
|
||||
'Max relative difference among violations: 0.5')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -776,8 +873,8 @@ class TestAlmostEqual(_GenericTest):
|
||||
|
||||
class TestApproxEqual:
|
||||
|
||||
def setup_method(self):
|
||||
self._assert_func = assert_approx_equal
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
assert_approx_equal(*args, **kwargs)
|
||||
|
||||
def test_simple_0d_arrays(self):
|
||||
x = np.array(1234.22)
|
||||
@@ -819,8 +916,8 @@ class TestApproxEqual:
|
||||
|
||||
class TestArrayAssertLess:
|
||||
|
||||
def setup_method(self):
|
||||
self._assert_func = assert_array_less
|
||||
def _assert_func(self, *args, **kwargs):
|
||||
assert_array_less(*args, **kwargs)
|
||||
|
||||
def test_simple_arrays(self):
|
||||
x = np.array([1.1, 2.2])
|
||||
@@ -838,6 +935,9 @@ class TestArrayAssertLess:
|
||||
b = np.array([2, 4, 6, 8])
|
||||
|
||||
expected_msg = ('Mismatched elements: 2 / 4 (50%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [2]: 6 (x), 6 (y)\n'
|
||||
' [3]: 20 (x), 8 (y)\n'
|
||||
'Max absolute difference among violations: 12\n'
|
||||
'Max relative difference among violations: 1.5')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -849,6 +949,11 @@ class TestArrayAssertLess:
|
||||
|
||||
self._assert_func(x, y)
|
||||
expected_msg = ('Mismatched elements: 4 / 4 (100%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [0, 0]: 1.2 (x), 1.1 (y)\n'
|
||||
' [0, 1]: 2.3 (x), 2.2 (y)\n'
|
||||
' [1, 0]: 3.4 (x), 3.3 (y)\n'
|
||||
' [1, 1]: 4.5 (x), 4.4 (y)\n'
|
||||
'Max absolute difference among violations: 0.1\n'
|
||||
'Max relative difference among violations: 0.09090909')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -867,6 +972,8 @@ class TestArrayAssertLess:
|
||||
|
||||
y[0, 0, 0] = 0
|
||||
expected_msg = ('Mismatched elements: 1 / 8 (12.5%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [0, 0, 0]: 1.0 (x), 0.0 (y)\n'
|
||||
'Max absolute difference among violations: 1.\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -910,12 +1017,20 @@ class TestArrayAssertLess:
|
||||
y = 999090.54
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 12 (8.33%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1, 1]: 999090.54 (x), 999090.54 (y)\n'
|
||||
'Max absolute difference among violations: 0.\n'
|
||||
'Max relative difference among violations: 0.')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
self._assert_func(x, y)
|
||||
|
||||
expected_msg = ('Mismatched elements: 12 / 12 (100%)\n'
|
||||
'First 5 mismatches are at indices:\n'
|
||||
' [0, 0]: 999090.54 (x), 3.4536 (y)\n'
|
||||
' [0, 1]: 999090.54 (x), 2390.5436 (y)\n'
|
||||
' [0, 2]: 999090.54 (x), 435.54657 (y)\n'
|
||||
' [0, 3]: 999090.54 (x), 324525.4535 (y)\n'
|
||||
' [1, 0]: 999090.54 (x), 5449.54 (y)\n'
|
||||
'Max absolute difference among violations: '
|
||||
'999087.0864\n'
|
||||
'Max relative difference among violations: '
|
||||
@@ -928,12 +1043,17 @@ class TestArrayAssertLess:
|
||||
y = np.array(87654.)
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 3 (33.3%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [0]: 546456.0 (x), 87654.0 (y)\n'
|
||||
'Max absolute difference among violations: 458802.\n'
|
||||
'Max relative difference among violations: 5.23423917')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
self._assert_func(x, y)
|
||||
|
||||
expected_msg = ('Mismatched elements: 2 / 3 (66.7%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [1]: 87654.0 (x), 0.0 (y)\n'
|
||||
' [2]: 87654.0 (x), 15.455 (y)\n'
|
||||
'Max absolute difference among violations: 87654.\n'
|
||||
'Max relative difference among violations: '
|
||||
'5670.5626011')
|
||||
@@ -943,12 +1063,18 @@ class TestArrayAssertLess:
|
||||
y = 0
|
||||
|
||||
expected_msg = ('Mismatched elements: 3 / 3 (100%)\n'
|
||||
'Mismatch at indices:\n'
|
||||
' [0]: 546456.0 (x), 0 (y)\n'
|
||||
' [1]: 0.0 (x), 0 (y)\n'
|
||||
' [2]: 15.455 (x), 0 (y)\n'
|
||||
'Max absolute difference among violations: 546456.\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
self._assert_func(x, y)
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 3 (33.3%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [1]: 0 (x), 0.0 (y)\n'
|
||||
'Max absolute difference among violations: 0.\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -1017,7 +1143,10 @@ class TestArrayAssertLess:
|
||||
with pytest.raises(AssertionError):
|
||||
self._assert_func(x, y.astype(np.float32), strict=True)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings(
|
||||
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
|
||||
".*:DeprecationWarning")
|
||||
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
|
||||
class TestWarns:
|
||||
|
||||
def test_warn(self):
|
||||
@@ -1134,12 +1263,16 @@ class TestAssertAllclose:
|
||||
b = np.array([x, y, x, x])
|
||||
c = np.array([x, y, x, z])
|
||||
expected_msg = ('Mismatched elements: 1 / 4 (25%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [3]: 0.001 (ACTUAL), 0.0 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 0.001\n'
|
||||
'Max relative difference among violations: inf')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
assert_allclose(b, c)
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 4 (25%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [3]: 0.0 (ACTUAL), 0.001 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 0.001\n'
|
||||
'Max relative difference among violations: 1.')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -1155,6 +1288,8 @@ class TestAssertAllclose:
|
||||
b = np.array([1, 1, 1, 2])
|
||||
|
||||
expected_msg = ('Mismatched elements: 1 / 4 (25%)\n'
|
||||
'Mismatch at index:\n'
|
||||
' [3]: 1 (ACTUAL), 2 (DESIRED)\n'
|
||||
'Max absolute difference among violations: 1\n'
|
||||
'Max relative difference among violations: 0.5')
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
@@ -1166,11 +1301,21 @@ class TestAssertAllclose:
|
||||
# Should not raise:
|
||||
assert_allclose(a, b, equal_nan=True)
|
||||
|
||||
a = np.array([complex(np.nan, np.inf)])
|
||||
b = np.array([complex(np.nan, np.inf)])
|
||||
assert_allclose(a, b, equal_nan=True)
|
||||
b = np.array([complex(np.nan, -np.inf)])
|
||||
assert_allclose(a, b, equal_nan=True)
|
||||
|
||||
def test_not_equal_nan(self):
|
||||
a = np.array([np.nan])
|
||||
b = np.array([np.nan])
|
||||
assert_raises(AssertionError, assert_allclose, a, b, equal_nan=False)
|
||||
|
||||
a = np.array([complex(np.nan, np.inf)])
|
||||
b = np.array([complex(np.nan, np.inf)])
|
||||
assert_raises(AssertionError, assert_allclose, a, b, equal_nan=False)
|
||||
|
||||
def test_equal_nan_default(self):
|
||||
# Make sure equal_nan default behavior remains unchanged. (All
|
||||
# of these functions use assert_array_compare under the hood.)
|
||||
@@ -1219,6 +1364,33 @@ class TestAssertAllclose:
|
||||
with pytest.raises(AssertionError):
|
||||
assert_allclose(x, x.astype(np.float32), strict=True)
|
||||
|
||||
def test_infs(self):
|
||||
a = np.array([np.inf])
|
||||
b = np.array([np.inf])
|
||||
assert_allclose(a, b)
|
||||
|
||||
b = np.array([3.])
|
||||
expected_msg = 'inf location mismatch:'
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
assert_allclose(a, b)
|
||||
|
||||
b = np.array([-np.inf])
|
||||
expected_msg = 'inf values mismatch:'
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
assert_allclose(a, b)
|
||||
b = np.array([complex(np.inf, 1.)])
|
||||
expected_msg = 'inf values mismatch:'
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
assert_allclose(a, b)
|
||||
|
||||
a = np.array([complex(np.inf, 1.)])
|
||||
b = np.array([complex(np.inf, 1.)])
|
||||
assert_allclose(a, b)
|
||||
|
||||
b = np.array([complex(np.inf, 2.)])
|
||||
expected_msg = 'inf values mismatch:'
|
||||
with pytest.raises(AssertionError, match=re.escape(expected_msg)):
|
||||
assert_allclose(a, b)
|
||||
|
||||
class TestArrayAlmostEqualNulp:
|
||||
|
||||
@@ -1589,6 +1761,7 @@ def _get_fresh_mod():
|
||||
return my_mod
|
||||
|
||||
|
||||
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
|
||||
def test_clear_and_catch_warnings():
|
||||
# Initial state of module, no warnings
|
||||
my_mod = _get_fresh_mod()
|
||||
@@ -1621,6 +1794,10 @@ def test_clear_and_catch_warnings():
|
||||
assert_warn_len_equal(my_mod, 0)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings(
|
||||
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
|
||||
".*:DeprecationWarning")
|
||||
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
|
||||
def test_suppress_warnings_module():
|
||||
# Initial state of module, no warnings
|
||||
my_mod = _get_fresh_mod()
|
||||
@@ -1667,6 +1844,10 @@ def test_suppress_warnings_module():
|
||||
assert_warn_len_equal(my_mod, 0)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings(
|
||||
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
|
||||
".*:DeprecationWarning")
|
||||
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
|
||||
def test_suppress_warnings_type():
|
||||
# Initial state of module, no warnings
|
||||
my_mod = _get_fresh_mod()
|
||||
@@ -1695,6 +1876,12 @@ def test_suppress_warnings_type():
|
||||
assert_warn_len_equal(my_mod, 0)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings(
|
||||
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
|
||||
".*:DeprecationWarning")
|
||||
@pytest.mark.thread_unsafe(
|
||||
reason="uses deprecated thread-unsafe warnings control utilities"
|
||||
)
|
||||
def test_suppress_warnings_decorate_no_record():
|
||||
sup = suppress_warnings()
|
||||
sup.filter(UserWarning)
|
||||
@@ -1710,6 +1897,12 @@ def test_suppress_warnings_decorate_no_record():
|
||||
assert_equal(len(w), 1)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings(
|
||||
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
|
||||
".*:DeprecationWarning")
|
||||
@pytest.mark.thread_unsafe(
|
||||
reason="uses deprecated thread-unsafe warnings control utilities"
|
||||
)
|
||||
def test_suppress_warnings_record():
|
||||
sup = suppress_warnings()
|
||||
log1 = sup.record()
|
||||
@@ -1747,9 +1940,16 @@ def test_suppress_warnings_record():
|
||||
warnings.warn('Some warning')
|
||||
warnings.warn('Some other warning')
|
||||
assert_equal(len(sup2.log), 1)
|
||||
assert_equal(len(sup.log), 1)
|
||||
# includes a DeprecationWarning for suppress_warnings
|
||||
assert_equal(len(sup.log), 2)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings(
|
||||
"ignore:.*NumPy warning suppression and assertion utilities are deprecated"
|
||||
".*:DeprecationWarning")
|
||||
@pytest.mark.thread_unsafe(
|
||||
reason="uses deprecated thread-unsafe warnings control utilities"
|
||||
)
|
||||
def test_suppress_warnings_forwarding():
|
||||
def warn_other_module():
|
||||
# Apply along axis is implemented in python; stacklevel=2 means
|
||||
@@ -1765,7 +1965,8 @@ def test_suppress_warnings_forwarding():
|
||||
for i in range(2):
|
||||
warnings.warn("Some warning")
|
||||
|
||||
assert_equal(len(sup.log), 2)
|
||||
# includes a DeprecationWarning for suppress_warnings
|
||||
assert_equal(len(sup.log), 3)
|
||||
|
||||
with suppress_warnings() as sup:
|
||||
sup.record()
|
||||
@@ -1774,7 +1975,8 @@ def test_suppress_warnings_forwarding():
|
||||
warnings.warn("Some warning")
|
||||
warnings.warn("Some warning")
|
||||
|
||||
assert_equal(len(sup.log), 2)
|
||||
# includes a DeprecationWarning for suppress_warnings
|
||||
assert_equal(len(sup.log), 3)
|
||||
|
||||
with suppress_warnings() as sup:
|
||||
sup.record()
|
||||
@@ -1784,7 +1986,8 @@ def test_suppress_warnings_forwarding():
|
||||
warnings.warn("Some warning")
|
||||
warn_other_module()
|
||||
|
||||
assert_equal(len(sup.log), 2)
|
||||
# includes a DeprecationWarning for suppress_warnings
|
||||
assert_equal(len(sup.log), 3)
|
||||
|
||||
with suppress_warnings() as sup:
|
||||
sup.record()
|
||||
@@ -1794,7 +1997,8 @@ def test_suppress_warnings_forwarding():
|
||||
warnings.warn("Some other warning")
|
||||
warn_other_module()
|
||||
|
||||
assert_equal(len(sup.log), 2)
|
||||
# includes a DeprecationWarning for suppress_warnings
|
||||
assert_equal(len(sup.log), 3)
|
||||
|
||||
|
||||
def test_tempdir():
|
||||
@@ -1835,6 +2039,7 @@ class my_cacw(clear_and_catch_warnings):
|
||||
class_modules = (sys.modules[__name__],)
|
||||
|
||||
|
||||
@pytest.mark.thread_unsafe(reason="checks global module & deprecated warnings")
|
||||
def test_clear_and_catch_warnings_inherit():
|
||||
# Test can subclass and add default modules
|
||||
my_mod = _get_fresh_mod()
|
||||
@@ -1845,6 +2050,7 @@ def test_clear_and_catch_warnings_inherit():
|
||||
|
||||
|
||||
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
|
||||
@pytest.mark.thread_unsafe(reason="garbage collector is global state")
|
||||
class TestAssertNoGcCycles:
|
||||
""" Test assert_no_gc_cycles """
|
||||
|
||||
|
||||
Reference in New Issue
Block a user