test_npfile.py :  » Math » SciPy » scipy » scipy » io » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » SciPy 
SciPy » scipy » scipy » io » tests » test_npfile.py
import os
from StringIO import StringIO
from tempfile import mkstemp
from numpy.testing import *
import numpy as np

from scipy.io.npfile import npfile,sys_endian_code

class TestNpFile(TestCase):

    def test_init(self):
        fd, fname = mkstemp()
        os.close(fd)
        npf = npfile(fname)
        arr = np.reshape(np.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 = np.reshape(np.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)

if __name__ == "__main__":
    run_module_suite()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.