[Scipy-svn] r6864 - in trunk/scipy/io/matlab: . tests

scipy-svn@scip... scipy-svn@scip...
Thu Nov 11 18:56:25 CST 2010


Author: matthew.brett@gmail.com
Date: 2010-11-11 18:56:23 -0600 (Thu, 11 Nov 2010)
New Revision: 6864

Modified:
   trunk/scipy/io/matlab/mio5_utils.pyx
   trunk/scipy/io/matlab/tests/test_mio5_utils.py
Log:
TEST: added test for zero bytes char read

Modified: trunk/scipy/io/matlab/mio5_utils.pyx
===================================================================
--- trunk/scipy/io/matlab/mio5_utils.pyx	2010-11-12 00:56:16 UTC (rev 6863)
+++ trunk/scipy/io/matlab/mio5_utils.pyx	2010-11-12 00:56:23 UTC (rev 6864)
@@ -124,7 +124,17 @@
     cdef public int is_global
     cdef size_t nzmax
 
+    def set_dims(self, dims):
+        """ Allow setting of dimensions from python
 
+        This is for constructing headers for tests
+        """
+        self.dims = dims
+        self.n_dims = len(dims)
+        for i, dim in enumerate(dims):
+            self.dims_ptr[i] = <cnp.int32_t>int(dim)
+
+
 cdef class VarReader5:
     cdef public int is_swapped, little_endian
     cdef int struct_as_record
@@ -571,6 +581,7 @@
         '''
         # calculate number of items in array from dims product
         cdef size_t size = 1
+        cdef int i
         for i in range(header.n_dims):
             size *= header.dims_ptr[i]
         return size

Modified: trunk/scipy/io/matlab/tests/test_mio5_utils.py
===================================================================
--- trunk/scipy/io/matlab/tests/test_mio5_utils.py	2010-11-12 00:56:16 UTC (rev 6863)
+++ trunk/scipy/io/matlab/tests/test_mio5_utils.py	2010-11-12 00:56:23 UTC (rev 6864)
@@ -158,5 +158,35 @@
     yield assert_true, el.flags.writeable
 
 
+def test_zero_byte_string():
+    # Tests hack to allow chars of non-zero length, but 0 bytes
+    # make reader-like thing
+    str_io = cStringIO()
+    r = _make_readerlike(str_io, '<')
+    c_reader = m5u.VarReader5(r)
+    tag_dt = np.dtype([('mdtype', 'u4'), ('byte_count', 'u4')])
+    tag = np.zeros((1,), dtype=tag_dt)
+    tag['mdtype'] = mio5p.miINT8
+    tag['byte_count'] = 1
+    hdr = m5u.VarHeader5()
+    # Try when string is 1 length
+    hdr.set_dims([1,])
+    _write_stream(str_io, tag.tostring() + '        ')
+    str_io.seek(0)
+    val = c_reader.read_char(hdr)
+    assert_equal(val, u' ')
+    # Now when string has 0 bytes 1 length
+    tag['byte_count'] = 0
+    _write_stream(str_io, tag.tostring())
+    str_io.seek(0)
+    val = c_reader.read_char(hdr)
+    assert_equal(val, u' ')
+    # Now when string has 0 bytes 4 length
+    str_io.seek(0)
+    hdr.set_dims([4,])
+    val = c_reader.read_char(hdr)
+    assert_array_equal(val, [u' '] * 4)
+
+
 if __name__ == "__main__":
     run_module_suite()



More information about the Scipy-svn mailing list