forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5a_attribute_test.go
68 lines (57 loc) · 1.56 KB
/
h5a_attribute_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
// 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 (
"os"
"reflect"
"testing"
)
func TestWriteAttribute(t *testing.T) {
DisplayErrors(true)
defer DisplayErrors(false)
defer os.Remove(fname)
f, err := CreateFile(fname, F_ACC_TRUNC)
if err != nil {
t.Fatalf("CreateFile failed: %s\n", err)
}
defer f.Close()
scalar, err := CreateDataspace(S_SCALAR)
if err != nil {
t.Fatalf("CreateDataspace failed: %s\n", err)
}
defer scalar.Close()
dset, err := f.CreateDataset("dset", T_NATIVE_USHORT, scalar)
if err != nil {
t.Fatalf("CreateDataset failed: %s\n", err)
}
defer dset.Close()
strVal := "I am a string attribute"
intVal := 42
fltVal := 1.234
arrVal := [3]byte{128, 0, 255}
attrs := map[string]struct {
Value interface{}
Type reflect.Type
}{
"My string attribute": {&strVal, reflect.TypeOf(strVal)},
"My int attribute": {&intVal, reflect.TypeOf(intVal)},
"My float attribute": {&fltVal, reflect.TypeOf(fltVal)},
"My array attribute": {&arrVal, reflect.TypeOf(arrVal)},
}
for name, v := range attrs {
dtype, err := NewDataTypeFromType(v.Type)
if err != nil {
t.Fatalf("NewDatatypeFromValue failed: %s\n", err)
}
defer dtype.Close()
attr, err := dset.CreateAttribute(name, dtype, scalar)
if err != nil {
t.Fatalf("CreateAttribute failed: %s\n", err)
}
defer attr.Close()
if err := attr.Write(v.Value, dtype); err != nil {
t.Fatalf("Attribute write failed: %s\n", err)
}
}
}