[Numpy-svn] r5384 - trunk/numpy/core/tests

numpy-svn@scip... numpy-svn@scip...
Thu Jul 10 06:50:19 CDT 2008


Author: alan.mcintyre
Date: 2008-07-10 06:50:15 -0500 (Thu, 10 Jul 2008)
New Revision: 5384

Added:
   trunk/numpy/core/tests/test_defchararray.py
Log:
Added test module for chararray.


Added: trunk/numpy/core/tests/test_defchararray.py
===================================================================
--- trunk/numpy/core/tests/test_defchararray.py	2008-07-10 02:48:10 UTC (rev 5383)
+++ trunk/numpy/core/tests/test_defchararray.py	2008-07-10 11:50:15 UTC (rev 5384)
@@ -0,0 +1,89 @@
+from numpy.testing import *
+from numpy.core import *
+import numpy as np
+
+class TestBasic(TestCase):
+    def test_construction(self):
+        A = np.array([['abc', '123'],
+                      ['789', 'xyz']])
+        A1 = A.view(np.chararray)
+        A2 = np.chararray.__new__(np.chararray, A.shape, itemsize=A.itemsize,
+                                  buffer=A)
+        assert all(A1 == A2)
+
+
+class TestWhitespace(TestCase):
+    def setUp(self):
+        self.A = np.array([['abc ', '123  '],
+                           ['789 ', 'xyz ']]).view(np.chararray)
+        self.B = np.array([['abc', '123'],
+                           ['789', 'xyz']]).view(np.chararray)
+
+    def test1(self):
+        assert all(self.A == self.B)
+
+
+class TestOperations(TestCase):
+    def setUp(self):
+        self.A = np.array([['abc', '123'],
+                           ['789', 'xyz']]).view(np.chararray)
+        self.B = np.array([['efg', '456'],
+                           ['051', 'tuv']]).view(np.chararray)
+
+    def test_add(self):
+        AB = np.array([['abcefg', '123456'],
+                       ['789051', 'xyztuv']]).view(np.chararray)
+        assert all(AB == (self.A + self.B))
+
+    def test_radd(self):
+        QA = np.array([['qabc', 'q123'],
+                       ['q789', 'qxyz']]).view(np.chararray)
+        assert all(QA == ('q' + self.A))
+
+    def test_mul(self):
+        A2 = np.array([['abcabc', '123123'],
+                       ['789789', 'xyzxyz']]).view(np.chararray)
+
+        assert all(A2 == (self.A * 2))
+        
+        for ob in [object(), 'qrs']:
+            try:
+                self.A * ob
+            except ValueError:
+                pass
+            else:
+                self.fail("chararray can only be multiplied by integers")
+
+    def test_rmul(self):
+        A2 = np.array([['abcabc', '123123'],
+                       ['789789', 'xyzxyz']]).view(np.chararray)
+
+        assert all(A2 == (2 * self.A))
+        
+        for ob in [object(), 'qrs']:
+            try:
+                ob * self.A
+            except ValueError:
+                pass
+            else:
+                self.fail("chararray can only be multiplied by integers")
+
+    def test_mod(self):
+        pass
+
+    def test_rmod(self):
+        assert ("%s" % self.A) == str(self.A)
+        assert ("%r" % self.A) == repr(self.A)
+        
+        for ob in [42, object()]:
+            try:
+                ob % self.A
+            except TypeError:
+                pass
+            else:
+                self.fail("chararray __rmod__ should fail with " \
+                          "non-string objects")
+
+
+if __name__ == "__main__":
+    run_module_suite()



More information about the Numpy-svn mailing list