diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/tests/test_numeric.py | 111 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 35 |
2 files changed, 75 insertions, 71 deletions
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 9551855ad..e2d648a3c 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -637,62 +637,65 @@ class TestFloatExceptions: self.assert_raises_fpe(fpeerr, flop, sc1, sc2[()]) self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2[()]) - def test_floating_exceptions(self): + # Test for all real and complex float types + @pytest.mark.parametrize("typecode", np.typecodes["AllFloat"]) + def test_floating_exceptions(self, typecode): # Test basic arithmetic function errors with np.errstate(all='raise'): - # Test for all real and complex float types - for typecode in np.typecodes['AllFloat']: - ftype = np.obj2sctype(typecode) - if np.dtype(ftype).kind == 'f': - # Get some extreme values for the type - fi = np.finfo(ftype) - ft_tiny = fi.machar.tiny - ft_max = fi.max - ft_eps = fi.eps - underflow = 'underflow' - divbyzero = 'divide by zero' - else: - # 'c', complex, corresponding real dtype - rtype = type(ftype(0).real) - fi = np.finfo(rtype) - ft_tiny = ftype(fi.machar.tiny) - ft_max = ftype(fi.max) - ft_eps = ftype(fi.eps) - # The complex types raise different exceptions - underflow = '' - divbyzero = '' - overflow = 'overflow' - invalid = 'invalid' - - # The value of tiny for double double is NaN, so we need to - # pass the assert - if not np.isnan(ft_tiny): - self.assert_raises_fpe(underflow, - lambda a, b: a/b, ft_tiny, ft_max) - self.assert_raises_fpe(underflow, - lambda a, b: a*b, ft_tiny, ft_tiny) - self.assert_raises_fpe(overflow, - lambda a, b: a*b, ft_max, ftype(2)) - self.assert_raises_fpe(overflow, - lambda a, b: a/b, ft_max, ftype(0.5)) - self.assert_raises_fpe(overflow, - lambda a, b: a+b, ft_max, ft_max*ft_eps) - self.assert_raises_fpe(overflow, - lambda a, b: a-b, -ft_max, ft_max*ft_eps) - self.assert_raises_fpe(overflow, - np.power, ftype(2), ftype(2**fi.nexp)) - self.assert_raises_fpe(divbyzero, - lambda a, b: a/b, ftype(1), ftype(0)) - self.assert_raises_fpe(invalid, - lambda a, b: a/b, ftype(np.inf), ftype(np.inf)) - self.assert_raises_fpe(invalid, - lambda a, b: a/b, ftype(0), ftype(0)) - self.assert_raises_fpe(invalid, - lambda a, b: a-b, ftype(np.inf), ftype(np.inf)) - self.assert_raises_fpe(invalid, - lambda a, b: a+b, ftype(np.inf), ftype(-np.inf)) - self.assert_raises_fpe(invalid, - lambda a, b: a*b, ftype(0), ftype(np.inf)) + ftype = np.obj2sctype(typecode) + if np.dtype(ftype).kind == 'f': + # Get some extreme values for the type + fi = np.finfo(ftype) + ft_tiny = fi.machar.tiny + ft_max = fi.max + ft_eps = fi.eps + underflow = 'underflow' + divbyzero = 'divide by zero' + else: + # 'c', complex, corresponding real dtype + rtype = type(ftype(0).real) + fi = np.finfo(rtype) + ft_tiny = ftype(fi.machar.tiny) + ft_max = ftype(fi.max) + ft_eps = ftype(fi.eps) + # The complex types raise different exceptions + underflow = '' + divbyzero = '' + overflow = 'overflow' + invalid = 'invalid' + + # The value of tiny for double double is NaN, so we need to + # pass the assert + if not np.isnan(ft_tiny): + self.assert_raises_fpe(underflow, + lambda a, b: a/b, ft_tiny, ft_max) + self.assert_raises_fpe(underflow, + lambda a, b: a*b, ft_tiny, ft_tiny) + self.assert_raises_fpe(overflow, + lambda a, b: a*b, ft_max, ftype(2)) + self.assert_raises_fpe(overflow, + lambda a, b: a/b, ft_max, ftype(0.5)) + self.assert_raises_fpe(overflow, + lambda a, b: a+b, ft_max, ft_max*ft_eps) + self.assert_raises_fpe(overflow, + lambda a, b: a-b, -ft_max, ft_max*ft_eps) + self.assert_raises_fpe(overflow, + np.power, ftype(2), ftype(2**fi.nexp)) + self.assert_raises_fpe(divbyzero, + lambda a, b: a/b, ftype(1), ftype(0)) + self.assert_raises_fpe( + invalid, lambda a, b: a/b, ftype(np.inf), ftype(np.inf) + ) + self.assert_raises_fpe(invalid, + lambda a, b: a/b, ftype(0), ftype(0)) + self.assert_raises_fpe( + invalid, lambda a, b: a-b, ftype(np.inf), ftype(np.inf) + ) + self.assert_raises_fpe( + invalid, lambda a, b: a+b, ftype(np.inf), ftype(-np.inf) + ) + self.assert_raises_fpe(invalid, + lambda a, b: a*b, ftype(0), ftype(np.inf)) def test_warnings(self): # test warning code path diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 8d7fa1e0a..b88c5e019 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -870,20 +870,20 @@ class TestFloat_power: class TestLog2: - def test_log2_values(self): + @pytest.mark.parametrize('dt', ['f', 'd', 'g']) + def test_log2_values(self, dt): x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - for dt in ['f', 'd', 'g']: - xf = np.array(x, dtype=dt) - yf = np.array(y, dtype=dt) - assert_almost_equal(np.log2(xf), yf) + xf = np.array(x, dtype=dt) + yf = np.array(y, dtype=dt) + assert_almost_equal(np.log2(xf), yf) - def test_log2_ints(self): + @pytest.mark.parametrize("i", range(1, 65)) + def test_log2_ints(self, i): # a good log2 implementation should provide this, # might fail on OS with bad libm - for i in range(1, 65): - v = np.log2(2.**i) - assert_equal(v, float(i), err_msg='at exponent %d' % i) + v = np.log2(2.**i) + assert_equal(v, float(i), err_msg='at exponent %d' % i) def test_log2_special(self): assert_equal(np.log2(1.), 0.) @@ -1069,18 +1069,19 @@ class TestSpecialFloats: assert_raises(FloatingPointError, np.cos, np.float32(-np.inf)) assert_raises(FloatingPointError, np.cos, np.float32(np.inf)) - def test_sqrt_values(self): + @pytest.mark.parametrize('dt', ['f', 'd', 'g']) + def test_sqrt_values(self, dt): with np.errstate(all='ignore'): x = [np.nan, np.nan, np.inf, np.nan, 0.] y = [np.nan, -np.nan, np.inf, -np.inf, 0.] - for dt in ['f', 'd', 'g']: - xf = np.array(x, dtype=dt) - yf = np.array(y, dtype=dt) - assert_equal(np.sqrt(yf), xf) + xf = np.array(x, dtype=dt) + yf = np.array(y, dtype=dt) + assert_equal(np.sqrt(yf), xf) - #with np.errstate(invalid='raise'): - # for dt in ['f', 'd', 'g']: - # assert_raises(FloatingPointError, np.sqrt, np.array(-100., dtype=dt)) + # with np.errstate(invalid='raise'): + # assert_raises( + # FloatingPointError, np.sqrt, np.array(-100., dtype=dt) + # ) def test_abs_values(self): x = [np.nan, np.nan, np.inf, np.inf, 0., 0., 1.0, 1.0] |