1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  * Programmer:  Raymond Lu <slu@ncsa.uiuc.edu>
16  *              Thursday, March 23, 2006
17  *
18  * Purpose:     Check if floating-point data created on big-endian and
19  *              little-endian machines can be read on the machine running this test.
20  */
21 
22 #include "h5test.h"
23 #include "H5srcdir.h"
24 
25 const char *FILENAME[] = {
26     "vms_data",
27     "le_data",
28     "be_data",
29     NULL
30 };
31 
32 #define DATASETNAME        "Array_le"
33 #define DATASETNAME1       "Array_be"
34 #define DATASETNAME2       "Scale_offset_float_data_le"
35 #define DATASETNAME3       "Scale_offset_float_data_be"
36 #define DATASETNAME4       "Scale_offset_double_data_le"
37 #define DATASETNAME5       "Scale_offset_double_data_be"
38 #define DATASETNAME6       "Scale_offset_char_data_le"
39 #define DATASETNAME7       "Scale_offset_char_data_be"
40 #define DATASETNAME8       "Scale_offset_short_data_le"
41 #define DATASETNAME9       "Scale_offset_short_data_be"
42 #define DATASETNAME10      "Scale_offset_int_data_le"
43 #define DATASETNAME11      "Scale_offset_int_data_be"
44 #define DATASETNAME12      "Scale_offset_long_long_data_le"
45 #define DATASETNAME13      "Scale_offset_long_long_data_be"
46 
47 #define DATASETNAME14      "Fletcher_float_data_le"
48 #define DATASETNAME15      "Fletcher_float_data_be"
49 #define DATASETNAME16      "Deflate_float_data_le"
50 #define DATASETNAME17      "Deflate_float_data_be"
51 #ifdef H5_HAVE_FILTER_SZIP
52 #define DATASETNAME18      "Szip_float_data_le"
53 #define DATASETNAME19      "Szip_float_data_be"
54 #endif /* H5_HAVE_FILTER_SZIP */
55 #define DATASETNAME20      "Shuffle_float_data_le"
56 #define DATASETNAME21      "Shuffle_float_data_be"
57 #define DATASETNAME22      "Nbit_float_data_le"
58 #define DATASETNAME23      "Nbit_float_data_be"
59 
60 #define NX 		6
61 #define NY 		6
62 
63 
64 /*-------------------------------------------------------------------------
65  * Function:    open_dataset
66  *
67  * Purpose:     Read and compare the data from a dataset.
68  *
69  * Return:      Success:        0
70  *              Failure:        1
71  *
72  * Programmer:  Raymond Lu
73  *              17 May 2011
74  *
75  * Modifications:
76  *
77  *-------------------------------------------------------------------------
78  */
check_data(const char * dsetname,hid_t fid,hbool_t floating_number)79 static int check_data(const char *dsetname, hid_t fid, hbool_t floating_number)
80 {
81     hid_t       dataset;         /* handles */
82     double      data_in[NX+1][NY]; /* input buffer */
83     double      data_out[NX+1][NY]; /* output buffer */
84     long long   int_data_in[NX+1][NY]; /* input buffer */
85     long long   int_data_out[NX+1][NY]; /* output buffer */
86     int         i, j;
87     unsigned 	nerrors = 0;
88 
89     /*
90      * Open the regular dataset.
91      */
92     if((dataset = H5Dopen2(fid, dsetname, H5P_DEFAULT)) < 0)
93         TEST_ERROR;
94 
95     /*
96      * Data and output buffer initialization.
97      */
98     for (j = 0; j < NX; j++) {
99         for (i = 0; i < NY; i++) {
100             data_in[j][i] = ((double)(i + j + 1))/3;
101             data_out[j][i] = 0.0F;
102 
103             int_data_in[j][i] = i + j;
104             int_data_out[j][i] = 0;
105         }
106     }
107     for (i = 0; i < NY; i++) {
108         data_in[NX][i] = -2.2F;
109         data_out[NX][i] = 0.0F;
110 
111         int_data_in[NX][i] = -2;
112         int_data_out[NX][i] = 0;
113     }
114 
115     /*
116      * Read data from hyperslab in the file into the hyperslab in
117      * memory and display.
118      */
119     if(floating_number) {
120 	if(H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
121 		data_out) < 0)
122 	    TEST_ERROR;
123 
124 	/* Check results */
125 	for (j=0; j<(NX+1); j++) {
126 	    for (i=0; i<NY; i++) {
127 		if (!DBL_REL_EQUAL(data_out[j][i], data_in[j][i], 0.001F)) {
128 		    if (!nerrors++) {
129 			H5_FAILED();
130 			printf("element [%d][%d] is %g but should have been %g\n",
131 			       j, i, data_out[j][i], data_in[j][i]);
132 		    }
133 		}
134 	    }
135 	}
136     } else {
137 	if(H5Dread(dataset, H5T_NATIVE_LLONG, H5S_ALL, H5S_ALL, H5P_DEFAULT,
138 		int_data_out) < 0)
139 	    TEST_ERROR;
140 
141 	/* Check results */
142 	for (j=0; j<(NX+1); j++) {
143 	    for (i=0; i<NY; i++) {
144 		if (int_data_out[j][i] != int_data_in[j][i]) {
145 		    if (!nerrors++) {
146 			H5_FAILED();
147 			printf("element [%d][%d] is %d but should have been %d\n",
148 			       j, i, (int)int_data_out[j][i],
149 			       (int)int_data_in[j][i]);
150 		    }
151 		}
152 	    }
153 	}
154     }
155 
156     /*
157      * Close/release resources.
158      */
159     if(H5Dclose(dataset) < 0)
160         TEST_ERROR
161 
162     /* Failure */
163     if (nerrors) {
164         printf("total of %d errors out of %d elements\n", nerrors, NX*NY);
165         return 1;
166     }
167 
168     PASSED();
169     return 0;
170 
171 error:
172     H5E_BEGIN_TRY {
173         H5Dclose(dataset);
174     } H5E_END_TRY;
175     return 1;
176 }
177 
178 
179 /*-------------------------------------------------------------------------
180  * Function:    open_dataset
181  *
182  * Purpose:     Handle each dataset from the data file.
183  *
184  * Return:      Success:        0
185  *              Failure:        Number of failures
186  *
187  * Programmer:  Raymond Lu
188  *              21 January 2011
189  *
190  * Modifications:
191  *
192  *-------------------------------------------------------------------------
193  */
open_dataset(char * fname)194 static int open_dataset(char *fname)
195 {
196     const char *pathname = H5_get_srcdir_filename(fname); /* Corrected test file name */
197     hid_t       file;         /* handles */
198     unsigned 	nerrors = 0;
199     const char  *not_supported= "    filter is not enabled.";
200 
201     /*
202      * Open the file.
203      */
204     if((file = H5Fopen(pathname, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
205         TEST_ERROR;
206 
207     TESTING("regular dataset of LE DOUBLE");
208     nerrors += check_data(DATASETNAME, file, TRUE);
209 
210     TESTING("regular dataset of BE DOUBLE");
211     nerrors += check_data(DATASETNAME1, file, TRUE);
212 
213     TESTING("dataset of LE FLOAT with scale-offset filter");
214     nerrors += check_data(DATASETNAME2, file, TRUE);
215 
216     TESTING("dataset of BE FLOAT with scale-offset filter");
217     nerrors += check_data(DATASETNAME3, file, TRUE);
218 
219     TESTING("dataset of LE DOUBLE with scale-offset filter");
220     nerrors += check_data(DATASETNAME4, file, TRUE);
221 
222     TESTING("dataset of BE DOUBLE with scale-offset filter");
223     nerrors += check_data(DATASETNAME5, file, TRUE);
224 
225     TESTING("dataset of LE CHAR with scale-offset filter");
226     nerrors += check_data(DATASETNAME6, file, FALSE);
227 
228     TESTING("dataset of BE CHAR with scale-offset filter");
229     nerrors += check_data(DATASETNAME7, file, FALSE);
230 
231     TESTING("dataset of LE SHORT with scale-offset filter");
232     nerrors += check_data(DATASETNAME8, file, FALSE);
233 
234     TESTING("dataset of BE SHORT with scale-offset filter");
235     nerrors += check_data(DATASETNAME9, file, FALSE);
236 
237     TESTING("dataset of LE INT with scale-offset filter");
238     nerrors += check_data(DATASETNAME10, file, FALSE);
239 
240     TESTING("dataset of BE INT with scale-offset filter");
241     nerrors += check_data(DATASETNAME11, file, FALSE);
242 
243     TESTING("dataset of LE LONG LONG with scale-offset filter");
244     nerrors += check_data(DATASETNAME12, file, FALSE);
245 
246     TESTING("dataset of BE LONG LONG with scale-offset filter");
247     nerrors += check_data(DATASETNAME13, file, FALSE);
248 
249     TESTING("dataset of LE FLOAT with Fletcher32 filter");
250     nerrors += check_data(DATASETNAME14, file, TRUE);
251 
252     TESTING("dataset of BE FLOAT with Fletcher32 filter");
253     nerrors += check_data(DATASETNAME15, file, TRUE);
254 
255     TESTING("dataset of LE FLOAT with Deflate filter");
256 #ifdef H5_HAVE_FILTER_DEFLATE
257     nerrors += check_data(DATASETNAME16, file, TRUE);
258 #else /*H5_HAVE_FILTER_DEFLATE*/
259     SKIPPED();
260     puts(not_supported);
261 #endif /*H5_HAVE_FILTER_DEFLATE*/
262 
263     TESTING("dataset of BE FLOAT with Deflate filter");
264 #ifdef H5_HAVE_FILTER_DEFLATE
265     nerrors += check_data(DATASETNAME17, file, TRUE);
266 #else /*H5_HAVE_FILTER_DEFLATE*/
267     SKIPPED();
268     puts(not_supported);
269 #endif /*H5_HAVE_FILTER_DEFLATE*/
270 
271     TESTING("dataset of LE FLOAT with Szip filter");
272 #ifdef H5_HAVE_FILTER_SZIP
273     nerrors += check_data(DATASETNAME18, file, TRUE);
274 #else /*H5_HAVE_FILTER_SZIP*/
275     SKIPPED();
276     puts(not_supported);
277 #endif /*H5_HAVE_FILTER_SZIP*/
278 
279     TESTING("dataset of BE FLOAT with Szip filter");
280 #ifdef H5_HAVE_FILTER_SZIP
281     nerrors += check_data(DATASETNAME19, file, TRUE);
282 #else /*H5_HAVE_FILTER_SZIP*/
283     SKIPPED();
284     puts(not_supported);
285 #endif /*H5_HAVE_FILTER_SZIP*/
286 
287     TESTING("dataset of LE FLOAT with Shuffle filter");
288     nerrors += check_data(DATASETNAME20, file, TRUE);
289 
290     TESTING("dataset of BE FLOAT with Shuffle filter");
291     nerrors += check_data(DATASETNAME21, file, TRUE);
292 
293     TESTING("dataset of LE FLOAT with Nbit filter");
294     nerrors += check_data(DATASETNAME22, file, TRUE);
295 
296     TESTING("dataset of BE FLOAT with Nbit filter");
297     nerrors += check_data(DATASETNAME23, file, TRUE);
298 
299     if(H5Fclose(file))
300         TEST_ERROR
301     return 0;
302 
303 error:
304     H5E_BEGIN_TRY {
305         H5Fclose(file);
306     } H5E_END_TRY;
307     return nerrors;
308 }
309 
310 
311 /*-------------------------------------------------------------------------
312  * Function:    main
313  *
314  * Purpose:     Tests reading files created on LE and BE systems.
315  *
316  * Return:      Success:        exit(0)
317  *              Failure:        exit(1)
318  *
319  * Programmer:  Raymond Lu
320  *              Thursday, March 23, 2006
321  *
322  *-------------------------------------------------------------------------
323  */
main(void)324 int main(void)
325 {
326     char        filename[1024];
327     unsigned 	nerrors = 0;
328 
329     h5_reset();
330 
331     puts("Testing reading data created on Linux");
332     h5_fixname(FILENAME[1], H5P_DEFAULT, filename, sizeof filename);
333     nerrors += open_dataset(filename);
334 
335     puts("Testing reading data created on Solaris");
336     h5_fixname(FILENAME[2], H5P_DEFAULT, filename, sizeof filename);
337     nerrors += open_dataset(filename);
338 
339     if (nerrors) {
340         printf("***** %u FAILURE%s! *****\n",
341                nerrors, 1==nerrors?"":"S");
342         HDexit(1);
343     }
344 
345     printf("All data type tests passed.\n");
346     return 0;
347 }
348