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

numpy-svn@scip... numpy-svn@scip...
Tue Dec 30 14:36:53 CST 2008


Author: ptvirtan
Date: 2008-12-30 14:36:07 -0600 (Tue, 30 Dec 2008)
New Revision: 6277

Modified:
   branches/fix_float_format/numpy/core/tests/test_multiarray.py
   branches/fix_float_format/numpy/core/tests/test_print.py
Log:
Improve fromfile/fromstring test coverage, and add tests to check locale-independency

Modified: branches/fix_float_format/numpy/core/tests/test_multiarray.py
===================================================================
--- branches/fix_float_format/numpy/core/tests/test_multiarray.py	2008-12-30 07:10:04 UTC (rev 6276)
+++ branches/fix_float_format/numpy/core/tests/test_multiarray.py	2008-12-30 20:36:07 UTC (rev 6277)
@@ -1,9 +1,12 @@
 import tempfile
 import sys
+import os
 import numpy as np
 from numpy.testing import *
 from numpy.core import *
 
+from test_print import in_foreign_locale
+
 class TestFlags(TestCase):
     def setUp(self):
         self.a = arange(10)
@@ -113,41 +116,6 @@
         d2 = dtype('f8')
         assert_equal(d2, dtype(float64))
 
-
-class TestFromstring(TestCase):
-    def test_binary(self):
-        a = fromstring('\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@',dtype='<f4')
-        assert_array_equal(a, array([1,2,3,4]))
-
-    def test_string(self):
-        a = fromstring('1,2,3,4', sep=',')
-        assert_array_equal(a, [1., 2., 3., 4.])
-
-    def test_counted_string(self):
-        a = fromstring('1,2,3,4', count=4, sep=',')
-        assert_array_equal(a, [1., 2., 3., 4.])
-        a = fromstring('1,2,3,4', count=3, sep=',')
-        assert_array_equal(a, [1., 2., 3.])
-
-    def test_string_with_ws(self):
-        a = fromstring('1 2  3     4   ', dtype=int, sep=' ')
-        assert_array_equal(a, [1, 2, 3, 4])
-
-    def test_counted_string_with_ws(self):
-        a = fromstring('1 2  3     4   ', count=3, dtype=int, sep=' ')
-        assert_array_equal(a, [1, 2, 3])
-
-    def test_ascii(self):
-        a = fromstring('1 , 2 , 3 , 4', sep=',')
-        b = fromstring('1,2,3,4', dtype=float, sep=',')
-        assert_array_equal(a, [1.,2.,3.,4.])
-        assert_array_equal(a,b)
-
-    def test_malformed(self):
-        a = fromstring('1.234 1,234', sep=' ')
-        assert_array_equal(a, [1.234, 1.])
-
-
 class TestZeroRank(TestCase):
     def setUp(self):
         self.d = array(0), array('x', object)
@@ -812,43 +780,96 @@
         assert_array_equal(x[1][idx],np.sort(x[1]))
 
 
-class TestFromToFile(TestCase):
+class TestIO(object):
     def setUp(self):
         shape = (4,7)
         rand = np.random.random
-
         self.x = rand(shape) + rand(shape).astype(np.complex)*1j
+        self.x[:,0] = [nan, inf, -inf, nan]
         self.dtype = self.x.dtype
+        self.filename = tempfile.mktemp()
 
-    def test_file(self):
-        # Test disabled on Windows, since the tempfile does not flush
-        # properly.  The test ensures that both filenames and file
-        # objects are accepted in tofile and fromfile, so as long as
-        # it runs on at least one platform, we should be ok.
-        if not sys.platform.startswith('win'):
-            tmp_file = tempfile.NamedTemporaryFile('wb',
-                                                   prefix='numpy_tofromfile')
-            self.x.tofile(tmp_file.file)
-            tmp_file.flush()
-            y = np.fromfile(tmp_file.name,dtype=self.dtype)
-            assert_array_equal(y,self.x.flat)
+    def tearDown(self):
+        if os.path.isfile(self.filename):
+            os.unlink(self.filename)
 
-    def test_filename(self):
-        filename = tempfile.mktemp()
-        f = open(filename,'wb')
+    def test_roundtrip_file(self):
+        f = open(self.filename, 'wb')
         self.x.tofile(f)
         f.close()
-        y = np.fromfile(filename,dtype=self.dtype)
-        assert_array_equal(y,self.x.flat)
+        # NB. doesn't work with flush+seek, due to use of C stdio
+        f = open(self.filename, 'rb')
+        y = np.fromfile(f, dtype=self.dtype)
+        f.close()
+        assert_array_equal(y, self.x.flat)
 
-    def test_malformed(self):
-        filename = tempfile.mktemp()
-        f = open(filename,'w')
-        f.write("1.234 1,234")
+    def test_roundtrip_filename(self):
+        self.x.tofile(self.filename)
+        y = np.fromfile(self.filename, dtype=self.dtype)
+        assert_array_equal(y, self.x.flat)
+
+    def _check_from(self, s, value, **kw):
+        y = np.fromstring(s, **kw)
+        assert_array_equal(y, value)
+
+        f = open(self.filename, 'wb')
+        f.write(s)
         f.close()
-        y = np.fromfile(filename, sep=' ')
-        assert_array_equal(y, [1.234, 1.])
+        y = np.fromfile(self.filename, **kw)
+        assert_array_equal(y, value)
 
