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
16  *              October 14, 2001
17  *
18  * Purpose:	Tests the H5Tget_native_type function.
19  */
20 
21 #include "h5test.h"
22 
23 const char *FILENAME[] = {
24         "ntypes",
25         NULL
26 };
27 
28 #define DIM0    100
29 #define DIM1    200
30 #define DIM3    20
31 
32 
33 int	ipoints2[DIM0][DIM1], icheck2[DIM0][DIM1];
34 short	spoints2[DIM0][DIM1], scheck2[DIM0][DIM1];
35 int	ipoints3[DIM0][DIM1][5], icheck3[DIM0][DIM1][5];
36 
37 #define DSET_ATOMIC_NAME_1	"atomic_type_1"
38 #define DSET_ATOMIC_NAME_2	"atomic_type_2"
39 #define DSET_ATOMIC_NAME_3	"atomic_type_3"
40 #define DSET_ATOMIC_NAME_4	"atomic_type_4"
41 #define DSET_ATOMIC_NAME_5	"atomic_type_5"
42 #define DSET_COMPOUND_NAME      "compound_type"
43 #define DSET_COMPOUND_NAME_2    "compound_type_2"
44 #define DSET_COMPOUND_NAME_3    "compound_type_3"
45 #define DSET_COMPOUND_NAME_4    "compound_type_4"
46 #define DSET_ENUM_NAME	        "enum_type"
47 #define DSET_ARRAY_NAME	        "array_type"
48 #define DSET_ARRAY2_NAME	"array_type_2"
49 #define DSET_VL_NAME	        "vl_type"
50 #define DSET_VLSTR_NAME         "vlstr_type"
51 #define DSET_STR_NAME           "str_type"
52 #define DSET_OPAQUE_NAME        "opaque_type"
53 #define DSET1_BITFIELD_NAME     "bitfield_type_1"
54 #define DSET2_BITFIELD_NAME     "bitfield_type_2"
55 
56 #define SPACE1_DIM1             4
57 #define SPACE1_RANK             1
58 #define SPACE2_RANK	        2
59 #define SPACE2_DIM1	        10
60 #define SPACE2_DIM2	        10
61 #define BITFIELD_ENUMB          8
62 
63 
64 /*-------------------------------------------------------------------------
65  * Function:	test_atomic_dtype
66  *
67  * Purpose:	Test H5Tget_native_type for atomic datatype
68  *
69  * Return:	Success:	0
70  *
71  *		Failure:	-1
72  *
73  * Programmer:	Raymond Lu
74  *		October 15, 2002
75  *
76  * Modifications:
77  *
78  *-------------------------------------------------------------------------
79  */
80 static herr_t
test_atomic_dtype(hid_t file)81 test_atomic_dtype(hid_t file)
82 {
83     hid_t	dataset = -1, space = -1;
84     hid_t       dtype = -1, native_type = -1;
85     int		i, j, n;
86     hsize_t	dims[2];
87     void       *tmp = NULL;
88 
89     TESTING("atomic datatype");
90 
91     /* Initialize the dataset */
92     for(i = n = 0; i < DIM0; i++)
93         for(j = 0; j < DIM1; j++)
94             ipoints2[i][j] = n++;
95 
96     /* Create the data space */
97     dims[0] = DIM0;
98     dims[1] = DIM1;
99     if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;
100 
101     /*------------------- Test data values ------------------------*/
102     /* Create the dataset */
103     if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_1, H5T_STD_I32BE, space,
104             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
105         TEST_ERROR;
106 
107     /* Write the data to the dataset */
108     if(H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints2) < 0)
109         TEST_ERROR;
110 
111     /* Close dataset */
112     if(H5Dclose(dataset) < 0) TEST_ERROR;
113 
114     /* Open dataset again to check H5Tget_native_type */
115     if((dataset = H5Dopen2(file, DSET_ATOMIC_NAME_1, H5P_DEFAULT)) < 0) TEST_ERROR;
116 
117     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
118 
119     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
120         TEST_ERROR;
121 
122     /* Verify the datatype retrieved and converted */
123     if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_INT))
124         TEST_ERROR;
125     if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I32BE))
126         TEST_ERROR;
127     if(H5T_INTEGER != H5Tget_class(native_type))
128         TEST_ERROR;
129 
130     /* Read the dataset back.  The temporary buffer is for special platforms
131      * like Cray. */
132     if(NULL == (tmp = HDmalloc((size_t)(DIM0 * DIM1 * H5Tget_size(native_type)))))
133         TEST_ERROR
134 
135     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
136         TEST_ERROR;
137 
138     /* Copy data from temporary buffer to destination buffer */
139     HDmemcpy(icheck2, tmp, (size_t)(DIM0 * DIM1 * H5Tget_size(native_type)));
140     HDfree(tmp);
141     tmp = NULL;
142 
143     /* Convert to the integer type */
144     if(H5Tconvert(native_type, H5T_NATIVE_INT, (DIM0 * DIM1), icheck2, NULL, H5P_DEFAULT) < 0)
145         TEST_ERROR;
146 
147     /* Check that the values read are the same as the values written */
148     for(i = 0; i < DIM0; i++)
149         for(j = 0; j < DIM1; j++)
150             if(ipoints2[i][j] != icheck2[i][j]) {
151                 H5_FAILED();
152                 HDprintf("    Read different values than written.\n");
153                 HDprintf("    At index %d,%d\n", i, j);
154                 goto error;
155             } /* end if */
156 
157     if(H5Dclose(dataset) < 0) TEST_ERROR;
158     if(H5Tclose(native_type) < 0) TEST_ERROR;
159     if(H5Tclose(dtype) < 0) TEST_ERROR;
160 
161     /*------------------ Test different data types ----------------*/
162 
163     /* Create the dataset of H5T_STD_I64LE */
164     if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_2, H5T_STD_I64LE, space,
165             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
166         TEST_ERROR;
167 
168     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
169 
170     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
171         TEST_ERROR;
172 
173     /* Verify the datatype retrieved and converted */
174     if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_LLONG))
175         TEST_ERROR;
176     if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I64LE))
177         TEST_ERROR;
178     if(H5T_INTEGER!=H5Tget_class(native_type))
179         TEST_ERROR;
180 
181     if(H5Dclose(dataset) < 0) TEST_ERROR;
182     if(H5Tclose(native_type) < 0) TEST_ERROR;
183     if(H5Tclose(dtype) < 0) TEST_ERROR;
184 
185 
186     /* Create the dataset of H5T_STD_I8LE */
187     if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_3, H5T_STD_I8LE, space,
188             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
189         TEST_ERROR;
190 
191     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
192 
193     if((native_type = H5Tget_native_type(dtype, H5T_DIR_ASCEND)) < 0)
194         TEST_ERROR;
195 
196     /* Verify the datatype retrieved and converted */
197     if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_CHAR))
198         TEST_ERROR;
199     if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I8LE))
200         TEST_ERROR;
201     if(H5T_INTEGER!=H5Tget_class(native_type))
202         TEST_ERROR;
203 
204     if(H5Dclose(dataset) < 0) TEST_ERROR;
205     if(H5Tclose(native_type) < 0) TEST_ERROR;
206     if(H5Tclose(dtype) < 0) TEST_ERROR;
207 
208 
209     /* Create the dataset of H5T_IEEE_F32BE */
210     if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_4, H5T_IEEE_F32BE, space,
211             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
212         TEST_ERROR;
213 
214     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
215 
216     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DESCEND)) < 0)
217         TEST_ERROR;
218 
219     /* Verify the datatype retrieved and converted */
220     if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_FLOAT))
221         TEST_ERROR;
222     if(H5Tget_size(native_type) < H5Tget_size(H5T_IEEE_F32BE))
223         TEST_ERROR;
224     if(H5T_FLOAT!=H5Tget_class(native_type))
225         TEST_ERROR;
226 
227     if(H5Dclose(dataset) < 0) TEST_ERROR;
228     if(H5Tclose(native_type) < 0) TEST_ERROR;
229     if(H5Tclose(dtype) < 0) TEST_ERROR;
230 
231 
232     /* Create the dataset of H5T_IEEE_F64BE */
233     if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_5, H5T_IEEE_F64BE, space,
234             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
235         TEST_ERROR;
236 
237     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
238 
239     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DESCEND)) < 0)
240         TEST_ERROR;
241 
242     /* Verify the datatype retrieved and converted */
243     if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_DOUBLE))
244         TEST_ERROR;
245     if(H5Tget_size(native_type) < H5Tget_size(H5T_IEEE_F64BE))
246         TEST_ERROR;
247     if(H5T_FLOAT != H5Tget_class(native_type))
248         TEST_ERROR;
249 
250     if(H5Dclose(dataset) < 0) TEST_ERROR;
251     if(H5Tclose(native_type) < 0) TEST_ERROR;
252     if(H5Tclose(dtype) < 0) TEST_ERROR;
253 
254 
255     /* Close dataspace */
256     if(H5Sclose(space) < 0) TEST_ERROR;
257 
258     PASSED();
259 
260     return 0;
261 
262 error:
263     if(tmp)
264         HDfree(tmp);
265 
266     H5E_BEGIN_TRY {
267         H5Dclose(dataset);
268         H5Tclose(native_type);
269         H5Tclose(dtype);
270         H5Sclose(space);
271     } H5E_END_TRY;
272 
273     return -1;
274 }
275 
276 
277 /*-------------------------------------------------------------------------
278  * Function:	test_compound_dtype2
279  *
280  * Purpose:	Test H5Tget_native_type for compound datatype
281  *
282  * Return:	Success:	0
283  *
284  *		Failure:	-1
285  *
286  * Programmer:	Raymond Lu
287  *		October 15, 2002
288  *
289  * Modifications:
290  *
291  *-------------------------------------------------------------------------
292  */
293 static herr_t
test_compound_dtype2(hid_t file)294 test_compound_dtype2(hid_t file)
295 {
296     typedef struct s2 {
297         short           c2;
298         long            l2;
299         long long       ll2;
300     } s2;
301     typedef struct s1 {
302         char            c;
303         int             i;
304         s2              st;
305         unsigned long long       l;
306     } s1;
307     hid_t	dataset = -1, space = -1;
308     hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid_m = -1,
309                 tid_m2 = -1, mem_id = -1, nest_mem_id = -1;
310     int		i, j, n;
311     hsize_t	dims[2];
312     s1         *temp_point = NULL, *temp_check = NULL;
313     s1 	       *points = NULL, *check = NULL;
314     void       *tmp = NULL, *bkg = NULL;
315 
316     TESTING("nested compound datatype");
317 
318     /* Allocate space for the points & check arrays */
319     if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
320         TEST_ERROR;
321     if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
322         TEST_ERROR;
323 
324     /* Initialize the dataset */
325     for(i = n = 0, temp_point = points; i < DIM0; i++) {
326         for(j = 0; j < DIM1; j++, temp_point++) {
327             temp_point->c = 't';
328             temp_point->i = n++;
329             temp_point->st.c2 = (short)(i + j);
330             temp_point->st.l2 = (i * 5 + j * 50) * n;
331             temp_point->st.ll2 = (i * 10 + j * 100) * n;
332             temp_point->l = (unsigned long long)((i * 40 + j * 400) * n);
333         } /* end for */
334     } /* end for */
335 
336     /* Create the data space */
337     dims[0] = DIM0;
338     dims[1] = DIM1;
339     if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;
340 
341     /* Create compound datatype for disk storage */
342     if((tid2=H5Tcreate(H5T_COMPOUND, sizeof(s2))) < 0) TEST_ERROR;
343     if((tid=H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
344 
345     /* Insert and pack members */
346     if(H5Tinsert(tid2, "c2", HOFFSET(s2, c2), H5T_STD_I16BE) < 0) TEST_ERROR;
347 #if H5_SIZEOF_LONG == 4
348     if(H5Tinsert(tid2, "l2", HOFFSET(s2, l2), H5T_STD_I32LE) < 0) TEST_ERROR;
349 #elif H5_SIZEOF_LONG == 8
350     if(H5Tinsert(tid2, "l2", HOFFSET(s2, l2), H5T_STD_I64LE) < 0) TEST_ERROR;
351 #else
352 #error "Unknown 'long' size"
353 #endif
354 #if H5_SIZEOF_LONG_LONG == 4
355     if(H5Tinsert(tid2, "ll2", HOFFSET(s2, ll2), H5T_STD_I32BE) < 0) TEST_ERROR;
356 #elif H5_SIZEOF_LONG_LONG == 8
357     if(H5Tinsert(tid2, "ll2", HOFFSET(s2, ll2), H5T_STD_I64BE) < 0) TEST_ERROR;
358 #else
359 #error "Unknown 'long long' size"
360 #endif
361 
362     if(H5Tinsert(tid, "c", HOFFSET(s1, c), H5T_STD_U8LE) < 0) TEST_ERROR;
363     if(H5Tinsert(tid, "i", HOFFSET(s1, i), H5T_STD_I32LE) < 0) TEST_ERROR;
364     if(H5Tinsert(tid, "st", HOFFSET(s1, st), tid2) < 0) TEST_ERROR;
365 #if H5_SIZEOF_LONG_LONG == 4
366     if(H5Tinsert(tid, "l", HOFFSET(s1, l), H5T_STD_U32BE) < 0) TEST_ERROR;
367 #elif H5_SIZEOF_LONG_LONG == 8
368     if(H5Tinsert(tid, "l", HOFFSET(s1, l), H5T_STD_U64BE) < 0) TEST_ERROR;
369 #else
370 #error "Unknown 'long long' size"
371 #endif
372 
373     /* Take away the paddings */
374     if(H5Tpack(tid) < 0) TEST_ERROR;
375 
376     /* Create the dataset */
377     if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME_2, tid, space,
378             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
379         TEST_ERROR;
380 
381     /* Create compound datatype for memory */
382     if((tid_m2 = H5Tcreate(H5T_COMPOUND, sizeof(s2))) < 0) TEST_ERROR;
383     if((tid_m = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
384 
385     /* Insert members */
386     if(H5Tinsert(tid_m2, "c2", HOFFSET(s2, c2), H5T_NATIVE_SHORT) < 0) TEST_ERROR;
387     if(H5Tinsert(tid_m2, "l2", HOFFSET(s2, l2), H5T_NATIVE_LONG) < 0) TEST_ERROR;
388     if(H5Tinsert(tid_m2, "ll2", HOFFSET(s2, ll2), H5T_NATIVE_LLONG) < 0) TEST_ERROR;
389     if(H5Tinsert(tid_m, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
390     if(H5Tinsert(tid_m, "i", HOFFSET(s1, i), H5T_NATIVE_INT) < 0) TEST_ERROR;
391     if(H5Tinsert(tid_m, "st", HOFFSET(s1, st), tid_m2) < 0) TEST_ERROR;
392     if(H5Tinsert(tid_m, "l", HOFFSET(s1, l), H5T_NATIVE_ULLONG) < 0) TEST_ERROR;
393 
394     /* Write the data to the dataset */
395     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
396         TEST_ERROR;
397 
398     /* Close dataset */
399     if(H5Dclose(dataset) < 0) TEST_ERROR;
400 
401     /* Close dataspace */
402     if(H5Sclose(space) < 0) TEST_ERROR;
403 
404 
405     /* Open dataset again to check H5Tget_native_type */
406     if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME_2, H5P_DEFAULT)) < 0) TEST_ERROR;
407 
408     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
409 
410     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
411         TEST_ERROR;
412 
413     if(H5Tequal(native_type, tid_m) != TRUE)
414         TEST_ERROR;
415 
416     /* Verify the datatype of each field retrieved and converted */
417     /* check the char member */
418     if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
419         TEST_ERROR;
420     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_SCHAR))
421         TEST_ERROR;
422     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I8LE))
423         TEST_ERROR;
424     if(H5T_INTEGER != H5Tget_class(mem_id))
425         TEST_ERROR;
426     H5Tclose(mem_id);
427 
428     /* check the integer member */
429     if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
430         TEST_ERROR;
431     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_INT))
432         TEST_ERROR;
433     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I32LE))
434         TEST_ERROR;
435     if(H5T_INTEGER!=H5Tget_class(mem_id))
436         TEST_ERROR;
437     H5Tclose(mem_id);
438 
439     /* check the unsigned long long member */
440     if((mem_id = H5Tget_member_type(native_type, 3)) < 0)
441         TEST_ERROR;
442     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_ULLONG))
443         TEST_ERROR;
444     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U64BE))
445         TEST_ERROR;
446     if(H5T_INTEGER!=H5Tget_class(mem_id))
447         TEST_ERROR;
448     H5Tclose(mem_id);
449 
450     /* check the nested compound member */
451     if((nest_mem_id = H5Tget_member_type(native_type, 2)) < 0)
452         TEST_ERROR;
453 
454     if((mem_id = H5Tget_member_type(nest_mem_id, 0)) < 0)
455         TEST_ERROR;
456     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_SHORT))
457         TEST_ERROR;
458     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I16BE))
459         TEST_ERROR;
460     if(H5T_INTEGER!=H5Tget_class(mem_id))
461         TEST_ERROR;
462     H5Tclose(mem_id);
463 
464     if((mem_id = H5Tget_member_type(nest_mem_id, 1)) < 0)
465         TEST_ERROR;
466     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LONG))
467         TEST_ERROR;
468 #if H5_SIZEOF_LONG==4
469     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I32LE)) TEST_ERROR;
470 #elif H5_SIZEOF_LONG==8
471     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64LE)) TEST_ERROR;
472 #else
473 #error "Unknown 'long' size"
474 #endif
475     if(H5T_INTEGER!=H5Tget_class(mem_id))
476         TEST_ERROR;
477     H5Tclose(mem_id);
478 
479     if((mem_id = H5Tget_member_type(nest_mem_id, 2)) < 0)
480         TEST_ERROR;
481     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
482         TEST_ERROR;
483 #if H5_SIZEOF_LONG_LONG==4
484     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I32LE)) TEST_ERROR;
485 #elif H5_SIZEOF_LONG_LONG==8
486     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64LE)) TEST_ERROR;
487 #else
488 #error "Unknown 'long long' size"
489 #endif
490     if(H5T_INTEGER!=H5Tget_class(mem_id))
491         TEST_ERROR;
492     H5Tclose(mem_id);
493 
494     /* Read the dataset back.  Temporary buffer is for special platforms like
495      * Cray */
496     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
497         TEST_ERROR
498     if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
499         TEST_ERROR;
500 
501     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
502         TEST_ERROR;
503 
504     HDmemcpy(check, tmp, DIM0*DIM1*H5Tget_size(native_type));
505     HDfree(tmp);
506     tmp = NULL;
507 
508     if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
509         TEST_ERROR;
510 
511     HDfree(bkg);
512     bkg = NULL;
513 
514     /* Check that the values read are the same as the values written */
515     for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++) {
516         for(j = 0; j < DIM1; j++, temp_point++, temp_check++) {
517             if(temp_point->c != temp_check->c ||
518                     temp_point->i != temp_check->i ||
519                     temp_point->st.c2 != temp_check->st.c2 ||
520                     temp_point->st.l2 != temp_check->st.l2 ||
521                     temp_point->st.ll2 != temp_check->st.ll2 ||
522                     temp_point->l != temp_check->l ) {
523                 H5_FAILED();
524                 HDprintf("    Read different values than written.\n");
525                 HDprintf("    At index %d,%d\n", i, j);
526                 goto error;
527             } /* end if */
528         } /* end for */
529     } /* end for */
530 
531     /* Close temporary datatypes */
532     if(H5Tclose(tid2) < 0) TEST_ERROR;
533     if(H5Tclose(tid) < 0) TEST_ERROR;
534     if(H5Tclose(tid_m2) < 0) TEST_ERROR;
535 
536     /* Close HDF5 objects */
537     H5Dclose(dataset);
538     H5Tclose(dtype);
539     H5Tclose(native_type);
540     H5Tclose(tid_m);
541 
542     /* Free memory for test data */
543     HDfree(points);
544     HDfree(check);
545 
546     PASSED();
547     return 0;
548 
549 error:
550     if(tmp)
551         HDfree(tmp);
552     if(bkg)
553         HDfree(bkg);
554     if(points)
555         HDfree(points);
556     if(check)
557         HDfree(check);
558 
559     H5E_BEGIN_TRY {
560         H5Tclose(tid);
561         H5Tclose(tid2);
562         H5Tclose(tid_m);
563         H5Tclose(tid_m2);
564         H5Sclose(space);
565         H5Tclose(mem_id);
566         H5Tclose(nest_mem_id);
567         H5Dclose(dataset);
568         H5Tclose(dtype);
569         H5Tclose(native_type);
570     } H5E_END_TRY;
571 
572     return -1;
573 }
574 
575 
576 /*-------------------------------------------------------------------------
577  * Function:	test_compound_dtype
578  *
579  * Purpose:	Test H5Tget_native_type for compound datatype
580  *
581  * Return:	Success:	0
582  *
583  *		Failure:	-1
584  *
585  * Programmer:	Raymond Lu
586  *		October 15, 2002
587  *
588  * Modifications:
589  *
590  *-------------------------------------------------------------------------
591  */
592 static herr_t
test_compound_dtype(hid_t file)593 test_compound_dtype(hid_t file)
594 {
595     typedef struct {
596         char            c;
597         unsigned int    i;
598         long long       l;
599     } s1;
600     hid_t	dataset = -1, space = -1;
601     hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, mem_id = -1;
602     int		i, j, n;
603     hsize_t	dims[2];
604     s1         *temp_point = NULL;
605     s1         *temp_check = NULL;
606     s1         *points = NULL;
607     s1         *check = NULL;
608     void       *tmp = NULL;
609     void       *bkg = NULL;
610 
611     TESTING("compound datatype");
612 
613     /* Allocate space for the points & check arrays */
614     if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
615         TEST_ERROR;
616     if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
617         TEST_ERROR;
618 
619     /* Initialize the dataset */
620     for(i = n = 0, temp_point = points; i < DIM0; i++)
621         for(j = 0; j < DIM1; j++, temp_point++) {
622             temp_point->c = 't';
623             temp_point->i = (unsigned int)(n++);
624             temp_point->l = (i * 10 + j * 100) * n;
625         } /* end for */
626 
627     /* Create the data space */
628     dims[0] = DIM0;
629     dims[1] = DIM1;
630     if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;
631 
632     /* Create compound datatype for disk storage */
633     if((tid = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
634 
635     /* Insert members */
636     if(H5Tinsert(tid, "c", 0, H5T_STD_U8LE) < 0) TEST_ERROR;
637     if(H5Tinsert(tid, "i", 1, H5T_STD_U32LE) < 0) TEST_ERROR;
638     if(H5Tinsert(tid, "l", 5, H5T_STD_I64BE) < 0) TEST_ERROR;
639 
640     /* Create the dataset */
641     if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME, tid, space,
642             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
643         TEST_ERROR;
644 
645     /* Create compound datatype for datatype in memory */
646     if((tid2 = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
647     if(H5Tinsert(tid2, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
648     if(H5Tinsert(tid2, "i", HOFFSET(s1, i), H5T_NATIVE_UINT) < 0) TEST_ERROR;
649     if(H5Tinsert(tid2, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;
650 
651     /* Write the data to the dataset */
652     if(H5Dwrite(dataset, tid2, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
653         TEST_ERROR;
654 
655     /* Close dataset */
656     if(H5Dclose(dataset) < 0) TEST_ERROR;
657 
658     /* Close dataspace */
659     if(H5Sclose(space) < 0) TEST_ERROR;
660 
661 
662     /* Open dataset again to check H5Tget_native_type */
663     if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
664 
665     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
666 
667     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
668         TEST_ERROR;
669 
670     if(H5Tequal(native_type, tid2) != TRUE)
671         TEST_ERROR;
672 
673     /* Verify the datatype of each field retrieved and converted */
674     if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
675         TEST_ERROR;
676     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UCHAR))
677         TEST_ERROR;
678     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U8LE))
679         TEST_ERROR;
680     if(H5T_INTEGER!=H5Tget_class(mem_id))
681         TEST_ERROR;
682     H5Tclose(mem_id);
683 
684     if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
685         TEST_ERROR;
686     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UINT))
687         TEST_ERROR;
688     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U32LE))
689         TEST_ERROR;
690     if(H5T_INTEGER!=H5Tget_class(mem_id))
691         TEST_ERROR;
692     H5Tclose(mem_id);
693 
694     if((mem_id = H5Tget_member_type(native_type, 2)) < 0)
695         TEST_ERROR;
696     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
697         TEST_ERROR;
698     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64BE))
699         TEST_ERROR;
700     if(H5T_INTEGER!=H5Tget_class(mem_id))
701         TEST_ERROR;
702     H5Tclose(mem_id);
703 
704     /* Read the dataset back.  Temporary buffer is for special platforms like
705      * Cray */
706     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
707         TEST_ERROR
708     if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
709         TEST_ERROR
710 
711     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
712         TEST_ERROR;
713 
714     HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
715     HDfree(tmp);
716     tmp = NULL;
717 
718     if(H5Tconvert(native_type, tid2, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
719         TEST_ERROR;
720 
721     HDfree(bkg);
722     bkg = NULL;
723 
724     /* Check that the values read are the same as the values written */
725     for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
726         for(j = 0; j < DIM1; j++, temp_point++, temp_check++)
727             if(temp_point->c != temp_check->c ||
728                     temp_point->i != temp_check->i ||
729                     temp_point->l != temp_check->l ) {
730                 H5_FAILED();
731                 HDprintf("    Read different values than written.\n");
732                 HDprintf("    At index %d,%d\n", i, j);
733                 goto error;
734             } /* end if */
735 
736     /* Close datatype */
737     if(H5Tclose(tid) < 0) TEST_ERROR;
738 
739     H5Dclose(dataset);
740     H5Tclose(dtype);
741     H5Tclose(native_type);
742     H5Tclose(tid2);
743 
744     /* Free memory for test data */
745     HDfree(points);
746     HDfree(check);
747 
748     PASSED();
749     return 0;
750 
751 error:
752     /* Free memory for test data */
753     if(tmp)
754         HDfree(tmp);
755     if(bkg)
756         HDfree(bkg);
757     if(points)
758         HDfree(points);
759     if(check)
760         HDfree(check);
761 
762     H5E_BEGIN_TRY {
763         H5Tclose(tid);
764         H5Sclose(space);
765         H5Tclose(mem_id);
766         H5Dclose(dataset);
767         H5Tclose(dtype);
768         H5Tclose(native_type);
769         H5Tclose(tid2);
770     } H5E_END_TRY;
771 
772     return -1;
773 }
774 
775 
776 /*-------------------------------------------------------------------------
777  * Function:	test_compound_dtype3
778  *
779  * Purpose:	Test H5Tget_native_type for compound datatype
780  *
781  * Return:	Success:	0
782  *
783  *		Failure:	-1
784  *
785  * Programmer:	Raymond Lu
786  *		October 15, 2002
787  *
788  * Modifications:
789  *
790  *-------------------------------------------------------------------------
791  */
792 static herr_t
test_compound_dtype3(hid_t file)793 test_compound_dtype3(hid_t file)
794 {
795     typedef struct {
796         char            c;
797         int             a[5];
798         long long       l;
799     } s1;
800     hid_t	dataset = -1, space = -1;
801     hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid_m = -1,
802                 tid_m2 = -1, mem_id = -1, nest_mem_id = -1;
803     hsize_t     array_dims[1] = {5};
804     int		i, j, k, n;
805     hsize_t	dims[2];
806     s1         *temp_point = NULL, *temp_check = NULL;
807     s1 	       *points = NULL, *check = NULL;
808     void       *tmp = NULL, *bkg = NULL;
809 
810     TESTING("compound datatype with array as field");
811 
812     /* Allocate space for the points & check arrays */
813     if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
814         TEST_ERROR;
815     if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
816         TEST_ERROR;
817 
818     /* Initialize the dataset */
819     for(i = n = 0, temp_point = points; i < DIM0; i++)
820         for(j = 0; j < DIM1; j++, temp_point++) {
821             temp_point->c = 't';
822             temp_point->l = (i * 10 + j * 100) * n;
823             for(k = 0; k < 5; k++)
824                 (temp_point->a)[k] = n++;
825         } /* end for */
826 
827     /* Create the data space */
828     dims[0] = DIM0;
829     dims[1] = DIM1;
830     if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;
831 
832     /* Create array datatype */
833     if((tid2 = H5Tarray_create2(H5T_STD_I32LE, 1, array_dims)) < 0) TEST_ERROR;
834 
835     /* Create compound datatype for disk storage */
836     if((tid = H5Tcreate(H5T_COMPOUND, 29)) < 0) TEST_ERROR;
837 
838     /* Insert members */
839     if(H5Tinsert(tid, "c", 0, H5T_STD_U8LE) < 0) TEST_ERROR;
840     if(H5Tinsert(tid, "a", 1, tid2) < 0) TEST_ERROR;
841     if(H5Tinsert(tid, "l", 21, H5T_STD_I64BE) < 0) TEST_ERROR;
842 
843     /* Create the dataset */
844     if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME_3, tid, space,
845             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
846         TEST_ERROR;
847 
848     /* Create array datatype */
849     if((tid_m2 = H5Tarray_create2(H5T_NATIVE_INT, 1, array_dims)) < 0) TEST_ERROR;
850 
851     /* Create compound datatype for datatype in memory */
852     if((tid_m = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
853     if(H5Tinsert(tid_m, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
854     if(H5Tinsert(tid_m, "a", HOFFSET(s1, a), tid_m2) < 0) TEST_ERROR;
855     if(H5Tinsert(tid_m, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;
856 
857     /* Write the data to the dataset */
858     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
859         TEST_ERROR;
860 
861     /* Close dataset */
862     if(H5Dclose(dataset) < 0) TEST_ERROR;
863 
864     /* Close datatype */
865     if(H5Tclose(tid) < 0) TEST_ERROR;
866     if(H5Tclose(tid2) < 0) TEST_ERROR;
867 
868     /* Close dataspace */
869     if(H5Sclose(space) < 0) TEST_ERROR;
870 
871 
872     /* Open dataset again to check H5Tget_native_type */
873     if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME_3, H5P_DEFAULT)) < 0) TEST_ERROR;
874 
875     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
876 
877     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
878         TEST_ERROR;
879 
880     /* Verify the datatype of each field retrieved and converted */
881     /* check the char member */
882     if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
883         TEST_ERROR;
884     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UCHAR))
885         TEST_ERROR;
886     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U8LE))
887         TEST_ERROR;
888     if(H5T_INTEGER!=H5Tget_class(mem_id))
889         TEST_ERROR;
890     H5Tclose(mem_id);
891 
892     /* check the array member */
893     if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
894         TEST_ERROR;
895     if(H5T_ARRAY!=H5Tget_class(mem_id))
896         TEST_ERROR;
897     if((nest_mem_id = H5Tget_super(mem_id)) < 0)
898         TEST_ERROR;
899     if(H5Tget_order(nest_mem_id) != H5Tget_order(H5T_NATIVE_INT))
900         TEST_ERROR;
901     if(H5Tget_size(nest_mem_id) < H5Tget_size(H5T_STD_I32LE))
902         TEST_ERROR;
903     if(H5T_INTEGER!=H5Tget_class(nest_mem_id))
904         TEST_ERROR;
905     H5Tclose(nest_mem_id);
906     H5Tclose(mem_id);
907 
908     /* check the long long member */
909     if((mem_id = H5Tget_member_type(native_type, 2)) < 0)
910         TEST_ERROR;
911     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
912         TEST_ERROR;
913     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64BE))
914         TEST_ERROR;
915     if(H5T_INTEGER!=H5Tget_class(mem_id))
916         TEST_ERROR;
917     H5Tclose(mem_id);
918 
919     /* Read the dataset back.  Temporary buffer is for special platforms like
920      * Cray */
921     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
922         TEST_ERROR;
923     if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
924         TEST_ERROR;
925 
926     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
927         TEST_ERROR;
928 
929     HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
930     HDfree(tmp);
931     tmp = NULL;
932 
933     if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
934         TEST_ERROR;
935 
936     HDfree(bkg);
937     bkg = NULL;
938 
939     /* Check that the values read are the same as the values written */
940     for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
941         for(j = 0; j < DIM1; j++, temp_point++, temp_check++) {
942             if(temp_point->c != temp_check->c ||
943                     temp_point->l != temp_check->l ) {
944                 H5_FAILED();
945                 HDprintf("    Read different values than written.\n");
946                 HDprintf("    At index %d,%d\n", i, j);
947                 goto error;
948             } /* end if */
949 
950             for(k = 0; k < 5; k++) {
951                 if(temp_point->a[k] != temp_check->a[k]) {
952                     H5_FAILED();
953                     HDprintf("    Read different values than written.\n");
954                     HDprintf("    At index %d,%d,%d\n", i, j, k);
955                     goto error;
956                 } /* end if */
957             } /* end for */
958         } /* end for */
959 
960     H5Dclose(dataset);
961     H5Tclose(dtype);
962     H5Tclose(native_type);
963     H5Tclose(tid_m);
964     H5Tclose(tid_m2);
965 
966     /* Free memory for test data */
967     HDfree(points);
968     HDfree(check);
969 
970     PASSED();
971     return 0;
972 
973 error:
974     /* Free memory for test data */
975     if(tmp)
976         HDfree(tmp);
977     if(bkg)
978         HDfree(bkg);
979     if(points)
980         HDfree(points);
981     if(check)
982         HDfree(check);
983 
984     H5E_BEGIN_TRY {
985         H5Sclose(space);
986         H5Tclose(mem_id);
987         H5Tclose(nest_mem_id);
988         H5Dclose(dataset);
989         H5Tclose(dtype);
990         H5Tclose(native_type);
991         H5Tclose(tid);
992         H5Tclose(tid2);
993         H5Tclose(tid_m);
994         H5Tclose(tid_m2);
995     } H5E_END_TRY;
996 
997     return -1;
998 }
999 
1000 
1001 /*-------------------------------------------------------------------------
1002  * Function:	test_compound_opaque
1003  *
1004  * Purpose:	Test H5Tget_native_type for compound datatype with opaque field
1005  *
1006  * Return:	Success:	0
1007  *
1008  *		Failure:	-1
1009  *
1010  * Programmer:	Quincey Koziol
1011  *		January 31, 2004
1012  *
1013  * Modifications:
1014  *
1015  *-------------------------------------------------------------------------
1016  */
1017 static herr_t
test_compound_opaque(hid_t file)1018 test_compound_opaque(hid_t file)
1019 {
1020     typedef struct {
1021         char            c;
1022         unsigned char   o[5];
1023         long long       l;
1024     } s1;
1025     hid_t	dataset = -1, space = -1;
1026     hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid_m = -1,
1027                 mem_id = -1;
1028     int		i, j, k, n;
1029     hsize_t	dims[2];
1030     s1         *temp_point = NULL, *temp_check = NULL;
1031     s1 	       *points = NULL, *check = NULL;
1032     void       *tmp = NULL, *bkg = NULL;
1033 
1034     TESTING("compound datatype with opaque field");
1035 
1036     /* Allocate space for the points & check arrays */
1037     if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
1038         TEST_ERROR;
1039     if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
1040         TEST_ERROR;
1041 
1042     /* Initialize the dataset */
1043     for(i = n = 0, temp_point = points; i < DIM0; i++)
1044         for(j = 0; j < DIM1; j++, temp_point++) {
1045             temp_point->c = 't';
1046             temp_point->l = (i * 10 + j * 100) * n;
1047             for(k = 0; k < 5; k++)
1048                 (temp_point->o)[k] = (unsigned char)(n++);
1049         } /* end for */
1050 
1051     /* Create the data space */
1052     dims[0] = DIM0;
1053     dims[1] = DIM1;
1054     if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;
1055 
1056     /* Create opaque datatype */
1057     if((tid2 = H5Tcreate(H5T_OPAQUE, sizeof(temp_point->o))) < 0) TEST_ERROR;
1058     if(H5Tset_tag(tid2, "testing opaque field") < 0) TEST_ERROR;
1059 
1060     /* Create compound datatype for disk storage */
1061     if((tid = H5Tcreate(H5T_COMPOUND, 14)) < 0) TEST_ERROR;
1062 
1063     /* Insert members */
1064     if(H5Tinsert(tid, "c", 0, H5T_STD_U8LE) < 0) TEST_ERROR;
1065     if(H5Tinsert(tid, "o", 1, tid2) < 0) TEST_ERROR;
1066     if(H5Tinsert(tid, "l", 6, H5T_STD_I64BE) < 0) TEST_ERROR;
1067 
1068     /* Create the dataset */
1069     if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME_4, tid, space,
1070             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
1071         TEST_ERROR;
1072 
1073     /* Create compound datatype for datatype in memory */
1074     if((tid_m = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
1075     if(H5Tinsert(tid_m, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
1076     if(H5Tinsert(tid_m, "o", HOFFSET(s1, o), tid2) < 0) TEST_ERROR;
1077     if(H5Tinsert(tid_m, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;
1078 
1079     /* Write the data to the dataset */
1080     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
1081         TEST_ERROR;
1082 
1083     /* Close dataset */
1084     if(H5Dclose(dataset) < 0) TEST_ERROR;
1085 
1086     /* Close datatype */
1087     if(H5Tclose(tid) < 0) TEST_ERROR;
1088     if(H5Tclose(tid2) < 0) TEST_ERROR;
1089 
1090     /* Close dataspace */
1091     if(H5Sclose(space) < 0) TEST_ERROR;
1092 
1093 
1094     /* Open dataset again to check H5Tget_native_type */
1095     if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME_4, H5P_DEFAULT)) < 0) TEST_ERROR;
1096 
1097     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
1098 
1099     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
1100         TEST_ERROR;
1101 
1102     /* Verify the datatype of each field retrieved and converted */
1103     /* check the char member */
1104     if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
1105         TEST_ERROR;
1106     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UCHAR))
1107         TEST_ERROR;
1108     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U8LE))
1109         TEST_ERROR;
1110     if(H5T_INTEGER!=H5Tget_class(mem_id))
1111         TEST_ERROR;
1112     H5Tclose(mem_id);
1113 
1114     /* check the array member */
1115     if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
1116         TEST_ERROR;
1117     if(H5T_OPAQUE!=H5Tget_class(mem_id))
1118         TEST_ERROR;
1119     if(H5Tget_size(mem_id) != sizeof(temp_point->o))
1120         TEST_ERROR;
1121     H5Tclose(mem_id);
1122 
1123     /* check the long long member */
1124     if((mem_id = H5Tget_member_type(native_type, 2)) < 0)
1125         TEST_ERROR;
1126     if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
1127         TEST_ERROR;
1128     if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64BE))
1129         TEST_ERROR;
1130     if(H5T_INTEGER!=H5Tget_class(mem_id))
1131         TEST_ERROR;
1132     H5Tclose(mem_id);
1133 
1134     /* Read the dataset back.  Temporary buffer is for special platforms like
1135      * Cray */
1136     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
1137         TEST_ERROR;
1138     if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
1139         TEST_ERROR;
1140 
1141     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
1142         TEST_ERROR;
1143 
1144     HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
1145     HDfree(tmp);
1146     tmp = NULL;
1147 
1148     if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
1149         TEST_ERROR;
1150 
1151     HDfree(bkg);
1152     bkg = NULL;
1153 
1154     /* Check that the values read are the same as the values written */
1155     for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
1156         for(j = 0; j < DIM1; j++, temp_point++, temp_check++) {
1157             if(temp_point->c != temp_check->c ||
1158                     temp_point->l != temp_check->l ) {
1159                 H5_FAILED();
1160                 HDprintf("    Read different values than written.\n");
1161                 HDprintf("    At index %d,%d\n", i, j);
1162                 goto error;
1163             } /* end if */
1164 
1165             for(k = 0; k < 5; k++) {
1166                 if(temp_point->o[k] != temp_check->o[k]) {
1167                     H5_FAILED();
1168                     HDprintf("    Read different values than written.\n");
1169                     HDprintf("    At index %d,%d,%d\n", i, j, k);
1170                     goto error;
1171                 } /* end if */
1172             } /* end for */
1173         } /* end for */
1174 
1175     H5Dclose(dataset);
1176     H5Tclose(dtype);
1177     H5Tclose(native_type);
1178     H5Tclose(tid_m);
1179 
1180     /* Free memory for test data */
1181     HDfree(points);
1182     HDfree(check);
1183 
1184     PASSED();
1185     return 0;
1186 
1187 error:
1188     /* Free memory for test data */
1189     if(tmp)
1190         HDfree(tmp);
1191     if(bkg)
1192         HDfree(bkg);
1193     if(points)
1194         HDfree(points);
1195     if(check)
1196         HDfree(check);
1197 
1198     H5E_BEGIN_TRY {
1199         H5Sclose(space);
1200         H5Tclose(mem_id);
1201         H5Dclose(dataset);
1202         H5Tclose(dtype);
1203         H5Tclose(native_type);
1204         H5Tclose(tid);
1205         H5Tclose(tid2);
1206         H5Tclose(tid_m);
1207     } H5E_END_TRY;
1208 
1209     return -1;
1210 }
1211 
1212 
1213 /*-------------------------------------------------------------------------
1214  * Function:	test_enum_dtype
1215  *
1216  * Purpose:	Test H5Tget_native_type for enumerate datatype
1217  *
1218  * Return:	Success:	0
1219  *
1220  *		Failure:	-1
1221  *
1222  * Programmer:	Raymond Lu
1223  *		October 15, 2002
1224  *
1225  * Modifications:
1226  *
1227  *-------------------------------------------------------------------------
1228  */
1229 static herr_t
test_enum_dtype(hid_t file)1230 test_enum_dtype(hid_t file)
1231 {
1232     hid_t	dataset = -1, space = -1;
1233     hid_t       tid = -1, tid_m = -1, dtype = -1, native_type = -1;
1234     int		i, j, n;
1235     hsize_t	dims[2];
1236     void        *tmp = NULL;
1237     short        colors[8];
1238     unsigned char sub_colors[16];
1239     const char  *mname[] = { "RED",
1240                             "GREEN",
1241                             "BLUE",
1242                             "YELLOW",
1243                             "PINK",
1244                             "PURPLE",
1245                             "ORANGE",
1246                             "WHITE" };
1247 
1248     TESTING("enum datatype");
1249 
1250     /* Initialize the dataset */
1251     for(i = 0; i < DIM0; i++)
1252         for(j = 0, n = 0; j < DIM1; j++, n++)
1253             spoints2[i][j] = (short)((i * 10 + j * 100 + n) % 8);
1254 
1255     /* Create the data space */
1256     dims[0] = DIM0;
1257     dims[1] = DIM1;
1258     if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;
1259 
1260     /* Construct enum type based on native type */
1261     if((tid = H5Tenum_create(H5T_STD_I16LE)) < 0) TEST_ERROR;
1262 
1263     for(i = 0; i < 8; i++) {
1264         sub_colors[i * 2] = (unsigned char)i;
1265         sub_colors[i * 2 + 1] = 0;
1266         if(H5Tenum_insert(tid, mname[i], &(sub_colors[i*2])) < 0) TEST_ERROR;
1267     } /* end for */
1268 
1269     /* Create the dataset */
1270     if((dataset = H5Dcreate2(file, DSET_ENUM_NAME, tid, space,
1271             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
1272         TEST_ERROR;
1273 
1274     /* Construct enum type based on native type in memory */
1275     if((tid_m = H5Tenum_create(H5T_NATIVE_SHORT)) < 0) TEST_ERROR;
1276 
1277     for(i = 0; i < 8; i++) {
1278         colors[i] = (short)i;
1279         if(H5Tenum_insert(tid_m, mname[i], &(colors[i])) < 0) TEST_ERROR;
1280     } /* end for */
1281 
1282     /* Write the data to the dataset */
1283     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, spoints2) < 0)
1284         TEST_ERROR;
1285 
1286     /* Close dataset */
1287     if(H5Dclose(dataset) < 0) TEST_ERROR;
1288 
1289     /* Close datatype */
1290     if(H5Tclose(tid) < 0) TEST_ERROR;
1291 
1292     /* Close dataspace */
1293     if(H5Sclose(space) < 0) TEST_ERROR;
1294 
1295     /* Open dataset again to check H5Tget_native_type */
1296     if((dataset = H5Dopen2(file, DSET_ENUM_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
1297 
1298     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
1299 
1300     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
1301         TEST_ERROR;
1302 
1303     /* Read the dataset back.  Temporary buffer is for special platforms like
1304      * Cray */
1305     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
1306         TEST_ERROR
1307 
1308     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
1309         TEST_ERROR;
1310 
1311     HDmemcpy(scheck2, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
1312     HDfree(tmp);
1313     tmp = NULL;
1314 
1315     if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), scheck2, NULL, H5P_DEFAULT) < 0)
1316         TEST_ERROR;
1317 
1318     /* Check that the values read are the same as the values written */
1319     for(i = 0; i < DIM0; i++)
1320         for(j = 0; j < DIM1; j++)
1321             if(spoints2[i][j] != scheck2[i][j]) {
1322                 H5_FAILED();
1323                 HDprintf("    Read different values than written.\n");
1324                 HDprintf("    At index %d,%d\n", i, j);
1325                 HDprintf(" spoints2[i][j]=%hd, scheck2[i][j]=%hd\n", spoints2[i][j],
1326                         scheck2[i][j]);
1327                 goto error;
1328             } /* end if */
1329 
1330     H5Dclose(dataset);
1331     H5Tclose(dtype);
1332     H5Tclose(native_type);
1333     H5Tclose(tid_m);
1334     PASSED();
1335     return 0;
1336 
1337 error:
1338     /* Free memory for test data */
1339     if(tmp)
1340         HDfree(tmp);
1341 
1342     H5E_BEGIN_TRY {
1343         H5Sclose(space);
1344         H5Dclose(dataset);
1345         H5Tclose(dtype);
1346         H5Tclose(native_type);
1347         H5Tclose(tid);
1348         H5Tclose(tid_m);
1349     } H5E_END_TRY;
1350 
1351     return -1;
1352 }
1353 
1354 
1355 /*-------------------------------------------------------------------------
1356  * Function:	test_array_dtype
1357  *
1358  * Purpose:	Test H5Tget_native_type for array datatype
1359  *
1360  * Return:	Success:	0
1361  *
1362  *		Failure:	-1
1363  *
1364  * Programmer:	Raymond Lu
1365  *		October 15, 2002
1366  *
1367  * Modifications:
1368  *
1369  *-------------------------------------------------------------------------
1370  */
1371 static herr_t
test_array_dtype(hid_t file)1372 test_array_dtype(hid_t file)
1373 {
1374     typedef struct {
1375         char    c;
1376         int     i;
1377         long long l;
1378     } s1;
1379     hid_t	dataset = -1, space = -1;
1380     hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid3 = -1, tid_m = -1;
1381     int		i, j, k, n;
1382     hsize_t	space_dims[2], array_dims[1]={5};
1383     s1         *temp_point = NULL, *temp_check = NULL;
1384     s1 	       *points = NULL, *check = NULL;
1385     void       *tmp = NULL;
1386 
1387     TESTING("array of compound datatype");
1388 
1389     /* Allocate space for the points & check arrays */
1390     if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1 * 5)))
1391         TEST_ERROR;
1392     if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1 * 5)))
1393         TEST_ERROR;
1394 
1395     /* Initialize the dataset */
1396     for(i = n = 0, temp_point = points; i < DIM0; i++)
1397         for(j = 0; j < DIM1; j++)
1398             for(k = 0; k < 5; k++,temp_point++) {
1399                 temp_point->c= 't';
1400                 temp_point->i= n++;
1401                 temp_point->l= (i * 10 + j * 100) * n;
1402             } /* end for */
1403 
1404     /* Create the data space */
1405     space_dims[0] = DIM0;
1406     space_dims[1] = DIM1;
1407     if((space = H5Screate_simple(2, space_dims, NULL)) < 0) TEST_ERROR;
1408 
1409     /* Create compound datatype for disk storage */
1410     if((tid2 = H5Tcreate(H5T_COMPOUND, 13)) < 0) TEST_ERROR;
1411 
1412     /* Insert members */
1413     if(H5Tinsert(tid2, "c", 0, H5T_STD_U8BE) < 0) TEST_ERROR;
1414     if(H5Tinsert(tid2, "i", 1, H5T_STD_U32LE) < 0) TEST_ERROR;
1415     if(H5Tinsert(tid2, "l", 5, H5T_STD_I64BE) < 0) TEST_ERROR;
1416 
1417     /* Create array datatype for disk storage */
1418     if((tid = H5Tarray_create2(tid2, 1, array_dims)) < 0) TEST_ERROR;
1419 
1420     /* Create the dataset */
1421     if((dataset = H5Dcreate2(file, DSET_ARRAY_NAME, tid, space,
1422             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
1423         TEST_ERROR;
1424 
1425     /* Create compound datatype for datatype in memory */
1426     if((tid3 = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
1427     if(H5Tinsert(tid3, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
1428     if(H5Tinsert(tid3, "i", HOFFSET(s1, i), H5T_NATIVE_UINT) < 0) TEST_ERROR;
1429     if(H5Tinsert(tid3, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;
1430 
1431     /* Create array datatype for memory */
1432     if((tid_m = H5Tarray_create2(tid3, 1, array_dims)) < 0) TEST_ERROR;
1433 
1434     /* Write the data to the dataset */
1435     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
1436         TEST_ERROR;
1437 
1438     /* Close dataset */
1439     if(H5Dclose(dataset) < 0) TEST_ERROR;
1440 
1441     /* Close datatype */
1442     if(H5Tclose(tid) < 0) TEST_ERROR;
1443     if(H5Tclose(tid2) < 0) TEST_ERROR;
1444 
1445     /* Close dataspace */
1446     if(H5Sclose(space) < 0) TEST_ERROR;
1447 
1448     /* Open dataset again to check H5Tget_native_type */
1449     if((dataset = H5Dopen2(file, DSET_ARRAY_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
1450 
1451     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
1452 
1453     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
1454         TEST_ERROR;
1455 
1456     /* Read the dataset back. Temporary buffer is for special platforms like
1457      * Cray */
1458     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
1459         TEST_ERROR
1460 
1461     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
1462         TEST_ERROR;
1463 
1464     HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
1465     HDfree(tmp);
1466     tmp = NULL;
1467 
1468     if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, NULL, H5P_DEFAULT) < 0)
1469         TEST_ERROR;
1470 
1471     /* Check that the values read are the same as the values written */
1472     for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
1473         for(j = 0; j < DIM1; j++)
1474             for(k = 0; k < 5; k++, temp_point++, temp_check++)
1475                 if(temp_point->c != temp_check->c ||
1476                         temp_point->i != temp_check->i ||
1477                         temp_point->l != temp_check->l ) {
1478                     H5_FAILED();
1479                     HDprintf("    Read different values than written.\n");
1480                     HDprintf("    At index %d,%d\n", i, j);
1481                     goto error;
1482                 } /* end if */
1483 
1484     /* Close HDF5 objects */
1485     if(H5Dclose(dataset)) TEST_ERROR;
1486     if(H5Tclose(native_type)) TEST_ERROR;
1487     if(H5Tclose(dtype)) TEST_ERROR;
1488     if(H5Tclose(tid_m) < 0) TEST_ERROR;
1489     if(H5Tclose(tid3) < 0) TEST_ERROR;
1490 
1491     /* Free memory for test data */
1492     HDfree(points);
1493     HDfree(check);
1494 
1495     PASSED();
1496     return 0;
1497 
1498 error:
1499     /* Free memory for test data */
1500     if(tmp)
1501         HDfree(tmp);
1502     if(points)
1503         HDfree(points);
1504     if(check)
1505         HDfree(check);
1506 
1507     H5E_BEGIN_TRY {
1508         H5Sclose(space);
1509         H5Dclose(dataset);
1510         H5Tclose(dtype);
1511         H5Tclose(native_type);
1512         H5Tclose(tid);
1513         H5Tclose(tid2);
1514         H5Tclose(tid3);
1515         H5Tclose(tid_m);
1516     } H5E_END_TRY;
1517 
1518     return -1;
1519 }
1520 
1521 
1522 /*-------------------------------------------------------------------------
1523  * Function:	test_array_dtype2
1524  *
1525  * Purpose:	Test H5Tget_native_type for array datatype
1526  *
1527  * Return:	Success:	0
1528  *
1529  *		Failure:	-1
1530  *
1531  * Programmer:	Raymond Lu
1532  *		October 15, 2002
1533  *
1534  * Modifications:
1535  *
1536  *-------------------------------------------------------------------------
1537  */
1538 static herr_t
test_array_dtype2(hid_t file)1539 test_array_dtype2(hid_t file)
1540 {
1541     hid_t	dataset = -1, space = -1;
1542     hid_t       dtype = -1, native_type = -1, tid = -1, tid_m = -1;
1543     int		i, j, k, n;
1544     hsize_t	space_dims[2], array_dims[1] = {5};
1545     void       *tmp = NULL;
1546 
1547     TESTING("array of atomic datatype");
1548 
1549     /* Initialize the dataset */
1550     for(i = n = 0;i < DIM0; i++)
1551         for(j = 0; j < DIM1; j++)
1552             for(k = 0; k < 5; k++)
1553                 ipoints3[i][j][k] = n++;
1554 
1555     /* Create the data space */
1556     space_dims[0] = DIM0;
1557     space_dims[1] = DIM1;
1558     if((space = H5Screate_simple(2, space_dims, NULL)) < 0) TEST_ERROR;
1559 
1560     /* Create array datatype for disk storage */
1561     if((tid = H5Tarray_create2(H5T_STD_I32LE, 1, array_dims)) < 0) TEST_ERROR;
1562 
1563     /* Create the dataset */
1564     if((dataset = H5Dcreate2(file, DSET_ARRAY2_NAME, tid, space,
1565             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
1566         TEST_ERROR;
1567 
1568     /* Create array datatype for memory */
1569     if((tid_m = H5Tarray_create2(H5T_NATIVE_INT, 1, array_dims)) < 0) TEST_ERROR;
1570 
1571     /* Write the data to the dataset */
1572     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints3) < 0)
1573         TEST_ERROR;
1574 
1575     /* Close dataset */
1576     if(H5Dclose(dataset) < 0) TEST_ERROR;
1577 
1578     /* Close datatype */
1579     if(H5Tclose(tid) < 0) TEST_ERROR;
1580 
1581     /* Close dataspace */
1582     if(H5Sclose(space) < 0) TEST_ERROR;
1583 
1584     /* Open dataset again to check H5Tget_native_type */
1585     if((dataset = H5Dopen2(file, DSET_ARRAY2_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
1586 
1587     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
1588 
1589     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
1590         TEST_ERROR;
1591 
1592     /* Read the dataset back.  Temporary buffer is for special platforms like
1593      * Cray */
1594     if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
1595         TEST_ERROR
1596 
1597     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
1598         TEST_ERROR;
1599 
1600     HDmemcpy(icheck3, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
1601     HDfree(tmp);
1602     tmp = NULL;
1603 
1604     if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), icheck3, NULL, H5P_DEFAULT) < 0)
1605         TEST_ERROR;
1606 
1607     /* Check that the values read are the same as the values written */
1608     for(i = 0; i < DIM0; i++)
1609         for(j = 0; j < DIM1; j++)
1610             for(k = 0; k < 5; k++)
1611                 if(icheck3[i][j][k] != ipoints3[i][j][k]) {
1612                     H5_FAILED();
1613                     HDprintf("    Read different values than written.\n");
1614                     HDprintf("    At index %d,%d\n", i, j);
1615                     goto error;
1616                 } /* end if */
1617 
1618     /* Close HDF5 objects */
1619     if(H5Dclose(dataset)) TEST_ERROR;
1620     if(H5Tclose(native_type)) TEST_ERROR;
1621     if(H5Tclose(dtype)) TEST_ERROR;
1622     if(H5Tclose(tid_m) < 0) TEST_ERROR;
1623 
1624     PASSED();
1625     return 0;
1626 
1627 error:
1628     /* Free memory for test data */
1629     if(tmp)
1630         HDfree(tmp);
1631 
1632     H5E_BEGIN_TRY {
1633         H5Sclose(space);
1634         H5Dclose(dataset);
1635         H5Tclose(dtype);
1636         H5Tclose(native_type);
1637         H5Tclose(tid);
1638         H5Tclose(tid_m);
1639     } H5E_END_TRY;
1640 
1641     return -1;
1642 }
1643 
1644 
1645 /*-------------------------------------------------------------------------
1646  * Function:	test_vl_dtype
1647  *
1648  * Purpose:	Test H5Tget_native_type for variable length datatype
1649  *
1650  * Return:	Success:	0
1651  *
1652  *		Failure:	-1
1653  *
1654  * Programmer:	Raymond Lu
1655  *		October 15, 2002
1656  *
1657  * Modifications:
1658  *
1659  *-------------------------------------------------------------------------
1660  */
1661 static herr_t
test_vl_dtype(hid_t file)1662 test_vl_dtype(hid_t file)
1663 {
1664     hvl_t       wdata[SPACE1_DIM1];   /* Information to write */
1665     hvl_t       rdata[SPACE1_DIM1];   /* Information read in */
1666     hvl_t       *t1, *t2;             /* Temporary pointer to VL information */
1667     hsize_t	dims1[] = {SPACE1_DIM1};
1668     hid_t	dataset = -1, space = -1;
1669     hid_t       dtype = -1, native_type = -1, nat_super_type = -1, tid = -1, tid2 = -1, tid_m = -1, tid_m2 = -1;
1670     size_t	i, j, k;
1671     void      **tmp = NULL;
1672 
1673     TESTING("variable length datatype");
1674 
1675     /* Allocate and initialize VL data to write */
1676     for(i = 0; i < SPACE1_DIM1; i++) {
1677         wdata[i].p = HDmalloc((i + 1) * sizeof(hvl_t));
1678         if(NULL == wdata[i].p) {
1679             H5_FAILED();
1680             HDprintf("    Cannot allocate memory for VL data! i=%u\n",(unsigned)i);
1681             goto error;
1682         } /* end if */
1683         wdata[i].len = i + 1;
1684         for(t1 = (hvl_t *)wdata[i].p, j = 0; j < (i + 1); j++, t1++) {
1685             t1->p = HDmalloc((j + 1) * sizeof(unsigned int));
1686             if(NULL == t1->p) {
1687                 H5_FAILED();
1688                 HDprintf("    Cannot allocate memory for VL data! i=%u, j=%u\n",(unsigned)i,(unsigned)j);
1689                 goto error;
1690             } /* end if */
1691             t1->len = j + 1;
1692             for(k = 0; k < (j + 1); k++)
1693                 ((unsigned int *)t1->p)[k] = (unsigned int)(i * 100 + j * 10 + k);
1694         } /* end for */
1695     } /* end for */
1696 
1697     /* Create dataspace for datasets */
1698     if((space = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0) TEST_ERROR;
1699 
1700     /* Create the base VL type */
1701     if((tid2 = H5Tvlen_create (H5T_STD_U32LE)) < 0) TEST_ERROR;
1702 
1703     /* Create a VL datatype for disk storage */
1704     if((tid = H5Tvlen_create(tid2)) < 0) TEST_ERROR
1705 
1706     /* Create a dataset */
1707     if((dataset = H5Dcreate2(file, DSET_VL_NAME, tid, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
1708         TEST_ERROR;
1709 
1710     /* Create a base VL datatype for memory */
1711     if((tid_m2 = H5Tvlen_create(H5T_NATIVE_UINT)) < 0) TEST_ERROR;
1712 
1713     /* Create a VL datatype for memory */
1714     if((tid_m = H5Tvlen_create (tid_m2)) < 0) TEST_ERROR;
1715 
1716     /* Write dataset to disk */
1717     if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) TEST_ERROR;
1718 
1719     /* Close Dataset */
1720     if(H5Dclose(dataset) < 0) TEST_ERROR;
1721 
1722     /* Close datatype */
1723     if(H5Tclose(tid2) < 0) TEST_ERROR;
1724     if(H5Tclose(tid) < 0) TEST_ERROR;
1725 
1726     /* Open a dataset */
1727     if((dataset = H5Dopen2(file, DSET_VL_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
1728 
1729     /* Get native datatype for dataset */
1730     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
1731 
1732     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
1733         TEST_ERROR;
1734 
1735     /* Also get native base type for this nested VL type.  Should be an integer type. */
1736     if((nat_super_type = H5Tget_super(native_type)) < 0)
1737         TEST_ERROR;
1738     if((nat_super_type = H5Tget_super(nat_super_type)) < 0)
1739         TEST_ERROR;
1740 
1741     /* Read dataset from disk */
1742     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata) < 0) TEST_ERROR;
1743 
1744     /* Compare data read in */
1745     for(i = 0; i < SPACE1_DIM1; i++) {
1746         if(wdata[i].len != rdata[i].len) {
1747             H5_FAILED();
1748             HDprintf("    VL data length don't match!, wdata[%d].len=%d, rdata[%d].len=%d\n",(int)i,(int)wdata[i].len,(int)i,(int)rdata[i].len);
1749             goto error;
1750         } /* end if */
1751         for(t1 = (hvl_t *)wdata[i].p, t2 = (hvl_t *)rdata[i].p, j = 0; j < rdata[i].len; j++, t1++, t2++) {
1752             if(t1->len != t2->len) {
1753                 H5_FAILED();
1754                 HDprintf("    VL data length don't match!, wdata[%d].len=%d, rdata[%d].len=%d\n",(int)i,(int)wdata[i].len,(int)i,(int)rdata[i].len);
1755                 goto error;
1756             } /* end if */
1757 
1758             /* use temporary buffer to convert datatype.  This is for special
1759              * platforms like Cray */
1760             if(NULL == (tmp = (void **)HDmalloc(t2->len * sizeof(unsigned int))))
1761                 TEST_ERROR
1762             HDmemcpy(tmp, t2->p, t2->len * H5Tget_size(nat_super_type));
1763 
1764             if(H5Tconvert(nat_super_type, H5T_NATIVE_UINT, t2->len, tmp, NULL, H5P_DEFAULT) < 0)
1765                 TEST_ERROR;
1766 
1767             for(k = 0; k < t2->len; k++) {
1768                 if(((unsigned int *)t1->p)[k] != ((unsigned int *)tmp)[k]) {
1769                     H5_FAILED();
1770                     HDprintf("    VL data don't match!, wdata[%u].p=%d, rdata[%u].p=%u\n",
1771                             (unsigned)i, ((unsigned int*)t1->p)[k], (unsigned)i, ((unsigned int*)tmp)[k]);
1772                     goto error;
1773                 } /* end if */
1774             } /* end for */
1775 
1776             HDfree(tmp);
1777             tmp = NULL;
1778         } /* end for */
1779     } /* end for */
1780 
1781     /* Reclaim the read VL data */
1782     if(H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, rdata) < 0) TEST_ERROR;
1783 
1784     /* Reclaim the write VL data */
1785     if(H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, wdata) < 0) TEST_ERROR;
1786 
1787     /* Close Dataset */
1788     if(H5Dclose(dataset) < 0) TEST_ERROR;
1789 
1790     /* Close datatype */
1791     if(H5Tclose(native_type) < 0) TEST_ERROR;
1792     native_type = -1;     /* reset so that error handling can check for VL reclaim */
1793     if(H5Tclose(dtype) < 0) TEST_ERROR;
1794     if(H5Tclose(tid_m) < 0) TEST_ERROR;
1795     if(H5Tclose(tid_m2) < 0) TEST_ERROR;
1796 
1797 
1798     /* Close disk dataspace */
1799     if(H5Sclose(space) < 0) TEST_ERROR;
1800 
1801     PASSED();
1802     return 0;
1803 
1804 error:
1805     /* Free memory for test data */
1806     if(tmp)
1807         HDfree(tmp);
1808 
1809     H5E_BEGIN_TRY {
1810         if(native_type > 0) {
1811             H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, rdata);
1812             H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, wdata);
1813         } /* end if */
1814         H5Sclose(space);
1815         H5Dclose(dataset);
1816         H5Tclose(dtype);
1817         H5Tclose(native_type);
1818         H5Tclose(nat_super_type);
1819         H5Tclose(tid);
1820         H5Tclose(tid2);
1821         H5Tclose(tid_m);
1822         H5Tclose(tid_m2);
1823     } H5E_END_TRY;
1824 
1825     return -1;
1826 } /* end test_vl_type() */
1827 
1828 
1829 /*-------------------------------------------------------------------------
1830  * Function:	test_vlstr_dtype
1831  *
1832  * Purpose:	Test H5Tget_native_type for variable length string datatype
1833  *
1834  * Return:	Success:	0
1835  *
1836  *		Failure:	-1
1837  *
1838  * Programmer:	Raymond Lu
1839  *		October 15, 2002
1840  *
1841  * Modifications:
1842  *
1843  *-------------------------------------------------------------------------
1844  */
1845 static herr_t
test_vlstr_dtype(hid_t file)1846 test_vlstr_dtype(hid_t file)
1847 {
1848     const char *wdata[SPACE1_DIM1]= {
1849             "Four score and seven years ago our forefathers brought forth on this continent a new nation,",
1850             "conceived in liberty and dedicated to the proposition that all men are created equal.",
1851             "Now we are engaged in a great civil war,",
1852             "testing whether that nation or any nation so conceived and so dedicated can long endure."
1853     };                                  /* Information to write */
1854     char       *rdata[SPACE1_DIM1];     /* Information read in */
1855     hbool_t     rdata_alloc = FALSE;    /* Whether the read data is allocated */
1856     hid_t	dataset = -1;	        /* Dataset ID			*/
1857     hid_t	sid1 = -1;              /* Dataspace ID			*/
1858     hid_t	tid1 = -1, dtype = -1, native_type = -1;     /* Datatype ID			*/
1859     hsize_t	dims1[] = {SPACE1_DIM1};
1860     unsigned    i;                          /* counting variable */
1861 
1862     /* Output message about test being performed */
1863     TESTING("variable length string datatype");
1864 
1865     /* Create dataspace for datasets */
1866     if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0) TEST_ERROR;
1867 
1868     /* Create a datatype to refer to */
1869     if((tid1 = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR;
1870 
1871     if(H5Tset_size(tid1,H5T_VARIABLE) < 0) TEST_ERROR;
1872     if(H5T_STRING != H5Tget_class(tid1) || !H5Tis_variable_str(tid1))
1873         TEST_ERROR;
1874 
1875     /* Create a dataset */
1876     if((dataset = H5Dcreate2(file, DSET_VLSTR_NAME, tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR;
1877 
1878     /* Write dataset to disk */
1879     if(H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) TEST_ERROR;
1880 
1881     /* Close Dataset */
1882     if(H5Dclose(dataset) < 0) TEST_ERROR;
1883 
1884     /* Open a dataset */
1885     if((dataset = H5Dopen2(file, DSET_VLSTR_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
1886 
1887     /* Get datatype for dataset */
1888     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
1889 
1890     /* Construct native type */
1891     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
1892         TEST_ERROR;
1893 
1894     /* Check if the data type is equal */
1895     if(!H5Tequal(native_type, tid1))
1896         TEST_ERROR;
1897 
1898     /* Read dataset from disk */
1899     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata) < 0) TEST_ERROR;
1900     rdata_alloc = TRUE;
1901 
1902     /* Compare data read in */
1903     for(i = 0; i < SPACE1_DIM1; i++) {
1904         if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
1905             H5_FAILED();
1906             HDprintf("    VL data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",
1907                     (int)i, (int)HDstrlen(wdata[i]), (int)i, (int)HDstrlen(rdata[i]));
1908             goto error;
1909         } /* end if */
1910         if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
1911             H5_FAILED();
1912             HDprintf("    VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",
1913                     (int)i, wdata[i], (int)i, rdata[i]);
1914             goto error;
1915         } /* end if */
1916     } /* end for */
1917 
1918     /* Close Dataset */
1919     if(H5Dclose(dataset) < 0) TEST_ERROR;
1920 
1921     /* Close datatype */
1922     if(H5Tclose(tid1) < 0) TEST_ERROR;
1923     if(H5Tclose(native_type) < 0) TEST_ERROR;
1924 
1925     /* Close disk dataspace */
1926     if(H5Sclose(sid1) < 0) TEST_ERROR;
1927 
1928     /* Free memory for rdata */
1929     for(i = 0; i < SPACE1_DIM1; i++)
1930         HDfree(rdata[i]);
1931     rdata_alloc = FALSE;
1932 
1933     PASSED();
1934     return 0;
1935 
1936 error:
1937     if(rdata_alloc) {
1938         /* Free memory for rdata */
1939         for(i = 0; i < SPACE1_DIM1; i++)
1940             HDfree(rdata[i]);
1941     } /* end if */
1942 
1943     H5E_BEGIN_TRY {
1944         H5Dclose(dataset);
1945         H5Tclose(dtype);
1946         H5Tclose(native_type);
1947         H5Tclose(sid1);
1948         H5Tclose(tid1);
1949     } H5E_END_TRY;
1950 
1951     return -1;
1952 } /* end test_vlstr_dtype() */
1953 
1954 
1955 /*-------------------------------------------------------------------------
1956  * Function:	test_str_dtype
1957  *
1958  * Purpose:	Test H5Tget_native_type for fixed-length string datatype
1959  *
1960  * Return:	Success:	0
1961  *
1962  *		Failure:	-1
1963  *
1964  * Programmer:	Raymond Lu
1965  *		October 15, 2002
1966  *
1967  * Modifications:
1968  *
1969  *-------------------------------------------------------------------------
1970  */
1971 static herr_t
test_str_dtype(hid_t file)1972 test_str_dtype(hid_t file)
1973 {
1974     const char  wdata[SPACE1_DIM1][4]= {
1975             "one",
1976             "two",
1977             "3rd",
1978             "4th"
1979     };                              /* Information to write */
1980     char        rdata[SPACE1_DIM1][4];  /* Information read in */
1981     hid_t	dataset = -1;	    /* Dataset ID			*/
1982     hid_t	sid1 = -1;          /* Dataspace ID			*/
1983     hid_t	tid1 = -1, dtype = -1, native_type = -1; /* Datatype ID			*/
1984     hsize_t	dims1[] = {SPACE1_DIM1};
1985     unsigned    i;                      /* counting variable */
1986 
1987     /* Output message about test being performed */
1988     TESTING("fixed-length string datatype");
1989 
1990     /* Create dataspace for datasets */
1991     if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0) TEST_ERROR;
1992 
1993     /* Create a datatype to refer to */
1994     if((tid1 = H5Tcopy (H5T_C_S1)) < 0) TEST_ERROR;
1995 
1996     if(H5Tset_size(tid1, 4) < 0) TEST_ERROR;
1997     if(H5T_STRING != H5Tget_class(tid1)) TEST_ERROR;
1998 
1999     /* Create a dataset */
2000     if((dataset = H5Dcreate2(file, DSET_STR_NAME, tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR;
2001 
2002     /* Write dataset to disk */
2003     if(H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) TEST_ERROR;
2004 
2005     /* Close Dataset */
2006     if(H5Dclose(dataset) < 0) TEST_ERROR;
2007 
2008     /* Open a dataset */
2009     if((dataset = H5Dopen2(file, DSET_STR_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
2010 
2011     /* Get datatype for dataset */
2012     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
2013 
2014     /* Construct native type */
2015     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
2016         TEST_ERROR;
2017 
2018     /* Check if the data type is equal */
2019     if(!H5Tequal(native_type, tid1) || H5T_STRING!=H5Tget_class(native_type))
2020         TEST_ERROR;
2021 
2022     /* Read dataset from disk */
2023     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata) < 0) TEST_ERROR;
2024 
2025     /* Compare data read in */
2026     for(i = 0; i < SPACE1_DIM1; i++) {
2027         if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
2028             H5_FAILED();
2029             HDprintf("    data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",
2030                     (int)i, (int)HDstrlen(wdata[i]), (int)i, (int)HDstrlen(rdata[i]));
2031             goto error;
2032         } /* end if */
2033         if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
2034             H5_FAILED();
2035             HDprintf("    data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",
2036                     (int)i, wdata[i], (int)i, rdata[i]);
2037             goto error;
2038         } /* end if */
2039     } /* end for */
2040 
2041     /* Close Dataset */
2042     if(H5Dclose(dataset) < 0) TEST_ERROR;
2043 
2044     /* Close datatype */
2045     if(H5Tclose(tid1) < 0) TEST_ERROR;
2046     if(H5Tclose(native_type) < 0) TEST_ERROR;
2047 
2048     /* Close disk dataspace */
2049     if(H5Sclose(sid1) < 0) TEST_ERROR;
2050 
2051     PASSED();
2052     return 0;
2053 
2054 error:
2055     H5E_BEGIN_TRY {
2056         H5Dclose(dataset);
2057         H5Tclose(dtype);
2058         H5Tclose(native_type);
2059         H5Tclose(tid1);
2060         H5Tclose(sid1);
2061     } H5E_END_TRY;
2062 
2063     return -1;
2064 } /* end test_str_dtype() */
2065 
2066 
2067 /*-------------------------------------------------------------------------
2068  * Function:	test_refer_dtype
2069  *
2070  * Purpose:	Test H5Tget_native_type for reference datatype
2071  *
2072  * Return:	Success:	0
2073  *
2074  *		Failure:	-1
2075  *
2076  * Programmer:	Raymond Lu
2077  *		October 15, 2002
2078  *
2079  * Modifications:
2080  *
2081  *-------------------------------------------------------------------------
2082  */
2083 static herr_t
test_refer_dtype(hid_t file)2084 test_refer_dtype(hid_t file)
2085 {
2086     /* Compound datatype */
2087     typedef struct s1_t {
2088         unsigned int a;
2089         unsigned int b;
2090         float c;
2091     } s1_t;
2092 
2093     hid_t	dataset = -1;   /* Dataset ID			*/
2094     hid_t	group = -1;     /* Group ID             */
2095     hid_t	sid1 = -1;      /* Dataspace ID			*/
2096     hid_t	tid1 = -1, dtype = -1, native_type = -1;       /* Datatype ID	*/
2097     hsize_t	dims1[] = {1};
2098     H5O_type_t  obj_type;       /* Object type */
2099     hobj_ref_t *wbuf = NULL,    /* buffer to write to disk */
2100                *rbuf = NULL;    /* buffer read from disk */
2101 
2102     /* Output message about test being performed */
2103     TESTING("reference datatype");
2104 
2105     /* Allocate write & read buffers */
2106     if(NULL == (wbuf = (hobj_ref_t *)HDmalloc(MAX(sizeof(unsigned), sizeof(hobj_ref_t)))))
2107         TEST_ERROR
2108     if(NULL == (rbuf = (hobj_ref_t *)HDmalloc(MAX(sizeof(unsigned), sizeof(hobj_ref_t)))))
2109         TEST_ERROR
2110 
2111     /* Create dataspace for datasets */
2112     if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0)
2113         TEST_ERROR;
2114 
2115     /* Create a group */
2116     if((group = H5Gcreate2(file, "Group1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
2117 
2118     /* Create a datatype to refer to */
2119     if((tid1 = H5Tcreate (H5T_COMPOUND, sizeof(s1_t))) < 0)
2120         TEST_ERROR;
2121 
2122     /* Insert fields */
2123     if(H5Tinsert (tid1, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT) < 0)
2124         TEST_ERROR;
2125 
2126     if(H5Tinsert (tid1, "b", HOFFSET(s1_t, b), H5T_NATIVE_INT) < 0)
2127         TEST_ERROR;
2128 
2129     if(H5Tinsert(tid1, "c", HOFFSET(s1_t, c), H5T_NATIVE_FLOAT) < 0)
2130         TEST_ERROR;
2131 
2132     /* Save datatype for later */
2133     if(H5Tcommit2(group, "Datatype1", tid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT) < 0)
2134         TEST_ERROR;
2135 
2136     /* Close datatype */
2137     if(H5Tclose(tid1) < 0)
2138         TEST_ERROR;
2139 
2140     /* Close group */
2141     if(H5Gclose(group) < 0)
2142         TEST_ERROR;
2143 
2144     /* Create a dataset */
2145     if((dataset = H5Dcreate2(file, "Dataset3", H5T_STD_REF_OBJ, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
2146         TEST_ERROR;
2147 
2148     /* Create reference to named datatype */
2149     if(H5Rcreate(wbuf, file, "/Group1/Datatype1", H5R_OBJECT, (hid_t)-1) < 0)
2150         TEST_ERROR;
2151     if(H5Rget_obj_type2(dataset, H5R_OBJECT, wbuf, &obj_type) < 0)
2152         TEST_ERROR;
2153     if(obj_type != H5O_TYPE_NAMED_DATATYPE)
2154         TEST_ERROR;
2155 
2156     /* Write selection to disk */
2157     if(H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0)
2158         TEST_ERROR;
2159 
2160     /* Close disk dataspace */
2161     if(H5Sclose(sid1) < 0)
2162         TEST_ERROR;
2163 
2164     /* Close Dataset */
2165     if(H5Dclose(dataset) < 0)
2166         TEST_ERROR;
2167 
2168     /* Open the dataset */
2169     if((dataset = H5Dopen2(file, "/Dataset3", H5P_DEFAULT)) < 0)
2170         TEST_ERROR;
2171 
2172     /* Get datatype for dataset */
2173     if((dtype = H5Dget_type(dataset)) < 0)
2174         TEST_ERROR;
2175 
2176     /* Construct native type */
2177     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
2178         TEST_ERROR;
2179 
2180     /* Check if the data type is equal */
2181     if(!H5Tequal(native_type, H5T_STD_REF_OBJ))
2182         TEST_ERROR;
2183 
2184     /* Read selection from disk */
2185     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf) < 0)
2186         TEST_ERROR;
2187 
2188     /* Open datatype object */
2189     if((tid1 = H5Rdereference2(dataset, H5P_DEFAULT, H5R_OBJECT, rbuf)) < 0)
2190         TEST_ERROR;
2191 
2192     /* Verify correct datatype */
2193     if(H5Tget_class(tid1) != H5T_COMPOUND)
2194         TEST_ERROR;
2195 
2196     if(H5Tget_nmembers(tid1)!=3)
2197         TEST_ERROR;
2198 
2199     /* Close datatype */
2200     if(H5Tclose(tid1) < 0)
2201         TEST_ERROR;
2202 
2203     if(H5Tclose(native_type) < 0)
2204         TEST_ERROR;
2205 
2206     /* Close Dataset */
2207     if(H5Dclose(dataset) < 0)
2208         TEST_ERROR;
2209 
2210     /* Free memory buffers */
2211     HDfree(wbuf);
2212     HDfree(rbuf);
2213 
2214     PASSED();
2215 
2216     return 0;
2217 
2218 error:
2219     if(wbuf)
2220         HDfree(wbuf);
2221     if(rbuf)
2222         HDfree(rbuf);
2223 
2224     H5E_BEGIN_TRY {
2225         H5Sclose(sid1);
2226         H5Gclose(group);
2227         H5Tclose(tid1);
2228         H5Tclose(dtype);
2229         H5Tclose(native_type);
2230         H5Dclose(dataset);
2231     } H5E_END_TRY;
2232 
2233     return -1;
2234 }   /* test_refer_dtype() */
2235 
2236 
2237 /*-------------------------------------------------------------------------
2238  * Function:	test_refer_dtype2
2239  *
2240  * Purpose:	Test H5Tget_native_type for reference
2241  *
2242  * Return:	Success:	0
2243  *
2244  *		Failure:	-1
2245  *
2246  * Programmer:	Raymond Lu
2247  *		October 15, 2002
2248  *
2249  * Modifications:
2250  *
2251  *-------------------------------------------------------------------------
2252  */
2253 static herr_t
test_refer_dtype2(hid_t file)2254 test_refer_dtype2(hid_t file)
2255 {
2256     hid_t           dset1 = -1,      /* Dataset ID			*/
2257                     dset2 = -1;      /* Dereferenced dataset ID */
2258     hid_t           sid1 = -1,       /* Dataspace ID	#1		*/
2259                     sid2 = -1;       /* Dataspace ID	#2		*/
2260     hid_t           dtype = -1, native_type = -1;
2261     hsize_t         dims1[] = { 1 }, dims2[] = { SPACE2_DIM1, SPACE2_DIM2 };
2262     hsize_t         start[SPACE2_RANK];     /* Starting location of hyperslab */
2263     hsize_t         stride[SPACE2_RANK];    /* Stride of hyperslab */
2264     hsize_t         count[SPACE2_RANK];     /* Element count of hyperslab */
2265     hsize_t         block[SPACE2_RANK];     /* Block size of hyperslab */
2266     hdset_reg_ref_t wbuf,       /* buffer to write to disk */
2267                     rbuf;       /* buffer read from disk */
2268     uint8_t        *dwbuf = NULL, /* Buffer for writing numeric data to disk */
2269                     *drbuf = NULL; /* Buffer for reading numeric data from disk */
2270     uint8_t        *tu8 = NULL; /* Temporary pointer to uint8 data */
2271     H5O_type_t      obj_type;   /* Object type */
2272     int             i;          /* counting variables */
2273 
2274     /* Output message about test being performed */
2275     TESTING("dataset region reference");
2276 
2277     /* Allocate write & read buffers */
2278     if(NULL == (dwbuf = (uint8_t *)HDmalloc(sizeof(uint8_t) * SPACE2_DIM1 * SPACE2_DIM2)))
2279         TEST_ERROR
2280     if(NULL == (drbuf = (uint8_t *)HDcalloc(sizeof(uint8_t), SPACE2_DIM1 * SPACE2_DIM2)))
2281         TEST_ERROR
2282 
2283     /* Create dataspace for datasets */
2284     if((sid2 = H5Screate_simple(SPACE2_RANK, dims2, NULL)) < 0)
2285         TEST_ERROR;
2286 
2287     /* Create a dataset */
2288     if((dset2 = H5Dcreate2(file, "Dataset2", H5T_STD_U8LE, sid2, H5P_DEFAULT,
2289             H5P_DEFAULT, H5P_DEFAULT)) < 0)
2290         TEST_ERROR;
2291 
2292     for(tu8 = dwbuf, i = 0; i < SPACE2_DIM1 * SPACE2_DIM2; i++)
2293         *tu8++ = (uint8_t)(i * 3);
2294 
2295     /* Write selection to disk */
2296     if(H5Dwrite(dset2, H5T_STD_U8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, dwbuf) < 0)
2297         TEST_ERROR;
2298 
2299     /* Close Dataset */
2300     if(H5Dclose(dset2) < 0)
2301         TEST_ERROR;
2302 
2303     /* Create dataspace for the reference dataset */
2304     if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0)
2305         TEST_ERROR;
2306 
2307     /* Create a reference dataset */
2308     if((dset1 = H5Dcreate2(file, "Dataset1", H5T_STD_REF_DSETREG, sid1,
2309             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
2310         TEST_ERROR;
2311 
2312     /* Create references */
2313     /* Select 6x6 hyperslab for first reference */
2314     start[0] = 2;
2315     start[1] = 2;
2316     stride[0] = 1;
2317     stride[1] = 1;
2318     count[0] = 1;
2319     count[1] = 1;
2320     block[0] = 6;
2321     block[1] = 6;
2322 
2323     if(H5Sselect_hyperslab(sid2, H5S_SELECT_SET, start, stride, count, block) < 0)
2324         TEST_ERROR;
2325 
2326     if((int)H5Sget_select_npoints(sid2) != 36)
2327         TEST_ERROR;
2328 
2329     /* Store first dataset region */
2330     if(H5Rcreate(&wbuf, file, "/Dataset2", H5R_DATASET_REGION, sid2) < 0)
2331         TEST_ERROR;
2332     if(H5Rget_obj_type2(dset1, H5R_DATASET_REGION, &wbuf, &obj_type) < 0)
2333         TEST_ERROR;
2334     if(obj_type != H5O_TYPE_DATASET)
2335         TEST_ERROR;
2336 
2337     /* Write selection to disk */
2338     if(H5Dwrite(dset1, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wbuf) < 0)
2339         TEST_ERROR;
2340 
2341     /* Close disk dataspace */
2342     if(H5Sclose(sid1) < 0)
2343         TEST_ERROR;
2344 
2345     /* Close Dataset */
2346     if(H5Dclose(dset1) < 0)
2347         TEST_ERROR;
2348 
2349     /* Close uint8 dataset dataspace */
2350     if(H5Sclose(sid2) < 0)
2351         TEST_ERROR;
2352 
2353     /* Open the dataset */
2354     if((dset1 = H5Dopen2(file, "/Dataset1", H5P_DEFAULT)) < 0)
2355         TEST_ERROR;
2356 
2357     /* Get datatype for dataset */
2358     if((dtype = H5Dget_type(dset1)) < 0)
2359         TEST_ERROR;
2360 
2361     /* Construct native type */
2362     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
2363         TEST_ERROR;
2364 
2365     /* Check if the data type is equal */
2366     if(!H5Tequal(native_type, H5T_STD_REF_DSETREG))
2367         TEST_ERROR;
2368 
2369     /* Read selection from disk */
2370     if(H5Dread(dset1, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, &rbuf) < 0)
2371         TEST_ERROR;
2372 
2373     /* Try to open objects */
2374     if((dset2 = H5Rdereference2(dset1, H5P_DEFAULT, H5R_DATASET_REGION, &rbuf)) < 0)
2375         TEST_ERROR;
2376 
2377     /* Check what H5Rget_obj_type2 function returns */
2378     if(H5Rget_obj_type2(dset1, H5R_DATASET_REGION, &rbuf, &obj_type) < 0)
2379         TEST_ERROR;
2380     if(obj_type != H5O_TYPE_DATASET)
2381         TEST_ERROR;
2382 
2383     /* Check information in referenced dataset */
2384     if((sid1 = H5Dget_space(dset2)) < 0)
2385         TEST_ERROR;
2386 
2387     if((int)H5Sget_simple_extent_npoints(sid1) != 100)
2388         TEST_ERROR;
2389 
2390     /* Read from disk */
2391     if(H5Dread(dset2, H5T_STD_U8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, drbuf) < 0)
2392         TEST_ERROR;
2393 
2394     for(tu8 = (uint8_t *)drbuf, i = 0; i < (SPACE2_DIM1 * SPACE2_DIM2); i++, tu8++)
2395         if(*tu8 != (uint8_t)(i * 3))
2396             TEST_ERROR;
2397 
2398     /* Get the hyperslab selection */
2399     if((sid2 = H5Rget_region(dset1, H5R_DATASET_REGION, &rbuf)) < 0)
2400         TEST_ERROR;
2401 
2402     /* Verify correct hyperslab selected */
2403     if((int)H5Sget_select_npoints(sid2) != 36)
2404         TEST_ERROR;
2405     if((int)H5Sget_select_hyper_nblocks(sid2) != 1)
2406         TEST_ERROR;
2407 
2408     /* Close region space */
2409     if(H5Sclose(sid2) < 0)
2410         TEST_ERROR;
2411 
2412     /* Close first space */
2413     if(H5Sclose(sid1) < 0)
2414         TEST_ERROR;
2415 
2416     /* Close dereferenced Dataset */
2417     if(H5Dclose(dset2) < 0)
2418         TEST_ERROR;
2419 
2420     /* Close Dataset */
2421     if(H5Dclose(dset1) < 0)
2422         TEST_ERROR;
2423 
2424     /* Free memory buffers */
2425     HDfree(dwbuf);
2426     HDfree(drbuf);
2427 
2428     PASSED();
2429     return 0;
2430 
2431 error:
2432     /* Free memory buffers */
2433     if(dwbuf)
2434         HDfree(dwbuf);
2435     if(drbuf)
2436         HDfree(drbuf);
2437 
2438     H5E_BEGIN_TRY {
2439         H5Sclose(sid2);
2440         H5Sclose(sid1);
2441         H5Tclose(dtype);
2442         H5Tclose(native_type);
2443         H5Dclose(dset2);
2444         H5Dclose(dset1);
2445     } H5E_END_TRY;
2446 
2447     return -1;
2448 }   /* test_refer_dtype2() */
2449 
2450 
2451 /*-------------------------------------------------------------------------
2452  * Function:	test_opaque_dtype
2453  *
2454  * Purpose:	Test H5Tget_native_type for opaque datatype
2455  *
2456  * Return:	Success:	0
2457  *
2458  *		Failure:	-1
2459  *
2460  * Programmer:	Raymond Lu
2461  *		October 15, 2002
2462  *
2463  * Modifications:
2464  *
2465  *-------------------------------------------------------------------------
2466  */
2467 static herr_t
test_opaque_dtype(hid_t file)2468 test_opaque_dtype(hid_t file)
2469 {
2470     hid_t	     type = -1, space = -1, dset = -1;
2471     hid_t            dataset = -1, dtype = -1, native_type = -1;
2472     size_t           i;
2473     unsigned char    wbuf[32], rbuf[32];
2474     hsize_t	     nelmts;
2475 
2476     TESTING("opaque datatype");
2477 
2478     /* opaque_1 */
2479     nelmts = sizeof(wbuf);
2480     if((type = H5Tcreate(H5T_OPAQUE, 1)) < 0) TEST_ERROR;
2481     if(H5Tset_tag(type, "testing 1-byte opaque type") < 0) TEST_ERROR;
2482     if((space = H5Screate_simple(1, &nelmts, NULL)) < 0) TEST_ERROR;
2483     if((dset = H5Dcreate2(file, DSET_OPAQUE_NAME, type, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
2484         TEST_ERROR;
2485 
2486     for(i = 0; i < sizeof(wbuf); i++)
2487         wbuf[i] = (unsigned char)0xff ^ (unsigned char)i;
2488 
2489     if(H5Dwrite(dset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0) TEST_ERROR;
2490     if(H5Sclose(space) < 0) TEST_ERROR;
2491     if(H5Dclose(dset) < 0) TEST_ERROR;
2492 
2493 
2494     /* Open dataset again to check H5Tget_native_type */
2495     if((dataset = H5Dopen2(file, DSET_OPAQUE_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
2496 
2497     if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;
2498 
2499     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
2500         TEST_ERROR;
2501 
2502     if(!H5Tequal(native_type, type)) TEST_ERROR;
2503 
2504     if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf) < 0)
2505         TEST_ERROR;
2506 
2507     for(i = 0; i < sizeof(rbuf); i++)
2508         if(rbuf[i] != wbuf[i]) {
2509             H5_FAILED();
2510             HDprintf("    Read different values than written.\n");
2511             HDprintf("    At index %u\n", (unsigned)i);
2512             goto error;
2513         } /* end if */
2514 
2515     if(H5Tclose(type) < 0) TEST_ERROR;
2516     if(H5Tclose(dtype) < 0) TEST_ERROR;
2517     if(H5Tclose(native_type) < 0) TEST_ERROR;
2518     if(H5Dclose(dataset) < 0) TEST_ERROR;
2519 
2520     PASSED();
2521     return 0;
2522 
2523 error:
2524     H5E_BEGIN_TRY {
2525         H5Sclose(space);
2526         H5Tclose(type);
2527         H5Tclose(dtype);
2528         H5Tclose(native_type);
2529         H5Dclose(dset);
2530         H5Dclose(dataset);
2531     } H5E_END_TRY;
2532 
2533     return -1;
2534 } /* test_opaque_dtype */
2535 
2536 
2537 /*-------------------------------------------------------------------------
2538  * Function:	test_bitfield_dtype
2539  *
2540  * Purpose:	Test H5Tget_native_type for bitfield datatype
2541  *
2542  * Return:	Success:	0
2543  *
2544  *		Failure:	-1
2545  *
2546  * Programmer:	Raymond Lu
2547  *		October 15, 2002
2548  *
2549  * Modifications:
2550  *              Raymond Lu
2551  *              1 December 2009
2552  *              I added the support for bitfield and changed the test to
2553  *              compare the data being read back.
2554  *-------------------------------------------------------------------------
2555  */
2556 static herr_t
test_bitfield_dtype(hid_t file)2557 test_bitfield_dtype(hid_t file)
2558 {
2559     hid_t		type=-1, space=-1, dset1=-1, dset2=-1;
2560     hid_t               dataset1=-1, dataset2=-1, dtype=-1, native_type=-1;
2561     size_t		ntype_size, i;
2562     unsigned char  	wbuf[BITFIELD_ENUMB*sizeof(int)];
2563     unsigned char       *p=NULL;
2564     void                *rbuf = NULL;
2565     unsigned int        intw[BITFIELD_ENUMB], intr[BITFIELD_ENUMB];
2566     hsize_t		nelmts;
2567 
2568     TESTING("bitfield datatype");
2569 
2570     nelmts = BITFIELD_ENUMB;
2571     if((type = H5Tcopy(H5T_STD_B32BE)) < 0) TEST_ERROR;
2572 
2573     if((space = H5Screate_simple(1, &nelmts, NULL)) < 0) TEST_ERROR;
2574 
2575     /* Create and write to dataset1 with a unsigned char buffer */
2576     if((dset1 = H5Dcreate2(file, DSET1_BITFIELD_NAME, type, space, H5P_DEFAULT, H5P_DEFAULT,
2577         H5P_DEFAULT)) < 0) TEST_ERROR;
2578 
2579     for(i = 0; i < BITFIELD_ENUMB*sizeof(int); i++)
2580         wbuf[i] = (unsigned char)((unsigned int)0xff ^ (unsigned int)i);
2581 
2582     if(H5Dwrite(dset1, H5T_NATIVE_B32, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0) TEST_ERROR;
2583     if(H5Dclose(dset1) < 0) TEST_ERROR;
2584 
2585     /* Create and write to dataset2 with a unsigned int buffer */
2586     if((dset2 = H5Dcreate2(file, DSET2_BITFIELD_NAME, type, space, H5P_DEFAULT, H5P_DEFAULT,
2587         H5P_DEFAULT)) < 0) TEST_ERROR;
2588 
2589     for(i = 0; i < BITFIELD_ENUMB; i++)
2590         intw[i] = (unsigned int)0xff << (unsigned int)((i*8)%32);
2591 
2592     if(H5Dwrite(dset2, H5T_NATIVE_B32, H5S_ALL, H5S_ALL, H5P_DEFAULT, intw) < 0) TEST_ERROR;
2593     if(H5Dclose(dset2) < 0) TEST_ERROR;
2594     if(H5Sclose(space) < 0) TEST_ERROR;
2595     if(H5Tclose(type) < 0) TEST_ERROR;
2596 
2597     /* Open dataset1 again to check H5Tget_native_type */
2598     if((dataset1 = H5Dopen2(file, DSET1_BITFIELD_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
2599 
2600     if((dtype = H5Dget_type(dataset1)) < 0) TEST_ERROR;
2601 
2602     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0) TEST_ERROR;
2603 
2604     if((ntype_size = H5Tget_size(native_type)) == 0) TEST_ERROR;
2605 
2606     rbuf = HDmalloc((size_t)nelmts*ntype_size);
2607 
2608     /* Read the data and compare them */
2609     if(H5Dread(dataset1, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf) < 0) TEST_ERROR;
2610 
2611     p = (unsigned char *)rbuf;
2612     for(i = 0; i < BITFIELD_ENUMB*4; i++) {
2613         if(*p != wbuf[i]) {
2614             H5_FAILED();
2615             HDprintf("    Read different values than written.\n");
2616             HDprintf("    At index %zu\n", i);
2617             TEST_ERROR;
2618         }
2619         p++;
2620     }
2621 
2622     if(H5Tclose(dtype) < 0) TEST_ERROR;
2623     if(H5Tclose(native_type) < 0) TEST_ERROR;
2624     if(H5Dclose(dataset1) < 0) TEST_ERROR;
2625     if(rbuf) HDfree(rbuf);
2626 
2627     /* Open dataset2 again to check H5Tget_native_type */
2628     if((dataset2 = H5Dopen2(file, DSET2_BITFIELD_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;
2629 
2630     if((dtype = H5Dget_type(dataset2)) < 0) TEST_ERROR;
2631 
2632     if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0) TEST_ERROR;
2633 
2634     /* Read the data and compare them */
2635     if(H5Dread(dataset2, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, intr) < 0) TEST_ERROR;
2636 
2637     for(i = 0; i < BITFIELD_ENUMB; i++) {
2638         if(intr[i] != intw[i]) {
2639             H5_FAILED();
2640             HDprintf("    Read different values than written.\n");
2641             HDprintf("    At index %zu\n", i);
2642             TEST_ERROR;
2643         }
2644     }
2645 
2646     if(H5Tclose(dtype) < 0) TEST_ERROR;
2647     if(H5Tclose(native_type) < 0) TEST_ERROR;
2648     if(H5Dclose(dataset2) < 0) TEST_ERROR;
2649 
2650     PASSED();
2651     return 0;
2652 
2653 error:
2654     H5E_BEGIN_TRY {
2655         H5Sclose(space);
2656         H5Tclose(type);
2657         H5Tclose(dtype);
2658         H5Tclose(native_type);
2659         H5Dclose(dset1);
2660         H5Dclose(dset2);
2661         H5Dclose(dataset1);
2662         H5Dclose(dataset2);
2663     } H5E_END_TRY;
2664 
2665     return -1;
2666 } /* test_bitfield_dtype */
2667 
2668 
2669 /*-------------------------------------------------------------------------
2670  * Function: test_ninteger
2671  *
2672  * Purpose: Test the native integer function; made to check the case
2673  * like the Cray SV1, where the size of short is 8 but precision is 32
2674  *
2675  * Return: Success: 0
2676  *  Failure: -1
2677  *
2678  * Programmer: pvn@ncsa.uiuc.edu
2679  *  September 3, 2004
2680  *
2681  * Modifications:
2682  *
2683  *-------------------------------------------------------------------------
2684  */
2685 static herr_t
test_ninteger(void)2686 test_ninteger(void)
2687 {
2688     hid_t     fid1 = -1;             /* file ID */
2689     hid_t     fid2 = -1;             /* file ID */
2690     hid_t     did1 = -1;             /* dataset ID */
2691     hid_t     did2 = -1;             /* dataset ID */
2692     hid_t     sid1 = -1;             /* dataspace ID */
2693     hid_t     dcpl1 = -1;            /* dataset creation property list ID */
2694     hid_t     dcpl2 = -1;            /* dataset creation property list ID */
2695     hid_t     tid1 = -1;             /* file datatype */
2696     hid_t     tid2 = -1;             /* file datatype */
2697     hid_t     nid1 = -1;             /* native datatype */
2698     hid_t     nid2 = -1;             /* native datatype */
2699     hsize_t   dims[1] = {DIM3};      /* dataspace dimensions */
2700     hsize_t   nelmts;                /* number of elements in dataset */
2701     int       rank = 1;              /* rank of dataset */
2702     int       buf[DIM3];
2703     int       chk[DIM3];
2704     int       i;
2705 
2706     TESTING("native integer ");
2707 
2708     for(i = 0; i < DIM3; i++)
2709         buf[i] = i;
2710 
2711     /*-------------------------------------------------------------------------
2712      * step1: create a file
2713      *-------------------------------------------------------------------------
2714      */
2715     /* create a file using default properties */
2716     if((fid1 = H5Fcreate("tstint1.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
2717         FAIL_STACK_ERROR
2718 
2719     /* create a data space */
2720     if((sid1 = H5Screate_simple(rank, dims, NULL)) < 0) FAIL_STACK_ERROR
2721 
2722     /* create dcpl  */
2723     if((dcpl1 = H5Pcreate(H5P_DATASET_CREATE)) < 0) FAIL_STACK_ERROR
2724 
2725     /* create a dataset */
2726     if((did1 = H5Dcreate2(fid1, "dset", H5T_NATIVE_INT, sid1, H5P_DEFAULT, dcpl1, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
2727 
2728     /* write */
2729     if(H5Dwrite(did1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) FAIL_STACK_ERROR
2730 
2731     /* close  */
2732     if(H5Sclose(sid1) < 0) FAIL_STACK_ERROR
2733     if(H5Pclose(dcpl1) < 0) FAIL_STACK_ERROR
2734     if(H5Dclose(did1) < 0) FAIL_STACK_ERROR
2735     if(H5Fclose(fid1) < 0) FAIL_STACK_ERROR
2736 
2737     /*-------------------------------------------------------------------------
2738      * step 2: open and create another file copying the data from file1
2739      *-------------------------------------------------------------------------
2740      */
2741 
2742     /* open */
2743     if((fid1 = H5Fopen("tstint1.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
2744 
2745     /* open dataset */
2746     if((did1 = H5Dopen2(fid1, "dset", H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
2747 
2748     if((sid1 = H5Dget_space(did1)) < 0) FAIL_STACK_ERROR
2749 
2750     /* get dcpl */
2751     if((dcpl1 = H5Dget_create_plist(did1)) < 0) FAIL_STACK_ERROR
2752 
2753     /* get file datatype */
2754     if((tid1 = H5Dget_type(did1)) < 0) FAIL_STACK_ERROR
2755 
2756     /* get native datatype */
2757     if((nid1 = H5Tget_native_type(tid1, H5T_DIR_DEFAULT)) < 0) FAIL_STACK_ERROR
2758 
2759     /* get size */
2760     if(H5Tget_size(nid1) == 0) FAIL_STACK_ERROR
2761 
2762     /* get rank */
2763     if((rank = H5Sget_simple_extent_ndims(sid1)) < 0) FAIL_STACK_ERROR
2764     HDmemset(dims, 0, sizeof dims);
2765 
2766     /* get dimension */
2767     if(H5Sget_simple_extent_dims(sid1, dims, NULL) < 0) FAIL_STACK_ERROR
2768     nelmts = 1;
2769     for(i = 0; i < rank; i++)
2770         nelmts *= dims[i];
2771 
2772     /* read */
2773     if(H5Dread(did1, nid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, chk) < 0) FAIL_STACK_ERROR
2774 
2775     /* create a file using default properties */
2776     if((fid2 = H5Fcreate("tstint2.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
2777 
2778     /* create a dataset using the native type */
2779     if((did2 = H5Dcreate2(fid2, "dset", nid1, sid1, H5P_DEFAULT, dcpl1, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
2780 
2781     /* write */
2782     if(H5Dwrite(did2, nid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, chk) < 0) FAIL_STACK_ERROR
2783 
2784     /* get dcpl */
2785     if((dcpl2 = H5Dget_create_plist(did2)) < 0) FAIL_STACK_ERROR
2786 
2787     /* get file datatype */
2788     if((tid2 = H5Dget_type(did2)) < 0) FAIL_STACK_ERROR
2789 
2790     /* get native datatype */
2791     if((nid2 = H5Tget_native_type(tid2, H5T_DIR_DEFAULT)) < 0) FAIL_STACK_ERROR
2792 
2793     /* check */
2794     if(H5Tget_precision(nid1) != H5Tget_precision(nid2)) {
2795         HDprintf("    Precision differ.\n");
2796         TEST_ERROR
2797     } /* end if */
2798 
2799     /* compare dataset creation property lists */
2800     if(H5Pequal(dcpl1, dcpl2) <= 0) {
2801         HDprintf("    Property lists differ.\n");
2802         TEST_ERROR
2803     } /* end if */
2804 
2805     /* check */
2806     for(i = 0; i < DIM3; i++)
2807         if(buf[i] != chk[i]) {
2808             H5_FAILED();
2809             HDprintf("    Read different values than written.\n");
2810             HDprintf("    At index %d\n", i);
2811             TEST_ERROR
2812         } /* end if */
2813 
2814     /* close  */
2815     if(H5Sclose(sid1) < 0) FAIL_STACK_ERROR
2816     if(H5Pclose(dcpl1) < 0) FAIL_STACK_ERROR
2817     if(H5Pclose(dcpl2) < 0) FAIL_STACK_ERROR
2818     if(H5Tclose(tid1) < 0) FAIL_STACK_ERROR
2819     if(H5Tclose(tid2) < 0) FAIL_STACK_ERROR
2820     if(H5Tclose(nid1) < 0) FAIL_STACK_ERROR
2821     if(H5Tclose(nid2) < 0) FAIL_STACK_ERROR
2822     if(H5Dclose(did1) < 0) FAIL_STACK_ERROR
2823     if(H5Dclose(did2) < 0) FAIL_STACK_ERROR
2824     if(H5Fclose(fid1) < 0) FAIL_STACK_ERROR
2825     if(H5Fclose(fid2) < 0) FAIL_STACK_ERROR
2826 
2827     PASSED();
2828     return 0;
2829 
2830 error:
2831     H5E_BEGIN_TRY {
2832         H5Pclose(dcpl1);
2833         H5Pclose(dcpl2);
2834         H5Tclose(tid1);
2835         H5Tclose(tid2);
2836         H5Tclose(nid1);
2837         H5Tclose(nid2);
2838         H5Dclose(did1);
2839         H5Dclose(did2);
2840         H5Sclose(sid1);
2841         H5Fclose(fid1);
2842         H5Fclose(fid2);
2843     } H5E_END_TRY;
2844 
2845     return -1;
2846 } /* end test_ninteger() */
2847 
2848 
2849 /*-------------------------------------------------------------------------
2850  * Function:	main
2851  *
2852  * Purpose:	Test H5Tget_native_type for different datatype
2853  *
2854  * Programmer:	Raymond Lu
2855  *		October 15, 2002
2856  *
2857  * Modifications:
2858  *
2859  *-------------------------------------------------------------------------
2860  */
2861 int
main(void)2862 main(void)
2863 {
2864     hid_t	file, fapl;
2865     int		nerrors = 0;
2866     char	filename[1024];
2867 
2868     h5_reset();
2869     fapl = h5_fileaccess();
2870 
2871     h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
2872     if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
2873         goto error;
2874 
2875     nerrors += test_atomic_dtype(file) < 0 	? 1 : 0;
2876     nerrors += test_compound_dtype(file) < 0 	? 1 : 0;
2877     nerrors += test_compound_dtype2(file) < 0 	? 1 : 0;
2878     nerrors += test_compound_dtype3(file) < 0 	? 1 : 0;
2879     nerrors += test_compound_opaque(file) < 0 	? 1 : 0;
2880     nerrors += test_enum_dtype(file) < 0 	? 1 : 0;
2881     nerrors += test_array_dtype(file) < 0 	? 1 : 0;
2882     nerrors += test_array_dtype2(file) < 0 	? 1 : 0;
2883     nerrors += test_vl_dtype(file) < 0 	        ? 1 : 0;
2884     nerrors += test_vlstr_dtype(file) < 0 	? 1 : 0;
2885     nerrors += test_str_dtype(file) < 0         ? 1 : 0;
2886     nerrors += test_refer_dtype(file) < 0 	? 1 : 0;
2887     nerrors += test_refer_dtype2(file) < 0 	? 1 : 0;
2888     nerrors += test_opaque_dtype(file) < 0 	? 1 : 0;
2889     nerrors += test_bitfield_dtype(file) < 0 	? 1 : 0;
2890     nerrors += test_ninteger() < 0              ? 1 : 0;
2891 
2892     if(H5Fclose(file) < 0)
2893         goto error;
2894 
2895     /* Verify symbol table messages are cached */
2896     nerrors += (h5_verify_cached_stabs(FILENAME, fapl) < 0 ? 1 : 0);
2897 
2898     if(nerrors)
2899         goto error;
2900 
2901     HDprintf("All native datatype tests passed.\n");
2902     h5_cleanup(FILENAME, fapl);
2903 
2904     return 0;
2905 
2906 error:
2907     H5E_BEGIN_TRY {
2908         H5Fclose(file);
2909         h5_cleanup(FILENAME, fapl);
2910     } H5E_END_TRY;
2911 
2912     nerrors = MAX(1, nerrors);
2913     HDprintf("***** %d DATASET TEST%s FAILED! *****\n",
2914             nerrors, 1 == nerrors ? "" : "S");
2915 
2916     return 1;
2917 }
2918 
2919