diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2010-11-15 21:58:38 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2010-11-16 19:10:14 -0700 |
commit | 91d4145f30d6795ee4b8e41f476f847b496a3f5d (patch) | |
tree | 60437f3b3bf5b56738b3c02505cf73cff8698356 | |
parent | 72a702d22cfcdba0346670defe887801c0e53eaf (diff) | |
download | numpy-91d4145f30d6795ee4b8e41f476f847b496a3f5d.tar.gz |
STY: Cleanup white space and break long lines.
-rw-r--r-- | numpy/core/tests/test_numeric.py | 96 |
1 files changed, 60 insertions, 36 deletions
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 60ea0a94b..fec9895b6 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -250,63 +250,87 @@ class TestSeterr(TestCase): finally: seterr(**err) + class TestFloatExceptions(TestCase): - def assert_raises_fpe(self, strmatch, operation, x, y): + def assert_raises_fpe(self, fpeerr, flop, x, y): + ftype = type(x) try: - operation(x, y) - assert_(False, "Did not raise a floating point %s error - with type %s" % (strmatch, type(x))) + flop(x, y) + assert_(False, + "Type %s did not raise fpe error '%s'." % (ftype, fpeerr)) except FloatingPointError, exc: - assert_(str(exc).find(strmatch) >= 0, - "Raised a floating point error as expected, but not a %s error - with type %s" % (strmatch, type(x))) - - def assert_op_raises_fpe(self, strmatch, operation, sc1, sc2): - """Given an operation and two scalar-typed values, checks that - the operation raises the specified floating point exception. - Tests all variants with 0-d array scalars as well""" - self.assert_raises_fpe(strmatch, operation, sc1, sc2); - self.assert_raises_fpe(strmatch, operation, sc1[()], sc2); - self.assert_raises_fpe(strmatch, operation, sc1, sc2[()]); - self.assert_raises_fpe(strmatch, operation, sc1[()], sc2[()]); + assert_(str(exc).find(fpeerr) >= 0, + "Type %s raised wrong fpe error '%s'." % (ftype, exc)) + + def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2): + """Check that fpe exception is raised. + + Given a floating operation `flop` and two scalar values, check that + the operation raises the floating point exception specified by + `fpeerr`. Tests all variants with 0-d array scalars as well. + + """ + self.assert_raises_fpe(fpeerr, flop, sc1, sc2); + self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2); + self.assert_raises_fpe(fpeerr, flop, sc1, sc2[()]); + self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2[()]); def test_floating_exceptions(self): """Test basic arithmetic function errors""" oldsettings = np.seterr(all='raise') try: - for typecode in np.typecodes['AllFloat']: # Test for all real and complex float types + # Test for all real and complex float types + for typecode in np.typecodes['AllFloat']: ftype = np.obj2sctype(typecode) - # Get some extreme values for the type if np.dtype(ftype).kind == 'f': + # Get some extreme values for the type fi = np.finfo(ftype) ft_tiny = fi.tiny ft_max = fi.max ft_eps = fi.eps - underflow_str = 'underflow' - divbyzero_str = 'divide by zero' - else: # 'c', complex - rtype = type(ftype(0).real) # corresponding real dtype + 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.tiny) ft_max = ftype(fi.max) ft_eps = ftype(fi.eps) - # The complex types end up raising different exceptions - underflow_str = '' - divbyzero_str = '' - - self.assert_op_raises_fpe(underflow_str, lambda a,b:a/b, ft_tiny, ft_max) - self.assert_op_raises_fpe(underflow_str, lambda a,b:a*b, ft_tiny, ft_tiny) - self.assert_op_raises_fpe('overflow', lambda a,b:a*b, ft_max, ftype(2)) - self.assert_op_raises_fpe('overflow', lambda a,b:a/b, ft_max, ftype(0.5)) - self.assert_op_raises_fpe('overflow', lambda a,b:a+b, ft_max, ft_max*ft_eps) - self.assert_op_raises_fpe('overflow', lambda a,b:a-b, -ft_max, ft_max*ft_eps) - self.assert_op_raises_fpe(divbyzero_str, lambda a,b:a/b, ftype(1), ftype(0)) - self.assert_op_raises_fpe('invalid', lambda a,b:a/b, ftype(0), ftype(0)) - self.assert_op_raises_fpe('invalid', lambda a,b:a-b, ftype(np.inf), ftype(np.inf)) - self.assert_op_raises_fpe('invalid', lambda a,b:a+b, ftype(np.inf), ftype(-np.inf)) - self.assert_op_raises_fpe('invalid', lambda a,b:a*b, ftype(0), ftype(np.inf)) - self.assert_op_raises_fpe('overflow', np.power, ftype(2), ftype(2**fi.nexp)) + # The complex types raise different exceptions + underflow = '' + divbyzero = '' + overflow = 'overflow' + invalid = 'invalid' + + 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(divbyzero, + lambda a,b:a/b, ftype(1), ftype(0)) + 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)) + self.assert_raises_fpe(overflow, + np.power, ftype(2), ftype(2**fi.nexp)) finally: np.seterr(**oldsettings) + class TestFromiter(TestCase): def makegen(self): for x in xrange(24): |