summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2022-04-02 20:28:00 -0700
committerSebastian Berg <sebastian@sipsolutions.net>2022-06-13 09:36:57 -0700
commit84fd4a5a0e064d1f6be7bc0c96663690faa0353b (patch)
tree7a1045985ac6870b27bce2731a04afa1243df121 /numpy
parent341d9885120123675737ab83e69b9642b28aeb17 (diff)
downloadnumpy-84fd4a5a0e064d1f6be7bc0c96663690faa0353b.tar.gz
TST: Fixup tests that cause FPEs during casts
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/tests/test_array_coercion.py25
-rw-r--r--numpy/core/tests/test_half.py6
-rw-r--r--numpy/core/tests/test_numeric.py4
-rw-r--r--numpy/core/tests/test_regression.py18
-rw-r--r--numpy/core/tests/test_ufunc.py5
-rw-r--r--numpy/ma/tests/test_core.py5
6 files changed, 36 insertions, 27 deletions
diff --git a/numpy/core/tests/test_array_coercion.py b/numpy/core/tests/test_array_coercion.py
index b8b8ef187..ed3ef7e67 100644
--- a/numpy/core/tests/test_array_coercion.py
+++ b/numpy/core/tests/test_array_coercion.py
@@ -373,28 +373,29 @@ class TestScalarDiscovery:
assert discovered_dtype.itemsize == dtype.itemsize
@pytest.mark.parametrize("dtype", np.typecodes["Integer"])
- def test_scalar_to_int_coerce_does_not_cast(self, dtype):
+ @pytest.mark.parametrize(["scalar", "error"],
+ [(np.float64(np.nan), ValueError),
+ (np.ulonglong(-1), OverflowError)])
+ def test_scalar_to_int_coerce_does_not_cast(self, dtype, scalar, error):
"""
Signed integers are currently different in that they do not cast other
NumPy scalar, but instead use scalar.__int__(). The hardcoded
exception to this rule is `np.array(scalar, dtype=integer)`.
"""
dtype = np.dtype(dtype)
- invalid_int = np.ulonglong(-1)
- float_nan = np.float64(np.nan)
-
- for scalar in [float_nan, invalid_int]:
- # This is a special case using casting logic and thus not failing:
+ # This is a special case using casting logic. It warns for the NaN
+ # but allows the cast (giving undefined behaviour).
+ with np.errstate(invalid="ignore"):
coerced = np.array(scalar, dtype=dtype)
cast = np.array(scalar).astype(dtype)
- assert_array_equal(coerced, cast)
+ assert_array_equal(coerced, cast)
- # However these fail:
- with pytest.raises((ValueError, OverflowError)):
- np.array([scalar], dtype=dtype)
- with pytest.raises((ValueError, OverflowError)):
- cast[()] = scalar
+ # However these fail:
+ with pytest.raises(error):
+ np.array([scalar], dtype=dtype)
+ with pytest.raises(error):
+ cast[()] = scalar
class TestTimeScalars:
diff --git a/numpy/core/tests/test_half.py b/numpy/core/tests/test_half.py
index 1b6fd21e1..b60f7ba9c 100644
--- a/numpy/core/tests/test_half.py
+++ b/numpy/core/tests/test_half.py
@@ -233,12 +233,14 @@ class TestHalf:
np.inf]
# Check float64->float16 rounding
- b = np.array(a, dtype=float16)
+ with np.errstate(over="ignore"):
+ b = np.array(a, dtype=float16)
assert_equal(b, rounded)
# Check float32->float16 rounding
a = np.array(a, dtype=float32)
- b = np.array(a, dtype=float16)
+ with np.errstate(over="ignore"):
+ b = np.array(a, dtype=float16)
assert_equal(b, rounded)
def test_half_correctness(self):
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py
index 0b03c6576..5b15e29b4 100644
--- a/numpy/core/tests/test_numeric.py
+++ b/numpy/core/tests/test_numeric.py
@@ -2939,7 +2939,9 @@ class TestLikeFuncs:
self.check_like_function(np.full_like, 1, True)
self.check_like_function(np.full_like, 1000, True)
self.check_like_function(np.full_like, 123.456, True)
- self.check_like_function(np.full_like, np.inf, True)
+ # Inf to integer casts cause invalid-value errors: ignore them.
+ with np.errstate(invalid="ignore"):
+ self.check_like_function(np.full_like, np.inf, True)
@pytest.mark.parametrize('likefunc', [np.empty_like, np.full_like,
np.zeros_like, np.ones_like])
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 4388023dc..4538c825d 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -326,20 +326,20 @@ class TestRegression:
assert_raises(ValueError, bfa)
assert_raises(ValueError, bfb)
- def test_nonarray_assignment(self):
+ @pytest.mark.parametrize("index",
+ [np.ones(10, dtype=bool), np.arange(10)],
+ ids=["boolean-arr-index", "integer-arr-index"])
+ def test_nonarray_assignment(self, index):
# See also Issue gh-2870, test for non-array assignment
# and equivalent unsafe casted array assignment
a = np.arange(10)
- b = np.ones(10, dtype=bool)
- r = np.arange(10)
- def assign(a, b, c):
- a[b] = c
+ with pytest.raises(ValueError):
+ a[index] = np.nan
- assert_raises(ValueError, assign, a, b, np.nan)
- a[b] = np.array(np.nan) # but not this.
- assert_raises(ValueError, assign, a, r, np.nan)
- a[r] = np.array(np.nan)
+ with np.errstate(invalid="warn"):
+ with pytest.warns(RuntimeWarning, match="invalid value"):
+ a[index] = np.array(np.nan) # Only warns
def test_unpickle_dtype_with_object(self):
# Implemented in r2840
diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py
index 852044d32..1142d66f5 100644
--- a/numpy/core/tests/test_ufunc.py
+++ b/numpy/core/tests/test_ufunc.py
@@ -620,8 +620,9 @@ class TestUfunc:
atol = max(np.finfo(dtout).tiny, 3e-308)
else:
atol = 3e-308
- # Some test values result in invalid for float16.
- with np.errstate(invalid='ignore'):
+ # Some test values result in invalid for float16
+ # and the cast to it may overflow to inf.
+ with np.errstate(invalid='ignore', over='ignore'):
res = np.true_divide(x, y, dtype=dtout)
if not np.isfinite(res) and tcout == 'e':
continue
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 679ed2090..04540bc70 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -4340,7 +4340,10 @@ class TestMaskedArrayFunctions:
tmp[(xm <= 2).filled(True)] = True
assert_equal(d._mask, tmp)
- ixm = xm.astype(int)
+ with np.errstate(invalid="warn"):
+ # The fill value is 1e20, it cannot be converted to `int`:
+ with pytest.warns(RuntimeWarning, match="invalid value"):
+ ixm = xm.astype(int)
d = where(ixm > 2, ixm, masked)
assert_equal(d, [-9, -9, -9, -9, -9, 4, -9, -9, 10, -9, -9, 3])
assert_equal(d.dtype, ixm.dtype)