File: DatasetTest.java

package info (click to toggle)
jhdf 2.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 39,996 kB
  • ctags: 17,959
  • sloc: java: 100,416; ansic: 24,920; sh: 2,256; makefile: 957; cpp: 48
file content (233 lines) | stat: -rw-r--r-- 8,010 bytes parent folder | download
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
/**
 * 
 */
package test.object;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;

import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.object.Dataset;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.h5.H5File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author rsinha
 * 
 */
public class DatasetTest {
    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DatasetTest.class);
    private static final H5File H5FILE = new H5File();

    private H5File testFile = null;
    String[] dsetNames = { H5TestFile.NAME_DATASET_INT, H5TestFile.NAME_DATASET_FLOAT, H5TestFile.NAME_DATASET_CHAR,
            H5TestFile.NAME_DATASET_STR, H5TestFile.NAME_DATASET_ENUM, H5TestFile.NAME_DATASET_IMAGE,
            H5TestFile.NAME_DATASET_COMPOUND };
    private Dataset[] dSets = new Dataset[dsetNames.length];

    @BeforeClass
    public static void createFile() throws Exception {
        try {
            int openID = H5.getOpenIDCount();
            if (openID > 0)
                System.out.println("DatasetTest BeforeClass: Number of IDs still open: " + openID);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            H5TestFile.createTestFile(null);
        }
        catch (final Exception ex) {
            System.out.println("*** Unable to create HDF5 test file. " + ex);
            System.exit(-1);
        }
    }

    @AfterClass
    public static void checkIDs() throws Exception {
        try {
            int openID = H5.getOpenIDCount();
            if (openID > 0)
                System.out.println("DatasetTest AfterClass: Number of IDs still open: " + openID);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    @Before
    public void openFiles() throws Exception {
        try {
            int openID = H5.getOpenIDCount();
            if (openID > 0)
                log.debug("Before: Number of IDs still open: " + openID);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        testFile = (H5File) H5FILE.open(H5TestFile.NAME_FILE_H5, FileFormat.READ);
        assertNotNull(testFile);
        testFile.open();
        for (int i = 0; i < dSets.length; i++) {
            dSets[i] = (Dataset) testFile.get(dsetNames[i]);
            dSets[i].init();
            assertNotNull(dSets[i]);
        }
    }

    @After
    public void removeFiles() throws Exception {
        if (testFile != null) {
            try {
                testFile.close();
            }
            catch (final Exception ex) {
            }
            testFile = null;
        }
        try {
            int openID = H5.getOpenIDCount();
            if (openID > 0)
                log.debug("After: Number of IDs still open: " + openID);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * For each dataset in the file we are:
     * <ul>
     * <li>checking the chunk size for the datasets.
     * <li>checking details about compression.
     * <li>cheking whether the byte array is to be converted to string.
     * <li>checking the dimension names.
     * <li>checking the dimension sizes.
     * <li>checking the rank.
     * <li>checking the selected dimensions.
     * <li>checking the selected indexes.
     * <li>checking the startdims.
     * <li>checking the stride.
     * <li>checking the width.
     * </ul>
     */
    @Test
    public void testMetadataAssociatedWithDataset() {
        log.debug("testMetadataAssociatedWithDataset");
        for (int i = 0; i < dsetNames.length; i++) {
            assertNull(dSets[i].getChunkSize());
            assertTrue(dSets[i].getCompression().equals("NONE"));
            assertTrue(dSets[i].getConvertByteToString()); // by default,
                                                           // strings are
                                                           // converted
            assertNull(dSets[i].getDimNames());
            assertTrue(Arrays.equals(dSets[i].getDims(), H5TestFile.DIMs));
            if (H5TestFile.NAME_DATASET_STR.equals("/" + dSets[i].getName())) {
                assertEquals(dSets[i].getHeight(), H5TestFile.DIM2);
            }
            else {
                assertEquals(dSets[i].getHeight(), H5TestFile.DIM1);
            }
            assertEquals(dSets[i].getRank(), H5TestFile.RANK);
            long[] array = new long[2];
            if (H5TestFile.NAME_DATASET_STR.equals("/" + dSets[i].getName())) {
                array[0] = 1;
                array[1] = 10;
            }
            else {
                array[0] = 50;
                array[1] = 10;
            }
            assertTrue(Arrays.equals(dSets[i].getSelectedDims(), array));
            int[] arrayInt = new int[3];
            if (H5TestFile.NAME_DATASET_STR.equals("/" + dSets[i].getName())) {
                arrayInt[0] = 1;
                arrayInt[1] = 0;
                arrayInt[2] = 2;
            }
            else {
                arrayInt[0] = 0;
                arrayInt[1] = 1;
                arrayInt[2] = 2;
            }
            assertTrue(Arrays.equals(dSets[i].getSelectedIndex(), arrayInt));
            array[0] = 0;
            array[1] = 0;
            assertTrue(Arrays.equals(dSets[i].getStartDims(), array));
            array[0] = 1;
            array[1] = 1;
            assertTrue(Arrays.equals(dSets[i].getStride(), array));
            if (H5TestFile.NAME_DATASET_STR.equals("/" + dSets[i].getName())) {
                assertEquals(dSets[i].getWidth(), 1);
            }
            else {
                assertEquals(dSets[i].getWidth(), H5TestFile.DIM2);
            }
        }
        int nObjs = 0;
        try {
            nObjs = H5.H5Fget_obj_count(testFile.getFID(), HDF5Constants.H5F_OBJ_ALL);
        }
        catch (final Exception ex) {
            fail("H5.H5Fget_obj_count() failed. " + ex);
        }
        assertEquals(1, nObjs); // file id should be the only one left open
    }

    /**
     * Test converting the following unsigned values to signed values.
     * <ul>
     * <li>byte[] int8 = { - 1, - 128, 127, 0};
     * <li>short[] int16 = { - 1, - 32768, 32767, 0};
     * <li>int[] int32 = { - 1, - 2147483648, 2147483647, 0};
     * </ul>
     * Expected values
     * <ul>
     * <li>short[] uint8 = {255, 128, 127, 0};
     * <li>int[] uint16 = {65535, 32768, 32767, 0};
     * <li>long[] uint32 = {4294967295L, 2147483648L, 2147483647, 0};
     * </ul>
     */
    @Test
    public void testConvertFromUnsignedC() {
        log.debug("testConvertFromUnsignedC");
        byte[] int8 = { -1, -128, 127, 0 };
        short[] int16 = { -1, -32768, 32767, 0 };
        int[] int32 = { -1, -2147483648, 2147483647, 0 };

        short[] uint8 = { 255, 128, 127, 0 };
        int[] uint16 = { 65535, 32768, 32767, 0 };
        long[] uint32 = { 4294967295L, 2147483648L, 2147483647, 0 };

        short[] expected8 = (short[]) Dataset.convertFromUnsignedC(int8, null);
        assertTrue(Arrays.equals(expected8, uint8));

        int[] expected16 = (int[]) Dataset.convertFromUnsignedC(int16, null);
        assertTrue(Arrays.equals(expected16, uint16));

        long[] expected32 = (long[]) Dataset.convertFromUnsignedC(int32, null);
        assertTrue(Arrays.equals(expected32, uint32));
        int nObjs = 0;
        try {
            nObjs = H5.H5Fget_obj_count(testFile.getFID(), HDF5Constants.H5F_OBJ_ALL);
        }
        catch (final Exception ex) {
            fail("H5.H5Fget_obj_count() failed. " + ex);
        }
        assertEquals(1, nObjs); // file id should be the only one left open
    }
}