|
| 1 | +import unittest |
| 2 | +import matplotlib.mlab as mlab |
| 3 | +import numpy |
| 4 | +import StringIO |
| 5 | + |
| 6 | +class TestMlab(unittest.TestCase): |
| 7 | + def test_csv2rec_closefile(self): |
| 8 | + # If passed a file-like object, rec2csv should not close it. |
| 9 | + ra=numpy.rec.array([(123, 1197346475.0137341), (456, 123.456)], |
| 10 | + dtype=[('a', '<i8'), ('b', '<f8')]) |
| 11 | + fh = StringIO.StringIO() |
| 12 | + mlab.rec2csv( ra, fh ) |
| 13 | + self.failIf( fh.closed ) |
| 14 | + |
| 15 | + def test_csv2rec_roundtrip(self): |
| 16 | + # Make sure double-precision floats pass through. |
| 17 | + |
| 18 | + # A bug in numpy (fixed in r4602) meant that numpy scalars |
| 19 | + # lost precision when passing through repr(). csv2rec was |
| 20 | + # affected by this. This test will only pass on numpy >= |
| 21 | + # 1.0.5. |
| 22 | + ra=numpy.rec.array([(123, 1197346475.0137341), (456, 123.456)], |
| 23 | + dtype=[('a', '<i8'), ('b', '<f8')]) |
| 24 | + rec2csv_closes_files = True |
| 25 | + if rec2csv_closes_files: |
| 26 | + fh = 'mlab_unit_tmp.csv' |
| 27 | + else: |
| 28 | + fh = StringIO.StringIO() |
| 29 | + mlab.rec2csv( ra, fh ) |
| 30 | + if not rec2csv_closes_files: |
| 31 | + fh.seek(0) |
| 32 | + ra2 = mlab.csv2rec(fh) |
| 33 | + for name in ra.dtype.names: |
| 34 | + #print name, repr(ra[name]), repr(ra2[name]) |
| 35 | + self.failUnless( numpy.all(ra[name] == ra2[name]) ) # should not fail with numpy 1.0.5 |
| 36 | + |
| 37 | +if __name__=='__main__': |
| 38 | + unittest.main() |
0 commit comments