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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "h5test.h"
/* The file structure tested is as follows:
* /
* Group1
* Dataset1
* Dataset2
* Group2
* Dataset4
* Hard link to group1
* Dataset5
* Datatype1
*
* The purpose of the hard link to make sure that the reference lookup code
* does not infinite loop when looking for Dataset3.
*/
#define FILE1 "trefer1.h5"
#define FILE2 "trefer2.h5"
/* 1-D dataset with fixed dimensions */
#define SPACE1_RANK 1
#define SPACE1_DIM1 8
int
main(void)
{
hid_t fid1, fid2; /* HDF5 File IDs */
hid_t dataset; /* Dataset ID */
hid_t group, group2; /* Group ID */
hid_t sid1; /* Dataspace ID */
hid_t tid1; /* Datatype ID */
hid_t ref;
hsize_t dims1[] = {SPACE1_DIM1};
hobj_ref_t wbuf[SPACE1_DIM1]; /* Buffer to write to disk */
int tu32[SPACE1_DIM1]; /* Int data */
int i; /* counting variables */
char buf[100];
/* Compound datatype */
typedef struct s1_t {
unsigned int a;
unsigned int b;
float c;
} s1_t;
/* Create files */
if((fid1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
TEST_ERROR
if((fid2 = H5Fcreate(FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
TEST_ERROR
/* Create dataspace for datasets */
if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0)
TEST_ERROR
/* Create a group */
if((group = H5Gcreate(fid1, "Group1", (size_t)0)) < 0)
TEST_ERROR
/* Create a single dataset inside the second file, which will be mounted
* and used to mask objects in the first file */
if((dataset = H5Dcreate(fid2, "Dataset1", H5T_STD_U32LE, sid1, H5P_DEFAULT)) < 0)
TEST_ERROR
if(H5Dclose(dataset) < 0)
TEST_ERROR
/* Create a dataset (inside Group1) */
if((dataset = H5Dcreate(group, "Dataset1", H5T_STD_U32LE, sid1, H5P_DEFAULT)) < 0)
TEST_ERROR
/* Initialize data buffer */
for(i = 0; i < SPACE1_DIM1; i++)
tu32[i] = i * 3;
/* Write selection to disk */
if(H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, tu32) < 0)
TEST_ERROR
/* Close Dataset */
if(H5Dclose(dataset) < 0)
TEST_ERROR
/* Create another dataset (inside Group1) */
if((dataset = H5Dcreate(group, "Dataset2", H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT)) < 0)
TEST_ERROR
/* Close Dataset */
if(H5Dclose(dataset) < 0)
TEST_ERROR
/* Create a datatype to refer to */
if((tid1 = H5Tcreate(H5T_COMPOUND, sizeof(s1_t))) < 0)
TEST_ERROR
/* Insert fields */
if(H5Tinsert(tid1, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT) < 0)
TEST_ERROR
if(H5Tinsert(tid1, "b", HOFFSET(s1_t,b), H5T_NATIVE_INT) < 0)
TEST_ERROR
if(H5Tinsert(tid1, "c", HOFFSET(s1_t,c), H5T_NATIVE_FLOAT) < 0)
TEST_ERROR
/* Save datatype for later */
if(H5Tcommit(group, "Datatype1", tid1) < 0)
TEST_ERROR
/* Close datatype */
if(H5Tclose(tid1) < 0)
TEST_ERROR
/* Create a new group in group1 */
if((group2 = H5Gcreate(group, "Group2", (size_t)0)) < 0)
TEST_ERROR
/* Create a hard link to group1 in group2 */
if(H5Glink(fid1, H5G_LINK_HARD, "/Group1", "/Group1/Group2/Link") < 0)
TEST_ERROR
/* Create dataset in that group */
if((dataset = H5Dcreate(group2, "Dataset4", H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT)) < 0)
TEST_ERROR
/* Close Dataset */
if(H5Dclose(dataset) < 0)
TEST_ERROR
/* Close group */
if(H5Gclose(group) < 0)
TEST_ERROR
if(H5Gclose(group2) < 0)
TEST_ERROR
/* Open up that hard link and make a new dataset there */
if((group = H5Gopen(fid1, "/Group1/Group2/Link")) < 0)
TEST_ERROR
if((dataset = H5Dcreate(group, "Dataset5", H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT)) < 0)
TEST_ERROR
if(H5Dclose(dataset) < 0)
TEST_ERROR
if(H5Gclose(group) < 0)
TEST_ERROR
/* Create a dataset to store references */
if((dataset = H5Dcreate(fid1, "Dataset3", H5T_STD_REF_OBJ, sid1, H5P_DEFAULT)) < 0)
TEST_ERROR
/* Create reference to dataset */
if(H5Rcreate(&wbuf[0], fid1, "/Dataset3", H5R_OBJECT, -1) < 0)
TEST_ERROR
/* Create reference to dataset */
if(H5Rcreate(&wbuf[1], fid1, "/Group1/Dataset2", H5R_OBJECT, -1) < 0)
TEST_ERROR
/* Create reference to group */
if(H5Rcreate(&wbuf[2], fid1, "/Group1", H5R_OBJECT, -1) < 0)
TEST_ERROR
/* Create reference to named datatype */
if(H5Rcreate(&wbuf[3], fid1, "/Group1/Datatype1", H5R_OBJECT, -1) < 0)
TEST_ERROR
if(H5Rcreate(&wbuf[4], fid1, "/Group1/Group2/Dataset4", H5R_OBJECT, -1) < 0)
TEST_ERROR
if(H5Rcreate(&wbuf[5], fid1, "/Group1/Group2", H5R_OBJECT, -1) < 0)
TEST_ERROR
if(H5Rcreate(&wbuf[6], fid1, "/Group1/Group2/Link/Dataset5", H5R_OBJECT, -1) < 0)
TEST_ERROR
/* Create reference to root group */
if(H5Rcreate(&wbuf[7], fid1, "/", H5R_OBJECT, -1) < 0)
TEST_ERROR
/* Write selection to disk */
if(H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0)
TEST_ERROR
TESTING("getting path to normal dataset in root group");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[0]), (char*)buf, (size_t)100);
if(!((HDstrcmp(buf, "/Dataset3") == 0) && (i == 10))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[0], (char*)buf, (size_t)100);
if(!((HDstrcmp(buf, "/Dataset3") == 0) && (i == 10))) TEST_ERROR
PASSED()
HDmemset(buf, 0, (size_t)100);
TESTING("getting path to dataset in /Group1");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[1]), (char*)buf, (size_t)100);
if(!((HDstrcmp(buf, "/Group1/Dataset2") == 0) && (i == 17))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[1], (char*)buf, (size_t)100);
if(!((HDstrcmp(buf, "/Group1/Dataset2") == 0) && (i == 17))) TEST_ERROR
PASSED()
HDmemset(buf, 0, (size_t)100);
TESTING("getting path to /Group1");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[2]), (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1") == 0) && (i == 8))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[2], (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1") == 0) && (i == 8))) TEST_ERROR
PASSED()
HDmemset(buf, 0, 100);
TESTING("getting path to datatype in /Group1");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT , &wbuf[3]), (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Datatype1") == 0) && (i == 18))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[3], (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Datatype1") == 0) && (i == 18))) TEST_ERROR
PASSED()
HDmemset(buf, 0, 100);
TESTING("getting path to dataset in nested group");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT , &wbuf[4]), (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Group2/Dataset4") == 0) && (i == 24))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[4], (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Group2/Dataset4") == 0) && (i == 24))) TEST_ERROR
PASSED()
HDmemset(buf, 0, 100);
TESTING("getting path to nested group");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[5]), (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Group2") == 0) && (i == 15))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[5], (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Group2") == 0) && (i == 15))) TEST_ERROR
PASSED()
HDmemset(buf, 0, 100);
TESTING("getting path to dataset created via hard link");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[6]), (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Dataset5") == 0) && (i == 17))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[6], (char*)buf, 100);
if(!((HDstrcmp(buf, "/Group1/Dataset5") == 0) && (i == 17))) TEST_ERROR
PASSED()
HDmemset(buf, 0, 100);
TESTING("getting path to root group");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[7]), (char*)buf, 100);
if(!((HDstrcmp(buf, "/") == 0) && (i == 2))) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[7], (char*)buf, 100);
if(!((HDstrcmp(buf, "/") == 0) && (i == 2))) TEST_ERROR
PASSED()
/* Now we mount fid2 at /Group2 and look for dataset4. It shouldn't be found */
if(H5Fmount(fid1, "/Group1/Group2", fid2, H5P_DEFAULT) < 0)
TEST_ERROR
TESTING("getting path to dataset hidden by a mounted file");
i = H5Iget_name(H5Rdereference(dataset, H5R_OBJECT, &wbuf[4]), (char*)buf, 100);
if(i != 0) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[4], (char*)buf, 100);
if(i != 0) TEST_ERROR
PASSED()
/* Now we try unlinking dataset2 from the file and searching for it. It shouldn't be found */
if((ref = H5Rdereference(dataset, H5R_OBJECT, &wbuf[1])) < 0)
TEST_ERROR
if(H5Gunlink(fid1, "/Group1/Dataset2") < 0)
TEST_ERROR
TESTING("getting path to dataset that has been unlinked");
i = H5Iget_name(ref, (char*)buf, 100);
if(i != 0) TEST_ERROR
i = H5Rget_name(dataset, H5R_OBJECT, &wbuf[1], (char*)buf, 100);
if(i != 0) TEST_ERROR
PASSED()
/* Close disk dataspace */
if(H5Sclose(sid1) < 0)
TEST_ERROR
/* Close Dataset */
if(H5Dclose(dataset) < 0)
TEST_ERROR
/* Close file */
if(H5Fclose(fid1) < 0)
TEST_ERROR
if(H5Fclose(fid2) < 0)
TEST_ERROR
return 0;
error:
return 1;
}
|