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 <matzke@llnl.gov>
16 * Friday, January 30, 1998
17 *
18 * Purpose: Tests extendible datasets.
19 */
20
21 #include "h5test.h"
22
23 const char *FILENAME[] = {
24 "extend",
25 NULL
26 };
27
28 #define NX 100 /* USE AN EVEN NUMBER!*/
29 #define NY 100 /* USE AN EVEN NUMBER!*/
30
31 /* Data buffers */
32 static int buf1[NY][NX], buf2[NX / 2][NY / 2];
33
34
35 /*-------------------------------------------------------------------------
36 * Function: write_data
37 *
38 * Purpose: Create extendible dataset and test extend/write/read
39 *
40 * Return: Success: 0
41 * Failure: -1
42 *
43 * Programmer: Quincey Koziol
44 * Tuesday, June 10, 2003
45 *
46 *-------------------------------------------------------------------------
47 */
48 static int
write_data(const char * msg,hid_t file,const char * name,hid_t cparms,hid_t mem_space)49 write_data(const char *msg, hid_t file, const char *name, hid_t cparms, hid_t mem_space)
50 {
51 hid_t dataset, file_space, half_space;
52 static const hsize_t dims[2] = {NX, NY};
53 static const hsize_t half_dims[2] = {NX / 2, NY / 2};
54 hsize_t size[2];
55 hsize_t max_size[2] = {0, 0};
56 hsize_t offset[2];
57 int i, j, k, m;
58
59 TESTING(msg);
60
61 /* Create the dataset */
62 if((dataset = H5Dcreate2(file, name, H5T_NATIVE_INT, mem_space, H5P_DEFAULT, cparms, H5P_DEFAULT)) < 0) TEST_ERROR;
63
64 /* Write the data */
65 for(i = 0; i < 5; i++)
66 for(j = 0; j < 5; j++) {
67
68 /* Extend the dataset */
69 offset[0] = (hsize_t)(i * NX);
70 offset[1] = (hsize_t)(j * NY);
71 size[0] = offset[0] + NX;
72 size[1] = offset[1] + NY;
73 if(size[0] > max_size[0] || size[1] > max_size[1]) {
74 if(size[0] > max_size[0])
75 max_size[0] = size[0];
76 if(size[1] > max_size[1])
77 max_size[1] = size[1];
78 if(H5Dset_extent(dataset, max_size) < 0) TEST_ERROR;
79 } /* end if */
80
81 /* Select a hyperslab */
82 if((file_space = H5Dget_space(dataset)) < 0) TEST_ERROR;
83 if(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, dims, NULL) < 0) TEST_ERROR;
84
85 /* Write to the hyperslab */
86 if(H5Dwrite(dataset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, buf1) < 0) TEST_ERROR;
87 if(H5Sclose(file_space) < 0) TEST_ERROR;
88 } /* end for */
89
90 /* Read the data */
91 if((half_space = H5Screate_simple(2, half_dims, NULL)) < 0) TEST_ERROR;
92 if((file_space = H5Dget_space(dataset)) < 0) TEST_ERROR;
93 for(i = 0; i < 10; i++) {
94 for(j = 0; j < 10; j++) {
95
96 /* Select a hyperslab */
97 offset[0] = (hsize_t)(i * (NX / 2));
98 offset[1] = (hsize_t)(j * (NY / 2));
99 if(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, half_dims, NULL) < 0) TEST_ERROR;
100
101 /* Read */
102 if(H5Dread(dataset, H5T_NATIVE_INT, half_space, file_space, H5P_DEFAULT, buf2) < 0) TEST_ERROR;
103
104 /* Compare */
105 for(k = 0; k < (NX / 2); k++)
106 for(m = 0; m < (NY / 2); m++)
107 if(buf2[k][m] != buf1[(i % 2) * (NX / 2) + k][(j % 2) * (NY / 2) + m]) {
108 HDprintf(" i=%d, j=%d, k=%d, m=%d\n", i, j, k, m);
109 HDprintf(" buf2[%d][%d]=%d\n", k, m, buf2[k][m]);
110 HDprintf(" buf1[%d][%d]=%d\n", (i % 2) * (NX / 2) + k, (j % 2) * (NY / 2) + m, buf1[(i % 2) * (NX / 2) + k][(j % 2) * (NY / 2) + m]);
111 TEST_ERROR;
112 } /* end if */
113 } /* end for */
114 } /* end for */
115
116
117 /* Cleanup */
118 if(H5Dclose(dataset) < 0) TEST_ERROR;
119 if(H5Sclose(file_space) < 0) TEST_ERROR;
120 if(H5Sclose(half_space) < 0) TEST_ERROR;
121
122 PASSED();
123 return 0;
124
125 error:
126 return -1;
127 } /* end write_data() */
128
129 #ifndef H5_NO_DEPRECATED_SYMBOLS
130
131 /*-------------------------------------------------------------------------
132 * Function: write_data_deprec
133 *
134 * Purpose: Create extendible dataset and test extend/write/read, with
135 * deprecated API routine (H5Dextend)
136 *
137 * Return: Success: 0
138 * Failure: -1
139 *
140 * Programmer: Quincey Koziol
141 * Monday, October 8, 2007
142 *
143 *-------------------------------------------------------------------------
144 */
145 static int
write_data_deprec(const char * msg,hid_t file,const char * name,hid_t cparms,hid_t mem_space)146 write_data_deprec(const char *msg, hid_t file, const char *name, hid_t cparms, hid_t mem_space)
147 {
148 hid_t dataset, file_space, half_space;
149 static const hsize_t dims[2] = {NX, NY};
150 static const hsize_t half_dims[2] = {NX / 2, NY / 2};
151 static hsize_t size[2];
152 hsize_t offset[2];
153 int i, j, k, m;
154
155 TESTING(msg);
156
157 /* Create the dataset */
158 if((dataset = H5Dcreate2(file, name, H5T_NATIVE_INT, mem_space, H5P_DEFAULT, cparms, H5P_DEFAULT)) < 0) TEST_ERROR;
159
160 /* Write the data */
161 for(i = 0; i < 5; i++)
162 for(j = 0; j < 5; j++) {
163
164 /* Extend the dataset */
165 offset[0] = (hsize_t)(i * NX);
166 offset[1] = (hsize_t)(j * NY);
167 size[0] = offset[0] + NX;
168 size[1] = offset[1] + NY;
169 if(H5Dextend(dataset, size) < 0) TEST_ERROR;
170
171 /* Select a hyperslab */
172 if((file_space = H5Dget_space(dataset)) < 0) TEST_ERROR;
173 if(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, dims, NULL) < 0) TEST_ERROR;
174
175 /* Write to the hyperslab */
176 if(H5Dwrite(dataset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, buf1) < 0) TEST_ERROR;
177 if(H5Sclose(file_space) < 0) TEST_ERROR;
178 } /* end for */
179
180 /* Read the data */
181 if((half_space = H5Screate_simple(2, half_dims, NULL)) < 0) TEST_ERROR;
182 if((file_space = H5Dget_space(dataset)) < 0) TEST_ERROR;
183 for(i = 0; i < 10; i++) {
184 for(j = 0; j < 10; j++) {
185
186 /* Select a hyperslab */
187 offset[0] = (hsize_t)(i * (NX / 2));
188 offset[1] = (hsize_t)(j * (NY / 2));
189 if(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, half_dims, NULL) < 0) TEST_ERROR;
190
191 /* Read */
192 if(H5Dread(dataset, H5T_NATIVE_INT, half_space, file_space, H5P_DEFAULT, buf2) < 0) TEST_ERROR;
193
194 /* Compare */
195 for(k = 0; k < (NX / 2); k++)
196 for(m = 0; m < (NY / 2); m++)
197 if(buf2[k][m] != buf1[(i % 2) * (NX / 2) + k][(j % 2) * (NY / 2) + m]) {
198 HDprintf(" i=%d, j=%d, k=%d, m=%d\n", i, j, k, m);
199 HDprintf(" buf2[%d][%d]=%d\n", k, m, buf2[k][m]);
200 HDprintf(" buf1[%d][%d]=%d\n", (i % 2) * (NX / 2) + k, (j % 2) * (NY / 2) + m, buf1[(i % 2) * (NX / 2) + k][(j % 2) * (NY / 2) + m]);
201 TEST_ERROR;
202 } /* end if */
203 } /* end for */
204 } /* end for */
205
206
207 /* Cleanup */
208 if(H5Dclose(dataset) < 0) TEST_ERROR;
209 if(H5Sclose(file_space) < 0) TEST_ERROR;
210 if(H5Sclose(half_space) < 0) TEST_ERROR;
211
212 PASSED();
213 return 0;
214
215 error:
216 return -1;
217 } /* end write_data_deprec() */
218 #endif /* H5_NO_DEPRECATED_SYMBOLS */
219
220
221 /*-------------------------------------------------------------------------
222 * Function: main
223 *
224 * Purpose: Tests extendible datasets
225 *
226 * Return: EXIT_SUCCESS/EXIT_FAILURE
227 *
228 * Programmer: Robb Matzke
229 * Friday, January 30, 1998
230 *
231 *-------------------------------------------------------------------------
232 */
233 int
main(void)234 main (void)
235 {
236 hid_t file, mem_space, cparms;
237 hid_t fapl;
238 int nerrors = 0;
239 static const hsize_t dims[2] = {NX, NY};
240 static const hsize_t chunk_dims[2] = {NX/2, NY/2};
241 static hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
242 char filename[1024];
243 int i, j;
244
245 h5_reset();
246 fapl = h5_fileaccess();
247
248 /* Initialize buffer and space */
249 for(i = 0; i < NX; i++)
250 for(j = 0; j < NY; j++)
251 buf1[i][j] = i * NY + j;
252
253 if((mem_space = H5Screate_simple(2, dims, maxdims)) < 0) TEST_ERROR;
254
255 /* Create the file */
256 h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
257 if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR;
258
259 /* Create the dataset creation property list */
260 if((cparms = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR;
261 if(H5Pset_chunk(cparms, 2, chunk_dims) < 0) TEST_ERROR;
262
263 /* Test with incremental allocation */
264 nerrors += write_data("extendible dataset with incr. allocation", file, "dataset1a", cparms, mem_space) < 0 ? 1 : 0;
265 #ifndef H5_NO_DEPRECATED_SYMBOLS
266 nerrors += write_data_deprec("extendible dataset with incr. allocation w/deprec. symbols", file, "dataset1b", cparms, mem_space) < 0 ? 1 : 0;
267 #endif /* H5_NO_DEPRECATED_SYMBOLS */
268
269 /* Test with early allocation */
270 if(H5Pset_alloc_time(cparms, H5D_ALLOC_TIME_EARLY) < 0) TEST_ERROR;
271 nerrors += write_data("extendible dataset with early allocation", file, "dataset2a", cparms, mem_space) < 0 ? 1 : 0;
272 #ifndef H5_NO_DEPRECATED_SYMBOLS
273 nerrors += write_data_deprec("extendible dataset with early allocation w/deprec. symbols", file, "dataset2b", cparms, mem_space) < 0 ? 1 : 0;
274 #endif /* H5_NO_DEPRECATED_SYMBOLS */
275
276 if(H5Pclose(cparms) < 0) TEST_ERROR;
277 if(H5Sclose(mem_space) < 0) TEST_ERROR;
278 if(H5Fclose(file) < 0) TEST_ERROR;
279
280 /* Verify symbol table messages are cached */
281 nerrors += (h5_verify_cached_stabs(FILENAME, fapl) < 0 ? 1 : 0);
282
283 if(nerrors) {
284 HDprintf("***** %d FAILURE%s! *****\n", nerrors, (1 == nerrors) ? "" : "S");
285 HDexit(EXIT_FAILURE);
286 } /* end if */
287
288 HDprintf("All extend tests passed.\n");
289 h5_cleanup(FILENAME, fapl);
290
291 HDexit(EXIT_SUCCESS);
292
293 error:
294 HDprintf("*** One or more extend tests failed ***\n");
295 HDexit(EXIT_FAILURE);
296 } /* end main() */
297
298