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:  Robb Matzke <robb@arborea.spizella.com>
16  *              Thursday, October  1, 1998
17  *
18  * Purpose:	Tests dataset fill values.
19  */
20 #include "h5test.h"
21 #include "H5srcdir.h"
22 
23 /*
24  * Define NO_FILLING if you want to compare how this test works when there is
25  * no fill value (that is, when the fill value is zero).
26  */
27 /* #define NO_FILLING */
28 
29 const char *FILENAME[] = {
30     "fillval_1",
31     "fillval_2",
32     "fillval_3",
33     "fillval_4",
34     "fillval_5",
35     "fillval_6",
36     "fillval_7",
37     "fillval_8",
38     "fillval_9",
39     NULL
40 };
41 
42 /* Common type for compound datatype operations */
43 typedef struct {
44     float  a;
45     int    x;
46     double y;
47     char   z;
48 } comp_datatype;
49 
50 /* Common type for compound+vl datatype operations */
51 typedef struct {
52     int    x;
53     char   *a;
54     char   *b;
55     int    y;
56 } comp_vl_datatype;
57 
58 /* The fill_old.h5 is generated from gen_old_fill.c in HDF5 'test' directory
59  * for version 1.4(after 1.4.3).  To get this data file, simply compile
60  * gen_old_fill.c with HDF5 library (before v1.5) and run it. */
61 #define FILE_COMPATIBLE "fill_old.h5"
62 #define FILE_NAME_RAW	"fillval.raw"
63 
64 
65 /*-------------------------------------------------------------------------
66  * Function:    create_compound_type
67  *
68  * Purpose:     create a compound datatype
69  *
70  * Return:      Success:        datatype ID
71  *
72  *              Failure:        -1
73  *
74  * Programmer:  Raymond Lu
75  *              Monday, Jan 26, 2001
76  *
77  * Modifications:
78  *
79  *-------------------------------------------------------------------------
80  */
create_compound_type(void)81 static hid_t create_compound_type(void)
82 {
83     hid_t ret_value=-1;
84 
85     if((ret_value = H5Tcreate(H5T_COMPOUND, sizeof(comp_datatype))) < 0)
86         goto error;
87     if(H5Tinsert(ret_value, "a", HOFFSET(comp_datatype, a), H5T_NATIVE_FLOAT) < 0)
88         goto error;
89     if(H5Tinsert(ret_value, "x", HOFFSET(comp_datatype, x), H5T_NATIVE_INT) < 0)
90         goto error;
91     if(H5Tinsert(ret_value, "y", HOFFSET(comp_datatype, y), H5T_NATIVE_DOUBLE) < 0)
92         goto error;
93     if(H5Tinsert(ret_value, "z", HOFFSET(comp_datatype, z), H5T_NATIVE_CHAR) < 0)
94 	goto error;
95 
96     return ret_value;
97 
98 error:
99     H5E_BEGIN_TRY {
100         H5Tclose(ret_value);
101     } H5E_END_TRY;
102     return -1;
103 }
104 
105 
106 /*-------------------------------------------------------------------------
107  * Function:    create_compound_vl_type
108  *
109  * Purpose:     create a compound+vl datatype
110  *
111  * Return:      Success:        datatype ID
112  *
113  *              Failure:        -1
114  *
115  * Programmer:  Quincey Koziol
116  *              Tuesday, July 3, 2007
117  *
118  *-------------------------------------------------------------------------
119  */
120 static hid_t
create_compound_vl_type(void)121 create_compound_vl_type(void)
122 {
123     hid_t str_id = -1;          /* Datatype for VL-string fields */
124     hid_t ret_value = -1;
125 
126     /* Create a string datatype */
127     if((str_id = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR
128     if(H5Tset_size(str_id, H5T_VARIABLE) < 0) TEST_ERROR
129 
130     if((ret_value = H5Tcreate(H5T_COMPOUND, sizeof(comp_vl_datatype))) < 0) TEST_ERROR
131     if(H5Tinsert(ret_value, "x", HOFFSET(comp_vl_datatype, x), H5T_NATIVE_INT) < 0) TEST_ERROR
132     if(H5Tinsert(ret_value, "a", HOFFSET(comp_vl_datatype, a), str_id) < 0) TEST_ERROR
133     if(H5Tinsert(ret_value, "b", HOFFSET(comp_vl_datatype, b), str_id) < 0) TEST_ERROR
134     if(H5Tinsert(ret_value, "y", HOFFSET(comp_vl_datatype, y), H5T_NATIVE_INT) < 0) TEST_ERROR
135 
136     /* Close string datatype */
137     if(H5Tclose(str_id) < 0) TEST_ERROR
138 
139     return ret_value;
140 
141 error:
142     H5E_BEGIN_TRY {
143         H5Tclose(str_id);
144         H5Tclose(ret_value);
145     } H5E_END_TRY;
146     return -1;
147 } /* end create_compound_vl_type() */
148 
149 
150 /*-------------------------------------------------------------------------
151  * Function:	test_getset
152  *
153  * Purpose:	Tests the H5Pget_fill_value() and H5Pset_fill_value()
154  *		functions.
155  *
156  * Return:	Success:	0
157  *
158  *		Failure:	number of errors
159  *
160  * Programmer:	Robb Matzke
161  *              Thursday, October  1, 1998
162  *
163  * Modifications:
164  *
165  *-------------------------------------------------------------------------
166  */
167 static int
test_getset(void)168 test_getset(void)
169 {
170     hid_t	dcpl=-1;
171     int		fill_i;
172     hid_t	type_ss=-1, type_si=-1;
173     struct fill_si {
174 	int 	v1, v2;
175     }		fill_si;
176     struct fill_ss {
177 	short	v1, v2;
178     }		fill_ss, fill_ss_rd;
179 
180     TESTING("property lists");
181 
182     /*
183      * Create the dataset creation property list and the data types that will
184      * be used during this test.
185      */
186     if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
187     if((type_ss=H5Tcreate(H5T_COMPOUND, sizeof fill_ss)) < 0 ||
188 	H5Tinsert(type_ss, "v1", HOFFSET(struct fill_ss, v1),
189 		  H5T_NATIVE_SHORT) < 0 ||
190 	H5Tinsert(type_ss, "v2", HOFFSET(struct fill_ss, v2),
191 		  H5T_NATIVE_SHORT) < 0) {
192 	goto error;
193     }
194     if((type_si=H5Tcreate(H5T_COMPOUND, sizeof fill_si)) < 0 ||
195 	H5Tinsert(type_si, "v1", HOFFSET(struct fill_si, v1),
196 		  H5T_NATIVE_INT) < 0 ||
197 	H5Tinsert(type_si, "v2", HOFFSET(struct fill_si, v2),
198 		  H5T_NATIVE_INT) < 0) {
199 	goto error;
200     }
201 
202     /*
203      * Reading the fill value from a dataset creation property list that has
204      * no fill value should result in a failure.
205      */
206     H5E_BEGIN_TRY {
207 	H5Pget_fill_value(dcpl, H5T_NATIVE_INT, &fill_i);
208     } H5E_END_TRY;
209     if(fill_i != 0) {
210 	H5_FAILED();
211 	puts("    H5Pget_fill_value() should return default 0");
212 	goto error;
213     }
214 
215     /*
216      * Set the fill value using a struct as the data type.
217      */
218     fill_ss.v1 = 1111;
219     fill_ss.v2 = 2222;
220     if(H5Pset_fill_value(dcpl, type_ss, &fill_ss) < 0) goto error;
221 
222     /*
223      * Get the fill value using the same data type that was used to set it.
224      */
225     if(H5Pget_fill_value(dcpl, type_ss, &fill_ss_rd) < 0) goto error;
226     if(fill_ss.v1!=fill_ss_rd.v1 || fill_ss.v2!=fill_ss_rd.v2) {
227 	H5_FAILED();
228 	puts("    Failed to get fill value using same data type that was ");
229 	puts("    used to set the fill value.");
230 	goto error;
231     }
232 
233     /*
234      * Get the fill value using some other data type.
235      */
236     if(H5Pget_fill_value(dcpl, type_si, &fill_si) < 0) goto error;
237     if(fill_ss.v1!=fill_si.v1 || fill_ss.v2!=fill_si.v2) {
238 	H5_FAILED();
239 	puts("    Failed to get fill value using a data type other than what");
240 	puts("    was used to set the fill value.");
241 	goto error;
242     }
243 
244     /*
245      * Reset the fill value
246      */
247     if(H5Pset_fill_value(dcpl, type_si, &fill_si) < 0) goto error;
248     if(H5Pget_fill_value(dcpl, type_ss, &fill_ss) < 0) goto error;
249     if(fill_si.v1!=fill_ss.v1 || fill_si.v2!=fill_ss.v2) {
250 	H5_FAILED();
251 	puts("    Resetting the fill value was unsuccessful.");
252 	goto error;
253     }
254 
255     /* Success */
256     if(H5Pclose(dcpl) < 0) goto error;
257     if(H5Tclose(type_si) < 0) goto error;
258     if(H5Tclose(type_ss) < 0) goto error;
259     PASSED();
260     return 0;
261 
262  error:
263     H5E_BEGIN_TRY {
264 	H5Pclose(dcpl);
265 	H5Tclose(type_si);
266 	H5Tclose(type_ss);
267     } H5E_END_TRY;
268     return 1;
269 }
270 
271 
272 /*-------------------------------------------------------------------------
273  * Function:	test_getset_vl
274  *
275  * Purpose:	Tests the H5Pget_fill_value() and H5Pset_fill_value()
276  *		functions, using variable-length datatype.
277  *
278  * Return:	Success:	0
279  *		Failure:	number of errors
280  *
281  * Programmer:	Quincey Koziol
282  *              Thursday, May 31, 2007
283  *
284  *-------------------------------------------------------------------------
285  */
286 static int
test_getset_vl(hid_t fapl)287 test_getset_vl(hid_t fapl)
288 {
289     hsize_t dims[1] = {2};
290     hid_t fileid = (-1), spaceid = (-1), dtypeid = (-1), datasetid = (-1), plistid = (-1);
291     char fill_value[] = "aaaa";
292     char orig_fill_value[] = "aaaa";
293     char *f1 = fill_value;
294     char *f2;
295     char filename[1024];
296 
297     TESTING("property lists, with variable-length datatype");
298 
299     /* Create string type. */
300     if((dtypeid =  H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR
301     if(H5Tset_size(dtypeid, H5T_VARIABLE) < 0) TEST_ERROR
302 
303     /* Set up dataset creation property list, with fill value */
304     if((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR
305     if(H5Pset_fill_value(plistid, dtypeid, &f1) < 0) TEST_ERROR
306 
307     /* Modify original fill value string */
308     fill_value[0] = 'b';
309 
310     /* Retrieve fill value from property */
311     if(H5Pget_fill_value(plistid, dtypeid, &f2) < 0) TEST_ERROR
312 
313     /* Verify that the fill value is the original value */
314     if(HDstrcmp(f2, orig_fill_value)) TEST_ERROR
315 
316     /* Release the fill value retrieved */
317     HDfree(f2);
318 
319     /* Open file. */
320     h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
321     if((fileid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR
322 
323     /* Write an dataset of this type. */
324     if((spaceid = H5Screate_simple(1, dims, NULL)) < 0) TEST_ERROR
325     if((datasetid = H5Dcreate2(fileid, "Dataset", dtypeid, spaceid, H5P_DEFAULT, plistid, H5P_DEFAULT)) < 0) TEST_ERROR
326 
327     /* Close IDs (except datatype) */
328     if(H5Dclose(datasetid) < 0) TEST_ERROR
329     if(H5Pclose(plistid) < 0) TEST_ERROR
330     if(H5Sclose(spaceid) < 0) TEST_ERROR
331     if(H5Fclose(fileid) < 0) TEST_ERROR
332 
333 
334     /* Re-open file, group & dataset */
335     if((fileid = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) TEST_ERROR
336     if((datasetid = H5Dopen2(fileid, "Dataset", H5P_DEFAULT)) < 0) TEST_ERROR
337 
338     /* Get dataset's creation property list */
339     if((plistid = H5Dget_create_plist(datasetid)) < 0) TEST_ERROR
340 
341     /* Query fill value */
342     if(H5Pget_fill_value(plistid, dtypeid, &f2) < 0) TEST_ERROR
343 
344     /* Verify that the fill value is the original value */
345     if(HDstrcmp(f2, orig_fill_value)) TEST_ERROR
346 
347     /* Release the fill value retrieved */
348     HDfree(f2);
349 
350     /* Close IDs */
351     if(H5Dclose(datasetid) < 0) TEST_ERROR
352     if(H5Fclose(fileid) < 0) TEST_ERROR
353     if(H5Pclose(plistid) < 0) TEST_ERROR
354     if(H5Tclose(dtypeid) < 0) TEST_ERROR
355 
356     PASSED();
357     return 0;
358 
359  error:
360     H5E_BEGIN_TRY {
361     } H5E_END_TRY;
362     return 1;
363 } /* end test_getset_vl() */
364 
365 
366 /*-------------------------------------------------------------------------
367  * Function:	test_create
368  *
369  * Purpose:	Tests creating datasets that have fill values.
370  *
371  * Return:	Success:	0
372  *
373  *		Failure:	number of errors
374  *
375  * Programmer:	Robb Matzke
376  *              Thursday, October  1, 1998
377  *
378  * Modifications:
379  *		Many new cases have been added to this test since
380  *		the fill value design has been modified.
381  *
382  *-------------------------------------------------------------------------
383  */
384 static int
test_create(hid_t fapl,const char * base_name,H5D_layout_t layout)385 test_create(hid_t fapl, const char *base_name, H5D_layout_t layout)
386 {
387     hid_t	file=-1, space=-1, dcpl=-1, comp_type_id=-1;
388     hid_t	dset1=-1, dset2=-1, dset3=-1, dset4=-1, dset5=-1,
389 		dset6=-1, /* dset7=-1, */ dset8=-1, dset9=-1;
390     hsize_t     cur_size[5] = {2, 8, 8, 4, 2};
391     hsize_t	ch_size[5] = {1, 1, 1, 4, 1};
392     short	rd_s, fill_s = 0x1234;
393     long	rd_l, fill_l = 0x4321;
394     char	filename[1024];
395     H5D_space_status_t	allocation;
396     H5D_alloc_time_t    alloc_time;
397     H5D_fill_time_t	fill_time;
398     comp_datatype       rd_c, fill_ctype;
399 
400     if(H5D_CHUNKED==layout) {
401 	TESTING("chunked dataset creation");
402     } else if(H5D_COMPACT==layout) {
403         TESTING("compact dataset creation");
404     } else {
405 	TESTING("contiguous dataset creation");
406     }
407 
408     /*
409      * Create a file.
410      */
411     h5_fixname(base_name, fapl, filename, sizeof filename);
412     if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
413 	goto error;
414     if((space=H5Screate_simple(5, cur_size, cur_size)) < 0) goto error;
415     if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
416     if(H5D_CHUNKED==layout) {
417 	if(H5Pset_chunk(dcpl, 5, ch_size) < 0) goto error;
418     } else if(H5D_COMPACT==layout) {
419         if(H5Pset_layout(dcpl, H5D_COMPACT) < 0) goto error;
420     }
421 
422     /* Create a compound datatype */
423     if((comp_type_id = create_compound_type()) < 0) goto error;
424 
425     /* I. Test cases for late space allocation except compact dataset */
426 
427     if(H5D_COMPACT!=layout) {
428         if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_LATE) < 0) goto error;
429         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
430 
431         /* 1. Compound datatype test */
432         if(H5Pget_fill_value(dcpl, comp_type_id, &fill_ctype) < 0) goto error;
433         fill_ctype.y = 4444;
434         if(H5Pset_fill_value(dcpl, comp_type_id, &fill_ctype) < 0) goto error;
435         if((dset9 = H5Dcreate2(file, "dset9", comp_type_id, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
436             goto error;
437 
438         /* The three datasets test three fill
439          * conversion paths: small to large, large to small, and no conversion.
440          * They depend on `short' being smaller than `long'.
441          */
442         /* 2. Small to large fill conversion */
443 #ifndef NO_FILLING
444         if(H5Pset_fill_value(dcpl, H5T_NATIVE_SHORT, &fill_s) < 0) goto error;
445 #endif
446         if((dset1=H5Dcreate2(file, "dset1", H5T_NATIVE_LONG, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
447 	   goto error;
448 
449         /* 3. Large to small fill conversion */
450 #ifndef NO_FILLING
451         if(H5Pset_fill_value(dcpl, H5T_NATIVE_LONG, &fill_l) < 0) goto error;
452 #endif
453         if((dset2=H5Dcreate2(file, "dset2", H5T_NATIVE_SHORT, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
454 	   goto error;
455 
456         /* 4. No conversion */
457 #ifndef NO_FILLING
458         if(H5Pset_fill_value(dcpl, H5T_NATIVE_LONG, &fill_l) < 0) goto error;
459 #endif
460         if((dset3=H5Dcreate2(file, "dset3", H5T_NATIVE_LONG, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
461 	   goto error;
462 
463         /* 5. late space allocation and never write fill value */
464         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
465         if((dset4=H5Dcreate2(file, "dset4", H5T_NATIVE_LONG, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
466             goto error;
467 
468         /* 6. fill value is undefined while fill write time is H5D_FILL_TIME_ALLOC.
469          * Supposed to fail. */
470         if(H5Pset_fill_value(dcpl, (hid_t)-1, NULL) < 0) goto error;
471         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
472         H5E_BEGIN_TRY {
473             if(H5Dcreate2(file, "dset7", H5T_NATIVE_LONG, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)!=FAIL)
474                 goto error;
475         } H5E_END_TRY;
476     }
477 
478     /* II. Test early space allocation cases */
479 
480     if(H5Pclose(dcpl) < 0)  goto error;
481     if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
482     if(H5D_CHUNKED==layout) {
483         if(H5Pset_chunk(dcpl, 5, ch_size) < 0) goto error;
484     } else if(H5D_COMPACT==layout) {
485         if(H5Pset_layout(dcpl, H5D_COMPACT) < 0) goto error;
486     }
487     if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_EARLY) < 0) goto error;
488 
489     /* 1. Compound datatype test */
490     if(H5Pget_fill_value(dcpl, comp_type_id, &fill_ctype) < 0) goto error;
491     fill_ctype.y = 4444;
492     if(H5Pset_fill_value(dcpl, comp_type_id, &fill_ctype) < 0) goto error;
493     if((dset8 = H5Dcreate2(file, "dset8", comp_type_id, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
494         goto error;
495 
496 
497     if(H5Pset_fill_value(dcpl, H5T_NATIVE_LONG, &fill_l) < 0) goto error;
498 
499     /* 2. Never write fill value */
500     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
501     if((dset5 = H5Dcreate2(file, "dset5", H5T_NATIVE_INT, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
502         goto error;
503 
504     /* 3. Write fill value at space allocation time */
505     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
506     if((dset6 = H5Dcreate2(file, "dset6", H5T_NATIVE_LONG, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
507 	goto error;
508 
509     /* 4. fill value is undefined while fill write time is H5D_FILL_TIME_ALLOC.
510      * Supposed to fail. */
511     if(H5Pset_fill_value(dcpl, (hid_t)-1, NULL) < 0) goto error;
512     H5E_BEGIN_TRY {
513         if(H5Dcreate2(file, "dset7", H5T_NATIVE_LONG, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)!=FAIL)
514             goto error;
515     } H5E_END_TRY;
516 
517     /* Close everything */
518     if(H5D_COMPACT != layout) {
519         if(H5Dclose(dset1) < 0) goto error;
520         if(H5Dclose(dset2) < 0) goto error;
521         if(H5Dclose(dset3) < 0) goto error;
522         if(H5Dclose(dset4) < 0) goto error;
523         if(H5Dclose(dset9) < 0) goto error;
524     }
525     if(H5Dclose(dset5) < 0) goto error;
526     if(H5Dclose(dset6) < 0) goto error;
527     if(H5Dclose(dset8) < 0) goto error;
528     if(H5Sclose(space) < 0) goto error;
529     if(H5Pclose(dcpl) < 0)  goto error;
530     if(H5Fclose(file) < 0)  goto error;
531 
532     /* Open the file and get the dataset fill value from each dataset */
533     if((file = H5Fopen(filename, H5F_ACC_RDONLY, fapl)) < 0)
534 	goto error;
535 
536     /* I. Check cases for late space allocation except compact dataset */
537     if(H5D_COMPACT != layout) {
538         /* 1. Large to small conversion */
539         if((dset1 = H5Dopen2(file, "dset1", H5P_DEFAULT)) < 0) goto error;
540         if((dcpl = H5Dget_create_plist(dset1)) < 0) goto error;
541 #ifndef NO_FILLING
542         if(H5Pget_fill_value(dcpl, H5T_NATIVE_SHORT, &rd_s) < 0) goto error;
543         if(rd_s != fill_s) {
544 	   H5_FAILED();
545 	   printf("    %d: Got a different fill value than what was set.",__LINE__);
546 	   printf("    Got %d, set %d\n", rd_s, fill_s);
547 	   goto error;
548         }
549 #endif
550         if(H5Dclose(dset1) < 0) goto error;
551         if(H5Pclose(dcpl) < 0) goto error;
552 
553         /* 2. Small to large conversion */
554         if((dset2 = H5Dopen2(file, "dset2", H5P_DEFAULT)) < 0) goto error;
555         if((dcpl = H5Dget_create_plist(dset2)) < 0) goto error;
556 #ifndef NO_FILLING
557         if(H5Pget_fill_value(dcpl, H5T_NATIVE_LONG, &rd_l) < 0) goto error;
558         if(rd_l!=fill_l) {
559 	   H5_FAILED();
560 	   printf("    %d: Got a different fill value than what was set.",__LINE__);
561 	   printf("    Got %ld, set %ld\n", rd_l, fill_l);
562 	   goto error;
563         }
564 #endif
565         if(H5Dclose(dset2) < 0) goto error;
566         if(H5Pclose(dcpl) < 0) goto error;
567 
568         /* 3. No conversion */
569         if((dset3 = H5Dopen2(file, "dset3", H5P_DEFAULT)) < 0) goto error;
570         if((dcpl = H5Dget_create_plist(dset3)) < 0) goto error;
571 #ifndef NO_FILLING
572         if(H5Pget_fill_value(dcpl, H5T_NATIVE_LONG, &rd_l) < 0) goto error;
573         if(rd_l != fill_l) {
574 	   H5_FAILED();
575 	   printf("    %d: Got a different fill value than what was set.",__LINE__);
576 	   printf("    Got %ld, set %ld\n", rd_l, fill_l);
577 	   goto error;
578         }
579 #endif
580         if(H5Pget_alloc_time(dcpl, &alloc_time) < 0) goto error;
581         if(H5Pget_fill_time(dcpl, &fill_time) < 0) goto error;
582         if(alloc_time != H5D_ALLOC_TIME_LATE) {
583             H5_FAILED();
584             puts("    Got non-H5D_ALLOC_TIME_LATE space allocation time.");
585             printf("    Got %d\n", alloc_time);
586         }
587         if(fill_time != H5D_FILL_TIME_ALLOC) {
588             H5_FAILED();
589             puts("    Got non-H5D_FILL_TIME_ALLOC fill value write time.");
590             printf("    Got %d\n", fill_time);
591         }
592         if(H5Dclose(dset3) < 0) goto error;
593         if(H5Pclose(dcpl) < 0) goto error;
594 
595         /* 4. late space allocation and never write fill value */
596         if((dset4 = H5Dopen2(file, "dset4", H5P_DEFAULT)) < 0) goto error;
597         if(H5Dget_space_status(dset4, &allocation) < 0) goto error;
598         if(layout == H5D_CONTIGUOUS && allocation != H5D_SPACE_STATUS_NOT_ALLOCATED) {
599             H5_FAILED();
600             puts("    Got allocated space instead of unallocated.");
601             printf("    Got %d\n", allocation);
602             goto error;
603         }
604         if((dcpl = H5Dget_create_plist(dset4)) < 0) goto error;
605         if(H5Pget_alloc_time(dcpl, &alloc_time) < 0) goto error;
606         if(H5Pget_fill_time(dcpl, &fill_time) < 0) goto error;
607         if(alloc_time != H5D_ALLOC_TIME_LATE) {
608 	   H5_FAILED();
609 	   puts("    Got non-H5D_ALLOC_TIME_LATE space allocation time.");
610 	   printf("    Got %d\n", alloc_time);
611         }
612         if(fill_time != H5D_FILL_TIME_NEVER) {
613 	   H5_FAILED();
614 	   puts("    Got non-H5D_FILL_TIME_NEVER fill value write time.");
615     	   printf("    Got %d\n", fill_time);
616         }
617         if(H5Dclose(dset4) < 0) goto error;
618         if(H5Pclose(dcpl) < 0) goto error;
619 
620         /* 5. Compound datatype test */
621         if((dset9 = H5Dopen2(file, "dset9", H5P_DEFAULT)) < 0) goto error;
622         if((dcpl = H5Dget_create_plist(dset9)) < 0) goto error;
623         if(H5Pget_fill_value(dcpl, comp_type_id, &rd_c) < 0) goto error;
624         if( rd_c.a!=0 || rd_c.y != fill_ctype.y || rd_c.x != 0 || rd_c.z != '\0') {
625            H5_FAILED();
626            puts("    Got wrong fill value");
627            printf("    Got rd_c.a=%f, rd_c.y=%f and rd_c.x=%d, rd_c.z=%c\n",
628                   rd_c.a, rd_c.y, rd_c.x, rd_c.z);
629         }
630         if(H5Dclose(dset9) < 0) goto error;
631         if(H5Pclose(dcpl) < 0) goto error;
632     }
633 
634     /* II. Check early space allocation cases */
635 
636     /* 1. Never write fill value */
637     if((dset5 = H5Dopen2(file, "dset5", H5P_DEFAULT)) < 0) goto error;
638     if((dcpl = H5Dget_create_plist(dset5)) < 0) goto error;
639     if(H5Dget_space_status(dset5, &allocation) < 0) goto error;
640     if(layout == H5D_CONTIGUOUS && allocation != H5D_SPACE_STATUS_ALLOCATED) {
641         H5_FAILED();
642         printf("    %d: Got unallocated space instead of allocated.\n",__LINE__);
643         printf("    Got %d\n", allocation);
644         goto error;
645     }
646     if(H5Pget_alloc_time(dcpl, &alloc_time) < 0) goto error;
647     if(alloc_time != H5D_ALLOC_TIME_EARLY) {
648         H5_FAILED();
649         puts("    Got non-H5D_ALLOC_TIME_EARLY space allocation time.");
650         printf("    Got %d\n", alloc_time);
651     }
652     if(H5Pget_fill_time(dcpl, &fill_time) < 0) goto error;
653     if(fill_time != H5D_FILL_TIME_NEVER) {
654         H5_FAILED();
655         puts("    Got non-H5D_FILL_TIME_NEVER fill value write time.");
656         printf("    Got %d\n", fill_time);
657     }
658     if(H5Dclose(dset5) < 0) goto error;
659     if(H5Pclose(dcpl) < 0) goto error;
660 
661     /* 2. test writing fill value at space allocation time */
662     if((dset6 = H5Dopen2(file, "dset6", H5P_DEFAULT)) < 0) goto error;
663     if((dcpl = H5Dget_create_plist(dset6)) < 0) goto error;
664     if(H5Dget_space_status(dset6, &allocation) < 0) goto error;
665     if(layout == H5D_CONTIGUOUS && allocation != H5D_SPACE_STATUS_ALLOCATED) {
666         H5_FAILED();
667         printf("    %d: Got unallocated space instead of allocated.\n",__LINE__);
668         printf("    Got %d\n", allocation);
669         goto error;
670     }
671     if(H5Pget_fill_value(dcpl, H5T_NATIVE_LONG, &rd_l) < 0) goto error;
672     if(rd_l != fill_l) {
673         H5_FAILED();
674 	printf("    %d: Got a different fill value than what was set.",__LINE__);
675         printf("    Got %ld, set %ld\n", rd_l, fill_l);
676         goto error;
677     }
678     if(H5Pget_alloc_time(dcpl, &alloc_time) < 0) goto error;
679     if(alloc_time != H5D_ALLOC_TIME_EARLY) {
680         H5_FAILED();
681         puts("    Got non-H5D_ALLOC_TIME_EARLY space allocation time.");
682         printf("    Got %d\n", alloc_time);
683     }
684     if(H5Pget_fill_time(dcpl, &fill_time) < 0) goto error;
685     if(fill_time != H5D_FILL_TIME_ALLOC) {
686         H5_FAILED();
687         puts("    Got non-H5D_FILL_TIME_ALLOC fill value write time.");
688         printf("    Got %d\n", fill_time);
689     }
690     if(H5Dclose(dset6) < 0) goto error;
691     if(H5Pclose(dcpl) < 0) goto error;
692 
693     /* 3. Compound datatype test */
694     if((dset8 = H5Dopen2(file, "dset8", H5P_DEFAULT)) < 0) goto error;
695     if((dcpl = H5Dget_create_plist(dset8)) < 0) goto error;
696     if(H5Pget_fill_value(dcpl, comp_type_id, &rd_c) < 0) goto error;
697     if(rd_c.a != 0 || rd_c.y != fill_ctype.y || rd_c.x != 0 || rd_c.z!='\0') {
698         H5_FAILED();
699         puts("    Got wrong fill value");
700         printf("    Got rd_c.a=%f, rd_c.y=%f and rd_c.x=%d, rd_c.z=%c\n",
701 		rd_c.a, rd_c.y, rd_c.x, rd_c.z);
702     }
703     if(H5Dclose(dset8) < 0) goto error;
704     if(H5Pclose(dcpl) < 0) goto error;
705 
706     if(H5Fclose(file) < 0) goto error;
707 
708     PASSED();
709     return 0;
710 
711 error:
712     H5E_BEGIN_TRY {
713 	H5Pclose(dcpl);
714 	H5Sclose(space);
715         if(H5D_COMPACT != layout) {
716 	   H5Dclose(dset1);
717 	   H5Dclose(dset2);
718 	   H5Dclose(dset3);
719            H5Dclose(dset4);
720            H5Dclose(dset9);
721         }
722         H5Dclose(dset5);
723         H5Dclose(dset6);
724 	H5Dclose(dset8);
725 	H5Fclose(file);
726     } H5E_END_TRY;
727     return 1;
728 }
729 
730 /*-------------------------------------------------------------------------
731  * Function:	test_rdwr_cases
732  *
733  * Purpose:	Tests fill values read and write for datasets.
734  *
735  * Return:	Success:	0
736  *
737  *		Failure:	1
738  *
739  * Programmer:	Robb Matzke
740  *              Thursday, October  1, 1998
741  *
742  * Modifications:
743  * 		This function is called by test_rdwr to write and read
744  *		dataset for different cases.
745  *
746  *-------------------------------------------------------------------------
747  */
748 static int
test_rdwr_cases(hid_t file,hid_t dcpl,const char * dname,void * _fillval,H5D_fill_time_t fill_time,H5D_layout_t layout,H5T_class_t datatype,hid_t ctype_id)749 test_rdwr_cases(hid_t file, hid_t dcpl, const char *dname, void *_fillval,
750 		H5D_fill_time_t fill_time, H5D_layout_t layout,
751 		H5T_class_t datatype, hid_t ctype_id)
752 {
753     hid_t	fspace=-1, mspace=-1, dset1=-1, dset2=-1;
754     hsize_t	cur_size[5] = {2, 8, 8, 4, 2};
755     hsize_t	one[5] = {1, 1, 1, 1, 1};
756     hsize_t	hs_size[5], hs_stride[5];
757     hsize_t	hs_offset[5], nelmts;
758     int		fillval=(-1), val_rd, should_be;
759     int		i, j, *buf=NULL, odd;
760     unsigned    u;
761     comp_datatype       rd_c, fill_c, should_be_c;
762     comp_datatype	*buf_c=NULL;
763     H5D_space_status_t  allocation;
764 
765     if(datatype==H5T_INTEGER)
766         fillval = *(int*)_fillval;
767     else if(datatype==H5T_COMPOUND) {
768 	fill_c.a=((comp_datatype*)_fillval)->a;
769         fill_c.x=((comp_datatype*)_fillval)->x;
770         fill_c.y=((comp_datatype*)_fillval)->y;
771         fill_c.z=((comp_datatype*)_fillval)->z;
772     } else {
773         puts("Invalid type for test");
774         goto error;
775     }
776 
777     /* Create dataset */
778     if((fspace = H5Screate_simple(5, cur_size, cur_size)) < 0)
779         goto error;
780     if(datatype == H5T_INTEGER && (dset1 = H5Dcreate2(file, dname, H5T_NATIVE_INT, fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
781         goto error;
782     if(datatype == H5T_COMPOUND && (dset2 = H5Dcreate2(file, dname, ctype_id, fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
783         goto error;
784 
785     /* Read some data and make sure it's the fill value */
786     if((mspace = H5Screate_simple(5, one, NULL)) < 0)
787         goto error;
788     for (i=0; i<1000; i++) {
789 	for (j=0; j<5; j++)
790 	    hs_offset[j] = rand() % cur_size[j];
791 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL,
792 				one, NULL) < 0) goto error;
793 
794    	/* case for atomic datatype */
795 	if(datatype==H5T_INTEGER) {
796             if(H5Dread(dset1, H5T_NATIVE_INT, mspace, fspace, H5P_DEFAULT,
797 		&val_rd) < 0) goto error;
798 	    if(fill_time!=H5D_FILL_TIME_NEVER && val_rd!=fillval) {
799 	        H5_FAILED();
800                 HDfprintf(stdout, "%u: Value read was not a fill value.\n", (unsigned)__LINE__);
801 	        HDfprintf(stdout,"    Elmt={%Hu,%Hu,%Hu,%Hu,%Hu}, read: %u, "
802 		       "Fill value: %u\n",
803 		       hs_offset[0], hs_offset[1],
804 		       hs_offset[2], hs_offset[3],
805 		       hs_offset[4], val_rd, fillval);
806 	        goto error;
807 	    }
808 	/* case for compound datatype */
809 	} else if(datatype==H5T_COMPOUND) {
810             if(H5Dread(dset2, ctype_id, mspace, fspace, H5P_DEFAULT,
811                 &rd_c) < 0) goto error;
812             if(fill_time!=H5D_FILL_TIME_NEVER && (rd_c.a!=fill_c.a ||
813 		rd_c.x!=fill_c.x || rd_c.y!=fill_c.y ||
814 		rd_c.z!=fill_c.z)) {
815                 H5_FAILED();
816                 HDfprintf(stdout, "%u: Value read was not a fill value.\n", (unsigned)__LINE__);
817                 HDfprintf(stdout,"    Elmt={%Hu,%Hu,%Hu,%Hu,%Hu}, read: %f, %d, %f, %c"
818                        "Fill value: %f, %d, %f, %c\n",
819                        hs_offset[0], hs_offset[1],
820                        hs_offset[2], hs_offset[3],
821                        hs_offset[4], rd_c.a, rd_c.x, rd_c.y, rd_c.z,
822 			fill_c.a, fill_c.x, fill_c.y, fill_c.z);
823                 goto error;
824             }
825         }
826     }
827     if(H5Sclose(mspace) < 0) goto error;
828 
829     /* Select all odd data locations in the file dataset */
830     for (i=0, nelmts=1; i<5; i++) {
831 	hs_size[i] = cur_size[i]/2;
832 	hs_offset[i] = 0;
833 	hs_stride[i] = 2;
834 	nelmts *= hs_size[i];
835     }
836     if((mspace=H5Screate_simple(5, hs_size, hs_size)) < 0) goto error;
837     if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, hs_stride,
838                             hs_size, NULL) < 0) goto error;
839 
840     /* Read non-contiguous selection from empty dataset */
841 
842     /* case for atomic datatype */
843     if(datatype==H5T_INTEGER) {
844         /*check for overflow*/
845         HDassert((nelmts * sizeof(int)) == (hsize_t)((size_t)(nelmts * sizeof(int))));
846         buf = HDmalloc((size_t)(nelmts * sizeof(int)));
847 
848         if(H5Dread(dset1, H5T_NATIVE_INT, mspace, fspace, H5P_DEFAULT, buf) < 0)
849             goto error;
850 
851         /* Verify values, except if no fill value written */
852         if(fill_time != H5D_FILL_TIME_NEVER) {
853             for(u = 0; u < nelmts; u++) {
854                 if(buf[u] != fillval) {
855                     H5_FAILED();
856                     HDfprintf(stdout, "%u: Value read was not a fill value.\n", (unsigned)__LINE__);
857                     HDfprintf(stdout,"    Elmt={%Hu, %Hu, %Hu, %Hu, %Hu}, read: %u, "
858                            "Fill value: %u\n",
859                            hs_offset[0], hs_offset[1],
860                            hs_offset[2], hs_offset[3],
861                            hs_offset[4], buf[u], fillval);
862                     goto error;
863                 } /* end if */
864             } /* end for */
865         } /* end if */
866     }
867     /* case for compound datatype */
868     else if(datatype == H5T_COMPOUND) {
869         /*check for overflow*/
870         HDassert((nelmts * sizeof(comp_datatype)) ==
871 	    (hsize_t)((size_t)(nelmts * sizeof(comp_datatype))));
872 	buf_c = (comp_datatype *)HDmalloc((size_t)nelmts * sizeof(comp_datatype));
873 
874         if(H5Dread(dset2, ctype_id, mspace, fspace, H5P_DEFAULT, buf_c) < 0)
875             goto error;
876 
877         /* Verify values, except if no fill value written */
878         if(fill_time != H5D_FILL_TIME_NEVER) {
879             for(u = 0; u < nelmts; u++) {
880                 if(buf_c[u].a != fill_c.a || buf_c[u].x != fill_c.x ||
881                         buf_c[u].y != fill_c.y || buf_c[u].z != fill_c.z) {
882                     H5_FAILED();
883                     HDfprintf(stdout, "%u: Value read was not a fill value.\n", (unsigned)__LINE__);
884                     HDfprintf(stdout,"    Elmt={%Hu, %Hu, %Hu, %Hu, %Hu}, read: %f, %d, %f, %c"
885                             "Fill value: %f, %d, %f, %c\n",
886                             hs_offset[0], hs_offset[1],
887                             hs_offset[2], hs_offset[3],
888                             hs_offset[4],
889                             buf_c[u].a, buf_c[u].x, buf_c[u].y, buf_c[u].z,
890                             fill_c.a, fill_c.x, fill_c.y, fill_c.z);
891                     goto error;
892                 } /* end if */
893             } /* end for */
894         } /* end if */
895     }
896 
897     /* Write to all odd data locations */
898 
899     /* case for atomic datatype */
900     if(datatype == H5T_INTEGER) {
901         for(u = 0; u < nelmts; u++)
902             buf[u] = 9999;
903         if(H5Dwrite(dset1, H5T_NATIVE_INT, mspace, fspace, H5P_DEFAULT, buf) < 0)
904             goto error;
905     }
906     /* case for compound datatype */
907     else if(datatype == H5T_COMPOUND) {
908         HDmemset(buf_c, 0, ((size_t)nelmts * sizeof(comp_datatype)));
909         for(u = 0; u < nelmts; u++) {
910 	    buf_c[u].a = 1111.11F;
911  	    buf_c[u].x = 2222;
912 	    buf_c[u].y = 3333.3333F;
913 	    buf_c[u].z = 'd';
914 	}
915         if(H5Dwrite(dset2, ctype_id, mspace, fspace, H5P_DEFAULT, buf_c) < 0)
916             goto error;
917     }
918 
919     /* Check if space is allocated */
920     if(datatype==H5T_INTEGER && H5Dget_space_status(dset1, &allocation) < 0)
921 	goto error;
922     if(datatype==H5T_COMPOUND && H5Dget_space_status(dset2, &allocation) < 0)
923         goto error;
924     if(layout == H5D_CONTIGUOUS && allocation != H5D_SPACE_STATUS_ALLOCATED) {
925         H5_FAILED();
926         printf("    %d: Got unallocated space instead of allocated.\n",__LINE__);
927         printf("    Got %d\n", allocation);
928         goto error;
929     }
930     HDfree(buf);
931     buf = NULL;
932     H5Sclose(mspace);
933 
934     /* Read some data and make sure it's the right value */
935     if((mspace = H5Screate_simple(5, one, NULL)) < 0)
936         goto error;
937     for(i = 0; i < 1000; i++) {
938 	for(j = 0, odd = 0; j < 5; j++) {
939 	    hs_offset[j] = rand() % cur_size[j];
940 	    odd += (int)(hs_offset[j]%2);
941 	} /* end for */
942 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0)
943             goto error;
944 
945 	/* case for atomic datatype */
946         if(datatype==H5T_INTEGER) {
947 	    if(H5Dread(dset1, H5T_NATIVE_INT, mspace, fspace, H5P_DEFAULT, &val_rd) < 0)
948                 goto error;
949             if(fill_time == H5D_FILL_TIME_ALLOC) {
950                 should_be = odd ? fillval : 9999;
951                 if(val_rd!=should_be) {
952                     H5_FAILED();
953                     HDfprintf(stdout, "%u: Value read was not correct.\n", (unsigned)__LINE__);
954                     printf("    Elmt={%ld,%ld,%ld,%ld,%ld}, read: %u, "
955                            "should be: %u\n",
956                            (long)hs_offset[0], (long)hs_offset[1],
957                            (long)hs_offset[2], (long)hs_offset[3],
958                            (long)hs_offset[4], val_rd, should_be);
959                     goto error;
960                 }
961 	    }
962 	    else if(fill_time == H5D_FILL_TIME_NEVER && !odd) {
963 	        should_be = 9999;
964 	        if(val_rd!=should_be) {
965 	            H5_FAILED();
966                     HDfprintf(stdout, "%u: Value read was not correct.\n", (unsigned)__LINE__);
967 	            printf("    Elmt={%ld,%ld,%ld,%ld,%ld}, read: %u, "
968 		           "should be: %u\n",
969 		           (long)hs_offset[0], (long)hs_offset[1],
970 		           (long)hs_offset[2], (long)hs_offset[3],
971 		           (long)hs_offset[4], val_rd, should_be);
972 	            goto error;
973 	        }
974 	    } else if(fill_time == H5D_FILL_TIME_NEVER && odd) {
975  	        /*Trash data. Don't compare*/
976 	    }
977 	} /* end for datatype==H5T_INTEGER */
978 	/* case for compound datatype */
979 	else if(datatype==H5T_COMPOUND) {
980             if(H5Dread(dset2, ctype_id, mspace, fspace, H5P_DEFAULT, &rd_c) < 0)
981                 goto error;
982             if(fill_time == H5D_FILL_TIME_ALLOC) {
983 		if(odd) {
984 		    should_be_c.a=fill_c.a;
985 		    should_be_c.x=fill_c.x;
986 		    should_be_c.y=fill_c.y;
987 		    should_be_c.z=fill_c.z;
988 		} else {
989 		    should_be_c.a=buf_c[0].a;
990 		    should_be_c.x=buf_c[0].x;
991 		    should_be_c.y=buf_c[0].y;
992 		    should_be_c.z=buf_c[0].z;
993 		}
994 		if( rd_c.a!=should_be_c.a || rd_c.x!=should_be_c.x ||
995 		    rd_c.y!=should_be_c.y || rd_c.z!=should_be_c.z)  {
996                     H5_FAILED();
997                     HDfprintf(stdout, "%u: Value read was not correct.\n", (unsigned)__LINE__);
998                     printf("    Elmt={%ld,%ld,%ld,%ld,%ld}, read: %f,%d,%f,%c "
999                            "should be: %f,%d,%f,%c\n",
1000                            (long)hs_offset[0], (long)hs_offset[1],
1001                            (long)hs_offset[2], (long)hs_offset[3],
1002                            (long)hs_offset[4],
1003 			   rd_c.a, rd_c.x, rd_c.y, rd_c.z, should_be_c.a,
1004 		           should_be_c.x,should_be_c.y,should_be_c.z);
1005                     goto error;
1006  		}
1007 	    } /* end for fill_time == H5D_FILL_TIME_ALLOC */
1008 	    else if(fill_time == H5D_FILL_TIME_NEVER && !odd) {
1009                 should_be_c.a=buf_c[0].a;
1010                 should_be_c.x=buf_c[0].x;
1011                 should_be_c.y=buf_c[0].y;
1012                 should_be_c.z=buf_c[0].z;
1013                 if( rd_c.a!=should_be_c.a || rd_c.x!=should_be_c.x ||
1014                     rd_c.y!=should_be_c.y || rd_c.z!=should_be_c.z)  {
1015                     H5_FAILED();
1016                     HDfprintf(stdout, "%u: Value read was not correct.\n", (unsigned)__LINE__);
1017                     printf("    Elmt={%ld,%ld,%ld,%ld,%ld}, read: %f,%d,%f,%c "
1018                            "should be: %f,%d,%f,%c\n",
1019                            (long)hs_offset[0], (long)hs_offset[1],
1020                            (long)hs_offset[2], (long)hs_offset[3],
1021                            (long)hs_offset[4],
1022                            rd_c.a, rd_c.x, rd_c.y, rd_c.z, should_be_c.a,
1023                            should_be_c.x,should_be_c.y,should_be_c.z);
1024                     goto error;
1025                 }
1026 	    } /* end for fill_time == H5D_FILL_TIME_NEVER */
1027             else if(fill_time == H5D_FILL_TIME_NEVER && odd) {
1028                 /*Trash data. Don't compare*/
1029             }
1030 	} /* end for datatype==H5T_COMPOUND */
1031     }
1032     if(datatype == H5T_COMPOUND) {
1033         HDfree(buf_c);
1034         buf_c = NULL;
1035     } /* end if */
1036 
1037     if(H5Sclose(mspace) < 0) goto error;
1038     if(datatype==H5T_INTEGER && H5Dclose(dset1) < 0) goto error;
1039     if(datatype==H5T_COMPOUND && H5Dclose(dset2) < 0) goto error;
1040     if(H5Sclose(fspace) < 0) goto error;
1041     return 0;
1042 
1043  error:
1044     H5E_BEGIN_TRY {
1045 	if(datatype==H5T_INTEGER) H5Dclose(dset1);
1046 	if(datatype==H5T_COMPOUND) H5Dclose(dset2);
1047 	H5Sclose(fspace);
1048 	H5Sclose(mspace);
1049     } H5E_END_TRY;
1050 
1051     return 1;
1052 }
1053 
1054 
1055 /*-------------------------------------------------------------------------
1056  * Function:    test_rdwr
1057  *
1058  * Purpose:     Tests fill values for datasets.
1059  *
1060  * Return:      Success:        0
1061  *
1062  *              Failure:        number of errors
1063  *
1064  * Programmer:  Robb Matzke
1065  *              Thursday, October  1, 1998
1066  *
1067  * Modifications:
1068  *		Many new cases have been added to this test since the
1069  *		fill value design is modified.
1070  *
1071  *-------------------------------------------------------------------------
1072  */
1073 static int
test_rdwr(hid_t fapl,const char * base_name,H5D_layout_t layout)1074 test_rdwr(hid_t fapl, const char *base_name, H5D_layout_t layout)
1075 {
1076     char        filename[1024];
1077     hid_t 	file=-1, dcpl=-1, ctype_id=-1;
1078     hsize_t     ch_size[5] = {2, 8, 8, 4, 2};
1079     int		nerrors=0;
1080     int         fillval = 0x4c70f1cd;
1081     comp_datatype       fill_ctype={0,0,0,0};
1082 
1083     if(H5D_CHUNKED==layout) {
1084         TESTING("chunked dataset I/O");
1085     } else if(H5D_COMPACT==layout) {
1086         TESTING("compact dataset I/O");
1087     } else {
1088         TESTING("contiguous dataset I/O");
1089     }
1090 
1091     h5_fixname(base_name, fapl, filename, sizeof filename);
1092     if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
1093         goto error;
1094 
1095     if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
1096     if(H5D_CHUNKED==layout) {
1097         if(H5Pset_chunk(dcpl, 5, ch_size) < 0) goto error;
1098     } else if(H5D_COMPACT==layout) {
1099         if(H5Pset_layout(dcpl, H5D_COMPACT) < 0) goto error;
1100     }
1101     if((ctype_id=create_compound_type()) < 0) goto error;
1102 
1103 
1104     /* I. Test H5D_ALLOC_TIME_LATE space allocation cases */
1105     if(H5D_COMPACT != layout) {
1106         if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_LATE) < 0) goto error;
1107 
1108         /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value to be default */
1109         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
1110         fillval = 0;
1111         nerrors += test_rdwr_cases(file, dcpl, "dset1", &fillval, H5D_FILL_TIME_ALLOC,
1112                                    layout, H5T_INTEGER, (hid_t)-1);
1113 
1114         /* case for H5D_FILL_TIME_NEVER as fill write time and fill value to be default */
1115         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
1116         nerrors += test_rdwr_cases(file, dcpl, "dset2", &fillval, H5D_FILL_TIME_NEVER,
1117                                    layout, H5T_INTEGER, (hid_t)-1);
1118 
1119         /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value is user-defined */
1120         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
1121         fillval = 0x4c70f1cd;
1122         if(H5Pset_fill_value(dcpl, H5T_NATIVE_INT, &fillval) < 0) goto error;
1123         nerrors += test_rdwr_cases(file, dcpl, "dset3", &fillval, H5D_FILL_TIME_ALLOC,
1124                                    layout, H5T_INTEGER, (hid_t)-1);
1125 
1126         /* case for H5D_FILL_TIME_NEVER as fill write time and fill value is user-defined */
1127         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
1128         if(H5Pset_fill_value(dcpl, H5T_NATIVE_INT, &fillval) < 0) goto error;
1129         nerrors += test_rdwr_cases(file, dcpl, "dset4", &fillval, H5D_FILL_TIME_NEVER,
1130                                    layout, H5T_INTEGER, (hid_t)-1);
1131 
1132         /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value is undefined */
1133         /* This case has been tested in test_create() function */
1134 
1135         /* case for H5D_FILL_TIME_NEVER as fill write time and fill value is undefined */
1136         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
1137         if(H5Pset_fill_value(dcpl, (hid_t)-1, NULL) < 0) goto error;
1138         nerrors += test_rdwr_cases(file, dcpl, "dset5", &fillval, H5D_FILL_TIME_NEVER,
1139                                    layout, H5T_INTEGER, (hid_t)-1);
1140 
1141         /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value is user-defined
1142          * as compound type */
1143         if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
1144         HDmemset(&fill_ctype, 0, sizeof(fill_ctype));
1145         fill_ctype.y = 4444.4444F;
1146         if(H5Pset_fill_value(dcpl, ctype_id, &fill_ctype) < 0) goto error;
1147         nerrors += test_rdwr_cases(file, dcpl, "dset11", &fill_ctype, H5D_FILL_TIME_ALLOC,
1148 				layout, H5T_COMPOUND, ctype_id);
1149 
1150         if(H5Pclose(dcpl) < 0) goto error;
1151         if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
1152         if(H5D_CHUNKED==layout) {
1153             if(H5Pset_chunk(dcpl, 5, ch_size) < 0) goto error;
1154         }
1155     }
1156 
1157 
1158     /* II.  Test H5D_ALLOC_TIME_EARLY space allocation cases */
1159     if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_EARLY) < 0) goto error;
1160 
1161     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value to be default */
1162     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
1163     fillval = 0;
1164     nerrors += test_rdwr_cases(file, dcpl, "dset6", &fillval, H5D_FILL_TIME_ALLOC,
1165                                layout, H5T_INTEGER, (hid_t)-1);
1166 
1167     /* case for H5D_FILL_TIME_NEVER as fill write time and fill value to be default */
1168     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
1169     nerrors += test_rdwr_cases(file, dcpl, "dset7", &fillval, H5D_FILL_TIME_NEVER, layout,
1170                                H5T_INTEGER, (hid_t)-1);
1171 
1172     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value is user-defined */
1173     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
1174     fillval = 0x4c70f1cd;
1175     if(H5Pset_fill_value(dcpl, H5T_NATIVE_INT, &fillval) < 0) goto error;
1176     nerrors += test_rdwr_cases(file, dcpl, "dset8", &fillval, H5D_FILL_TIME_ALLOC,
1177                                layout, H5T_INTEGER, (hid_t)-1);
1178 
1179     /* case for H5D_FILL_TIME_NEVER as fill write time and fill value is user-defined */
1180     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
1181     if(H5Pset_fill_value(dcpl, H5T_NATIVE_INT, &fillval) < 0) goto error;
1182     nerrors += test_rdwr_cases(file, dcpl, "dset9", &fillval, H5D_FILL_TIME_NEVER,
1183                                layout, H5T_INTEGER, (hid_t)-1);
1184 
1185     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value is undefined */
1186     /* This case has been tested in test_create() function */
1187 
1188     /* case for H5D_FILL_TIME_NEVER as fill write time and fill value is undefined */
1189     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
1190     if(H5Pset_fill_value(dcpl, (hid_t)-1, NULL) < 0) goto error;
1191     nerrors += test_rdwr_cases(file, dcpl, "dset10", &fillval, H5D_FILL_TIME_NEVER,
1192                                layout, H5T_INTEGER, (hid_t)-1);
1193 
1194     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value is user-defined
1195      * as compound type */
1196     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
1197     HDmemset(&fill_ctype, 0, sizeof(fill_ctype));
1198     fill_ctype.y = 4444.4444F;
1199     if(H5Pset_fill_value(dcpl, ctype_id, &fill_ctype) < 0) goto error;
1200     nerrors += test_rdwr_cases(file, dcpl, "dset12", &fill_ctype, H5D_FILL_TIME_ALLOC,
1201                                 layout, H5T_COMPOUND, ctype_id);
1202 
1203 
1204     if(nerrors)
1205 	goto error;
1206     if(H5Pclose(dcpl) < 0) goto error;
1207     if(H5Tclose(ctype_id) < 0) goto error;
1208     if(H5Fclose(file) < 0) goto error;
1209     PASSED();
1210     return 0;
1211 
1212  error:
1213     H5E_BEGIN_TRY {
1214         H5Pclose(dcpl);
1215 	H5Tclose(ctype_id);
1216         H5Fclose(file);
1217     } H5E_END_TRY;
1218     return nerrors;
1219 }
1220 
1221 
1222 /*-------------------------------------------------------------------------
1223  * Function:	test_extend_init_integer
1224  *
1225  * Purpose:	Initializes integer values
1226  *
1227  * Return:	Success:	0
1228  *		Failure:	< 0
1229  *
1230  * Programmer:	Quincey Koziol
1231  *              Tuesday, July  3, 2007
1232  *
1233  *-------------------------------------------------------------------------
1234  */
1235 static int
test_extend_init_integer(void * _buf,size_t nelmts,const void * _val)1236 test_extend_init_integer(void *_buf, size_t nelmts, const void *_val)
1237 {
1238     int *buf = (int *)_buf;                     /* Buffer to initialize */
1239     const int   *val = (const int *)_val;       /* Value to use */
1240 
1241     while(nelmts) {
1242         *buf++ = *val;
1243         nelmts--;
1244     } /* end while */
1245 
1246     return 0;
1247 } /* end test_extend_init_integer() */
1248 
1249 
1250 /*-------------------------------------------------------------------------
1251  * Function:	test_extend_verify_integer
1252  *
1253  * Purpose:	Verifies integer values
1254  *
1255  * Return:	Success:	0
1256  *		Failure:	< 0
1257  *
1258  * Programmer:	Quincey Koziol
1259  *              Tuesday, July  3, 2007
1260  *
1261  *-------------------------------------------------------------------------
1262  */
1263 static int
test_extend_verify_integer(unsigned lineno,const hsize_t * offset,const void * _test_val,const void * _compare_val)1264 test_extend_verify_integer(unsigned lineno, const hsize_t *offset,
1265     const void *_test_val, const void *_compare_val)
1266 {
1267     const int   *test_val = (const int *)_test_val;             /* Value to test */
1268     const int   *compare_val = (const int *)_compare_val;       /* Value to compare against */
1269 
1270     /* Verify value */
1271     if(*test_val != *compare_val) {
1272         HDfprintf(stdout, "%u: Value read was not expected.\n", lineno);
1273         HDfprintf(stdout,"    Elmt = {%Hu, %Hu, %Hu, %Hu, %Hu}, read: %d, "
1274                 "expected: %d\n",
1275                 offset[0], offset[1],
1276                 offset[2], offset[3],
1277                 offset[4], *test_val, *compare_val);
1278         goto error;
1279     } /* end if */
1280 
1281     return 0;
1282 
1283 error:
1284     return -1;
1285 } /* end test_extend_verify_integer() */
1286 
1287 
1288 /*-------------------------------------------------------------------------
1289  * Function:	test_extend_release_integer
1290  *
1291  * Purpose:	Release element of integer value
1292  *
1293  * Return:	Success:	0
1294  *		Failure:	< 0
1295  *
1296  * Programmer:	Quincey Koziol
1297  *              Tuesday, July  3, 2007
1298  *
1299  *-------------------------------------------------------------------------
1300  */
1301 static int
test_extend_release_integer(void H5_ATTR_UNUSED * _elmt)1302 test_extend_release_integer(void H5_ATTR_UNUSED *_elmt)
1303 {
1304     return 0;
1305 } /* end test_extend_release_integer() */
1306 
1307 
1308 /*-------------------------------------------------------------------------
1309  * Function:	test_extend_init_cmpd_vl
1310  *
1311  * Purpose:	Initializes compound+vl values
1312  *
1313  * Return:	Success:	0
1314  *		Failure:	< 0
1315  *
1316  * Programmer:	Quincey Koziol
1317  *              Tuesday, July  3, 2007
1318  *
1319  *-------------------------------------------------------------------------
1320  */
1321 static int
test_extend_init_cmpd_vl(void * _buf,size_t nelmts,const void * _val)1322 test_extend_init_cmpd_vl(void *_buf, size_t nelmts, const void *_val)
1323 {
1324     comp_vl_datatype *buf = (comp_vl_datatype *)_buf;                     /* Buffer to initialize */
1325     const comp_vl_datatype   *val = (const comp_vl_datatype *)_val;       /* Value to use */
1326 
1327     while(nelmts) {
1328         /* Shallow copy all fields */
1329         *buf = *val;
1330 
1331         /* Deep copy string fields */
1332         buf->a = HDstrdup(val->a);
1333         buf->b = HDstrdup(val->b);
1334 
1335         buf++;
1336         nelmts--;
1337     } /* end while */
1338 
1339     return 0;
1340 } /* end test_extend_init_cmpd_vl() */
1341 
1342 
1343 /*-------------------------------------------------------------------------
1344  * Function:	test_extend_verify_cmpd_vl
1345  *
1346  * Purpose:	Verifies compound+vl values
1347  *
1348  * Return:	Success:	0
1349  *		Failure:	< 0
1350  *
1351  * Programmer:	Quincey Koziol
1352  *              Tuesday, July  3, 2007
1353  *
1354  *-------------------------------------------------------------------------
1355  */
1356 static int
test_extend_verify_cmpd_vl(unsigned lineno,const hsize_t * offset,const void * _test_val,const void * _compare_val)1357 test_extend_verify_cmpd_vl(unsigned lineno, const hsize_t *offset,
1358     const void *_test_val, const void *_compare_val)
1359 {
1360     const comp_vl_datatype   *test_val = (const comp_vl_datatype *)_test_val;             /* Value to test */
1361     const comp_vl_datatype   *compare_val = (const comp_vl_datatype *)_compare_val;       /* Value to compare against */
1362 
1363     /* Verify value */
1364     if((test_val->x != compare_val->x) ||
1365             HDstrcmp(test_val->a, compare_val->a) ||
1366             HDstrcmp(test_val->b, compare_val->b) ||
1367             (test_val->y != compare_val->y)) {
1368         HDfprintf(stdout, "%u: Value read was not expected.\n", lineno);
1369         HDfprintf(stdout,"    Elmt = {%Hu, %Hu, %Hu, %Hu, %Hu}, read: {%d, '%s', '%s', %d} "
1370                 "expected: {%d, '%s', '%s', %d}\n",
1371                 offset[0], offset[1], offset[2], offset[3], offset[4],
1372                 test_val->x, test_val->a, test_val->b, test_val->y,
1373                 compare_val->x, compare_val->a, compare_val->b, compare_val->y);
1374         goto error;
1375     } /* end if */
1376 
1377     return 0;
1378 
1379 error:
1380     return -1;
1381 } /* end test_extend_verify_cmpd_vl() */
1382 
1383 
1384 /*-------------------------------------------------------------------------
1385  * Function:	test_extend_release_cmpd_vl
1386  *
1387  * Purpose:	Release element of compound+vl value
1388  *
1389  * Return:	Success:	0
1390  *		Failure:	< 0
1391  *
1392  * Programmer:	Quincey Koziol
1393  *              Tuesday, July  3, 2007
1394  *
1395  *-------------------------------------------------------------------------
1396  */
1397 static int
test_extend_release_cmpd_vl(void * _elmt)1398 test_extend_release_cmpd_vl(void *_elmt)
1399 {
1400     comp_vl_datatype   *elmt = (comp_vl_datatype *)_elmt;             /* Element to free */
1401 
1402     /* Free memory for string fields */
1403     HDfree(elmt->a);
1404     HDfree(elmt->b);
1405 
1406     return 0;
1407 } /* end test_extend_release_integer() */
1408 
1409 
1410 /*-------------------------------------------------------------------------
1411  * Function:	test_extend_cases
1412  *
1413  * Purpose:	Called to test fill values with various different values
1414  *
1415  * Return:	Success:	0
1416  *		Failure:	number of errors
1417  *
1418  * Programmer:	Quincey Koziol
1419  *              Tuesday, July  3, 2007
1420  *
1421  *-------------------------------------------------------------------------
1422  */
1423 static int
test_extend_cases(hid_t file,hid_t _dcpl,const char * dset_name,hsize_t * ch_size,hsize_t * start_size,hsize_t * max_size,hid_t dtype,void * fillval)1424 test_extend_cases(hid_t file, hid_t _dcpl, const char *dset_name,
1425     hsize_t *ch_size, hsize_t *start_size, hsize_t *max_size, hid_t dtype, void *fillval)
1426 {
1427     hid_t	fspace = -1, mspace = -1;       /* File & memory dataspaces */
1428     hid_t	dset = -1;                      /* Dataset ID */
1429     hid_t       dcpl = -1;                      /* Dataset creation property list */
1430     hsize_t	extend_size[5];                 /* Dimensions to extend to */
1431     hsize_t	one[5] = {1, 1, 1, 1, 1};       /* Dimensions of single element dataspace */
1432     hsize_t	hs_size[5], hs_stride[5], hs_offset[5];
1433     size_t	nelmts;
1434     H5T_class_t dtype_class;            /* Class of datatype */
1435     int         (*init_rtn)(void *, size_t, const void *);
1436     int         (*verify_rtn)(unsigned, const hsize_t *, const void *, const void *);
1437     int         (*release_rtn)(void *);
1438     size_t      val_size;               /* Size of element */
1439     void        *val_rd, *should_be, *init_val, *odd_val, *even_val;
1440     int		val_rd_i, init_val_i = 9999;
1441     comp_vl_datatype	val_rd_c, init_val_c = {87, "baz", "mumble", 129};
1442     void	*buf = NULL;
1443     unsigned	odd;                    /* Whether an odd or even coord. was read */
1444     unsigned	i, j;                   /* Local index variables */
1445 
1446     /* Make copy of dataset creation property list */
1447     if((dcpl = H5Pcopy(_dcpl)) < 0) TEST_ERROR
1448 
1449 #ifndef NO_FILLING
1450     if(H5Pset_fill_value(dcpl, dtype, fillval) < 0) TEST_ERROR
1451 #endif
1452 
1453     /* Get class of datatype */
1454     if((dtype_class = H5Tget_class(dtype)) < 0) TEST_ERROR
1455 
1456     /* Create a dataspace */
1457     if((fspace = H5Screate_simple(5, start_size, max_size)) < 0) TEST_ERROR
1458 
1459     /* Create dataset */
1460     if((dset = H5Dcreate2(file, dset_name, dtype, fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR
1461 
1462 
1463     /* Set up pointers to elements */
1464     if(dtype_class == H5T_INTEGER) {
1465         /* Initialize specific values for this datatype */
1466         val_size = sizeof(int);
1467         init_val = &init_val_i;
1468         init_rtn = test_extend_init_integer;
1469         verify_rtn = test_extend_verify_integer;
1470         release_rtn = test_extend_release_integer;
1471         val_rd = &val_rd_i;
1472         odd_val = fillval;
1473         even_val = &init_val_i;
1474     } /* end if */
1475     else {
1476         /* Sanity check */
1477         assert(dtype_class == H5T_COMPOUND);
1478 
1479         /* Initialize specific values for this datatype */
1480         val_size = sizeof(comp_vl_datatype);
1481         init_val = &init_val_c;
1482         init_rtn = test_extend_init_cmpd_vl;
1483         verify_rtn = test_extend_verify_cmpd_vl;
1484         release_rtn = test_extend_release_cmpd_vl;
1485         val_rd = &val_rd_c;
1486         odd_val = fillval;
1487         even_val = &init_val_c;
1488     } /* end else */
1489 
1490 
1491     /* Read some data and make sure it's the fill value */
1492     if((mspace = H5Screate_simple(5, one, NULL)) < 0) TEST_ERROR
1493     for(i = 0; i < 1000; i++) {
1494         /* Set offset for random element */
1495 	for(j = 0; j < 5; j++)
1496 	    hs_offset[j] = rand() % start_size[j];
1497 
1498         /* Select the random element */
1499 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1500 
1501         /* Read the random element */
1502 	if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1503 
1504         /* Verify the element read in */
1505         if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, fillval) < 0) TEST_ERROR
1506 
1507         /* Release any VL components */
1508         if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1509 
1510         /* Clear the read buffer */
1511         HDmemset(val_rd, 0, val_size);
1512     } /* end for */
1513     if(H5Sclose(mspace) < 0) TEST_ERROR
1514 
1515 
1516     /* Initialize dataspace & hyperslab info */
1517     for(i = 0, nelmts = 1; i < 5; i++) {
1518 	hs_size[i] = (start_size[i] + 1) / 2;
1519 	hs_offset[i] = 0;
1520 	hs_stride[i] = 2;
1521 	nelmts *= hs_size[i];
1522     } /* end for */
1523 
1524     /* Check for overflow */
1525     assert((nelmts * val_size) == (hsize_t)((size_t)(nelmts * val_size)));
1526 
1527     /* Allocate & initialize buffer */
1528     buf = HDmalloc((size_t)(nelmts * val_size));
1529     init_rtn(buf, nelmts, init_val);
1530 
1531     /* Create dataspace describing memory buffer */
1532     if((mspace = H5Screate_simple(5, hs_size, hs_size)) < 0) TEST_ERROR
1533 
1534     /* Select elements within file dataspace */
1535     if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, hs_stride, hs_size, NULL) < 0) TEST_ERROR
1536 
1537     /* Write to all even data locations */
1538     if(H5Dwrite(dset, dtype, mspace, fspace, H5P_DEFAULT, buf) < 0) TEST_ERROR
1539 
1540     /* Close memory dataspace */
1541     if(H5Sclose(mspace) < 0) TEST_ERROR
1542 
1543 
1544     /* Read some data and make sure it's the right value */
1545     if((mspace = H5Screate_simple(5, one, NULL)) < 0) TEST_ERROR
1546     for(i = 0; i < 1000; i++) {
1547         /* Set offset for random element */
1548 	for(j = 0, odd = 0; j < 5; j++) {
1549 	    hs_offset[j] = rand() % start_size[j];
1550 	    odd += (unsigned)(hs_offset[j] % 2);
1551 	} /* end for */
1552 
1553         /* Select the random element */
1554 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1555 
1556         /* Read the random element */
1557 	if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1558 
1559         /* Verify the element read in */
1560 	should_be = odd ? odd_val : even_val;
1561         if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, should_be) < 0) TEST_ERROR
1562 
1563         /* Release any VL components */
1564         if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1565 
1566         /* Clear the read buffer */
1567         HDmemset(val_rd, 0, val_size);
1568     } /* end for */
1569     if(H5Sclose(mspace) < 0) TEST_ERROR
1570 
1571 
1572     /* Extend the dataset one element in each dimension */
1573     for(i = 0; i < 5; i++)
1574         extend_size[i] = start_size[i] + 1;
1575     if(H5Dset_extent(dset, extend_size) < 0) TEST_ERROR
1576 
1577     /* Re-open file dataspace */
1578     if(H5Sclose(fspace) < 0) TEST_ERROR
1579     if((fspace = H5Dget_space(dset)) < 0) TEST_ERROR
1580 
1581 
1582     /* Read some data and make sure it's the right value */
1583     if((mspace = H5Screate_simple(5, one, NULL)) < 0) TEST_ERROR
1584     for(i = 0; i < 1000; i++) {
1585         /* Set offset for random element */
1586 	for(j = 0, odd = 0; j < 5; j++) {
1587 	    hs_offset[j] = rand() % extend_size[j];
1588 	    if(hs_offset[j] >= start_size[j])
1589 		odd = 1;
1590 	    else
1591   		odd += (unsigned)(hs_offset[j] % 2);
1592 	} /* end for */
1593 
1594         /* Select the random element */
1595 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1596 
1597         /* Read the random element */
1598 	if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1599 
1600         /* Verify the element read in */
1601 	should_be = odd ? odd_val : even_val;
1602         if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, should_be) < 0) TEST_ERROR
1603 
1604         /* Release any VL components */
1605         if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1606 
1607         /* Clear the read buffer */
1608         HDmemset(val_rd, 0, val_size);
1609     } /* end for */
1610     if(H5Sclose(mspace) < 0) TEST_ERROR
1611 
1612 
1613     /* Extend the dataset to the maximum dimension sizes */
1614     if(H5Dset_extent(dset, max_size) < 0) TEST_ERROR
1615 
1616     /* Re-open file dataspace */
1617     if(H5Sclose(fspace) < 0) TEST_ERROR
1618     if((fspace = H5Dget_space(dset)) < 0) TEST_ERROR
1619 
1620 
1621     /* Read some data and make sure it's the right value */
1622     if((mspace = H5Screate_simple(5, one, NULL)) < 0) TEST_ERROR
1623     for(i = 0; i < 1000; i++) {
1624         /* Set offset for random element */
1625 	for(j = 0, odd = 0; j < 5; j++) {
1626 	    hs_offset[j] = rand() % max_size[j];
1627 	    if(hs_offset[j] >= start_size[j])
1628 		odd = 1;
1629 	    else
1630   		odd += (unsigned)(hs_offset[j] % 2);
1631 	} /* end for */
1632 
1633         /* Select the random element */
1634 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1635 
1636         /* Read the random element */
1637 	if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1638 
1639         /* Verify the element read in */
1640 	should_be = odd ? odd_val : even_val;
1641         if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, should_be) < 0) TEST_ERROR
1642 
1643         /* Release any VL components */
1644         if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1645 
1646         /* Clear the read buffer */
1647         HDmemset(val_rd, 0, val_size);
1648     } /* end for */
1649     if(H5Sclose(mspace) < 0) TEST_ERROR
1650 
1651 
1652     /* Shrink the dataset to half of it's maximum size, plus 1/2 of a chunk */
1653     for(i = 0; i < 5; i++)
1654         extend_size[i] = (max_size[i] / 2) + (ch_size[i] / 2);
1655     if(H5Dset_extent(dset, extend_size) < 0) TEST_ERROR
1656 
1657     /* Re-open file dataspace */
1658     if(H5Sclose(fspace) < 0) TEST_ERROR
1659     if((fspace = H5Dget_space(dset)) < 0) TEST_ERROR
1660 
1661 
1662     /* Read some data and make sure it's the right value */
1663     if((mspace = H5Screate_simple(5, one, NULL)) < 0) TEST_ERROR
1664     for(i = 0; i < 1000; i++) {
1665         /* Set offset for random element */
1666 	for(j = 0, odd = 0; j < 5; j++) {
1667 	    hs_offset[j] = rand() % extend_size[j];
1668 	    if(hs_offset[j] >= start_size[j])
1669 		odd = 1;
1670 	    else
1671   		odd += (unsigned)(hs_offset[j] % 2);
1672 	} /* end for */
1673 
1674         /* Select the random element */
1675 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1676 
1677         /* Read the random element */
1678 	if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1679 
1680         /* Verify the element read in */
1681 	should_be = odd ? odd_val : even_val;
1682         if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, should_be) < 0) TEST_ERROR
1683 
1684         /* Release any VL components */
1685         if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1686 
1687         /* Clear the read buffer */
1688         HDmemset(val_rd, 0, val_size);
1689     } /* end for */
1690     if(H5Sclose(mspace) < 0) TEST_ERROR
1691 
1692 
1693     /* Extend the dataset's size by one element, in a dimension that won't
1694      *  cause additional chunks to be needed */
1695     extend_size[2] += 1;
1696     if(H5Dset_extent(dset, extend_size) < 0) TEST_ERROR
1697 
1698     /* Re-open file dataspace */
1699     if(H5Sclose(fspace) < 0) TEST_ERROR
1700     if((fspace = H5Dget_space(dset)) < 0) TEST_ERROR
1701 
1702 
1703     /* Create dataspace for single element sized bufer */
1704     if((mspace = H5Screate_simple(5, one, NULL)) < 0) TEST_ERROR
1705 
1706     /* Set location for "top-most" element in dataset to write */
1707     for(i = 0; i < 5; i++)
1708         hs_offset[i] = extend_size[i] - 1;
1709 
1710     /* Select the element */
1711     if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1712 
1713     /* Write one element in a chunk that's 'partial' and overwrite a fill
1714      *  value that was initialized in the H5Dset_extent() routine.  This will
1715      *  overwrite a fill-value element, which must be de-allocated properly or
1716      *  next read of another fill-value initialized element in this chunk will
1717      *  fail.
1718      */
1719     if(H5Dwrite(dset, dtype, mspace, fspace, H5P_DEFAULT, buf) < 0) TEST_ERROR
1720 
1721     /* Read value back in */
1722     if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1723 
1724     /* Verify the element read in is the value written out */
1725     if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, buf) < 0) TEST_ERROR
1726 
1727     /* Set the element back to fillval */
1728     if(H5Dwrite(dset, dtype, mspace, fspace, H5P_DEFAULT, fillval) < 0) TEST_ERROR
1729 
1730     /* Release any VL components */
1731     if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1732 
1733     /* Clear the read buffer */
1734     HDmemset(val_rd, 0, val_size);
1735 
1736 
1737     /* Set location for another element initialized by H5Dset_extent() */
1738     hs_offset[3] -= 1;
1739 
1740     /* Select the element */
1741     if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1742 
1743     /* Read value back in */
1744     if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1745 
1746     /* Verify the element read in is the fill-value */
1747     if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, fillval) < 0) TEST_ERROR
1748 
1749     /* Release any VL components */
1750     if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1751 
1752     /* Clear the read buffer */
1753     HDmemset(val_rd, 0, val_size);
1754 
1755 
1756     /* Read some data and make sure it's the right value */
1757     for(i = 0; i < 1000; i++) {
1758         /* Set offset for random element */
1759 	for(j = 0, odd = 0; j < 5; j++) {
1760 	    hs_offset[j] = rand() % extend_size[j];
1761 	    if(hs_offset[j] >= start_size[j])
1762 		odd = 1;
1763 	    else
1764   		odd += (unsigned)(hs_offset[j] % 2);
1765 	} /* end for */
1766 
1767         /* Select the random element */
1768 	if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0) TEST_ERROR
1769 
1770         /* Read the random element */
1771 	if(H5Dread(dset, dtype, mspace, fspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1772 
1773         /* Verify the element read in */
1774 	should_be = odd ? odd_val : even_val;
1775         if(verify_rtn((unsigned)__LINE__, hs_offset, val_rd, should_be) < 0) TEST_ERROR
1776 
1777         /* Release any VL components */
1778         if(H5Dvlen_reclaim(dtype, mspace, H5P_DEFAULT, val_rd) < 0) TEST_ERROR
1779 
1780         /* Clear the read buffer */
1781         HDmemset(val_rd, 0, val_size);
1782     } /* end for */
1783     if(H5Sclose(mspace) < 0) TEST_ERROR
1784 
1785 
1786     /* Release elements & memory buffer */
1787     for(i = 0; i < nelmts; i++)
1788         release_rtn((void *)((char *)buf + (val_size * i)));
1789     HDfree(buf);
1790     buf = NULL;
1791 
1792     /* Cleanup IDs */
1793     if(H5Pclose(dcpl) < 0) TEST_ERROR
1794     if(H5Dclose(dset) < 0) TEST_ERROR
1795     if(H5Sclose(fspace) < 0) TEST_ERROR
1796 
1797     return 0;
1798 
1799 error:
1800     if(buf)
1801         HDfree(buf);
1802     H5E_BEGIN_TRY {
1803 	H5Pclose(dcpl);
1804 	H5Dclose(dset);
1805 	H5Sclose(fspace);
1806 	H5Sclose(mspace);
1807     } H5E_END_TRY;
1808 
1809     return -1;
1810 } /* end test_extend_cases() */
1811 
1812 
1813 /*-------------------------------------------------------------------------
1814  * Function:	test_extend
1815  *
1816  * Purpose:	Test that filling works okay when a dataset is extended.
1817  *
1818  * Return:	Success:	0
1819  *
1820  *		Failure:	number of errors
1821  *
1822  * Programmer:	Robb Matzke
1823  *              Monday, October  5, 1998
1824  *
1825  * Modifications:
1826  *
1827  *-------------------------------------------------------------------------
1828  */
1829 static int
test_extend(hid_t fapl,const char * base_name,H5D_layout_t layout)1830 test_extend(hid_t fapl, const char *base_name, H5D_layout_t layout)
1831 {
1832     hid_t	file = -1;              /* File ID */
1833     hid_t	dcpl = -1;              /* Dataset creation property list ID */
1834     hid_t	cmpd_vl_tid = -1;       /* Compound+vl datatype ID */
1835     hsize_t	start_size[5] = {8, 8, 8, 4, 2};
1836     hsize_t	max_size[5] = {32, 32, 32, 16, 8};
1837     hsize_t	ch_size[5] = {1, 8, 8, 4, 2};
1838 #ifdef NO_FILLING
1839     int		fillval_i = 0;
1840 #else
1841     int		fillval_i = 0x4c70f1cd;
1842 #endif
1843     comp_vl_datatype fillval_c = {32, "foo", "bar", 64};         /* Fill value for compound+vl datatype tests */
1844     char	filename[1024];
1845 
1846     /* Print testing message */
1847     if(H5D_CHUNKED == layout)
1848 	TESTING("chunked dataset extend")
1849     else
1850 	TESTING("contiguous dataset extend")
1851 
1852     /* Create dataset creation property list */
1853     if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR
1854     if(H5D_CHUNKED == layout)
1855 	if(H5Pset_chunk(dcpl, 5, ch_size) < 0) TEST_ERROR
1856 
1857 #if 1
1858     /*
1859      * Remove this once contiguous datasets can support extensions in other
1860      * than the slowest varying dimension.  The purpose of this block is to
1861      * make only the slowest varying dimension extendible and yet have the
1862      * same total number of elements as originally.
1863      *
1864      * If this is removed prematurely then you will get an error `only the
1865      * first dimension can be extendible' as long as the test isn't skipped
1866      * below.
1867      */
1868     if(H5D_CONTIGUOUS==layout) {
1869 	max_size[0] = (max_size[0] * max_size[1] * max_size[2] *
1870 		       max_size[3] * max_size[4]) /
1871 		      (start_size[1] * start_size[2] * start_size[3] * start_size[4]);
1872 	max_size[1] = start_size[1];
1873 	max_size[2] = start_size[2];
1874 	max_size[3] = start_size[3];
1875 	max_size[4] = start_size[4];
1876     }
1877 #endif
1878 
1879 #if 1
1880     /*
1881      * Remove this once internal contiguous datasets can support
1882      * extending. If it's removed prematurely you will get an error
1883      * `extendible contiguous non-external dataset' as long as the test isn't
1884      * skipped below.
1885      */
1886     if(H5D_CONTIGUOUS==layout) {
1887         int fd;
1888         hsize_t	nelmts;
1889 
1890 	nelmts = max_size[0]*max_size[1]*max_size[2]*max_size[3]*max_size[4];
1891 	if((fd=HDopen(FILE_NAME_RAW, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 ||
1892 	    HDclose(fd) < 0) goto error;
1893 	if(H5Pset_external(dcpl, FILE_NAME_RAW, (off_t)0, (hsize_t)nelmts*sizeof(int)) < 0)
1894 	    goto error;
1895     }
1896 #endif
1897 
1898 #if 1
1899     /*
1900      * Remove this when contiguous datasets can be exended to some
1901      * predetermined fininte size, even if it's just in the slowest varying
1902      * dimension.  If it's removed prematurely then you'll get one of the
1903      * errors described above or `unable to select fill value region'.
1904      */
1905     if(H5D_CONTIGUOUS==layout) {
1906 	SKIPPED();
1907 	puts("    Not implemented yet -- needs H5S_SELECT_DIFF operator");
1908 	goto skip;
1909     }
1910 #endif
1911 
1912     /* Create a file and dataset */
1913     h5_fixname(base_name, fapl, filename, sizeof filename);
1914     if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR
1915 
1916     /* Get the compound+vl datatype */
1917     if((cmpd_vl_tid = create_compound_vl_type()) < 0) TEST_ERROR
1918 
1919     /* Test integer datatype case */
1920     if(test_extend_cases(file, dcpl, "dset1", ch_size, start_size, max_size,
1921             H5T_NATIVE_INT, &fillval_i) < 0) TEST_ERROR
1922 
1923     /* Test compound+vl datatype datatype case */
1924     if(test_extend_cases(file, dcpl, "dset2", ch_size, start_size, max_size,
1925             cmpd_vl_tid, &fillval_c) < 0) TEST_ERROR
1926 
1927     /* Cleanup */
1928     if(H5Tclose(cmpd_vl_tid) < 0) TEST_ERROR
1929     if(H5Pclose(dcpl) < 0) TEST_ERROR
1930     if(H5Fclose(file) < 0) TEST_ERROR
1931 
1932     PASSED();
1933 
1934     return 0;
1935 
1936 error:
1937     H5E_BEGIN_TRY {
1938         H5Tclose(cmpd_vl_tid);
1939 	H5Pclose(dcpl);
1940 	H5Fclose(file);
1941     } H5E_END_TRY;
1942     return 1;
1943 
1944 skip:
1945     H5E_BEGIN_TRY {
1946 	H5Pclose(dcpl);
1947 	H5Fclose(file);
1948     } H5E_END_TRY;
1949     return 0;
1950 } /* end test_extend() */
1951 
1952 
1953 /*-------------------------------------------------------------------------
1954  * Function:    test_compatible
1955  *
1956  * Purpose:     Tests fill value and dataspace for datasets created by v1.4
1957  *              library.
1958  *
1959  * Return:      Success:        0
1960  *
1961  *              Failure:        number of errors
1962  *
1963  * Programmer:  Raymond Lu
1964  *              Feb 27, 2002
1965  *
1966  * Modifications:
1967  *
1968  *-------------------------------------------------------------------------
1969  */
1970 static int
test_compatible(void)1971 test_compatible(void)
1972 {
1973     hid_t      file=-1, dset1=-1, dset2=-1;
1974     hid_t      dcpl1=-1, dcpl2=-1, fspace=-1, mspace=-1;
1975     int        rd_fill=0, fill_val=4444, val_rd=0;
1976     hsize_t    dims[2], one[2]={1,1};
1977     hsize_t   hs_offset[2]={3,4};
1978     H5D_fill_value_t status;
1979     char       *srcdir = getenv("srcdir"); /*where the src code is located*/
1980     char       testfile[512]="";  /* test file name */
1981 
1982     TESTING("contiguous dataset compatibility with v. 1.4");
1983 
1984   /* Generate correct name for test file by prepending the source path */
1985   if(srcdir && ((strlen(srcdir) + strlen(FILE_COMPATIBLE) + 1) <
1986      sizeof(testfile))) {
1987      HDstrcpy(testfile, srcdir);
1988      HDstrcat(testfile, "/");
1989   }
1990   HDstrcat(testfile, FILE_COMPATIBLE);
1991 
1992   if((file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) {
1993       printf("    Could not open file %s. Try set $srcdir to point at the "
1994               "source directory of test\n", testfile);
1995       goto error;
1996   }
1997 
1998   if((dset1 = H5Dopen2(file, "dset1", H5P_DEFAULT)) < 0) goto error;
1999   if((dcpl1 = H5Dget_create_plist(dset1)) < 0) goto error;
2000   if(H5Pfill_value_defined(dcpl1, &status) < 0) goto error;
2001   if(status != H5D_FILL_VALUE_UNDEFINED) {
2002       H5_FAILED();
2003       printf("    %d: Got a different fill value than what was set.",__LINE__);
2004       printf("    Got status=%ld, suppose to be H5D_FILL_VALUE_UNDEFINED\n",
2005             (long)status);
2006       goto error;
2007   }
2008   if((fspace = H5Dget_space(dset1)) < 0) goto error;
2009   if(H5Sget_simple_extent_dims(fspace, dims, NULL) < 0) goto error;
2010   if(dims[0] != 8 || dims[1] != 8) {
2011       H5_FAILED();
2012       puts("    Got a different dimension size than what was set.");
2013       printf("    Got dims[0]=%ld, dims[1]=%ld, set 8x8\n", (long)dims[0], (long)dims[1]);
2014       goto error;
2015   }
2016   if((mspace = H5Screate_simple(2, one, NULL)) < 0) goto error;
2017   if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0)
2018       goto error;
2019   if(H5Dread(dset1, H5T_NATIVE_INT, mspace, fspace, H5P_DEFAULT, &val_rd) < 0)
2020       goto error;
2021   if(val_rd != 0) {
2022       H5_FAILED();
2023       puts("    Got a different value than what was set.");
2024       printf("    Got %ld, set 0\n", (long)val_rd);
2025       goto error;
2026   }
2027   if(H5Pclose(dcpl1) < 0) goto error;
2028   if(H5Sclose(fspace) < 0) goto error;
2029   if(H5Sclose(mspace) < 0) goto error;
2030   if(H5Dclose(dset1) < 0) goto error;
2031 
2032 
2033   if((dset2 = H5Dopen2(file, "dset2", H5P_DEFAULT)) < 0) goto error;
2034   if((dcpl2 = H5Dget_create_plist(dset2)) < 0) goto error;
2035   if(H5Pfill_value_defined(dcpl2, &status) < 0) goto error;
2036   if(status != H5D_FILL_VALUE_USER_DEFINED) {
2037       H5_FAILED();
2038       printf("    %d: Got a different fill value than what was set.",__LINE__);
2039       printf("    Got status=%ld, suppose to be H5D_FILL_VALUE_USER_DEFINED\n",
2040             (long)status);
2041       goto error;
2042   }
2043   if(H5Pget_fill_value(dcpl2, H5T_NATIVE_INT, &rd_fill) < 0) goto error;
2044   if(rd_fill != fill_val) {
2045       H5_FAILED();
2046       printf("    %d: Got a different fill value than what was set.",__LINE__);
2047       printf("    Got %ld, set %ld\n", (long)rd_fill, (long)fill_val);
2048       goto error;
2049   }
2050   fspace = -1;
2051   if((fspace = H5Dget_space(dset2)) < 0) goto error;
2052   dims[0] = dims[1] = (hsize_t)-1;
2053   if(H5Sget_simple_extent_dims(fspace, dims, NULL) < 0) goto error;
2054   if(dims[0] != 8 || dims[1] != 8) {
2055       H5_FAILED();
2056       puts("    Got a different dimension size than what was set.");
2057       printf("    Got dims[0]=%ld, dims[1]=%ld, set 8x8\n", (long)dims[0], (long)dims[1]);
2058       goto error;
2059   }
2060   if((mspace=H5Screate_simple(2, one, NULL)) < 0) goto error;
2061   if(H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_offset, NULL, one, NULL) < 0)
2062       goto error;
2063   if(H5Dread(dset2, H5T_NATIVE_INT, mspace, fspace, H5P_DEFAULT, &val_rd) < 0)
2064       goto error;
2065   if(val_rd != fill_val) {
2066       H5_FAILED();
2067       puts("    Got a different value than what was set.");
2068       printf("    Got %ld, set %ld\n", (long)val_rd, (long)fill_val);
2069       goto error;
2070   }
2071   if(H5Pclose(dcpl2) < 0) goto error;
2072   if(H5Sclose(fspace) < 0) goto error;
2073   if(H5Sclose(mspace) < 0) goto error;
2074   if(H5Dclose(dset2) < 0) goto error;
2075 
2076   if(H5Fclose(file) < 0) goto error;
2077 
2078   PASSED();
2079 
2080     return 0;
2081 
2082 error:
2083     H5E_BEGIN_TRY {
2084         H5Pclose(dcpl1);
2085         H5Sclose(fspace);
2086         H5Sclose(mspace);
2087         H5Dclose(dset1);
2088         H5Pclose(dcpl2);
2089         H5Sclose(fspace);
2090         H5Dclose(dset2);
2091         H5Fclose(file);
2092     } H5E_END_TRY;
2093     return 1;
2094 }
2095 
2096 /*-------------------------------------------------------------------------
2097  * Function:	test_partalloc_cases
2098  *
2099  * Purpose:	Tests fill values read and write for datasets.
2100  *
2101  * Return:	Success:	0
2102  *
2103  *		Failure:	1
2104  *
2105  * Programmer:	Joel Plutchak
2106  *              April 15, 2013
2107  *
2108  * Modifications:
2109  * 		This function is called by test_rdwr to write and read
2110  *		dataset for different cases of chunked datasets with
2111  *              unallocated chunks.
2112  *
2113  *-------------------------------------------------------------------------
2114  */
2115 
2116 static int
test_partalloc_cases(hid_t file,hid_t dcpl,const char * dname,H5D_fill_time_t fill_time)2117 test_partalloc_cases(hid_t file, hid_t dcpl, const char *dname, H5D_fill_time_t fill_time)
2118 {
2119     hid_t	fspace=-1, dset1=-1, rspace = -1;
2120     herr_t	ret;
2121     hsize_t	ds_size[2] = {4, 4};
2122     hsize_t	max_size[2] = {H5S_UNLIMITED,4};
2123     hsize_t     chunk_size[2] = {1, 4};
2124     int		fillval=(-1);
2125     int     w_values[] = {42};            /* New value to be written */
2126     int     f_values[4] = {88,88,88,88};  /* pre-seed read buffer with known values */
2127     int     r_values[4] = {88,88,88,88};  /* pre-seed read buffer with known values */
2128     hsize_t coord[1][2];                  /* coordinate(s) of point to write */
2129     hsize_t start[2], count[2];
2130 
2131     fillval = 0;   /* default fill value is zero */
2132 
2133     /* Create dataset with 4x4 integer dataset */
2134     if((fspace = H5Screate_simple(2, ds_size, max_size)) < 0)
2135         goto error;
2136     if((dset1 = H5Dcreate2(file, dname, H5T_NATIVE_INT, fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
2137         goto error;
2138 
2139     /*
2140      * Select a point in the file dataspace.
2141      */
2142     coord[0][0]=0; coord[0][1]=0;
2143     if (H5Sselect_elements( fspace, H5S_SELECT_SET, (size_t)1, (const hsize_t *)coord))
2144         goto error;
2145 
2146     /*
2147      * Write single data point to the dataset.
2148      */
2149     if ((ret = H5Dwrite(dset1, H5T_NATIVE_INT, H5S_ALL, fspace, H5P_DEFAULT, w_values))< 0) {
2150         goto error;
2151     }
2152 
2153     /* Read a line/chunk and make sure values are right */
2154     rspace = H5Screate_simple(2, chunk_size, NULL);
2155 
2156     /* Read the first row of elements: one known plus three fill */
2157     start[0] = 0;
2158     start[1] = 0;
2159     count[0] = 1;
2160     count[1] = 4;
2161     if ((ret = H5Sselect_hyperslab(fspace, H5S_SELECT_SET, start, NULL, count, NULL)) < 0)
2162         goto error;
2163     if ((ret = H5Sselect_all(rspace)) < 0)
2164 	goto error;
2165     if(H5Dread(dset1, H5T_NATIVE_INT, rspace, fspace, H5P_DEFAULT, &r_values) < 0)
2166         goto error;
2167 
2168     /* Read the third row of elements: all fill */
2169     start[0] = 2;
2170     start[1] = 0;
2171     count[0] = 1;
2172     count[1] = 4;
2173     if ((ret = H5Sselect_hyperslab(fspace, H5S_SELECT_SET, start, NULL, count, NULL)) < 0)
2174         goto error;
2175     if(H5Dread(dset1, H5T_NATIVE_INT, rspace, fspace, H5P_DEFAULT, &f_values) < 0)
2176         goto error;
2177 
2178     if(fill_time != H5D_FILL_TIME_NEVER) {
2179         /* check allocated chunk */
2180         if ((r_values[0] != w_values[0]) ||
2181                 (r_values[1] != fillval) ||
2182                 (r_values[2] != fillval) ||
2183                 (r_values[3] != fillval)) {
2184             H5_FAILED();
2185             HDfprintf(stdout, "%u: Allocated chunk value read was not correct.\n", (unsigned)__LINE__);
2186             printf("    {%ld,%ld,%ld,%ld} should be {%ld,%ld,%ld,%ld}\n",
2187                     (long)r_values[0], (long)r_values[1],
2188                     (long)r_values[2], (long)r_values[3],
2189                     (long)w_values[0], (long)fillval,
2190                     (long)fillval, (long)fillval );
2191             goto error;
2192         }
2193         /* check unallocated chunk */
2194         if ((f_values[0] != fillval) ||
2195                 (f_values[1] != fillval) ||
2196                 (f_values[2] != fillval) ||
2197                 (f_values[3] != fillval)) {
2198             H5_FAILED();
2199             HDfprintf(stdout, "%u: Unallocated chunk value read was not correct.\n", (unsigned)__LINE__);
2200             printf("    {%ld,%ld,%ld,%ld} should be {%ld,%ld,%ld,%ld}\n",
2201                     (long)f_values[0], (long)f_values[1],
2202                     (long)f_values[2], (long)f_values[3],
2203                     (long)fillval, (long)fillval,
2204                     (long)fillval, (long)fillval );
2205             goto error;
2206         }
2207         /* for the "never fill" case expect to get trash values, so skip */
2208     }
2209     else if(fill_time == H5D_FILL_TIME_NEVER) {
2210     }
2211 
2212     if(H5Sclose(rspace) < 0) goto error;
2213     if(H5Dclose(dset1) < 0) goto error;
2214     if(H5Sclose(fspace) < 0) goto error;
2215     return 0;
2216 
2217  error:
2218     H5E_BEGIN_TRY {
2219 	H5Dclose(dset1);
2220 	H5Sclose(fspace);
2221 	H5Sclose(rspace);
2222     } H5E_END_TRY;
2223 
2224     return 1;
2225 }
2226 
2227 
2228 /*-------------------------------------------------------------------------
2229  * Function:    test_partalloc
2230  *
2231  * Purpose:     Tests fill values for chunked, partially-allocated datasets.
2232  *              Regression test for HDFFV-8247.
2233  *
2234  * Return:      Success:        0
2235  *
2236  *              Failure:        number of errors
2237  *
2238  * Programmer:  Joel Plutchak
2239  *              April 15, 2013
2240  *
2241  *-------------------------------------------------------------------------
2242  */
2243 static int
test_partalloc(hid_t fapl,const char * base_name)2244 test_partalloc(hid_t fapl, const char *base_name)
2245 {
2246     char        filename[1024];
2247     hid_t 	file=-1, dcpl=-1;
2248     hsize_t     ch_size[2] = {1, 4};
2249     int		nerrors=0;
2250 
2251     TESTING("chunked dataset partially allocated I/O");
2252 
2253     h5_fixname(base_name, fapl, filename, sizeof filename);
2254     if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
2255         goto error;
2256 
2257     if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
2258     if(H5Pset_chunk(dcpl, 2, ch_size) < 0) goto error;
2259 
2260     /* I. Test H5D_ALLOC_TIME_LATE space allocation cases */
2261     if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_LATE) < 0) goto error;
2262 #ifdef DEBUG
2263     fprintf( stdout, "\nALLOC_TIME_LATE\n" );
2264 #endif
2265 
2266     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value to be default */
2267     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
2268 #ifdef DEBUG
2269     fprintf( stdout, "   FILL_TIME_ALLOC\n" );
2270 #endif
2271     nerrors += test_partalloc_cases(file, dcpl, "dset1", H5D_FILL_TIME_ALLOC);
2272 
2273     /* case for H5D_FILL_TIME_NEVER as fill write time and fill value to be default */
2274     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
2275 #ifdef DEBUG
2276     fprintf( stdout, "   FILL_TIME_NEVER\n" );
2277 #endif
2278     nerrors += test_partalloc_cases(file, dcpl, "dset2", H5D_FILL_TIME_NEVER );
2279 
2280     /* case for H5D_FILL_TIME_IFSET as fill write time and fill value to be default */
2281     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_IFSET) < 0) goto error;
2282 #ifdef DEBUG
2283     fprintf( stdout, "   FILL_TIME_IFSET\n" );
2284 #endif
2285     nerrors += test_partalloc_cases(file, dcpl, "dset3", H5D_FILL_TIME_IFSET );
2286 
2287     /* II.  Test H5D_ALLOC_TIME_INCR space allocation cases */
2288     if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_INCR) < 0) goto error;
2289 #ifdef DEBUG
2290     fprintf( stdout, "\nALLOC_TIME_INCR\n" );
2291 #endif
2292 
2293     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value to be default */
2294     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
2295 #ifdef DEBUG
2296     fprintf( stdout, "   FILL_TIME_ALLOC\n" );
2297 #endif
2298     nerrors += test_partalloc_cases(file, dcpl, "dset4", H5D_FILL_TIME_ALLOC );
2299 
2300     /* case for H5D_FILL_TIME_NEVER as fill write time and fill value to be default */
2301     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
2302 #ifdef DEBUG
2303     fprintf( stdout, "   FILL_TIME_NEVER\n" );
2304 #endif
2305     nerrors += test_partalloc_cases(file, dcpl, "dset5", H5D_FILL_TIME_NEVER );
2306 
2307     /* case for H5D_FILL_TIME_IFSET as fill write time and fill value to be default */
2308     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_IFSET) < 0) goto error;
2309 #ifdef DEBUG
2310     fprintf( stdout, "   FILL_TIME_IFSET\n" );
2311 #endif
2312     nerrors += test_partalloc_cases(file, dcpl, "dset6", H5D_FILL_TIME_IFSET );
2313 
2314     /* III.  Test H5D_ALLOC_TIME_EARLY space allocation cases */
2315     if(H5Pset_alloc_time(dcpl, H5D_ALLOC_TIME_EARLY) < 0) goto error;
2316 #ifdef DEBUG
2317     fprintf( stdout, "\nALLOC_TIME_EARLY\n" );
2318 #endif
2319 
2320     /* case for H5D_FILL_TIME_ALLOC as fill write time and fill value to be default */
2321     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0) goto error;
2322 #ifdef DEBUG
2323     fprintf( stdout, "   FILL_TIME_ALLOC\n" );
2324 #endif
2325     nerrors += test_partalloc_cases(file, dcpl, "dset7", H5D_FILL_TIME_ALLOC );
2326 
2327     /* case for H5D_FILL_TIME_NEVER as fill write time and fill value to be default */
2328     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_NEVER) < 0) goto error;
2329 #ifdef DEBUG
2330     fprintf( stdout, "   FILL_TIME_NEVER\n" );
2331 #endif
2332     nerrors += test_partalloc_cases(file, dcpl, "dset8", H5D_FILL_TIME_NEVER );
2333 
2334     /* case for H5D_FILL_TIME_IFSET as fill write time and fill value to be default */
2335     if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_IFSET) < 0) goto error;
2336 #ifdef DEBUG
2337     fprintf( stdout, "   FILL_TIME_IFSET\n" );
2338 #endif
2339     nerrors += test_partalloc_cases(file, dcpl, "dset9", H5D_FILL_TIME_IFSET );
2340 
2341     if(nerrors)
2342 	goto error;
2343     if(H5Pclose(dcpl) < 0) goto error;
2344     if(H5Fclose(file) < 0) goto error;
2345     PASSED();
2346     return 0;
2347 
2348  error:
2349     H5E_BEGIN_TRY {
2350         H5Pclose(dcpl);
2351         H5Fclose(file);
2352     } H5E_END_TRY;
2353     return nerrors;
2354 }
2355 
2356 
2357 /*-------------------------------------------------------------------------
2358  * Function:	main
2359  *
2360  * Purpose:	Tests fill values
2361  *
2362  * Return:	Success:
2363  *
2364  *		Failure:
2365  *
2366  * Programmer:	Robb Matzke
2367  *              Thursday, October  1, 1998
2368  *
2369  * Modifications:
2370  *
2371  *-------------------------------------------------------------------------
2372  */
2373 int
main(int argc,char * argv[])2374 main(int argc, char *argv[])
2375 {
2376     int	nerrors=0, argno, test_contig=1, test_chunk=1, test_compact=1;
2377     hid_t	fapl = (-1), fapl2 = (-1);    /* File access property lists */
2378     hbool_t new_format;     /* Whether to use the new format or not */
2379 
2380     if(argc >= 2) {
2381         test_contig = test_chunk = test_compact = 0;
2382         for(argno = 1; argno < argc; argno++) {
2383             if(!strcmp(argv[argno], "contiguous"))
2384                 test_contig = 1;
2385             else if(!strcmp(argv[argno], "chunked"))
2386                 test_chunk = 1;
2387             else if(!strcmp(argv[argno], "compact"))
2388                 test_compact =1;
2389             else {
2390                 fprintf(stderr, "usage: %s [contiguous] [chunked] [compact]\n", argv[0]);
2391                 exit(1);
2392             }
2393         } /* end for */
2394     } /* end if */
2395 
2396     h5_reset();
2397     fapl = h5_fileaccess();
2398 
2399     /* Property list tests */
2400     nerrors += test_getset();
2401     nerrors += test_getset_vl(fapl);
2402 
2403     /* Copy the file access property list */
2404     if((fapl2 = H5Pcopy(fapl)) < 0) TEST_ERROR
2405 
2406     /* Set the "use the latest version of the format" bounds for creating objects in the file */
2407     if(H5Pset_libver_bounds(fapl2, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) TEST_ERROR
2408 
2409     /* Loop over using new group format */
2410     for(new_format = FALSE; new_format <= TRUE; new_format++) {
2411         hid_t my_fapl;
2412 
2413         /* Set the FAPL for the type of format */
2414         if(new_format) {
2415             puts("\nTesting with new file format:");
2416             my_fapl = fapl2;
2417         } /* end if */
2418         else {
2419             puts("Testing with old file format:");
2420             my_fapl = fapl;
2421         } /* end else */
2422 
2423         /* Chunked storage layout tests */
2424         if(test_chunk) {
2425             nerrors += test_create(my_fapl, FILENAME[0], H5D_CHUNKED);
2426             nerrors += test_rdwr  (my_fapl, FILENAME[2], H5D_CHUNKED);
2427             nerrors += test_extend(my_fapl, FILENAME[4], H5D_CHUNKED);
2428             nerrors += test_partalloc(my_fapl, FILENAME[8]);
2429         } /* end if */
2430 
2431         /* Contiguous storage layout tests */
2432         if(test_contig) {
2433             nerrors += test_create(my_fapl, FILENAME[1], H5D_CONTIGUOUS);
2434             nerrors += test_rdwr  (my_fapl, FILENAME[3], H5D_CONTIGUOUS);
2435             nerrors += test_extend(my_fapl, FILENAME[5], H5D_CONTIGUOUS);
2436             nerrors += test_compatible();
2437         } /* end if */
2438 
2439         /* Compact dataset storage tests */
2440         if(test_compact) {
2441             nerrors += test_create(my_fapl, FILENAME[6], H5D_COMPACT);
2442             nerrors += test_rdwr  (my_fapl, FILENAME[7], H5D_COMPACT);
2443         } /* end if */
2444     } /* end for */
2445 
2446     /* Close 2nd FAPL */
2447     H5Pclose(fapl2);
2448 
2449     /* Verify symbol table messages are cached */
2450     nerrors += (h5_verify_cached_stabs(FILENAME, fapl) < 0 ? 1 : 0);
2451 
2452     if(nerrors)
2453         goto error;
2454     puts("All fill value tests passed.");
2455 
2456     if(h5_cleanup(FILENAME, fapl))
2457         HDremove(FILE_NAME_RAW);
2458 
2459     return 0;
2460 
2461 error:
2462     puts("***** FILL VALUE TESTS FAILED *****");
2463     return 1;
2464 }
2465 
2466