3
Test NumPy dtype to HDF5 type conversion.
7
from h5py import tests, h5t, h5r
9
class TestIntegers(tests.HTest):
12
""" (Types) Integer dtype to HDF5 literal """
13
htype = h5t.py_create('i')
14
self.assertIsInstance(htype, h5t.TypeIntegerID)
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)
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)
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)
33
class TestFloats(tests.HTest):
36
""" (Types) Float dtype to HDF5 literal """
37
htype = h5t.py_create('f')
38
self.assertIsInstance(htype, h5t.TypeFloatID)
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)
45
class TestString(tests.HTest):
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)
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)
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)
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)
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)
81
class TestArray(tests.HTest):
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))
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)
104
class TestRef(tests.HTest):
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)
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)
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)
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)
130
class TestComplex(tests.HTest):
132
def test_complex(self):
133
""" (Types) Simple complex creation """
134
htype = h5t.py_create('c8')
135
self.assertIsInstance(htype, h5t.TypeCompoundID)
137
class TestCompound(tests.HTest):
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)
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)