summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2022-07-05 11:45:20 -0700
committerSebastian Berg <sebastianb@nvidia.com>2022-10-12 10:41:40 +0200
commitd47b09c047c2128e820f4d4f27f10b3fd7d8b05b (patch)
treee8e433de743424205c6e80b7b21fb6fd3336c0ce /numpy
parentfed11cd2553dc3b9742a1540cd3805c5689c1ec0 (diff)
downloadnumpy-d47b09c047c2128e820f4d4f27f10b3fd7d8b05b.tar.gz
TST: Improve tests for python integer special cases
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/tests/test_nep50_promotions.py52
1 files changed, 43 insertions, 9 deletions
diff --git a/numpy/core/tests/test_nep50_promotions.py b/numpy/core/tests/test_nep50_promotions.py
index 2ee4e8d76..168b67a86 100644
--- a/numpy/core/tests/test_nep50_promotions.py
+++ b/numpy/core/tests/test_nep50_promotions.py
@@ -61,19 +61,53 @@ def test_nep50_examples():
assert res.dtype == np.float64
-def test_nep50_without_warnings():
- # Test that avoid the "warn" method, since that may lead to different
- # code paths in some cases.
- # Set promotion to weak (no warning), the auto-fixture will reset it.
+@pytest.mark.parametrize("dtype", np.typecodes["AllInteger"])
+def test_nep50_weak_integers(dtype):
+ # Avoids warning (different code path for scalars)
np._set_promotion_state("weak")
+ scalar_type = np.dtype(dtype).type
+
+ maxint = int(np.iinfo(dtype).max)
+
with np.errstate(over="warn"):
with pytest.warns(RuntimeWarning):
- res = np.uint8(100) + 200
- assert res.dtype == np.uint8
+ res = scalar_type(100) + maxint
+ assert res.dtype == dtype
- with pytest.warns(RuntimeWarning):
- res = np.float32(1) + 3e100
- assert res.dtype == np.float32
+ # Array operations are not expected to warn, but should give the same
+ # result dtype.
+ res = np.array(100, dtype=dtype) + maxint
+ assert res.dtype == dtype
+
+
+@pytest.mark.parametrize("dtype", np.typecodes["AllFloat"])
+def test_nep50_weak_integers_with_inexact(dtype):
+ # Avoids warning (different code path for scalars)
+ np._set_promotion_state("weak")
+ scalar_type = np.dtype(dtype).type
+
+ too_big_int = int(np.finfo(dtype).max) * 2
+
+ if dtype in "dDG":
+ # These dtypes currently convert to Python float internally, which
+ # raises an OverflowError, while the other dtypes overflow to inf.
+ # NOTE: It may make sense to normalize the behavior!
+ with pytest.raises(OverflowError):
+ scalar_type(1) + too_big_int
+
+ with pytest.raises(OverflowError):
+ np.array(1, dtype=dtype) + too_big_int
+ else:
+ # Otherwise, we overflow to infinity:
+ with pytest.warns(RuntimeWarning):
+ res = scalar_type(1) + too_big_int
+ assert res.dtype == dtype
+ assert res == np.inf
+
+ with pytest.warns(RuntimeWarning):
+ res = np.array(1, dtype=dtype) + too_big_int
+ assert res.dtype == dtype
+ assert res == np.inf
def test_nep50_integer_conversion_errors():