[Numpy-svn] r6317 - branches/fix_float_format/numpy/core/tests

numpy-svn@scip... numpy-svn@scip...
Mon Jan 12 15:22:37 CST 2009


Author: ptvirtan
Date: 2009-01-12 15:22:24 -0600 (Mon, 12 Jan 2009)
New Revision: 6317

Modified:
   branches/fix_float_format/numpy/core/tests/test_multiarray.py
   branches/fix_float_format/numpy/core/tests/test_print.py
Log:
More tests for fromstring, fromfile, tostring, tofile.

Modified: branches/fix_float_format/numpy/core/tests/test_multiarray.py
===================================================================
--- branches/fix_float_format/numpy/core/tests/test_multiarray.py	2009-01-10 01:53:05 UTC (rev 6316)
+++ branches/fix_float_format/numpy/core/tests/test_multiarray.py	2009-01-12 21:22:24 UTC (rev 6317)
@@ -781,11 +781,13 @@
 
 
 class TestIO(object):
+    """Test tofile, fromfile, tostring, and fromstring"""
+    
     def setUp(self):
-        shape = (4,7)
+        shape = (2,4,3)
         rand = np.random.random
         self.x = rand(shape) + rand(shape).astype(np.complex)*1j
-        self.x[:,0] = [nan, inf, -inf, nan]
+        self.x[0,:,1] = [nan, inf, -inf, nan]
         self.dtype = self.x.dtype
         self.filename = tempfile.mktemp()
 
@@ -808,6 +810,30 @@
         y = np.fromfile(self.filename, dtype=self.dtype)
         assert_array_equal(y, self.x.flat)
 
+    def test_roundtrip_binary_str(self):
+        s = self.x.tostring()
+        y = np.fromstring(s, dtype=self.dtype)
+        assert_array_equal(y, self.x.flat)
+
+        s = self.x.tostring('F')
+        y = np.fromstring(s, dtype=self.dtype)
+        assert_array_equal(y, self.x.flatten('F'))
+
+    def test_roundtrip_str(self):
+        x = self.x.real.ravel()
+        s = "@".join(map(str, x))
+        y = np.fromstring(s, sep="@")
+        # NB. str imbues less precision
+        nan_mask = ~np.isfinite(x)
+        assert_array_equal(x[nan_mask], y[nan_mask])
+        assert_array_almost_equal(x[~nan_mask], y[~nan_mask], decimal=5)
+
+    def test_roundtrip_repr(self):
+        x = self.x.real.ravel()
+        s = "@".join(map(repr, x))
+        y = np.fromstring(s, sep="@")
+        assert_array_equal(x, y)
+
     def _check_from(self, s, value, **kw):
         y = np.fromstring(s, **kw)
         assert_array_equal(y, value)
@@ -842,6 +868,7 @@
     def test_counted_string(self):
         self._check_from('1,2,3,4', [1., 2., 3., 4.], count=4, sep=',')
         self._check_from('1,2,3,4', [1., 2., 3.], count=3, sep=',')
+        self._check_from('1,2,3,4', [1., 2., 3., 4.], count=-1, sep=',')
 
     def test_string_with_ws(self):
         self._check_from('1 2  3     4   ', [1, 2, 3, 4], dtype=int, sep=' ')
@@ -857,6 +884,33 @@
     def test_malformed(self):
         self._check_from('1.234 1,234', [1.234, 1.], sep=' ')
 
+    def test_long_sep(self):
+        self._check_from('1_x_3_x_4_x_5', [1,3,4,5], sep='_x_')
+
+    def test_dtype(self):
+        v = np.array([1,2,3,4], dtype=np.int_)
+        self._check_from('1,2,3,4', v, sep=',', dtype=np.int_)
+
+    def test_tofile_sep(self):
+        x = np.array([1.51, 2, 3.51, 4], dtype=float)
+        f = open(self.filename, 'w')
+        x.tofile(f, sep=',')
+        f.close()
+        f = open(self.filename, 'r')
+        s = f.read()
+        f.close()
+        assert_equal(s, '1.51,2.0,3.51,4.0')
+
+    def test_tofile_format(self):
+        x = np.array([1.51, 2, 3.51, 4], dtype=float)
+        f = open(self.filename, 'w')
+        x.tofile(f, sep=',', format='%.2f')
+        f.close()
+        f = open(self.filename, 'r')
+        s = f.read()
+        f.close()
+        assert_equal(s, '1.51,2.00,3.51,4.00')
+
     @in_foreign_locale
     def _run_in_foreign_locale(self, func, fail=False):
         np.testing.dec.knownfailureif(fail)(func)(self)
@@ -868,6 +922,8 @@
         yield self._run_in_foreign_locale, TestIO.test_counted_string
         yield self._run_in_foreign_locale, TestIO.test_ascii
         yield self._run_in_foreign_locale, TestIO.test_malformed
+        yield self._run_in_foreign_locale, TestIO.test_tofile_sep
+        yield self._run_in_foreign_locale, TestIO.test_tofile_format
 
 
 class TestFromBuffer(TestCase):

Modified: branches/fix_float_format/numpy/core/tests/test_print.py
===================================================================
--- branches/fix_float_format/numpy/core/tests/test_print.py	2009-01-10 01:53:05 UTC (rev 6316)
+++ branches/fix_float_format/numpy/core/tests/test_print.py	2009-01-12 21:22:24 UTC (rev 6317)
@@ -43,7 +43,7 @@
                      err_msg='Failed str formatting for type %s' % tp)
 
 def test_nan_inf_float():
-    """ Check formatting.
+    """ Check formatting of nan & inf.
 
         This is only for the str function, and only for simple types.
         The precision of np.float and np.longdouble aren't the same as the
@@ -75,7 +75,7 @@
                      err_msg='Failed str formatting for type %s' % tp)
 
 def test_complex_types():
-    """Check formatting.
+    """Check formatting of complex types.
 
         This is only for the str function, and only for simple types.
         The precision of np.float and np.longdouble aren't the same as the



More information about the Numpy-svn mailing list