summaryrefslogtreecommitdiffstats
path: root/test/API/H5_api_test_util.c
blob: 41640b4da539a6e43e7aea9bfe4fd14ee1575f7f (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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "H5_api_test.h"
#include "H5_api_test_util.h"

/*
 * The maximum allowable size of a generated datatype.
 *
 * NOTE: HDF5 currently has limits on the maximum size of
 * a datatype of an object, as this information is stored
 * in the object header. In order to provide maximum
 * compatibility between the native VOL connector and others
 * for this test suite, we limit the size of a datatype here.
 * This value should be adjusted as future HDF5 development
 * allows.
 */
#define GENERATED_DATATYPE_MAX_SIZE 65536

/*
 * The maximum size of a datatype for compact objects that
 * must fit within the size of a native HDF5 object header message.
 * This is typically used for attributes and compact datasets.
 */
#define COMPACT_DATATYPE_MAX_SIZE 1024

/* The maximum level of recursion that the generate_random_datatype()
 * function should go down to, before being forced to choose a base type.
 */
#define TYPE_GEN_RECURSION_MAX_DEPTH 3

/* The number of predefined standard integer types in HDF5
 */
#define NUM_PREDEFINED_INT_TYPES 16

/* The number of predefined floating point types in HDF5
 */
#define NUM_PREDEFINED_FLOAT_TYPES 6

/* The maximum number of members allowed in an HDF5 compound type, as
 * generated by the generate_random_datatype() function, for ease of
 * development.
 */
#define COMPOUND_TYPE_MAX_MEMBERS 4

/* The maximum number and size of the dimensions of an HDF5 array
 * datatype, as generated by the generate_random_datatype() function.
 */
#define ARRAY_TYPE_MAX_DIMS 4

/* The maximum number of members and the maximum size of those
 * members' names for an HDF5 enum type, as generated by the
 * generate_random_datatype() function.
 */
#define ENUM_TYPE_MAX_MEMBER_NAME_LENGTH 256
#define ENUM_TYPE_MAX_MEMBERS            16

/* The maximum size of an HDF5 string datatype, as created by the
 * generate_random_datatype() function.
 */
#define STRING_TYPE_MAX_SIZE 1024

/*
 * The maximum dimensionality and dimension size of a dataspace
 * generated for an attribute or compact dataset.
 */
#define COMPACT_SPACE_MAX_DIM_SIZE 4
#define COMPACT_SPACE_MAX_DIMS     3

typedef hid_t (*generate_datatype_func)(H5T_class_t parent_class, bool is_compact);

static hid_t generate_random_datatype_integer(H5T_class_t parent_class, bool is_compact);
static hid_t generate_random_datatype_float(H5T_class_t parent_class, bool is_compact);
static hid_t generate_random_datatype_string(H5T_class_t parent_class, bool is_compact);
static hid_t generate_random_datatype_compound(H5T_class_t parent_class, bool is_compact);
static hid_t generate_random_datatype_reference(H5T_class_t parent_class, bool is_compact);
static hid_t generate_random_datatype_enum(H5T_class_t parent_class, bool is_compact);
static hid_t generate_random_datatype_array(H5T_class_t parent_class, bool is_compact);

/*
 * Helper function to generate a random HDF5 datatype in order to thoroughly
 * test support for datatypes. The parent_class parameter is to support
 * recursive generation of datatypes. In most cases, this function should be
 * called with H5T_NO_CLASS for the parent_class parameter.
 */
hid_t
generate_random_datatype(H5T_class_t parent_class, bool is_compact)
{
    generate_datatype_func gen_func;
    static int             depth = 0;
    size_t                 type_size;
    hid_t                  datatype  = H5I_INVALID_HID;
    hid_t                  ret_value = H5I_INVALID_HID;

    depth++;

roll_datatype:
    switch (rand() % H5T_NCLASSES) {
        case H5T_INTEGER:
            gen_func = generate_random_datatype_integer;
            break;
        case H5T_FLOAT:
            gen_func = generate_random_datatype_float;
            break;
        case H5T_TIME:
            /* Time datatype is unsupported, try again */
            goto roll_datatype;
            break;
        case H5T_STRING:
            gen_func = generate_random_datatype_string;
            break;
        case H5T_BITFIELD:
            /* Bitfield datatype is unsupported, try again */
            goto roll_datatype;
            break;
        case H5T_OPAQUE:
            /* Opaque datatype is unsupported, try again */
            goto roll_datatype;
            break;
        case H5T_COMPOUND:
            /* Currently only allows arrays of integer, float or string. Pick another type if we
             * are creating an array of something other than these. Also don't allow recursion
             * to go too deep. Pick another type that doesn't recursively call this function. */
            if ((H5T_ARRAY == parent_class) || ((depth + 1) > TYPE_GEN_RECURSION_MAX_DEPTH))
                goto roll_datatype;

            gen_func = generate_random_datatype_compound;
            break;
        case H5T_REFERENCE:
            /* Temporarily disable generation of reference datatypes */
            goto roll_datatype;

            /* Currently only allows arrays of integer, float or string. Pick another type if we
             * are creating an array of something other than these. */
            if (H5T_ARRAY == parent_class)
                goto roll_datatype;

            gen_func = generate_random_datatype_reference;
            break;
        case H5T_ENUM:
            /* Currently doesn't currently support ARRAY of ENUM, so try another type
             * if this happens. */
            if (H5T_ARRAY == parent_class)
                goto roll_datatype;

            gen_func = generate_random_datatype_enum;
            break;
        case H5T_VLEN:
            /* Variable-length datatypes are unsupported, try again */
            goto roll_datatype;
            break;
        case H5T_ARRAY:
            /* Currently doesn't currently support ARRAY of ARRAY, so try another type
             * if this happens. Also check for too much recursion. */
            if ((H5T_ARRAY == parent_class) || ((depth + 1) > TYPE_GEN_RECURSION_MAX_DEPTH))
                goto roll_datatype;

            gen_func = generate_random_datatype_array;
            break;
        default:
            printf("    invalid datatype class\n");
            goto done;
            break;
    }

    if ((datatype = (gen_func)(parent_class, is_compact)) < 0) {
        printf("    couldn't generate datatype\n");
        goto done;
    }

    /*
     * Check to make sure that the generated datatype does
     * not exceed the maximum datatype size and doesn't exceed
     * the maximum compact datatype size if a compact datatype
     * was requested.
     */
    if (depth == 1) {
        if (0 == (type_size = H5Tget_size(datatype))) {
            printf("    failed to retrieve datatype's size\n");
            H5Tclose(datatype);
            datatype = H5I_INVALID_HID;
            goto done;
        }

        if ((type_size > GENERATED_DATATYPE_MAX_SIZE) ||
            (is_compact && (type_size > COMPACT_DATATYPE_MAX_SIZE))) {
            /*
             * Generate a new datatype.
             */
            H5Tclose(datatype);
            datatype = H5I_INVALID_HID;
            goto roll_datatype;
        }
    }

    ret_value = datatype;

done:
    depth--;

    return ret_value;
}

static hid_t
generate_random_datatype_integer(H5T_class_t H5_ATTR_UNUSED parent_class, bool H5_ATTR_UNUSED is_compact)
{
    hid_t type_to_copy = H5I_INVALID_HID;
    hid_t datatype     = H5I_INVALID_HID;
    hid_t ret_value    = H5I_INVALID_HID;

    switch (rand() % NUM_PREDEFINED_INT_TYPES) {
        case 0:
            type_to_copy = H5T_STD_I8BE;
            break;
        case 1:
            type_to_copy = H5T_STD_I8LE;
            break;
        case 2:
            type_to_copy = H5T_STD_I16BE;
            break;
        case 3:
            type_to_copy = H5T_STD_I16LE;
            break;
        case 4:
            type_to_copy = H5T_STD_I32BE;
            break;
        case 5:
            type_to_copy = H5T_STD_I32LE;
            break;
        case 6:
            type_to_copy = H5T_STD_I64BE;
            break;
        case 7:
            type_to_copy = H5T_STD_I64LE;
            break;
        case 8:
            type_to_copy = H5T_STD_U8BE;
            break;
        case 9:
            type_to_copy = H5T_STD_U8LE;
            break;
        case 10:
            type_to_copy = H5T_STD_U16BE;
            break;
        case 11:
            type_to_copy = H5T_STD_U16LE;
            break;
        case 12:
            type_to_copy = H5T_STD_U32BE;
            break;
        case 13:
            type_to_copy = H5T_STD_U32LE;
            break;
        case 14:
            type_to_copy = H5T_STD_U64BE;
            break;
        case 15:
            type_to_copy = H5T_STD_U64LE;
            break;
        default:
            printf("    invalid value for predefined integer type; should not happen\n");
            goto done;
    }

    if ((datatype = H5Tcopy(type_to_copy)) < 0) {
        printf("    couldn't copy predefined integer type\n");
        goto done;
    }

    ret_value = datatype;

done:
    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

static hid_t
generate_random_datatype_float(H5T_class_t H5_ATTR_UNUSED parent_class, bool H5_ATTR_UNUSED is_compact)
{
    hid_t type_to_copy = H5I_INVALID_HID;
    hid_t datatype     = H5I_INVALID_HID;
    hid_t ret_value    = H5I_INVALID_HID;

    switch (rand() % NUM_PREDEFINED_FLOAT_TYPES) {
        case 0:
            type_to_copy = H5T_IEEE_F16BE;
            break;
        case 1:
            type_to_copy = H5T_IEEE_F16LE;
            break;
        case 2:
            type_to_copy = H5T_IEEE_F32BE;
            break;
        case 3:
            type_to_copy = H5T_IEEE_F32LE;
            break;
        case 4:
            type_to_copy = H5T_IEEE_F64BE;
            break;
        case 5:
            type_to_copy = H5T_IEEE_F64LE;
            break;

        default:
            printf("    invalid value for floating point type; should not happen\n");
            goto done;
    }

    if ((datatype = H5Tcopy(type_to_copy)) < 0) {
        printf("    couldn't copy predefined floating-point type\n");
        goto done;
    }

    ret_value = datatype;

done:
    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

static hid_t
generate_random_datatype_string(H5T_class_t H5_ATTR_UNUSED parent_class, bool H5_ATTR_UNUSED is_compact)
{
    hid_t datatype  = H5I_INVALID_HID;
    hid_t ret_value = H5I_INVALID_HID;

    /* Note: currently only H5T_CSET_ASCII is supported for the character set and
     * only H5T_STR_NULLTERM is supported for string padding for variable-length
     * strings and only H5T_STR_NULLPAD is supported for string padding for
     * fixed-length strings, but these may change in the future.
     */
#if 0 /* Currently, all VL types are disabled */
    if (0 == (rand() % 2)) {
#endif
    if ((datatype = H5Tcreate(H5T_STRING, (size_t)(rand() % STRING_TYPE_MAX_SIZE) + 1)) < 0) {
        printf("    couldn't create fixed-length string datatype\n");
        goto done;
    }

    if (H5Tset_strpad(datatype, H5T_STR_NULLPAD) < 0) {
        printf("    couldn't set H5T_STR_NULLPAD for fixed-length string type\n");
        goto done;
    }
#if 0 /* Currently, all VL types are disabled */
    }
    else {

            if ((datatype = H5Tcreate(H5T_STRING, H5T_VARIABLE)) < 0) {
                H5_FAILED();
                printf("    couldn't create variable-length string datatype\n");
                goto done;
            }

            if (H5Tset_strpad(datatype, H5T_STR_NULLTERM) < 0) {
                H5_FAILED();
                printf("    couldn't set H5T_STR_NULLTERM for variable-length string type\n");
                goto done;
            }
    }
#endif

    if (H5Tset_cset(datatype, H5T_CSET_ASCII) < 0) {
        printf("    couldn't set string datatype character set\n");
        goto done;
    }

    ret_value = datatype;

done:
    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

static hid_t
generate_random_datatype_compound(H5T_class_t H5_ATTR_UNUSED parent_class, bool is_compact)
{
    size_t num_members   = 0;
    size_t next_offset   = 0;
    size_t compound_size = 0;
    hid_t  compound_members[COMPOUND_TYPE_MAX_MEMBERS];
    hid_t  datatype  = H5I_INVALID_HID;
    hid_t  ret_value = H5I_INVALID_HID;

    for (size_t i = 0; i < COMPOUND_TYPE_MAX_MEMBERS; i++)
        compound_members[i] = H5I_INVALID_HID;

    if ((datatype = H5Tcreate(H5T_COMPOUND, 1)) < 0) {
        printf("    couldn't create compound datatype\n");
        goto done;
    }

    num_members = (size_t)(rand() % COMPOUND_TYPE_MAX_MEMBERS + 1);

    for (size_t i = 0; i < num_members; i++) {
        size_t member_size;
        char   member_name[256];

        snprintf(member_name, 256, "compound_member%zu", i);

        if ((compound_members[i] = generate_random_datatype(H5T_COMPOUND, is_compact)) < 0) {
            printf("    couldn't create compound datatype member %zu\n", i);
            goto done;
        }

        if (!(member_size = H5Tget_size(compound_members[i]))) {
            printf("    couldn't get compound member %zu size\n", i);
            goto done;
        }

        compound_size += member_size;

        if (H5Tset_size(datatype, compound_size) < 0) {
            printf("    couldn't set size for compound datatype\n");
            goto done;
        }

        if (H5Tinsert(datatype, member_name, next_offset, compound_members[i]) < 0) {
            printf("    couldn't insert compound datatype member %zu\n", i);
            goto done;
        }

        next_offset += member_size;
    }

    ret_value = datatype;

done:
    for (size_t i = 0; i < COMPOUND_TYPE_MAX_MEMBERS; i++) {
        if (compound_members[i] > 0 && H5Tclose(compound_members[i]) < 0) {
            printf("    couldn't close compound member %zu\n", i);
        }
    }

    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

static hid_t
generate_random_datatype_reference(H5T_class_t H5_ATTR_UNUSED parent_class, bool H5_ATTR_UNUSED is_compact)
{
    hid_t datatype  = H5I_INVALID_HID;
    hid_t ret_value = H5I_INVALID_HID;

#if 0 /* Region references are currently unsupported */
    if (0 == (rand() % 2)) {
#endif
    if ((datatype = H5Tcopy(H5T_STD_REF_OBJ)) < 0) {
        printf("    couldn't copy object reference datatype\n");
        goto done;
    }
#if 0 /* Region references are currently unsupported */
    }
    else {
        if ((datatype = H5Tcopy(H5T_STD_REF_DSETREG)) < 0) {
            H5_FAILED();
            printf("    couldn't copy region reference datatype\n");
            goto done;
        }
    }
#endif

    ret_value = datatype;

done:
    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

static hid_t
generate_random_datatype_enum(H5T_class_t H5_ATTR_UNUSED parent_class, bool H5_ATTR_UNUSED is_compact)
{
    size_t num_members      = 0;
    hid_t  datatype         = H5I_INVALID_HID;
    int   *enum_member_vals = NULL;
    hid_t  ret_value        = H5I_INVALID_HID;

    if ((datatype = H5Tenum_create(H5T_NATIVE_INT)) < 0) {
        printf("    couldn't create enum datatype\n");
        goto done;
    }

    num_members = (size_t)(rand() % ENUM_TYPE_MAX_MEMBERS + 1);

    if (NULL == (enum_member_vals = malloc(num_members * sizeof(int)))) {
        printf("    couldn't allocate space for enum members\n");
        goto done;
    }

    for (size_t i = 0; i < num_members; i++) {
        bool unique;
        char name[ENUM_TYPE_MAX_MEMBER_NAME_LENGTH];
        int  enum_val;

        snprintf(name, ENUM_TYPE_MAX_MEMBER_NAME_LENGTH, "enum_val%zu", i);

        do {
            enum_val = rand();

            /* Check for uniqueness of enum member */
            unique = true;
            for (size_t j = 0; j < i; j++)
                if (enum_val == enum_member_vals[j])
                    unique = false;
        } while (!unique);

        enum_member_vals[i] = enum_val;

        if (H5Tenum_insert(datatype, name, &enum_val) < 0) {
            printf("    couldn't insert member into enum datatype\n");
            goto done;
        }
    }

    ret_value = datatype;

done:
    free(enum_member_vals);

    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

static hid_t
generate_random_datatype_array(H5T_class_t H5_ATTR_UNUSED parent_class, bool is_compact)
{
    unsigned ndims         = 0;
    hsize_t *array_dims    = NULL;
    hid_t    base_datatype = H5I_INVALID_HID;
    hid_t    datatype      = H5I_INVALID_HID;
    hid_t    ret_value     = H5I_INVALID_HID;

    ndims = (unsigned)(rand() % ARRAY_TYPE_MAX_DIMS + 1);

    if (NULL == (array_dims = malloc(ndims * sizeof(*array_dims)))) {
        printf("    couldn't allocate space for array datatype dims\n");
        goto done;
    }

    for (size_t i = 0; i < ndims; i++)
        array_dims[i] = (hsize_t)(rand() % MAX_DIM_SIZE + 1);

    if ((base_datatype = generate_random_datatype(H5T_ARRAY, is_compact)) < 0) {
        printf("    couldn't create array base datatype\n");
        goto done;
    }

    if ((datatype = H5Tarray_create2(base_datatype, ndims, array_dims)) < 0) {
        printf("    couldn't create array datatype\n");
        goto done;
    }

    ret_value = datatype;

done:
    free(array_dims);

    if (base_datatype >= 0) {
        if (H5Tclose(base_datatype) < 0)
            printf("    couldn't close array base datatype\n");
    }

    if ((ret_value == H5I_INVALID_HID) && (datatype >= 0)) {
        if (H5Tclose(datatype) < 0)
            printf("    couldn't close datatype\n");
    }

    return ret_value;
}

/*
 * Helper function to generate a random HDF5 dataspace in order to thoroughly
 * test support for dataspaces.
 */
hid_t
generate_random_dataspace(int rank, const hsize_t *max_dims, hsize_t *dims_out, bool is_compact)
{
    hsize_t dataspace_dims[H5S_MAX_RANK];
    size_t  i;
    hid_t   dataspace_id = H5I_INVALID_HID;

    if (rank < 0)
        TEST_ERROR;
    if (is_compact && (rank > COMPACT_SPACE_MAX_DIMS)) {
        printf("    current rank of compact dataspace (%lld) exceeds maximum dimensionality (%lld)\n",
               (long long)rank, (long long)COMPACT_SPACE_MAX_DIMS);
        TEST_ERROR;
    }

    /*
     * XXX: if max_dims is specified, make sure that the dimensions generated
     * are not larger than this.
     */
    for (i = 0; i < (size_t)rank; i++) {
        if (is_compact)
            dataspace_dims[i] = (hsize_t)(rand() % COMPACT_SPACE_MAX_DIM_SIZE + 1);
        else
            dataspace_dims[i] = (hsize_t)(rand() % MAX_DIM_SIZE + 1);

        if (dims_out)
            dims_out[i] = dataspace_dims[i];
    }

    if ((dataspace_id = H5Screate_simple(rank, dataspace_dims, max_dims)) < 0)
        TEST_ERROR;

    return dataspace_id;

error:
    return H5I_INVALID_HID;
}

int
create_test_container(char *filename, uint64_t vol_cap_flags)
{
    hid_t file_id  = H5I_INVALID_HID;
    hid_t group_id = H5I_INVALID_HID;

    if (!(vol_cap_flags & H5VL_CAP_FLAG_FILE_BASIC)) {
        printf("   VOL connector doesn't support file creation\n");
        goto error;
    }

    if ((file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
        printf("    couldn't create testing container file '%s'\n", filename);
        goto error;
    }

    if (vol_cap_flags & H5VL_CAP_FLAG_GROUP_BASIC) {
        /* Create container groups for each of the test interfaces
         * (group, attribute, dataset, etc.).
         */
        if ((group_id = H5Gcreate2(file_id, GROUP_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >=
            0) {
            printf("    created container group for Group tests\n");
            H5Gclose(group_id);
        }

        if ((group_id = H5Gcreate2(file_id, ATTRIBUTE_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT,
                                   H5P_DEFAULT)) >= 0) {
            printf("    created container group for Attribute tests\n");
            H5Gclose(group_id);
        }

        if ((group_id =
                 H5Gcreate2(file_id, DATASET_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >= 0) {
            printf("    created container group for Dataset tests\n");
            H5Gclose(group_id);
        }

        if ((group_id =
                 H5Gcreate2(file_id, DATATYPE_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >= 0) {
            printf("    created container group for Datatype tests\n");
            H5Gclose(group_id);
        }

        if ((group_id = H5Gcreate2(file_id, LINK_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >=
            0) {
            printf("    created container group for Link tests\n");
            H5Gclose(group_id);
        }

        if ((group_id = H5Gcreate2(file_id, OBJECT_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >=
            0) {
            printf("    created container group for Object tests\n");
            H5Gclose(group_id);
        }

        if ((group_id = H5Gcreate2(file_id, MISCELLANEOUS_TEST_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT,
                                   H5P_DEFAULT)) >= 0) {
            printf("    created container group for Miscellaneous tests\n");
            H5Gclose(group_id);
        }
    }

    if (H5Fclose(file_id) < 0) {
        printf("    failed to close testing container\n");
        goto error;
    }

    return 0;

error:
    H5E_BEGIN_TRY
    {
        H5Gclose(group_id);
        H5Fclose(file_id);
    }
    H5E_END_TRY

    return -1;
}

/*
 * Add a prefix to the given filename. The caller
 * is responsible for freeing the returned filename
 * pointer with free().
 */
herr_t
prefix_filename(const char *prefix, const char *filename, char **filename_out)
{
    char  *out_buf   = NULL;
    herr_t ret_value = SUCCEED;

    if (!prefix) {
        printf("    invalid file prefix\n");
        ret_value = FAIL;
        goto done;
    }
    if (!filename || (*filename == '\0')) {
        printf("    invalid filename\n");
        ret_value = FAIL;
        goto done;
    }
    if (!filename_out) {
        printf("    invalid filename_out buffer\n");
        ret_value = FAIL;
        goto done;
    }

    if (NULL == (out_buf = malloc(H5_API_TEST_FILENAME_MAX_LENGTH))) {
        printf("    couldn't allocated filename buffer\n");
        ret_value = FAIL;
        goto done;
    }

    snprintf(out_buf, H5_API_TEST_FILENAME_MAX_LENGTH, "%s%s", prefix, filename);

    *filename_out = out_buf;

done:
    return ret_value;
}

/*
 * Calls H5Fdelete on the given filename. If a prefix string
 * is given, adds that prefix string to the filename before
 * calling H5Fdelete
 */
herr_t
remove_test_file(const char *prefix, const char *filename)
{
    const char *test_file;
    char       *prefixed_filename = NULL;
    herr_t      ret_value         = SUCCEED;

    if (prefix) {
        if (prefix_filename(prefix, filename, &prefixed_filename) < 0) {
            printf("    couldn't prefix filename\n");
            ret_value = FAIL;
            goto done;
        }

        test_file = prefixed_filename;
    }
    else
        test_file = filename;

    if (H5Fdelete(test_file, H5P_DEFAULT) < 0) {
        printf("    couldn't remove file '%s'\n", test_file);
        ret_value = FAIL;
        goto done;
    }

done:
    free(prefixed_filename);

    return ret_value;
}