[python-hdf5storage] 06/13: Added tests for writing and reading back numpy chararrays.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Feb 29 08:25:24 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 0.1.12
in repository python-hdf5storage.

commit 9d734967afed6a30991cf67cf3154459a9fc8d62
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sat Feb 13 20:55:10 2016 +0100

    Added tests for writing and reading back numpy chararrays.
---
 tests/asserts.py             |  2 +-
 tests/test_write_readback.py | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/tests/asserts.py b/tests/asserts.py
index 98a4b58..000f97a 100644
--- a/tests/asserts.py
+++ b/tests/asserts.py
@@ -218,7 +218,7 @@ def assert_equal_matlab_format(a, b):
                     c = np.atleast_1d(b)
                     c = c.view(np.dtype('S' \
                         + str(c.shape[-1]*c.dtype.itemsize)))
-                    c = np.uint32(c.view(np.dtype('uint8')))
+                    c = np.uint32(c.view(np.ndarray).view(np.dtype('uint8')))
                     c = c.view(np.dtype('U' + str(c.shape[-1])))
                     c = np.atleast_2d(c)
                     assert a.dtype == c.dtype
diff --git a/tests/test_write_readback.py b/tests/test_write_readback.py
index c144d5f..70b7ad7 100644
--- a/tests/test_write_readback.py
+++ b/tests/test_write_readback.py
@@ -197,6 +197,24 @@ class TestPythonMatlabFormat(object):
                                   self.options)
         self.assert_equal(out, data)
 
+    def check_numpy_chararray(self, dimensions):
+        # Makes a random numpy array of bytes, converts it to a
+        # chararray, writes it and reads it back, and then compares it.
+        shape = random_numpy_shape(dimensions,
+                                   max_array_axis_length)
+        data = random_numpy(shape, 'S').view(np.chararray).copy()
+        out = self.write_readback(data, random_name(),
+                                  self.options)
+        self.assert_equal(out, data)
+
+    def check_numpy_chararray_empty(self, num_chars):
+        # Makes an empty numpy array of the given type, writes it and
+        # reads it back, and then compares it.
+        data = np.array([], 'S' + str(num_chars)).view(np.chararray).copy()
+        out = self.write_readback(data, random_name(),
+                                  self.options)
+        self.assert_equal(out, data)
+
     def check_python_collection(self, tp):
         # Makes a random collection of the specified type, writes it and
         # reads it back, and then compares it.
@@ -436,6 +454,15 @@ class TestPythonMatlabFormat(object):
     def test_numpy_recarray_field_forward_slash(self):
         self.check_numpy_recarray_field_special_char('/')
 
+    def test_numpy_chararray(self):
+        dims = range(1, 4)
+        for dim in dims:
+            yield self.check_numpy_chararray, dim
+
+    def test_numpy_chararray_empty(self):
+        for n in range(1, 10):
+            yield self.check_numpy_chararray_empty, n
+
     def test_python_collection(self):
         for tp in (list, tuple, set, frozenset, collections.deque):
             yield self.check_python_collection, tp

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-hdf5storage.git



More information about the debian-science-commits mailing list