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

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 af25163fb0b49541d02f7a1e90df32dd6f28abd2
Author: Freja Nordsiek <fnordsie at gmail.com>
Date:   Sat Feb 13 20:18:26 2016 +0100

    Added tests for writing and reading back numpy recarrays.
---
 tests/test_write_readback.py | 79 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 2 deletions(-)

diff --git a/tests/test_write_readback.py b/tests/test_write_readback.py
index 71b9a9e..c144d5f 100644
--- a/tests/test_write_readback.py
+++ b/tests/test_write_readback.py
@@ -158,6 +158,45 @@ class TestPythonMatlabFormat(object):
                                   self.options)
         self.assert_equal(out, data)
 
+    def check_numpy_recarray(self, dimensions):
+        # Makes a random structured ndarray of the given type, converts
+        # it to a recarray, writes it and reads it back, and then
+        # compares it.
+        shape = random_numpy_shape(dimensions, \
+            max_structured_ndarray_axis_length)
+        data = random_structured_numpy_array(shape).view(np.recarray).copy()
+        out = self.write_readback(data, random_name(),
+                                  self.options)
+        self.assert_equal(out, data)
+
+    def check_numpy_recarray_empty(self, dimensions):
+        # Makes a random structured ndarray of the given type, converts
+        # it to a recarray, writes it and reads it back, and then
+        # compares it.
+        shape = random_numpy_shape(dimensions, \
+            max_structured_ndarray_axis_length)
+        data = random_structured_numpy_array(shape, (1, 0)).view(np.recarray).copy()
+        out = self.write_readback(data, random_name(),
+                                  self.options)
+        self.assert_equal(out, data)
+
+    def check_numpy_recarray_field_special_char(self, ch):
+        # Makes a random 1d structured ndarray with the character
+        # in one field, converts it to a recarray, writes it and reads
+        # it back, and then compares it.
+        field_names = [random_str_ascii(max_dict_key_length)
+                       for i in range(2)]
+        field_names[1] = field_names[1][0] + ch + field_names[1][1:]
+        if sys.hexversion < 0x03000000:
+            for i in range(len(field_names)):
+                field_names[i] = field_names[i].encode('UTF-8')
+        shape = random_numpy_shape(1, \
+            max_structured_ndarray_axis_length)
+        data = random_structured_numpy_array(shape, names=field_names).view(np.recarray).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.
@@ -355,8 +394,8 @@ class TestPythonMatlabFormat(object):
         # it.
         shape = random_numpy_shape(1, \
             max_structured_ndarray_axis_length)
-        data = random_structured_numpy_array(shape,
-                                             nonascii_fields=True)
+        data = random_structured_numpy_array(shape, \
+            nonascii_fields=True).view(np.recarray).copy()
         out = self.write_readback(data, random_name(),
                                   self.options)
         self.assert_equal(out, data)
@@ -369,6 +408,34 @@ class TestPythonMatlabFormat(object):
     def test_numpy_structured_array_field_forward_slash(self):
         self.check_numpy_structured_array_field_special_char('/')
 
+    def test_numpy_recarray(self):
+        for i in range(1, 4):
+            yield self.check_numpy_recarray, i
+
+    def test_numpy_recarray_empty(self):
+        for i in range(1, 4):
+            yield self.check_numpy_recarray_empty, i
+
+    def test_numpy_recarray_unicode_fields(self):
+        # Makes a random 1d structured ndarray with non-ascii characters
+        # in its fields, converts it to a recarray, writes it and reads
+        # it back, and then compares it.
+        shape = random_numpy_shape(1, \
+            max_structured_ndarray_axis_length)
+        data = random_structured_numpy_array(shape,
+                                             nonascii_fields=True)
+        out = self.write_readback(data, random_name(),
+                                  self.options)
+        self.assert_equal(out, data)
+
+    @raises(NotImplementedError)
+    def test_numpy_recarray_field_null_character(self):
+        self.check_numpy_recarray_field_special_char('\x00')
+
+    @raises(NotImplementedError)
+    def test_numpy_recarray_field_forward_slash(self):
+        self.check_numpy_recarray_field_special_char('/')
+
     def test_python_collection(self):
         for tp in (list, tuple, set, frozenset, collections.deque):
             yield self.check_python_collection, tp
@@ -438,6 +505,10 @@ class TestPythonFormat(TestPythonMatlabFormat):
     def test_numpy_structured_array_field_forward_slash(self):
         self.check_numpy_structured_array_field_special_char('/')
 
+    # Won't throw an exception unlike the parent.
+    def test_numpy_recarray_field_forward_slash(self):
+        self.check_numpy_recarray_field_special_char('/')
+
 
 class TestNoneFormat(TestPythonMatlabFormat):
     def __init__(self):
@@ -463,6 +534,10 @@ class TestNoneFormat(TestPythonMatlabFormat):
     def test_numpy_structured_array_field_forward_slash(self):
         self.check_numpy_structured_array_field_special_char('/')
 
+    # Won't throw an exception unlike the parent.
+    def test_numpy_recarray_field_forward_slash(self):
+        self.check_numpy_recarray_field_special_char('/')
+
     def assert_equal(self, a, b):
         assert_equal_none_format(a, b)
 

-- 
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