summaryrefslogtreecommitdiffstats
path: root/test/set_extent.c
blob: 3615e380e597617f31e10bbb5a35cde8a4d962fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

#include "hdf5.h"
#include "h5test.h"


/*-------------------------------------------------------------------------
 *
 * Tests the function H5Dset_extent. In the current version of the library 
 * the dataset MUST be chunked.
 *
 *-------------------------------------------------------------------------
 */

#define RANK 2


int main( void )
{
 
 hid_t   file_id;
 hid_t   dataset_id;
 hid_t   space_id;  
 hid_t   plist_id;
 hsize_t dims[RANK] = { 90, 90 };
 hsize_t dims_new[RANK] = { 70, 70 };
 hsize_t dims_chunk[RANK] = { 20, 20 };
 hsize_t dims_out[RANK];
 hsize_t maxdims[RANK] = { H5S_UNLIMITED, H5S_UNLIMITED };
 int     data[ 90 ][ 90 ];
 int     buf1[ 70 ][ 70 ];
 int     buf2[ 90 ][ 90 ];
 herr_t  status; 
 int     i, j, n = 0;
 
 
 for( i = 0; i < 90; i++ )
 {
  for( j = 0; j < 90; j++ )
  {
   data[i][j] = n++;
  }
 }
 
 
/*-------------------------------------------------------------------------
 * Test H5Dset_extent with chunks on the raw data cache
 *-------------------------------------------------------------------------
 */
 
 TESTING("extend dataset create");
 
 
 /* Create a new file using default properties. */
 file_id = H5Fcreate( "set_extent_create.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
 
 /* Create the data space with unlimited dimensions. */
 space_id = H5Screate_simple( RANK, dims, maxdims ); 
 
 /* Modify dataset creation properties, i.e. enable chunking. */
 plist_id = H5Pcreate (H5P_DATASET_CREATE);
 status = H5Pset_chunk( plist_id, RANK, dims_chunk);
 
 
/*-------------------------------------------------------------------------
 * Create and write one dataset
 *-------------------------------------------------------------------------
 */
 
 /* Create a new dataset */
 dataset_id = H5Dcreate( file_id , "Dataset", H5T_NATIVE_INT, space_id, plist_id );
 
 /* Write the data. */
 status = H5Dwrite( dataset_id , H5T_NATIVE_INT, space_id, H5S_ALL, H5P_DEFAULT, data );
 
/*-------------------------------------------------------------------------
 * Set new dimensions for the array; shrink it 
 *-------------------------------------------------------------------------
 */
 
 /* Set new dimensions for the array. */
 status = H5Dset_extent( dataset_id , dims_new );
 
 /* Get the space. */
 space_id = H5Dget_space( dataset_id );
 
 /* Get dimensions. */
 status = H5Sget_simple_extent_dims( space_id, dims_out, NULL);
 
 if ( dims_out[0] != dims_new[0] ) 
  goto out;
 
 
/*-------------------------------------------------------------------------
 * Read
 *-------------------------------------------------------------------------
 */
 
 /* Read the new dataset. */
 status = H5Dread( dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1 );
 
 
 /* Compare the read array with the original array */
 for( i = 0; i < dims_out[0]; i++ )
 {
  for( j = 0; j < dims_out[1]; j++ )
  {
   if (  buf1[i][j] != data[i][j] ) {
    goto out;
   }
  }
 }
 
 
#if 0
 
/*-------------------------------------------------------------------------
 * Set new dimensions for the array; expand it again 
 *-------------------------------------------------------------------------
 */
 
 /* Set new dimensions for the array. */
 status = H5Dset_extent( dataset_id , dims );
 
 /* Get the space. */
 space_id = H5Dget_space( dataset_id );
 
 /* Get dimensions. */
 status = H5Sget_simple_extent_dims( space_id, dims_out, NULL);
 
 if ( dims_out[0] != dims[0] ) 
  goto out;
 
 
/*-------------------------------------------------------------------------
 * Read
 *-------------------------------------------------------------------------
 */
 
 /* Read the new dataset. */
 status = H5Dread( dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2 );
 
 /* Compare the read array with the original array */
 for( i = 0; i < dims_out[0]; i++ )
 {
  for( j = 0; j < dims_out[1]; j++ )
  {
   if ( i > 70 || j > 70 )
   {
    if (  buf2[i][j] != 0 ) {
     goto out;
    }
   }
   else
   {
    if (  buf2[i][j] != data[i][j] ) {
     goto out;
    }
   }
   
  }
 }
 
#endif
 
/*-------------------------------------------------------------------------
 * Close/release resources
 *-------------------------------------------------------------------------
 */
 
 H5Dclose( dataset_id );
 H5Sclose( space_id );
 H5Pclose( plist_id  );
 H5Fclose( file_id );
 
 
 
 PASSED();
 
 
/*-------------------------------------------------------------------------
 * Test H5Dset_extent with chunks written to file
 *-------------------------------------------------------------------------
 */
 
 TESTING("extend dataset read");
 
 /* Create a new file using default properties. */
 file_id = H5Fcreate( "set_extent_read.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
 
 /* Create the data space with unlimited dimensions. */
 space_id = H5Screate_simple( RANK, dims, maxdims ); 
 
 /* Modify dataset creation properties, i.e. enable chunking. */
 plist_id = H5Pcreate (H5P_DATASET_CREATE);
 status = H5Pset_chunk( plist_id, RANK, dims_chunk);
 
 /* Create a new dataset within the file using cparms creation properties. */
 dataset_id = H5Dcreate( file_id , "Dataset", H5T_NATIVE_INT, space_id, plist_id );
 
 /* Write the data. */
 status = H5Dwrite( dataset_id , H5T_NATIVE_INT, space_id, H5S_ALL, H5P_DEFAULT, data );
 
 /* Close/release resources. */
 H5Dclose( dataset_id );
 H5Sclose( space_id );
 H5Pclose( plist_id  );
 H5Fclose( file_id );
 
 
 /* Open the file */
 file_id = H5Fopen( "set_extent_read.h5", H5F_ACC_RDWR, H5P_DEFAULT );
 
 /* Open the dataset */
 dataset_id = H5Dopen( file_id , "Dataset" );
 
#if 0
 /* Read the dataset. */
 status = H5Dread( dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data );
#endif
 
 /* Set new dimensions for the array. */
 status = H5Dset_extent( dataset_id, dims_new );
 
 /* Get the space. */
 space_id = H5Dget_space( dataset_id );
 
 /* Get dimensions. */
 status = H5Sget_simple_extent_dims( space_id, dims_out, NULL);
 
 if ( dims_out[0] != dims_new[0] ) 
  goto out;
 
#if 1
 
 /* Read the new dataset. */
 status = H5Dread( dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1 );
 
 /* Compare the read array with the original array */
 for( i = 0; i < dims_out[0]; i++ )
 {
  for( j = 0; j < dims_out[1]; j++ )
  {
   if (  buf1[i][j] != data[i][j] ) {
    goto out;
   }
  }
 }
#endif
 
 
 
#if 0
 
/*-------------------------------------------------------------------------
 * Set new dimensions for the array; expand it again 
 *-------------------------------------------------------------------------
 */
 
 /* Set new dimensions for the array. */
 status = H5Dset_extent( dataset_id , dims );
 
 /* Get the space. */
 space_id = H5Dget_space( dataset_id );
 
 /* Get dimensions. */
 status = H5Sget_simple_extent_dims( space_id, dims_out, NULL);
 
 if ( dims_out[0] != dims[0] ) 
  goto out;
 
 /* Read the new dataset. */
 status = H5Dread( dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2 );
 
 /* Compare the read array with the original array */
 for( i = 0; i < dims_out[0]; i++ )
 {
  for( j = 0; j < dims_out[1]; j++ )
  {
   if ( i > 70 || j > 70 )
   {
    if (  buf2[i][j] != 0 ) {
     goto out;
    }
   }
   else
   {
    if (  buf2[i][j] != data[i][j] ) {
     goto out;
    }
   }
   
  }
 }
 
#endif
 
/*-------------------------------------------------------------------------
 * Close/release resources
 *-------------------------------------------------------------------------
 */
 
 H5Dclose( dataset_id );
 H5Sclose( space_id );
 H5Fclose( file_id );
 
 PASSED();
 
 return 0;
 
 
out:
 H5Dclose( dataset_id );
 H5Sclose( space_id );
 H5Pclose( plist_id  );
 H5Fclose( file_id );
 H5_FAILED();
 return 1;
 
}