forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5t_types_test.go
189 lines (174 loc) · 5.29 KB
/
h5t_types_test.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hdf5
import "testing"
func TestSimpleDatatypes(t *testing.T) {
// Smoke tests for the simple datatypes
tests := []struct {
v interface{}
hasIllegalPtr bool
}{
{v: int(0), hasIllegalPtr: false},
{v: int8(0), hasIllegalPtr: false},
{v: int16(0), hasIllegalPtr: false},
{v: int32(0), hasIllegalPtr: false},
{v: int64(0), hasIllegalPtr: false},
{v: uint(0), hasIllegalPtr: false},
{v: uint8(0), hasIllegalPtr: false},
{v: uint16(0), hasIllegalPtr: false},
{v: uint32(0), hasIllegalPtr: false},
{v: uint64(0), hasIllegalPtr: false},
{v: float32(0), hasIllegalPtr: false},
{v: float64(0), hasIllegalPtr: false},
{v: string(""), hasIllegalPtr: false},
{v: ([]int)(nil), hasIllegalPtr: false},
{v: [1]int{0}, hasIllegalPtr: false},
{v: bool(true), hasIllegalPtr: false},
{v: (*int)(nil), hasIllegalPtr: false},
{v: (*int8)(nil), hasIllegalPtr: false},
{v: (*int16)(nil), hasIllegalPtr: false},
{v: (*int32)(nil), hasIllegalPtr: false},
{v: (*int64)(nil), hasIllegalPtr: false},
{v: (*uint)(nil), hasIllegalPtr: false},
{v: (*uint8)(nil), hasIllegalPtr: false},
{v: (*uint16)(nil), hasIllegalPtr: false},
{v: (*uint32)(nil), hasIllegalPtr: false},
{v: (*uint64)(nil), hasIllegalPtr: false},
{v: (*float32)(nil), hasIllegalPtr: false},
{v: (*float64)(nil), hasIllegalPtr: false},
{v: (*string)(nil), hasIllegalPtr: true},
{v: (*[]int)(nil), hasIllegalPtr: true},
{v: (*[1]int)(nil), hasIllegalPtr: false},
{v: (*bool)(nil), hasIllegalPtr: false},
{v: (**int)(nil), hasIllegalPtr: true},
{v: (**int8)(nil), hasIllegalPtr: true},
{v: (**int16)(nil), hasIllegalPtr: true},
{v: (**int32)(nil), hasIllegalPtr: true},
{v: (**int64)(nil), hasIllegalPtr: true},
{v: (**uint)(nil), hasIllegalPtr: true},
{v: (**uint8)(nil), hasIllegalPtr: true},
{v: (**uint16)(nil), hasIllegalPtr: true},
{v: (**uint32)(nil), hasIllegalPtr: true},
{v: (**uint64)(nil), hasIllegalPtr: true},
{v: (**float32)(nil), hasIllegalPtr: true},
{v: (**float64)(nil), hasIllegalPtr: true},
{v: (**string)(nil), hasIllegalPtr: true},
{v: (**[]int)(nil), hasIllegalPtr: true},
{v: (**[1]int)(nil), hasIllegalPtr: true},
{v: (**bool)(nil), hasIllegalPtr: true},
}
for _, test := range tests {
dt, err := NewDatatypeFromValue(test.v)
if err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
gotIllegalPtr := dt.hasIllegalGoPointer()
if gotIllegalPtr != test.hasIllegalPtr {
t.Errorf("unexpected illegal pointer status for %T: got:%t want:%t", test.v, gotIllegalPtr, test.hasIllegalPtr)
}
}
}
// Test for array datatypes. Checks that the number of dimensions is correct.
func TestArrayDatatype(t *testing.T) {
tests := map[int]interface{}{
1: [8]int{1, 1, 2, 3, 5, 8, 13},
2: [2][2]int{{1, 2}, {3, 4}},
3: [2][2][2]int{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
}
for dims, val := range tests {
dt, err := NewDatatypeFromValue(val)
if err != nil {
t.Fatal(err)
}
if dt.hasIllegalGoPointer() {
t.Errorf("unexpected illegal pointer for %T", val)
}
adt := ArrayType{*dt}
if adt.NDims() != dims {
t.Errorf("wrong number of dimensions: got %d, want %d", adt.NDims(), dims)
}
}
}
func TestStructDatatype(t *testing.T) {
test := struct {
a int32
b string
c struct {
a int32
b string
}
}{}
// Test that the type can be constructed and that the number of
// members is as expected.
var dtypes []*Datatype
// "Regular" value
dtype, err := NewDatatypeFromValue(test)
if err != nil {
t.Fatal(err)
}
if dtype.hasIllegalGoPointer() {
t.Errorf("unexpected illegal pointer for %T", test)
}
dtypes = append(dtypes, dtype)
// pointer to value
dtype, err = NewDatatypeFromValue(&test)
if err != nil {
t.Fatal(err)
}
if !dtype.hasIllegalGoPointer() {
t.Errorf("expected illegal pointer for %T", &test)
}
dtypes = append(dtypes, dtype)
for _, dtype := range dtypes {
dt := CompoundType{*dtype}
if dt.NMembers() != 3 {
t.Errorf("wrong number of members: got %d, want %d", dt.NMembers(), 3)
}
memberClasses := []TypeClass{
T_INTEGER,
T_STRING,
T_COMPOUND,
}
// Test the member classes, and also test that they can be constructed
for idx, class := range memberClasses {
if dt.MemberClass(idx) != class {
t.Errorf("wrong TypeClass: got %v, want %v", dt.MemberClass(idx), class)
}
_, err := dt.MemberType(idx)
if err != nil {
t.Fatal(err)
}
}
// Test the member names
memberNames := []string{"a", "b", "c"}
for idx, name := range memberNames {
if dt.MemberName(idx) != name {
t.Errorf("wrong name: got %q, want %q", dt.MemberName(idx), name)
}
if dt.MemberIndex(name) != idx {
t.Errorf("wrong index: got %d, want %d", dt.MemberIndex(name), idx)
}
}
// Pack the datatype, otherwise offsets are implementation defined
dt.Pack()
memberOffsets := []int{0, 4, 12}
for idx, offset := range memberOffsets {
if dt.MemberOffset(idx) != offset {
t.Errorf("wrong offset: got %d, want %d", dt.MemberOffset(idx), offset)
}
}
}
}
func TestCloseBehavior(t *testing.T) {
var s struct {
a int
b float64
}
dtype, err := NewDatatypeFromValue(s)
if err != nil {
t.Fatal(err)
}
defer dtype.Close()
}