8000 TST: Parametrize a few more tests. · numpy/numpy@a254171 · GitHub
[go: up one dir, main page]

Skip to content

Commit a254171

Browse files
committed
TST: Parametrize a few more tests.
Move the for loops outside the test body so I can get more than one error at once. STY: Fix long lines flagged by linter.
1 parent e30548d commit a254171

File tree

2 files changed

+75
-71
lines changed

2 files changed

+75
-71
lines changed

numpy/core/tests/test_numeric.py

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -637,62 +637,65 @@ def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
637637
self.assert_raises_fpe(fpeerr, flop, sc1, sc2[()])
638638
self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2[()])
639639

640-
def test_floating_exceptions(self):
640+
# Test for all real and complex float types
641+
@pytest.mark.parametrize("typecode", np.typecodes["AllFloat"])
642+
def test_floating_exceptions(self, typecode):
641643
# Test basic arithmetic function errors
642644
with np.errstate(all='raise'):
643-
# Test for all real and complex float types
644-
for typecode in np.typecodes['AllFloat']:
645-
ftype = np.obj2sctype(typecode)
646-
if np.dtype(ftype).kind == 'f':
647-
# Get some extreme values for the type
648-
fi = np.finfo(ftype)
649-
ft_tiny = fi.machar.tiny
650-
ft_max = fi.max
651-
ft_eps = fi.eps
652-
underflow = 'underflow'
653-
divbyzero = 'divide by zero'
654-
else:
655-
# 'c', complex, corresponding real dtype
656-
rtype = type(ftype(0).real)
657-
fi = np.finfo(rtype)
658-
ft_tiny = ftype(fi.machar.tiny)
659-
ft_max = ftype(fi.max)
660-
ft_eps = ftype(fi.eps)
661-
# The complex types raise different exceptions
662-
underflow = ''
663-
divbyzero = ''
664-
overflow = 'overflow'
665-
invalid = 'invalid'
666-
667-
# The value of tiny for double double is NaN, so we need to
668-
# pass the assert
669-
if not np.isnan(ft_tiny):
670-
self.assert_raises_fpe(underflow,
671-
lambda a, b: a/b, ft_tiny, ft_max)
672-
self.assert_raises_fpe(underflow,
673-
lambda a, b: a*b, ft_tiny, ft_tiny)
674-
self.assert_raises_fpe(overflow,
675-
lambda a, b: a*b, ft_max, ftype(2))
676-
self.assert_raises_fpe(overflow,
677-
lambda a, b: a/b, ft_max, ftype(0.5))
678-
self.assert_raises_fpe(overflow,
679-
lambda a, b: a+b, ft_max, ft_max*ft_eps)
680-
self.assert_raises_fpe(overflow,
681-
lambda a, b: a-b, -ft_max, ft_max*ft_eps)
682-
self.assert_raises_fpe(overflow,
683-
np.power, ftype(2), ftype(2**fi.nexp))
684-
self.assert_raises_fpe(divbyzero,
685-
lambda a, b: a/b, ftype(1), ftype(0))
686-
self.assert_raises_fpe(invalid,
687-
lambda a, b: a/b, ftype(np.inf), ftype(np.inf))
688-
self.assert_raises_fpe(invalid,
689-
lambda a, b: a/b, ftype(0), ftype(0))
690-
self.assert_raises_fpe(invalid,
691-
lambda a, b: a-b, ftype(np.inf), ftype(np.inf))
692-
self.assert_raises_fpe(invalid,
693-
lambda a, b: a+b, ftype(np.inf), ftype(-np.inf))
694-
self.assert_raises_fpe(invalid,
695-
lambda a, b: a*b, ftype(0), ftype(np.inf))
645+
ftype = np.obj2sctype(typecode)
646+
if np.dtype(ftype).kind == 'f':
647+
# Get some extreme values for the type
648+
fi = np.finfo(ftype)
649+
ft_tiny = fi.machar.tiny
650+
ft_max = fi.max
651+
ft_eps = fi.eps
652+
underflow = 'underflow'
653+
divbyzero = 'divide by zero'
654+
else:
655+
# 'c', complex, corresponding real dtype
656+
rtype = type(ftype(0).real)
657+
fi = np.finfo(rtype)
658+
ft_tiny = ftype(fi.machar.tiny)
659+
ft_max = ftype(fi.max)
660+
ft_eps = ftype(fi.eps)
661+
# The complex types raise different exceptions
662+
underflow = ''
663+
divbyzero = ''
664+
overflow = 'overflow'
665+
invalid = 'invalid'
666+
667+
# The value of tiny for double double is NaN, so we need to
668+
# pass the assert
669+
if not np.isnan(ft_tiny):
670+
self.assert_raises_fpe(underflow,
671+
lambda a, b: a/b, ft_tiny, ft_max)
672+
self.assert_raises_fpe(underflow,
673+
lambda a, b: a*b, ft_tiny, ft_tiny)
674+
self.assert_raises_fpe(overflow,
675+
lambda a, b: a*b, ft_max, ftype(2))
676+
self.assert_raises_fpe(overflow,
677+
lambda a, b: a/b, ft_max, ftype(0.5))
678+
self.assert_raises_fpe(overflow,
679+
lambda a, b: a+b, ft_max, ft_max*ft_eps)
680+
self.assert_raises_fpe(overflow,
681+
lambda a, b: a-b, -ft_max, ft_max*ft_eps)
682+
self.assert_raises_fpe(overflow,
683+
np.power, ftype(2), ftype(2**fi.nexp))
684+
self.assert_raises_fpe(divbyzero,
685+
lambda a, b: a/b, ftype(1), ftype(0))
686+
self.assert_raises_fpe(
687+
invalid, lambda a, b: a/b, ftype(np.inf), ftype(np.inf)
688+
)
689+
self.assert_raises_fpe(invalid,
690+
lambda a, b: a/b, ftype(0), ftype(0))
691+
self.assert_raises_fpe(
692+
invalid, lambda a, b: a-b, ftype(np.inf), ftype(np.inf)
693+
)
694+
self.assert_raises_fpe(
695+
invalid, lambda a, b: a+b, ftype(np.inf), ftype(-np.inf)
696+
)
697+
self.assert_raises_fpe(invalid,
698+
lambda a, b: a*b, ftype(0), ftype(np.inf))
696699

