File: test_npfile.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (105 lines) | stat: -rw-r--r-- 3,626 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from StringIO import StringIO
import os
from tempfile import mkstemp
from numpy.testing import *
import numpy as N

set_package_path()
from io.npfile import npfile, sys_endian_code
restore_path()

class test_npfile(NumpyTestCase):

    def test_init(self):
        fd, fname = mkstemp()
        os.close(fd)
        npf = npfile(fname)
        arr = N.reshape(N.arange(10), (5,2))
        self.assertRaises(IOError, npf.write_array, arr)
        npf.close()
        npf = npfile(fname, 'w')
        npf.write_array(arr)
        npf.rewind()
        self.assertRaises(IOError, npf.read_array,
                          arr.dtype, arr.shape)
        npf.close()
        os.remove(fname)

        npf = npfile(StringIO(), endian='>', order='F')
        assert npf.endian == '>', 'Endian not set correctly'
        assert npf.order == 'F', 'Order not set correctly'
        npf.endian = '<'
        assert npf.endian == '<', 'Endian not set correctly'
        
    def test_parse_endian(self):
        npf = npfile(StringIO())
        swapped_code = sys_endian_code == '<' and '>' or '<'
        assert npf.parse_endian('native') == sys_endian_code
        assert npf.parse_endian('swapped') == swapped_code
        assert npf.parse_endian('l') == '<'
        assert npf.parse_endian('B') == '>'
        assert npf.parse_endian('dtype') == 'dtype'
        self.assertRaises(ValueError, npf.parse_endian, 'nonsense')

    def test_read_write_raw(self):
        npf = npfile(StringIO())
        str = 'test me with this string'
        npf.write_raw(str)
        npf.rewind()
        assert str == npf.read_raw(len(str))
        
    def test_remaining_bytes(self):
        npf = npfile(StringIO())
        assert npf.remaining_bytes() == 0
        npf.write_raw('+' * 10)
        assert npf.remaining_bytes() == 0
        npf.rewind()
        assert npf.remaining_bytes() == 10
        npf.seek(5)
        assert npf.remaining_bytes() == 5

    def test_read_write_array(self):
        npf = npfile(StringIO())
        arr = N.reshape(N.arange(10), (5,2))
        # Arr as read in fortran order
        f_arr = arr.reshape((2,5)).T
        # Arr written in fortran order read in C order
        cf_arr = arr.T.reshape((5,2))
        # Byteswapped array
        bo = arr.dtype.byteorder
        swapped_code = sys_endian_code == '<' and '>' or '<'
        if bo in ['=', sys_endian_code]:
            nbo = swapped_code
        else:
            nbo = sys_endian_code
        bs_arr = arr.newbyteorder(nbo)
        adt = arr.dtype
        shp = arr.shape
        npf.write_array(arr)
        npf.rewind()
        assert_array_equal(npf.read_array(adt), arr.flatten())
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp), arr)
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp, endian=swapped_code),
                           bs_arr)
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp, order='F'),
                           f_arr)
        npf.rewind()
        npf.write_array(arr, order='F')
        npf.rewind()
        assert_array_equal(npf.read_array(adt), arr.flatten('F'))
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp),
                           cf_arr)
        
        npf = npfile(StringIO(), endian='swapped', order='F')
        npf.write_array(arr)
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp), arr)
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp, endian='dtype'), bs_arr)
        npf.rewind()
        assert_array_equal(npf.read_array(adt, shp, order='C'), cf_arr)