summaryrefslogtreecommitdiffstats
path: root/tools/test/h5stat/h5stat_gentest.c
blob: ae110325c32951b5391082b8ecc216c492c2dbbf (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Generate the binary hdf5 files for the h5stat tests.
 * Usage: just execute the program without any arguments will
 * generate all the binary hdf5 files
 *
 * If you regenerate the test files (e.g., changing some code,
 * trying it on a new platform, ...), you need to verify the correctness
 * of the expected output and update the corresponding *.ddl files.
 */
#include "hdf5.h"
#include "H5private.h"

/* For gen_newgrat_file() */
#define NEWGRAT_FILE     "h5stat_newgrat.h5"
#define DATASET_NAME    "DATASET_NAME"
#define GROUP_NAME    "GROUP"
#define ATTR_NAME    "ATTR"
#define NUM_GRPS     35000
#define NUM_ATTRS    100

/* Declarations for gen_idx_file() */
#define IDX_FILE     "h5stat_idx.h5"
#define DSET        "dset"
#define DSET_FILTER    "dset_filter"

/* For gen_threshold_file() */
#define THRESHOLD_FILE         "h5stat_threshold.h5"
#define THRES_ATTR_NAME        "attr"
#define THRES_ATTR_GRP_NAME    "grp_attr"
#define THRES_DSET_NAME     "dset"
#define THRES_NUM        10
#define THRES_NUM_25        25

/* For gen_err_refcount() */
#define ERR_REFCOUNT_FILE   "h5stat_err_refcount.h5"

/*
 * Generate HDF5 file with latest format with
 * NUM_GRPS groups and NUM_ATTRS attributes for the dataset
 */
static void
gen_newgrat_file(const char *fname)
{
    hid_t fcpl          = -1;   /* File creation property */
    hid_t fapl          = -1;   /* File access property */
    hid_t fid           = -1;   /* File id */
    hid_t gid           = -1;   /* Group id */
    hid_t tid           = -1;   /* Datatype id */
    hid_t sid           = -1;   /* Dataspace id */
    hid_t attr_id       = -1;   /* Attribute id */
    hid_t did           = -1;   /* Dataset id */
    char name[30];    /* Group name */
    char attrname[30];    /* Attribute name */
    int  i;        /* Local index variable */

    /* Get a copy file access property list */
    if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        goto error;

    /* Set to use latest library format */
    if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
        goto error;

    /* Get a copy of file creation property list */
    if((fcpl = H5Pcreate(H5P_FILE_CREATE)) < 0)
        goto error;

    /* Set file space handling strategy */
    if(H5Pset_file_space_strategy(fcpl, H5F_FSPACE_STRATEGY_FSM_AGGR, 1, (hsize_t)1) < 0)
        goto error;

     /* Create file */
    if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, fcpl, fapl)) < 0)
        goto error;

    /* Create NUM_GRPS groups in the root group */
    for(i = 1; i <= NUM_GRPS; i++) {
        sprintf(name, "%s%d", GROUP_NAME,i);
        if((gid = H5Gcreate2(fid, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        if(H5Gclose(gid) < 0)
            goto error;
    } /* end for */

    /* Create a datatype to commit and use */
    if((tid = H5Tcopy(H5T_NATIVE_INT)) < 0)
        goto error;

    /* Create dataspace for dataset */
    if((sid = H5Screate(H5S_SCALAR)) < 0)
        goto error;

    /* Create dataset */
    if((did = H5Dcreate2(fid, DATASET_NAME, tid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create NUM_ATTRS for the dataset */
    for(i = 1; i <= NUM_ATTRS; i++) {
        sprintf(attrname, "%s%d", ATTR_NAME,i);
        if((attr_id = H5Acreate2(did, attrname, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        if(H5Aclose(attr_id) < 0)
            goto error;
    } /* end for */

    /* Close dataset, dataspace, datatype, file */
error:
    H5E_BEGIN_TRY {
        H5Pclose(fapl);
        H5Pclose(fcpl);
        H5Aclose(attr_id);
        H5Dclose(did);
        H5Tclose(tid);
        H5Sclose(sid);
        H5Gclose(gid);
        H5Fclose(fid);
    } H5E_END_TRY;
} /* gen_newgrat_file() */

/*
 * Generate an HDF5 file with groups, datasets, attributes for testing the options:
 *    -l N (--links=N): Set the threshold for # of links when printing information for small groups.
 *    -m N (--dims=N): Set the threshold for the # of dimension sizes when printing information for small datasets.
 *    -a N (--numattrs=N): Set the threshold for the # of attributes when printing information for small # of attributes.
 */
static void
gen_threshold_file(const char *fname)
{
    hid_t fid  = -1;                /* File ID */
    hid_t sid0 = -1;                /* Dataspace IDs */
    hid_t sid1 = -1;                /* Dataspace IDs */
    hid_t sid2 = -1;                /* Dataspace IDs */
    hid_t sid3 = -1;                /* Dataspace IDs */
    hid_t sid4 = -1;                /* Dataspace IDs */
    hid_t did  = -1;                /* Dataset ID */
    hid_t attr_id = -1;             /* Attribute ID */
    hid_t gid = -1;                 /* Group ID */
    hsize_t two_dims[] = {2, 5};    /* Dimension array */
    hsize_t one_dims[] = {6};       /* Dimension array */
    hsize_t zero_dims[] = {0};      /* Dimension array */
    char name[30];                  /* Name */
    unsigned i;                     /* Local index variable */

    /* Create file */
    if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create 1-D dataspace with zero dimension size */
    if((sid0 = H5Screate_simple(1, zero_dims, NULL)) < 0)
        goto error;

    /* Create 1-D dataspace with non-zero dimension size*/
    if((sid1 = H5Screate_simple(1, one_dims, NULL)) < 0)
        goto error;

    /* Create 2-D dataspace */
    if((sid2 = H5Screate_simple(2, two_dims, NULL)) < 0)
        goto error;

    /* Create scalar dataspace */
    if((sid3 = H5Screate(H5S_SCALAR)) < 0)
        goto error;

    /* Create null dataspace */
    if((sid4 = H5Screate(H5S_NULL)) < 0)
        goto error;

    /* Create an attribute for the root group */
    if((attr_id = H5Acreate2(fid, "attr", H5T_NATIVE_INT, sid1, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;
    if(H5Aclose(attr_id) < 0)
        goto error;

    /* Create 1-D dataset with zero dimension size for the root group */
    if((did = H5Dcreate2(fid, "zero_dset", H5T_NATIVE_UCHAR, sid0, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create 11 attributes for the dataset */
    for(i = 1; i <= (THRES_NUM+1); i++) {
        sprintf(name, "%s%d", THRES_ATTR_NAME,i);
        if((attr_id = H5Acreate2(did, name, H5T_NATIVE_INT, sid1, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        if(H5Aclose(attr_id) < 0)
            goto error;
    }
    if(H5Dclose(did) < 0)
        goto error;

    /* Create dataset with scalar dataspace for the root group */
    if((did = H5Dcreate2(fid, "scalar_dset", H5T_NATIVE_UCHAR, sid3, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;
    if(H5Dclose(did) < 0)
        goto error;

    /* Create dataset with null dataspace for the root group */
    if((did = H5Dcreate2(fid, "null_dset", H5T_NATIVE_UCHAR, sid4, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;
    if(H5Dclose(did) < 0)
        goto error;

    /* Create 2-D dataset for the root group */
    if((did = H5Dcreate2(fid, "dset", H5T_NATIVE_UCHAR, sid2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create 10 attributes for the 2-D dataset */
    for(i = 1; i <= THRES_NUM; i++) {
        sprintf(name, "%s%d", THRES_ATTR_NAME,i);
        if((attr_id = H5Acreate2(did, name, H5T_NATIVE_INT, sid1, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;
        if(H5Aclose(attr_id) < 0)
            goto error;
    }
    if(H5Dclose(did) < 0)
        goto error;

    /* Create first group */
    if((gid = H5Gcreate2(fid, "group1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create an attribute for the group */
    if((attr_id = H5Acreate2(gid, "ATTR", H5T_NATIVE_INT, sid3, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Close attribute */
    if(H5Aclose(attr_id) < 0)
        goto error;

    /* Create 10 1-D datasets with non-zero dimension size for the group */
    for(i = 1; i <= THRES_NUM; i++) {
        /* set up dataset name */
        sprintf(name, "%s%d", THRES_DSET_NAME,i);

        /* Create the dataset */
        if((did = H5Dcreate2(gid, name, H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;

        /* Close the dataset */
        if(H5Dclose(did) < 0)
            goto error;
    }

    /* Close the group */
    if(H5Gclose(gid) < 0)
        goto error;


    /* Create second group */
    if((gid = H5Gcreate2(fid, "group2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create 25 attributes for the group */
    for(i = 1; i <= THRES_NUM_25; i++) {
    /* Set up attribute name */
        sprintf(name, "%s%d", THRES_ATTR_GRP_NAME,i);

        /* Create the attribute */
        if((attr_id = H5Acreate2(gid, name, H5T_NATIVE_INT, sid2, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;

        /* Close the attribute */
        if(H5Aclose(attr_id) < 0)
            goto error;
    }

    /* Close the group */
    if(H5Gclose(gid) < 0)
        goto error;

    /* Create third group */
    if((gid = H5Gcreate2(fid, "group3", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create 9 1-D datasets with non-zero dimension size for the group */
    for(i = 1; i < THRES_NUM; i++) {
        /* set up dataset name */
        sprintf(name, "%s%d", THRES_DSET_NAME,i);

        /* Create the dataset */
        if((did = H5Dcreate2(gid, name, H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto error;

        /* Close the dataset */
        if(H5Dclose(did) < 0)
            goto error;
    }

error:
    H5E_BEGIN_TRY {
        H5Gclose(gid);
        H5Aclose(attr_id);
        H5Dclose(did);
        H5Sclose(sid0);
        H5Sclose(sid1);
        H5Sclose(sid2);
        H5Sclose(sid3);
        H5Sclose(sid4);
        H5Fclose(fid);
    } H5E_END_TRY;

} /* gen_threshold_file() */

/*
 * Function: gen_idx_file
 *
 * Purpose: Create a file with datasets that use Fixed Array indexing:
 *       one dataset: fixed dimension, chunked layout, w/o filters
 *         one dataset: fixed dimension, chunked layout, w/ filters
 *
 */
static void
gen_idx_file(const char *fname)
{
    hid_t    fapl = -1;            /* file access property id */
    hid_t    fid = -1;                /* file id */
    hid_t    sid = -1;                /* space id */
    hid_t    dcpl = -1;                /* dataset creation property id */
    hid_t    did = -1, did2 = -1;        /* dataset id */
    hsize_t  dims[1] = {10};     /* dataset dimension */
    hsize_t  c_dims[1] = {2};    /* chunk dimension */
    int      i;            /* local index variable */
    int      buf[10];            /* data buffer */

    /* Get a copy of the file access property */
    if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        goto error;

    /* Set the "use the latest format" bounds for creating objects in the file */
    if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
        goto error;

    /* Create file */
    if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        goto error;

    /* Create data */
    for(i = 0; i < 10; i++)
        buf[i] = i;

    /* Set chunk */
    if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0)
        goto error;

    if(H5Pset_chunk(dcpl, 1, c_dims) < 0)
        goto error;

    /* Create a 1D dataset */
    if((sid = H5Screate_simple(1, dims, NULL)) < 0)
        goto error;
    if((did  = H5Dcreate2(fid, DSET, H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
        goto error;

    /* Write to the dataset */
    if(H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
        goto error;

#if defined (H5_HAVE_FILTER_DEFLATE)
    /* set deflate data */
    if(H5Pset_deflate(dcpl, 9) < 0)
        goto error;

    /* Create and write the dataset */
    if((did2  = H5Dcreate2(fid, DSET_FILTER, H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
        goto error;
    if(H5Dwrite(did2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
        goto error;

    /* Close the dataset */
    if(H5Dclose(did2) < 0)
        goto error;
#endif

    /* closing: dataspace, dataset, file */
error:
    H5E_BEGIN_TRY {
        H5Pclose(fapl);
        H5Pclose(dcpl);
        H5Sclose(sid);
        H5Dclose(did);
        H5Fclose(fid);
#if defined (H5_HAVE_FILTER_DEFLATE)
        H5Dclose(did2);
#endif
    } H5E_END_TRY;

} /* gen_idx_file() */

/*
 * Function: gen_err_refcount_file
 *
 * Purpose: Create a file with a refcount message ID.
 *          Then a refcount message ID is written to a
 *          message in a version 1 object header.
 *          This will trigger the error as a version 1
 *          object header does not support a refcount message.
 *          This is to verify HDFFV-10333 that h5stat will exit
 *          gracefully when encountered error similar to
 *          H5O_refcount_decode in the jira issue.
 *
 */
static void
gen_err_refcount(const char *fname)
{
    hid_t fid = -1;     /* File identifier */
    hid_t sid = -1;     /* Dataspace message */
    hid_t did = -1;     /* Dataset identifier */
    hid_t gid = -1;     /* Group identifier */
    hid_t aid1 = -1, aid2 = -1;     /* Attribute identifier */
    hid_t tid = -1;     /* Datatype identifier */
    int i, n;           /* Local index variables */
    int buf[10];        /* Data buffer */
    hsize_t dims[1];    /* Dimension size */
    int fd = -1;        /* File descriptor */
    unsigned short val = 22;        /* The refcount message ID */

    /* Initialize data buffer */
    n = 0;
    for(i = 0; i < 10; i++)
        buf[i] = n++;

    /* Create the file */
    if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create a group */
    if((gid = H5Gcreate2(fid, "group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Create a committed datatype in the group */
    if((tid = H5Tcopy(H5T_NATIVE_INT)) < 0)
        goto error;
    if(H5Tcommit2(gid, "dtype", tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT) < 0)
        goto error;

    /* Create the dataspace */
    dims[0] = 10;
    if((sid = H5Screate_simple(1, dims, NULL)) < 0)
        goto error;

    /* Create a dataset with the committed datatype in the file */
    if((did = H5Dcreate2(fid, "dset", tid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;
    /* Write to the dataset */
    if(H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
        goto error;

    /* Attach an attribute with the committed datatype to the group */
    if((aid1 = H5Acreate2(gid, "attr", tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;
    /* Attach an attribute with the committed datatype to the dataset */
    if((aid2 = H5Acreate2(did, "attr", tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Closing */
    if(H5Aclose(aid1) < 0)
        goto error;
    if(H5Aclose(aid2) < 0)
        goto error;
    if(H5Sclose(sid) < 0)
        goto error;
    if(H5Dclose(did) < 0)
        goto error;
    if(H5Gclose(gid) < 0)
        goto error;
    if(H5Tclose(tid) < 0)
        goto error;
    if(H5Fclose(fid) < 0)
        goto error;

    /* This section of code will write a refcount message ID to a message in the
       version 1 object header which does not support a refcount message */
    /* Offset of the message ID to modify is as follows: */
    /* 4520: the offset of the object header containing the attribute message
             with the committed datatype */
    /* 24: the offset in the object header containing the version of the
           attribute message */
    if((fd = HDopen(fname, O_RDWR, 0633)) >= 0) {
        HDlseek(fd, 4520+24, SEEK_SET);
        HDwrite(fd, &val, 2);
        HDclose(fd);
    }

error:
    H5E_BEGIN_TRY {
        H5Gclose(gid);
        H5Dclose(did);
        H5Tclose(tid);
        H5Sclose(sid);
        H5Aclose(aid1);
        H5Aclose(aid2);
        H5Fclose(fid);
    } H5E_END_TRY;
} /* gen_err_refcount() */

/*
 * The following two test files are generated with older versions
 * of the library for HDFFV-10333.  They are used for testing in
 * testh5stat.sh.in.
 *
 * (1) h5stat_err_old_layout.h5
 *     This file is generated with the 1.6 library so that a file
 *     with a version 2 layout message is created.
 *     Then a "0" is written to the "dimension" field in the layout
 *     message to trigger the error.
 *     This is to verify HDFFV-10333 that h5stat will exit gracefully
 *     when encountered error similar to H5O__layout_decode in the
 *     jira issue.
 *
 * (2) h5stat_err_old_fill.h5
 *     This file is generated with the 1.4 library so that a file
 *     with an old fill value message is created.
 *     Then an illegal size is written to the "size" fild in the
 *     fill value message to trigger the error.
 *     This is to verify HDFFV-10333 that h5stat will exit gracefully
 *     when encountered error similar to H5O_fill_old_decode in the
 *     jira issue.
 */

int main(void)
{
    gen_newgrat_file(NEWGRAT_FILE);
    gen_threshold_file(THRESHOLD_FILE);

    /* Generate an HDF file to test for datasets with Fixed Array indexing */
    gen_idx_file(IDX_FILE);

    /* Generate a file with a refcount message ID */
    gen_err_refcount(ERR_REFCOUNT_FILE);

    return 0;
}