summaryrefslogtreecommitdiffstats
path: root/java/test/TestH5Pvirtual.java
blob: 40cfbacf423fb68af33150ea96aa0287f9af8c87 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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 COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;

import hdf.hdf5lib.H5;
import hdf.hdf5lib.HDF5Constants;
import hdf.hdf5lib.exceptions.HDF5Exception;
import hdf.hdf5lib.exceptions.HDF5LibraryException;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

public class TestH5Pvirtual {
    @Rule public TestName testname = new TestName();

    private static final String H5_FILE = "vds.h5";
    private static final String SRC_FILE[] = {
            "v-0.h5",
            "v-1.h5",
            "v-2.h5"
        };
    private static final String SRC_DATASET[] = {
            "A",
            "B",
            "C"
        };
    private static final int DIM_Y = 6;
    private static final int VDSDIM_X = 4;
    private static final int VDSDIM_Y = 6;
    private static final int fill_value = -1;
    long[] H5dims = { DIM_Y };
    long[] VDSH5dims = { VDSDIM_X, VDSDIM_Y };
    long H5fid = -1;
    long H5dsid = -1;
    long H5dssid = -1;
    long H5dvsid = -1;
    long H5did = -1;
    long H5dcplid = -1;
    long H5dapl_id = -1;

    private final void _deleteFile(String filename) {
        File file = new File(filename);

        if (file.exists()) {
            try {file.delete();} catch (SecurityException e) {}
        }
    }

    private final long _createDataset(long fid, long dsid, String name, long dcpl, long dapl) {
        long did = -1;
        long space_id = -1;
        long[] start = {0, 0};
        long[] stride = null;
        long[] count = {1, 1};
        long[] block = {1, VDSDIM_Y};

        try {
            H5dssid = H5.H5Screate_simple(1, H5dims, null);
            for (int i = 0; i < 3; i++) {
                start[0] = i;
                /* Select i-th row in the virtual dataset; selection in the source datasets is the same. */
                H5.H5Sselect_hyperslab(dsid, HDF5Constants.H5S_SELECT_SET, start, stride, count, block);
                H5.H5Pset_virtual(dcpl, dsid, SRC_FILE[i], SRC_DATASET[i], H5dssid);
            }
            did = H5.H5Dcreate(fid, name, HDF5Constants.H5T_NATIVE_INT, dsid,
                    HDF5Constants.H5P_DEFAULT, dcpl, dapl);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("H5.H5Dcreate: " + err);
        }
        assertTrue("TestH5Pvirtual._createDataset: ", did > 0);

        return did;
    }