+    def test_nan(self):
+        self._check_from("nan +nan -nan NaN nan(foo) +NaN(BAR) -NAN(q_u_u_x_)",
+                         [nan, nan, nan, nan, nan, nan, nan],
+                         sep=' ')
+
+    def test_inf(self):
+        self._check_from("inf +inf -inf infinity -Infinity iNfInItY -inF",
+                         [inf, inf, -inf, inf, -inf, inf, -inf], sep=' ')
+
+    def test_numbers(self):
+        self._check_from("1.234 -1.234 .3 .3e55 -123133.1231e+133",
+                         [1.234, -1.234, .3, .3e55, -123133.1231e+133], sep=' ')
+
+    def test_binary(self):
+        self._check_from('\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@',
+                         array([1,2,3,4]),
+                         dtype='<f4')
+
+    def test_string(self):
+        self._check_from('1,2,3,4', [1., 2., 3., 4.], sep=',')
+
+    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=',')
+
+    def test_string_with_ws(self):
+        self._check_from('1 2  3     4   ', [1, 2, 3, 4], dtype=int, sep=' ')
+
+    def test_counted_string_with_ws(self):
+        self._check_from('1 2  3     4   ', [1,2,3], count=3, dtype=int,
+                         sep=' ')
+
+    def test_ascii(self):
+        self._check_from('1 , 2 , 3 , 4', [1.,2.,3.,4.], sep=',')
+        self._check_from('1,2,3,4', [1.,2.,3.,4.], dtype=float, sep=',')
+
+    def test_malformed(self):
+        self._check_from('1.234 1,234', [1.234, 1.], sep=' ')
+
+    @in_foreign_locale
+    def _run_in_foreign_locale(self, func, fail=False):
+        np.testing.dec.knownfailureif(fail)(func)(self)
+
+    def test_locale(self):
+        yield self._run_in_foreign_locale, TestIO.test_numbers
+        yield self._run_in_foreign_locale, TestIO.test_nan
+        yield self._run_in_foreign_locale, TestIO.test_inf
+        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
+
+
 class TestFromBuffer(TestCase):
     def tst_basic(self,buffer,expected,kwargs):
         assert_array_equal(np.frombuffer(buffer,**kwargs),expected)

Modified: branches/fix_float_format/numpy/core/tests/test_print.py
===================================================================
--- branches/fix_float_format/numpy/core/tests/test_print.py	2008-12-30 07:10:04 UTC (rev 6276)
+++ branches/fix_float_format/numpy/core/tests/test_print.py	2008-12-30 20:36:07 UTC (rev 6277)
@@ -1,5 +1,6 @@
 import numpy as np
 from numpy.testing import *
+import nose
 
 import locale
 import sys
@@ -150,54 +151,39 @@
     for t in [np.complex64, np.cdouble, np.clongdouble] :
         yield check_complex_type_print, t
 
-# Locale tests: scalar types formatting should be independant of the locale
-def has_french_locale():
-    curloc = locale.getlocale(locale.LC_NUMERIC)
-    try:
-        if not sys.platform == 'win32':
-            locale.setlocale(locale.LC_NUMERIC, 'fr_FR')
-        else:
-            locale.setlocale(locale.LC_NUMERIC, 'FRENCH')
-
-        st = True
-    except:
-        st = False
-    finally:
-        locale.setlocale(locale.LC_NUMERIC, locale=curloc)
-
-    return st
-
-def _test_locale_independance(tp):
+# Locale tests: scalar types formatting should be independent of the locale
+def in_foreign_locale(func):
     # XXX: How to query locale on a given system ?
 
     # French is one language where the decimal is ',' not '.', and should be
     # relatively common on many systems
-    curloc = locale.getlocale(locale.LC_NUMERIC)
-    try:
-        if not sys.platform == 'win32':
-            locale.setlocale(locale.LC_NUMERIC, 'fr_FR')
-        else:
-            locale.setlocale(locale.LC_NUMERIC, 'FRENCH')
+    def wrapper(*args, **kwargs):
+        curloc = locale.getlocale(locale.LC_NUMERIC)
+        try:
+            try:
+                if not sys.platform == 'win32':
+                    locale.setlocale(locale.LC_NUMERIC, 'fr_FR')
+                else:
+                    locale.setlocale(locale.LC_NUMERIC, 'FRENCH')
+            except locale.Error:
+                raise nose.SkipTest("Skipping locale test, because "
+                                    "French locale not found")
+            return func(*args, **kwargs)
+        finally:
+            locale.setlocale(locale.LC_NUMERIC, locale=curloc)
+    return nose.tools.make_decorator(func)(wrapper)
 
-        assert_equal(str(tp(1.2)), str(float(1.2)),
-                     err_msg='Failed locale test for type %s' % tp)
-    finally:
-        locale.setlocale(locale.LC_NUMERIC, locale=curloc)
-
-@np.testing.dec.skipif(not has_french_locale(),
-                       "Skipping locale test, French locale not found")
+@in_foreign_locale
 def test_locale_single():
-    return _test_locale_independance(np.float32)
+    assert_equal(str(np.float32(1.2)), str(float(1.2)))
 
-@np.testing.dec.skipif(not has_french_locale(),
-                       "Skipping locale test, French locale not found")
+@in_foreign_locale
 def test_locale_double():
-    return _test_locale_independance(np.double)
+    assert_equal(str(np.double(1.2)), str(float(1.2)))
 
-@np.testing.dec.skipif(not has_french_locale(),
-                       "Skipping locale test, French locale not found")
+@in_foreign_locale
 def test_locale_longdouble():
-    return _test_locale_independance(np.longdouble)
+    assert_equal(str(np.longdouble(1.2)), str(float(1.2)))
 
 if __name__ == "__main__":
     run_module_suite()



More information about the Numpy-svn mailing list