[Numpy-svn] r8329 - trunk/numpy/lib/tests

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Apr 12 23:26:38 EDT 2010


Author: charris
Date: 2010-04-12 22:26:38 -0500 (Mon, 12 Apr 2010)
New Revision: 8329

Modified:
   trunk/numpy/lib/tests/test_ufunclike.py
Log:
ENH: Rewrite doctest in test_ufunclike.py as normal nose tests. Remove test of
sign ufunc, it belongs elsewhere.

Modified: trunk/numpy/lib/tests/test_ufunclike.py
===================================================================
--- trunk/numpy/lib/tests/test_ufunclike.py	2010-04-13 00:38:49 UTC (rev 8328)
+++ trunk/numpy/lib/tests/test_ufunclike.py	2010-04-13 03:26:38 UTC (rev 8329)
@@ -1,85 +1,71 @@
-"""
->>> import numpy.core as nx
->>> import numpy.lib.ufunclike as U
+from numpy.testing import *
+import numpy.core as nx
+import numpy.lib.ufunclike as ufl
+import warnings
 
-Test fix:
->>> a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]])
->>> U.fix(a)
-array([[ 1.,  1.,  1.,  1.],
-       [-1., -1., -1., -1.]])
->>> y = nx.zeros(a.shape, float)
->>> U.fix(a, y)
-array([[ 1.,  1.,  1.,  1.],
-       [-1., -1., -1., -1.]])
->>> y
-array([[ 1.,  1.,  1.,  1.],
-       [-1., -1., -1., -1.]])
+class TestUfunclike(TestCase):
 
-Test isposinf, isneginf, sign
->>> a = nx.array([nx.Inf, -nx.Inf, nx.NaN, 0.0, 3.0, -3.0])
->>> U.isposinf(a)
-array([ True, False, False, False, False, False], dtype=bool)
->>> U.isneginf(a)
-array([False,  True, False, False, False, False], dtype=bool)
->>> olderr = nx.seterr(invalid='ignore')
->>> nx.sign(a)
-array([  1.,  -1.,  NaN,   0.,   1.,  -1.])
->>> olderr = nx.seterr(**olderr)
+    def test_isposinf(self):
+        a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
+        out = nx.zeros(a.shape, bool)
+        tgt = nx.array([True, False, False, False, False, False])
 
-Same thing with an output array:
->>> y = nx.zeros(a.shape, bool)
->>> U.isposinf(a, y)
-array([ True, False, False, False, False, False], dtype=bool)
->>> y
-array([ True, False, False, False, False, False], dtype=bool)
->>> U.isneginf(a, y)
-array([False,  True, False, False, False, False], dtype=bool)
->>> y
-array([False,  True, False, False, False, False], dtype=bool)
->>> olderr = nx.seterr(invalid='ignore')
->>> nx.sign(a, y)
-array([ True,  True,  True, False,  True,  True], dtype=bool)
->>> olderr = nx.seterr(**olderr)
->>> y
-array([ True,  True,  True, False,  True,  True], dtype=bool)
+        res = ufl.isposinf(a)
+        assert_equal(res, tgt)
+        res = ufl.isposinf(a, out)
+        assert_equal(res, tgt)
+        assert_equal(out, tgt)
 
-Now log2:
->>> a = nx.array([4.5, 2.3, 6.5])
->>> U.log2(a)
-array([ 2.169925  ,  1.20163386,  2.70043972])
->>> 2**_
-array([ 4.5,  2.3,  6.5])
->>> y = nx.zeros(a.shape, float)
->>> U.log2(a, y)
-array([ 2.169925  ,  1.20163386,  2.70043972])
->>> y
-array([ 2.169925  ,  1.20163386,  2.70043972])
+    def test_isneginf(self):
+        a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
+        out = nx.zeros(a.shape, bool)
+        tgt = nx.array([False, True, False, False, False, False])
 
-"""
+        res = ufl.isneginf(a)
+        assert_equal(res, tgt)
+        res = ufl.isneginf(a, out)
+        assert_equal(res, tgt)
+        assert_equal(out, tgt)
 
-from numpy.testing import *
-import numpy.core as nx
-import numpy.lib.ufunclike as ufl
+    def test_log2(self):
+        a = nx.array([4.5, 2.3, 6.5])
+        out = nx.zeros(a.shape, float)
+        tgt = nx.array([2.169925, 1.20163386, 2.70043972])
+        with warnings.catch_warnings():
+            warnings.filterwarnings("ignore",category=DeprecationWarning)
+            res = ufl.log2(a)
+            assert_almost_equal(res, tgt)
+            res = ufl.log2(a, out)
+            assert_almost_equal(res, tgt)
+            assert_almost_equal(out, tgt)
 
-def test():
-    return rundocs()
+    def test_fix(self):
+        a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]])
+        out = nx.zeros(a.shape, float)
+        tgt = nx.array([[ 1., 1., 1., 1.], [-1., -1., -1., -1.]])
 
-def test_fix_with_subclass():
-    class MyArray(nx.ndarray):
-        def __new__(cls, data, metadata=None):
-            res = nx.array(data, copy=True).view(cls)
-            res.metadata = metadata
-            return res
-        def __array_wrap__(self, obj, context=None):
-            obj.metadata = self.metadata
-            return obj
-            
-    a = nx.array([1.1, -1.1])
-    m = MyArray(a, metadata='foo')
-    f = ufl.fix(m)
-    assert_array_equal(f, nx.array([1,-1]))
-    assert isinstance(f, MyArray)
-    assert_equal(f.metadata, 'foo')
+        res = ufl.fix(a)
+        assert_equal(res, tgt)
+        res = ufl.fix(a, out)
+        assert_equal(res, tgt)
+        assert_equal(out, tgt)
 
+    def test_fix_with_subclass(self):
+        class MyArray(nx.ndarray):
+            def __new__(cls, data, metadata=None):
+                res = nx.array(data, copy=True).view(cls)
+                res.metadata = metadata
+                return res
+            def __array_wrap__(self, obj, context=None):
+                obj.metadata = self.metadata
+                return obj
+
+        a = nx.array([1.1, -1.1])
+        m = MyArray(a, metadata='foo')
+        f = ufl.fix(m)
+        assert_array_equal(f, nx.array([1,-1]))
+        assert isinstance(f, MyArray)
+        assert_equal(f.metadata, 'foo')
+
 if __name__ == "__main__":
     run_module_suite()




More information about the Numpy-svn mailing list