    private final void _createH5File(long fcpl, long fapl) {
        int[] dset_data = new int[DIM_Y];
        // Create source files and datasets
        for (int i=0; i < 3; i++) {
            long space_id = -1;
            long dset_id = -1;
            long file_id = -1;
            for (int j = 0; j < DIM_Y; j++) dset_data[j] = i+1;

            try {
                file_id = H5.H5Fcreate(SRC_FILE[i], HDF5Constants.H5F_ACC_TRUNC,
                        HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
                space_id = H5.H5Screate_simple(1, H5dims, null);
                dset_id = H5.H5Dcreate(file_id, SRC_DATASET[i], HDF5Constants.H5T_NATIVE_INT, space_id,
                        HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
                H5.H5Dwrite (dset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT,
                        dset_data);
            }
            catch (Throwable err) {
                err.printStackTrace();
                fail("TestH5Pvirtual.createH5file: " + err);
            }
            finally {
                if (dset_id > 0)
                    try {H5.H5Dclose(dset_id);} catch (Exception ex) {}
                if (space_id > 0)
                    try {H5.H5Sclose(space_id);} catch (Exception ex) {}
                if (file_id > 0)
                    try {H5.H5Fclose(file_id);} catch (Exception ex) {}
            }
        }

        try {
            int[] fill_value = {-1};
            H5fid = H5.H5Fcreate(H5_FILE, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
            H5dsid = H5.H5Screate_simple(2, VDSH5dims, null);
            H5dcplid = H5.H5Pcreate(HDF5Constants.H5P_DATASET_CREATE);
            H5.H5Pset_fill_value(H5dcplid, HDF5Constants.H5T_NATIVE_INT, fill_value);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("TestH5Pvirtual.createH5file: " + err);
        }
        assertTrue("TestH5Pvirtual.createH5file: H5.H5Fcreate: ", H5fid > 0);
        assertTrue("TestH5Pvirtual.createH5file: H5.H5Screate_simple: ", H5dsid > 0);
        assertTrue("TestH5Pvirtual.createH5file: H5.H5Pcreate: ", H5dcplid > 0);

        try {
            H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL);
        }
        catch (Throwable err) {
            err.printStackTrace();
        }
    }

    @Before
    public void createH5file()
            throws NullPointerException, HDF5Exception {
        assertTrue("H5 open ids is 0",H5.getOpenIDCount()==0);
        System.out.print(testname.getMethodName());
        _createH5File(HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
        H5dapl_id = H5.H5Pcreate(HDF5Constants.H5P_DATASET_ACCESS);
        assertTrue("TestH5Pvirtual.createH5file: H5.H5Pcreate: ", H5dapl_id > 0);
  }

    @After
    public void deleteH5file() throws HDF5LibraryException {
        if (H5dapl_id > 0)
            try {H5.H5Pclose(H5dapl_id);} catch (Exception ex) {}
        if (H5dcplid > 0)
            try {H5.H5Pclose(H5dcplid);} catch (Exception ex) {}
        if (H5dsid > 0)
            try {H5.H5Sclose(H5dsid);} catch (Exception ex) {}
        if (H5fid > 0)
            try {H5.H5Fclose(H5fid);} catch (Exception ex) {}
        for (int i = 0; i < 3; i++) {
            _deleteFile(SRC_FILE[i]);
        }
        _deleteFile(H5_FILE);
        System.out.println();
    }

    @Test
    public void testH5Pvirtual_storage() {
        int layout = -1;

        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            layout = H5.H5Pget_layout (H5dcplid);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("H5.H5Pget_layout: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
        assertTrue("testH5Pvirtual_storage", HDF5Constants.H5D_VIRTUAL == layout);
    }

    @Test
    public void testH5Pget_virtual_count() {
        long num_map = -1;

        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            num_map = H5.H5Pget_virtual_count(H5dcplid);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("H5.H5Pget_virtual_count: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
        assertTrue("testH5Pget_virtual_count: "+num_map, num_map >= 0);
        assertEquals(3, num_map);
    }

    @Test
    public void testH5Pget_source_filename() throws Throwable {
        String filename = null;

        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            filename = (H5.H5Pget_virtual_filename (H5dcplid, 2));
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("H5.H5Pget_virtual_filename: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
        assertTrue("testH5Pget_source_filename: "+filename, filename.compareTo("v-2.h5") == 0);
    }

    @Test
    public void testH5Pget_source_datasetname() throws Throwable {
        String datasetname = null;

        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            datasetname = H5.H5Pget_virtual_dsetname (H5dcplid, 1);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("H5.H5Pget_virtual_dsetname: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
        assertTrue("testH5Pget_source_datasetname: "+datasetname, datasetname.compareTo("B") == 0);
    }

    @Test
    public void testH5Pget_selection_source_dataset() throws Throwable {
        long src_space = -1;
        long src_selection = -1;

        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            src_space = H5.H5Pget_virtual_srcspace (H5dcplid, 0);
            src_selection = H5.H5Sget_select_type(src_space);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Pget_selection_source_dataset: " + err);
        }
        finally {
            if (src_space > 0)
                try {H5.H5Sclose(src_space);} catch (Exception ex) {}
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
        assertTrue("testH5Pget_selection_source_dataset", src_selection == HDF5Constants.H5S_SEL_ALL);
    }

    @Test
    public void testH5Pget_mapping_parameters() {
        long num_map = -1;

        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            try {
                num_map = H5.H5Pget_virtual_count(H5dcplid);
            }
            catch (Throwable err) {
                err.printStackTrace();
                fail("H5.H5Pget_virtual_count: " + err);
            }
            for (int i = 0; i < num_map; i++) {
                int vselection = -1;
                long vspace = -1;
                long nblocks;   // Number of hyperslab blocks
                long blocks[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};    // List of blocks
                long[] start = {i, 0};
                long[] stride = {1, 1};
                long[] count = {1, 1};
                long[] block = {1, VDSDIM_Y};
                long q_start[] = new long[2];
                long q_stride[] = new long[2];
                long q_count[] = new long[2];
                long q_block[] = new long[2];
                boolean is_regular = false;

                try {
                    try {
                        vspace = H5.H5Pget_virtual_vspace (H5dcplid, i);
                    }
                    catch (Throwable err) {
                        err.printStackTrace();
                        fail("H5.H5Pget_virtual_vspace: " + err);
                    }
                    try {
                        vselection = H5.H5Sget_select_type(vspace);
                    }
                    catch (Throwable err) {
                        err.printStackTrace();
                        fail("H5.H5Sget_select_type: " + err);
                    }
                    assertTrue("testH5Pget_mapping_parameters["+i+"]", vselection == HDF5Constants.H5S_SEL_HYPERSLABS);

                    // Verify that there is only one block
                    nblocks = H5.H5Sget_select_hyper_nblocks(vspace);
                    assertTrue("H5Sget_select_hyper_nblocks", nblocks == 1);

                    // Retrieve the block defined
                    H5.H5Sget_select_hyper_blocklist(vspace, 0, nblocks, blocks);

                    // Verify that the correct block is defined
                    assertTrue("H5.H5Sget_select_hyper_blocklist["+i+"] [0]: "+blocks[0], start[0] == blocks[0]);
                    assertTrue("H5.H5Sget_select_hyper_blocklist["+i+"] [1]: "+blocks[1], start[1] == blocks[1]);
                    assertTrue("H5.H5Sget_select_hyper_blocklist["+i+"] [2]: "+blocks[2], (block[0]-1+i) == blocks[2]);
                    assertTrue("H5.H5Sget_select_hyper_blocklist["+i+"] [3]: "+blocks[3], (block[1]-1) == blocks[3]);
                    // We also can use new APIs to get start, stride, count and block
                    is_regular = H5.H5Sis_regular_hyperslab(vspace);
                    assertTrue("H5.H5Sis_regular_hyperslab", is_regular);
                    H5.H5Sget_regular_hyperslab (vspace, q_start, q_stride, q_count, q_block);

                    // Verify the hyperslab parameters
                    for(int u = 0; u < 2; u++) {
                        assertTrue("H5Sget_regular_hyperslab, start", start[u] == q_start[u]);
                        assertTrue("H5Sget_regular_hyperslab, stride", stride[u] == q_stride[u]);
                        assertTrue("H5Sget_regular_hyperslab, count", count[u] == q_count[u]);
                        assertTrue("H5Sget_regular_hyperslab, block", block[u] == q_block[u]);
                    }
               }
                catch (Throwable err) {
                    err.printStackTrace();
                    fail("H5.testH5Pget_mapping_parameters: " + err);
                }
                finally {
                    if (vspace > 0)
                        try {H5.H5Sclose(vspace);} catch (Exception ex) {}
                }
            }
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Pget_mapping_parameters: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
    }

    @Test
    public void testH5Pset_get_virtual_view() {
        int ret_val = -1;
        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            ret_val = H5.H5Pget_virtual_view(H5dapl_id);
            assertTrue("H5Pget_virtual_view", ret_val >= 0);
            assertEquals(HDF5Constants.H5D_VDS_LAST_AVAILABLE, ret_val);
            H5.H5Pset_virtual_view(H5dapl_id, HDF5Constants.H5D_VDS_FIRST_MISSING);
            ret_val = H5.H5Pget_virtual_view(H5dapl_id);
            assertTrue("H5Pget_virtual_view", ret_val >= 0);
            assertEquals(HDF5Constants.H5D_VDS_FIRST_MISSING, ret_val);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Pset_get_virtual_view: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
    }

    @Test
    public void  testH5Pset_get_virtual_printf_gap() {
        long ret_val = -1;
        H5did = _createDataset(H5fid, H5dsid, "VDS", H5dcplid, H5dapl_id);
        try {
            ret_val = H5.H5Pget_virtual_printf_gap(H5dapl_id);
            assertTrue("H5Pget_virtual_printf_gap", ret_val >= 0);
            assertEquals(0, ret_val);
            H5.H5Pset_virtual_printf_gap(H5dapl_id, 2);
            ret_val = H5.H5Pget_virtual_printf_gap(H5dapl_id);
            assertTrue("H5Pget_virtual_printf_gap", ret_val >= 0);
            assertEquals(2, ret_val);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("H5Pset_get_virtual_printf_gap: " + err);
        }
        finally {
            if (H5dssid > 0)
                try {H5.H5Sclose(H5dssid);} catch (Exception ex) {}
            if (H5did > 0)
                try {H5.H5Dclose(H5did);} catch (Exception ex) {}
        }
    }
}