~ubuntu-branches/ubuntu/precise/h5py/precise

« back to all changes in this revision

Viewing changes to h5py/tests/types/test_types.py

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2010-03-23 15:38:34 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100323153834-ysfm9nsr8gdnkid3
Tags: 1.3.0-1
* New upstream version.
  - Remove quilt patches.
  - Bump standards version to 3.8.4 (no changes required).
  - Switch to dpkg-source 3.0 (quilt) format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""
 
3
    Test NumPy dtype to HDF5 type conversion.
 
4
"""
 
5
 
 
6
import numpy as np
 
7
from h5py import tests, h5t, h5r
 
8
 
 
9
class TestIntegers(tests.HTest):
 
10
 
 
11
    def test_int(self):
 
12
        """ (Types) Integer dtype to HDF5 literal """
 
13
        htype = h5t.py_create('i')
 
14
        self.assertIsInstance(htype, h5t.TypeIntegerID)
 
15
 
 
16
    def test_int_log(self):
 
17
        """ (Types) Integer dtype to HDF5 logical """
 
18
        htype = h5t.py_create('i', logical=True)
 
19
        self.assertIsInstance(htype, h5t.TypeIntegerID)
 
20
 
 
21
    def test_enum_lit(self):
 
22
        """ (Types) Enum literal is HDF5 integer """
 
23
        dt = h5t.special_dtype(enum=('i', {'a': 1, 'b': 2}))
 
24
        htype = h5t.py_create(dt)
 
25
        self.assertIsInstance(htype, h5t.TypeIntegerID)
 
26
 
 
27
    def test_enum_log(self):
 
28
        """ (Types) Enum logical is HDF5 enum """
 
29
        dt = h5t.special_dtype(enum=('i', {'a': 1, 'b': 2}))
 
30
        htype = h5t.py_create(dt, logical=True)
 
31
        self.assertIsInstance(htype, h5t.TypeEnumID)
 
32
 
 
33
class TestFloats(tests.HTest):
 
34
 
 
35
    def test_float(self):
 
36
        """ (Types) Float dtype to HDF5 literal """
 
37
        htype = h5t.py_create('f')
 
38
        self.assertIsInstance(htype, h5t.TypeFloatID)
 
39
 
 
40
    def test_float_log(self):
 
41
        """ (Types) Float dtype to HDF5 logical """
 
42
        htype = h5t.py_create('f', logical=True)
 
43
        self.assertIsInstance(htype, h5t.TypeFloatID)
 
44
 
 
45
class TestString(tests.HTest):
 
46
 
 
47
    def test_string(self):
 
48
        """ (Types) String dtype to HDF5 literal """
 
49
        htype = h5t.py_create('S10')
 
50
        self.assertIsInstance(htype, h5t.TypeStringID)
 
51
        self.assertEqual(htype.get_size(), 10)
 
52
 
 
53
    def test_string(self):
 
54
        """ (Types) String dtype to HDF5 logical """
 
55
        htype = h5t.py_create('S10', logical=True)
 
56
        self.assertIsInstance(htype, h5t.TypeStringID)
 
57
        self.assertEqual(htype.get_size(), 10)
 
58
 
 
59
    def test_string(self):
 
60
        """ (Types) Length-1 string works OK """
 
61
        htype = h5t.py_create('S1')
 
62
        self.assertIsInstance(htype, h5t.TypeStringID)
 
63
        self.assertEqual(htype.get_size(), 1)
 
64
 
 
65
    def test_vlstring_lit(self):
 
66
        """ (Types) Vlen string literal is Python object pointer """
 
67
        dt = h5t.special_dtype(vlen=str)
 
68
        htype = h5t.py_create(dt)
 
69
        self.assertIsInstance(htype, h5t.TypeOpaqueID)
 
70
        self.assertEqual(htype, h5t.PYTHON_OBJECT)
 
71
 
 
72
    def test_vlstring_log(self):
 
73
        """ (Types) Vlen string logical is null-term HDF5 vlen ASCII string """
 
74
        dt = h5t.special_dtype(vlen=str)
 
75
        htype = h5t.py_create(dt, logical=True)
 
76
        self.assertIsInstance(htype, h5t.TypeStringID)
 
77
        self.assertEqual(htype.is_variable_str(), True)
 
78
        self.assertEqual(htype.get_cset(), h5t.CSET_ASCII)
 
79
        self.assertEqual(htype.get_strpad(), h5t.STR_NULLTERM)
 
80
 
 
81
class TestArray(tests.HTest):
 
82
 
 
83
    def test_array(self):
 
84
        """ (Types) Multidimensional array type """
 
85
        htype = h5t.py_create(('f',(2,2)))
 
86
        self.assertIsInstance(htype, h5t.TypeArrayID)
 
87
        self.assertEqual(htype.get_array_dims(), (2,2))
 
88
 
 
89
    def test_array_dtype(self):
 
90
        """ (Types) Array dtypes using non-tuple shapes """
 
91
        dt1 = np.dtype('f4', (2,))
 
92
        dt2 = np.dtype('f4', [2])
 
93
        dt3 = np.dtype('f4', 2)
 
94
        dt4 = np.dtype('f4', 2.1)
 
95
        ht1 = h5t.py_create(dt1)
 
96
        ht2 = h5t.py_create(dt2)
 
97
        ht3 = h5t.py_create(dt3)
 
98
        ht4 = h5t.py_create(dt4)
 
99
        self.assertEqual(ht1.dtype, dt1)
 
100
        self.assertEqual(ht2.dtype, dt1)
 
101
        self.assertEqual(ht3.dtype, dt1)
 
102
        self.assertEqual(ht4.dtype, dt1)
 
103
 
 
104
class TestRef(tests.HTest):
 
105
 
 
106
    def test_objref(self):
 
107
        """ (Types) Object reference literal is Python object """
 
108
        dt = h5t.special_dtype(ref=h5r.Reference)
 
109
        htype = h5t.py_create(dt)
 
110
        self.assertEqual(htype, h5t.PYTHON_OBJECT)
 
111
 
 
112
    def test_objref_log(self):
 
113
        """ (Types) Object reference logical is HDF5 reference """
 
114
        dt = h5t.special_dtype(ref=h5r.Reference)
 
115
        htype = h5t.py_create(dt, logical=True)
 
116
        self.assertEqual(htype, h5t.STD_REF_OBJ)
 
117
 
 
118
    def test_regref(self):
 
119
        """ (Types) Region reference literal is Python object """
 
120
        dt = h5t.special_dtype(ref=h5r.RegionReference)
 
121
        htype = h5t.py_create(dt)
 
122
        self.assertEqual(htype, h5t.PYTHON_OBJECT)
 
123
 
 
124
    def test_regref_log(self):
 
125
        """ (Types) Region reference logical is HDF5 dset reference """
 
126
        dt = h5t.special_dtype(ref=h5r.RegionReference)
 
127
        htype = h5t.py_create(dt, logical=True)
 
128
        self.assertEqual(htype, h5t.STD_REF_DSETREG)
 
129
 
 
130
class TestComplex(tests.HTest):
 
131
 
 
132
    def test_complex(self):
 
133
        """ (Types) Simple complex creation """
 
134
        htype = h5t.py_create('c8')
 
135
        self.assertIsInstance(htype, h5t.TypeCompoundID)
 
136
 
 
137
class TestCompound(tests.HTest):
 
138
 
 
139
    def test_simple(self):
 
140
        """ (Types) Simple compound type (round-trip) """
 
141
        dt = np.dtype([('a','i'), ('b','f'),('c','f8')])
 
142
        htype = h5t.py_create(dt)
 
143
        self.assertEqual(htype.dtype, dt)
 
144
 
 
145
    def test_recursive(self):
 
146
        """ (Types) Compound type containing compound type (round-trip) """
 
147
        dt1 = np.dtype([('a','i'),('b','f')])
 
148
        dt2 = np.dtype([('a',dt1),('b','f8')])
 
149
        htype = h5t.py_create(dt2)
 
150
        self.assertEqual(htype.dtype, dt2)
 
151
 
 
152
 
 
153
 
 
154
 
 
155
 
 
156
 
 
157
 
 
158
 
 
159