summaryrefslogtreecommitdiffstats
path: root/java/test/TestH5T.java
blob: 0121cc2429764491c5f23318470c5767b63eb679 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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://www.hdfgroup.org/licenses.               *
 * 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.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.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

public class TestH5T {
    @Rule
    public TestName testname            = new TestName();
    private static final String H5_FILE = "testT.h5";
    long H5fid                          = HDF5Constants.H5I_INVALID_HID;
    long H5strdid                       = HDF5Constants.H5I_INVALID_HID;

    private final void _deleteFile(String filename)
    {
        File file = null;
        try {
            file = new File(filename);
        }
        catch (Throwable err) {
        }

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

    @Before
    public void createH5file() throws NullPointerException, HDF5Exception
    {
        assertTrue("H5 open ids is 0", H5.getOpenIDCount() == 0);
        System.out.print(testname.getMethodName());

        H5fid = H5.H5Fcreate(H5_FILE, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
                             HDF5Constants.H5P_DEFAULT);
        assertTrue("H5.H5Fcreate", H5fid > 0);
        H5strdid = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
        assertTrue("H5.H5Tcopy", H5strdid > 0);

        H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL);
    }

    @After
    public void deleteH5file() throws HDF5LibraryException
    {
        if (H5strdid >= 0)
            try {
                H5.H5Tclose(H5strdid);
            }
            catch (Exception ex) {
            }
        if (H5fid > 0)
            try {
                H5.H5Fclose(H5fid);
            }
            catch (Exception ex) {
            }

        _deleteFile(H5_FILE);
        System.out.println();
    }

    @Test(expected = HDF5LibraryException.class)
    public void testH5Tequal_type_error() throws Throwable
    {
        H5.H5Tequal(HDF5Constants.H5T_INTEGER, H5strdid);
    }

    @Test
    public void testH5Tget_class()
    {
        try {
            int result = H5.H5Tget_class(H5strdid);
            assertTrue("H5.H5Tget_class", result > 0);
            String class_name = H5.H5Tget_class_name(result);
            assertTrue("H5.H5Tget_class", class_name.compareTo("H5T_STRING") == 0);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tget_class: " + err);
        }
    }

    @Test
    public void testH5Tget_size()
    {
        long dt_size = -1;

        try {
            dt_size = H5.H5Tget_size(H5strdid);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tget_size:H5.H5Tget_size " + err);
        }
        assertTrue("testH5Tget_size", dt_size > 0);
    }

    @Test
    public void testH5Tset_size()
    {
        long dt_size = 5;

        try {
            H5.H5Tset_size(H5strdid, dt_size);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tset_size:H5.H5Tset_size " + err);
        }
        try {
            dt_size = H5.H5Tget_size(H5strdid);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tget_size:H5.H5Tget_size " + err);
        }
        assertTrue("testH5Tget_size", dt_size == 5);
    }

    @Test
    public void testH5Tarray_create()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;
        long[] adims     = {3, 5};

        try {
            filetype_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_I64LE, 2, adims);
            assertTrue("testH5Tarray_create", filetype_id >= 0);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tarray_create.H5Tarray_create " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tget_array_ndims()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;
        int ndims        = 0;
        long[] adims     = {3, 5};

