Commits

Anonymous committed 04012f7

added test for `--npe-*=raise` options; codes are repeated. there should be
better way to write this.

  • Participants
  • Parent commits e498134

Comments (0)

Files changed (1)

+import unittest
+from nose.plugins import PluginTester
+import numpy
+from nosenumpyseterr import NumpySeterr
+
+DEFAULT_ERR = numpy.seterr()
+
+
+def maketc(npe_arg):
+    class TestPluginNumpySeterr(PluginTester, unittest.TestCase):
+        activate = "--npe-%s=raise" % npe_arg
+        plugins = [NumpySeterr()]
+
+        def tearDown(self):
+            # as current NumpySeterr does not have tear down mechanism,
+            # this code is mandatory
+            numpy.seterr(**DEFAULT_ERR)
+
+        # the following test_${A} functions checkes FloatingPointError is
+        # raised only when --npe-${A}=raise or --npe-all is specified
+
+        def test_divide(self):
+            raised = (
+                "FloatingPointError: divide by zero encountered in divide"
+                in self.output)
+            errmsg = "got:\n%s" % self.output
+            if npe_arg in ("all", "divide"):
+                assert raised, errmsg
+            else:
+                assert not raised, errmsg
+
+        def test_over(self):
+            raised = (
+                "FloatingPointError: overflow encountered in multiply"
+                in self.output)
+            errmsg = "got:\n%s" % self.output
+            if npe_arg in ("all", "over"):
+                assert raised, errmsg
+            else:
+                assert not raised, errmsg
+
+        def test_under(self):
+            raised = (
+                "FloatingPointError: underflow encountered in divide"
+                in self.output)
+            errmsg = "got:\n%s" % self.output
+            if npe_arg in ("all", "under"):
+                assert raised, errmsg
+            else:
+                assert not raised, errmsg
+
+        def test_invalid(self):
+            raised = (
+                "FloatingPointError: invalid value encountered in divide"
+                in self.output)
+            errmsg = "got:\n%s" % self.output
+            if npe_arg in ("all", "invalid"):
+                assert raised, errmsg
+            else:
+                assert not raised, errmsg
+
+        def makeSuite(self):
+
+            class TestDivide(unittest.TestCase):
+
+                def runTest(self):
+                    1.0 / numpy.array([0])
+
+            class TestOver(unittest.TestCase):
+
+                def runTest(self):
+                    finfo = numpy.finfo(float)
+                    numpy.array([finfo.max], dtype=float) * 2
+
+            class TestUnder(unittest.TestCase):
+
+                def runTest(self):
+                    finfo = numpy.finfo(float)
+                    (numpy.array(finfo.tiny, dtype=float) /
+                     numpy.array(finfo.max, dtype=float))
+
+            class TestInvalid(unittest.TestCase):
+
+                def runTest(self):
+                    numpy.zeros(1, dtype=float) / 0.0
+
+            return unittest.TestSuite([
+                TestDivide(), TestOver(), TestUnder(), TestInvalid()])
+
+    TestPluginNumpySeterr.__name__ += npe_arg.capitalize()
+    return TestPluginNumpySeterr
+
+
+TestPluginNumpySeterrAll = maketc("all")
+TestPluginNumpySeterrDivide = maketc("divide")
+TestPluginNumpySeterrOver = maketc("over")
+TestPluginNumpySeterrUnder = maketc("under")
+TestPluginNumpySeterrInvalid = maketc("invalid")
+
+
+if __name__ == '__main__':
+    unittest.main()