diff options
author | Alan McIntyre <alan.mcintyre@local> | 2008-07-10 11:50:15 +0000 |
---|---|---|
committer | Alan McIntyre <alan.mcintyre@local> | 2008-07-10 11:50:15 +0000 |
commit | dcf96aa6436ccd4fdd6acff24cc3216d064ff5be (patch) | |
tree | 2c56eac78734f51dba78a960e0cc3ab443962666 | |
parent | 7895d91a8328e9b3f8bfb7bba1fb7f8302adde51 (diff) | |
download | numpy-dcf96aa6436ccd4fdd6acff24cc3216d064ff5be.tar.gz |
Added test module for chararray.
-rw-r--r-- | numpy/core/tests/test_defchararray.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/numpy/core/tests/test_defchararray.py b/numpy/core/tests/test_defchararray.py new file mode 100644 index 000000000..2e8db9e9a --- /dev/null +++ b/numpy/core/tests/test_defchararray.py @@ -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() |