        try {
            filetype_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_I64LE, 2, adims);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tarray_create.H5Tarray_create " + err);
        }
        assertTrue("testH5Tget_array_ndims:H5Tarray_create", filetype_id >= 0);
        try {
            ndims = H5.H5Tget_array_ndims(filetype_id);
            assertTrue("testH5Tget_array_ndims", ndims == 2);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tget_array_ndims.H5Tget_array_ndims " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tget_array_dims()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;
        int ndims        = 0;
        long[] adims     = {3, 5};
        long[] rdims     = new long[2];

        try {
            filetype_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_I64LE, 2, adims);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tarray_create.H5Tarray_create " + err);
        }
        assertTrue("testH5Tget_array_dims:H5Tarray_create", filetype_id >= 0);
        try {
            ndims = H5.H5Tget_array_dims(filetype_id, rdims);
            assertTrue("testH5Tget_array_dims", ndims == 2);
            assertTrue("testH5Tget_array_dims", adims[0] == rdims[0]);
            assertTrue("testH5Tget_array_dims", adims[1] == rdims[1]);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tget_array_dims.H5Tget_array_dims " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tenum_functions()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;
        String enum_type = "Enum_type";
        byte[] enum_val  = new byte[1];
        String enum_name = null;

        // Create a enumerate datatype
        try {
            filetype_id = H5.H5Tcreate(HDF5Constants.H5T_ENUM, (long)1);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tenum_functions:H5Tcreate " + err);
        }
        assertTrue("testH5Tenum_functions:H5Tcreate", filetype_id >= 0);
        try {
            enum_val[0] = 10;
            H5.H5Tenum_insert(filetype_id, "RED", enum_val);
            enum_val[0] = 11;
            H5.H5Tenum_insert(filetype_id, "GREEN", enum_val);
            enum_val[0] = 12;
            H5.H5Tenum_insert(filetype_id, "BLUE", enum_val);
            enum_val[0] = 13;
            H5.H5Tenum_insert(filetype_id, "ORANGE", enum_val);
            enum_val[0] = 14;
            H5.H5Tenum_insert(filetype_id, "YELLOW", enum_val);

            // Query member number and member index by member name, for enumeration type.
            assertTrue("Can't get member number", H5.H5Tget_nmembers(filetype_id) == 5);
            assertTrue("Can't get correct index number", H5.H5Tget_member_index(filetype_id, "ORANGE") == 3);

            // Commit enumeration datatype and close it */
            H5.H5Tcommit(H5fid, enum_type, filetype_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
                         HDF5Constants.H5P_DEFAULT);

            H5.H5Tclose(filetype_id);

            // Open the dataytpe for query
            filetype_id = H5.H5Topen(H5fid, enum_type, HDF5Constants.H5P_DEFAULT);
            assertTrue("testH5Tenum_functions:H5Tcreate", filetype_id >= 0);

            // Query member number and member index by member name, for enumeration type
            assertTrue("Can't get member number", H5.H5Tget_nmembers(filetype_id) == 5);
            assertTrue("Can't get correct index number", H5.H5Tget_member_index(filetype_id, "ORANGE") == 3);

            // Query member value by member name, for enumeration type
            H5.H5Tenum_valueof(filetype_id, "ORANGE", enum_val);
            assertTrue("Incorrect value for enum member", enum_val[0] == 13);

            // Query member value by member index, for enumeration type
            H5.H5Tget_member_value(filetype_id, 2, enum_val);
            assertTrue("Incorrect value for enum member", enum_val[0] == 12);

            // Query member name by member value, for enumeration type
            enum_val[0] = 14;
            enum_name   = H5.H5Tenum_nameof(filetype_id, enum_val, 16);
            assertTrue("Incorrect name for enum member", enum_name.compareTo("YELLOW") == 0);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tenum_functions:query " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tenum_create_functions()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;
        byte[] enum_val  = new byte[1];

        // Create a enumerate datatype
        try {
            filetype_id = H5.H5Tenum_create(HDF5Constants.H5T_NATIVE_INT);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tenum_create_functions:H5Tcreate " + err);
        }
        assertTrue("testH5Tenum_create_functions:H5Tcreate", filetype_id >= 0);
        try {
            enum_val[0] = 10;
            H5.H5Tenum_insert(filetype_id, "RED", enum_val);
            enum_val[0] = 11;
            H5.H5Tenum_insert(filetype_id, "GREEN", enum_val);
            enum_val[0] = 12;
            H5.H5Tenum_insert(filetype_id, "BLUE", enum_val);
            enum_val[0] = 13;
            H5.H5Tenum_insert(filetype_id, "ORANGE", enum_val);
            enum_val[0] = 14;
            H5.H5Tenum_insert(filetype_id, "YELLOW", enum_val);

            // Query member number and member index by member name, for enumeration type.
            assertTrue("Can't get member number", H5.H5Tget_nmembers(filetype_id) == 5);
            assertTrue("Can't get correct index number", H5.H5Tget_member_index(filetype_id, "ORANGE") == 3);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tenum_create_functions:H5Tget_nmembers " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Topaque_functions()
    {
        long filetype_id   = HDF5Constants.H5I_INVALID_HID;
        String opaque_name = null;

        // Create a opaque datatype
        try {
            filetype_id = H5.H5Tcreate(HDF5Constants.H5T_OPAQUE, (long)4);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Topaque_functions:H5Tcreate " + err);
        }
        assertTrue("testH5Topaque_functions:H5Tcreate", filetype_id >= 0);

        try {
            H5.H5Tset_tag(filetype_id, "opaque type");
            opaque_name = H5.H5Tget_tag(filetype_id);
            assertTrue("Incorrect tag for opaque type", opaque_name.compareTo("opaque type") == 0);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Topaque_functions:H5Tset_get_tag " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tvlen_create()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;

        try {
            filetype_id = H5.H5Tvlen_create(HDF5Constants.H5T_C_S1);
            assertTrue("testH5Tvlen_create", filetype_id >= 0);

            // Check if datatype is VL type
            int vlclass = H5.H5Tget_class(filetype_id);
            assertTrue("testH5Tvlen_create:H5Tget_class", vlclass == HDF5Constants.H5T_VLEN);
            assertFalse("testH5Tis_variable_str:H5Tget_class", vlclass == HDF5Constants.H5T_STRING);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tvlen_create.H5Tvlen_create " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tis_variable_str()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;

        try {
            filetype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
            assertTrue("testH5Tis_variable_str.H5Tcopy: ", filetype_id >= 0);

            // Convert to variable-length string
            H5.H5Tset_size(filetype_id, HDF5Constants.H5T_VARIABLE);

            // Check if datatype is VL string
            int vlclass = H5.H5Tget_class(filetype_id);
            assertTrue("testH5Tis_variable_str:H5Tget_class", vlclass == HDF5Constants.H5T_STRING);
            assertFalse("testH5Tvlen_create:H5Tget_class", vlclass == HDF5Constants.H5T_VLEN);

            assertTrue("testH5Tis_variable_str:H5Tis_variable_str", H5.H5Tis_variable_str(filetype_id));

            // Verify that the class detects as a string
            assertTrue("testH5Tis_variable_str:H5Tdetect_class",
                       H5.H5Tdetect_class(filetype_id, HDF5Constants.H5T_STRING));
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tis_variable_str " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }

    @Test
    public void testH5Tcompound_functions()
    {
        long filetype_id = HDF5Constants.H5I_INVALID_HID;

        // Create a compound datatype
        try {
            filetype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, (long)16);
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tcompound_functions:H5Tcreate " + err);
        }
        assertTrue("testH5Tcompound_functions:H5Tcreate", filetype_id >= 0);
        try {
            H5.H5Tinsert(filetype_id, "Lon", 0, HDF5Constants.H5T_NATIVE_DOUBLE);
            H5.H5Tinsert(filetype_id, "Lat", 8, HDF5Constants.H5T_NATIVE_DOUBLE);

            // Query member number and member index by member name, for enumeration type.
            assertTrue("Can't get member number", H5.H5Tget_nmembers(filetype_id) == 2);
            assertTrue("Can't get correct index number", H5.H5Tget_member_index(filetype_id, "Lat") == 1);

            // We started to support this function for compound type in 1.8.6 release.
            int order = H5.H5Tget_order(filetype_id);
            assertFalse("Can't get order for compound type.", order == HDF5Constants.H5T_ORDER_ERROR);
            assertTrue("Wrong order for this type.",
                       (order == HDF5Constants.H5T_ORDER_LE) || (order == HDF5Constants.H5T_ORDER_BE));

            // Make certain that the correct classes can be detected
            assertTrue("Can't get correct class",
                       H5.H5Tdetect_class(filetype_id, HDF5Constants.H5T_COMPOUND));
            assertTrue("Can't get correct class", H5.H5Tdetect_class(filetype_id, HDF5Constants.H5T_FLOAT));
            // Make certain that an incorrect class is not detected
            assertFalse("Can get incorrect class", H5.H5Tdetect_class(filetype_id, HDF5Constants.H5T_TIME));

            // Query member name by member index
            String index_name = H5.H5Tget_member_name(filetype_id, 0);
            assertTrue("Incorrect name for member index", index_name.compareTo("Lon") == 0);

            // Query member offset by member no
            long index_offset = H5.H5Tget_member_offset(filetype_id, 1);
            assertTrue("Incorrect offset for member no", index_offset == 8);

            // Query member type by member index
            long index_type = H5.H5Tget_member_type(filetype_id, 0);
            assertTrue("Incorrect type for member index",
                       H5.H5Tequal(HDF5Constants.H5T_NATIVE_DOUBLE, index_type));
            if (index_type >= 0)
                try {
                    H5.H5Tclose(index_type);
                }
                catch (Exception ex) {
                }
        }
        catch (Throwable err) {
            err.printStackTrace();
            fail("testH5Tcompound_functions:query " + err);
        }
        finally {
            if (filetype_id >= 0)
                try {
                    H5.H5Tclose(filetype_id);
                }
                catch (Exception ex) {
                }
        }
    }
}