697700
def test_warnings(self):
698701
# test warning code path

numpy/core/tests/test_umath.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -870,20 +870,20 @@ def test_type_conversion(self):
870870

871871

872872
class TestLog2:
873-
def test_log2_values(self):
873+
@pytest.mark.parametrize('dt', ['f', 'd', 'g'])
874+
def test_log2_values(self, dt):
874875
x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
875876
y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
876-
for dt in ['f', 'd', 'g']:
877-
xf = np.array(x, dtype=dt)
878-
yf = np.array(y, dtype=dt)
879-
assert_almost_equal(np.log2(xf), yf)
877+
xf = np.array(x, dtype=dt)
878+
yf = np.array(y, dtype=dt)
879+
assert_almost_equal(np.log2(xf), yf)
880880

881-
def test_log2_ints(self):
881+
@pytest.mark.parametrize("i", range(1, 65))
882+
def test_log2_ints(self, i):
882883
# a good log2 implementation should provide this,
883884
# might fail on OS with bad libm
884-
for i in range(1, 65):
885-
v = np.log2(2.**i)
886-
assert_equal(v, float(i), err_msg='at exponent %d' % i)
885+
v = np.log2(2.**i)
886+
assert_equal(v, float(i), err_msg='at exponent %d' % i)
887887

888888
def test_log2_special(self):
889889
assert_equal(np.log2(1.), 0.)
@@ -1069,18 +1069,19 @@ def test_sincos_values(self):
10691069
assert_raises(FloatingPointError, np.cos, np.float32(-np.inf))
10701070
assert_raises(FloatingPointError, np.cos, np.float32(np.inf))
10711071

1072-
def test_sqrt_values(self):
1072+
@pytest.mark.parametrize('dt', ['f', 'd', 'g'])
1073+
def test_sqrt_values(self, dt):
10731074
with np.errstate(all='ignore'):
10741075
x = [np.nan, np.nan, np.inf, np.nan, 0.]
10751076
y = [np.nan, -np.nan, np.inf, -np.inf, 0.]
1076-
for dt in ['f', 'd', 'g']:
1077-
xf = np.array(x, dtype=dt)
1078-
yf = np.array(y, dtype=dt)
1079-
assert_equal(np.sqrt(yf), xf)
1077+
xf = np.array(x, dtype=dt)
1078+
yf = np.array(y, dtype=dt)
1079+
assert_equal(np.sqrt(yf), xf)
10801080

1081-
#with np.errstate(invalid='raise'):
1082-
# for dt in ['f', 'd', 'g']:
1083-
# assert_raises(FloatingPointError, np.sqrt, np.array(-100., dtype=dt))
1081+
# with np.errstate(invalid='raise'):
1082+
# assert_raises(
1083+
# FloatingPointError, np.sqrt, np.array(-100., dtype=dt)
1084+
# )
10841085

10851086
def test_abs_values(self):
10861087
x = [np.nan, np.nan, np.inf, np.inf, 0., 0., 1.0, 1.0]

0 commit comments

Comments
 (0)
0