forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5f_file_test.go
137 lines (120 loc) · 3.31 KB
/
h5f_file_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
// 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"
"testing"
)
func TestFile(t *testing.T) {
f, err := CreateFile(fname, F_ACC_TRUNC)
if err != nil {
t.Fatalf("CreateFile failed: %s", err)
}
defer os.Remove(fname)
defer f.Close()
if fileName := f.FileName(); fileName != fname {
t.Fatalf("FileName() have %v, want %v", fileName, fname)
}
// The file is also the root group
if name := f.Name(); name != "/" {
t.Fatalf("Name() have %v, want %v", name, fname)
}
if err := f.Flush(F_SCOPE_GLOBAL); err != nil {
t.Fatalf("Flush() failed: %s", err)
}
if !IsHDF5(fname) {
t.Fatalf("IsHDF5 returned false")
}
if n, err := f.NumObjects(); err != nil {
t.Fatalf("NumObjects failed: %s", err)
} else if n != 0 {
t.Fatalf("empty file had %d objects", n)
}
if _, err := f.ObjectNameByIndex(0); err == nil {
t.Fatalf("expected error")
}
f2 := f.File()
fName := f.FileName()
f2Name := f2.FileName()
if fName != f2Name {
t.Fatalf("f2 FileName() have %v, want %v", f2Name, fName)
}
// Test a Group
groupName := "test"
g, err := f.CreateGroup(groupName)
if err != nil {
t.Fatalf("CreateGroup() failed: %s", err)
}
if name := g.Name(); name != "/"+groupName {
t.Fatalf("Group Name() have %v, want /%v", name, groupName)
}
g2, err := f.OpenGroup(groupName)
if err != nil {
t.Fatalf("OpenGroup() failed: %s", err)
}
if name := g2.Name(); name != "/"+groupName {
t.Fatalf("Group Name() have %v, want /%v", name, groupName)
}
if n, err := f.NumObjects(); err != nil {
t.Fatalf("NumObjects failed: %s", err)
} else if n != 1 {
t.Fatalf("NumObjects: got %d, want %d", n, 1)
}
if name, err := f.ObjectNameByIndex(0); err != nil {
t.Fatalf("ObjectNameByIndex failed: %s", err)
} else if name != groupName {
t.Fatalf("ObjectNameByIndex: got %q, want %q", name, groupName)
}
if _, err := f.ObjectNameByIndex(1); err == nil {
t.Fatalf("expected error")
}
// Test a Dataset
ds, err := CreateDataspace(S_SCALAR)
if err != nil {
t.Fatalf("CreateDataspace failed: %s", err)
}
dsetName := "test_dataset"
dset, err := f.CreateDataset(dsetName, T_NATIVE_INT, ds)
if err != nil {
t.Fatalf("CreateDataset failed: %s", err)
}
if name := dset.Name(); name != "/"+dsetName {
t.Fatalf("Dataset Name() have %v, want /%v", name, dsetName)
}
dFile := dset.File()
if dFile.Name() != f.Name() {
t.Fatalf("Dataset File() have %v, want %v", dFile.Name(), f.Name())
}
if n, err := f.NumObjects(); err != nil {
t.Fatalf("NumObjects failed: %s", err)
} else if n != 2 {
t.Fatalf("NumObjects: got %d, want %d", n, 1)
}
for i, n := range []string{groupName, dsetName} {
if name, err := f.ObjectNameByIndex(uint(i)); err != nil {
t.Fatalf("ObjectNameByIndex failed: %s", err)
} else if name != n {
t.Fatalf("ObjectNameByIndex: got %q, want %q", name, groupName)
}
}
}
func TestClosedFile(t *testing.T) {
f, err := CreateFile(fname, F_ACC_TRUNC)
if err != nil {
t.Fatalf("CreateFile failed: %s", err)
}
fName := f.Name()
f2 := f.File()
f.Close()
f2Name := f2.FileName()
if f2Name != fname {
t.Fatalf("f2 FileName() have %v, want %v", f2Name, fName)
}
f2.Close()
os.Remove(fname)
f3 := f.File()
if f3 != nil {
t.Fatalf("expected file to be nil")
}
}