summaryrefslogtreecommitdiffstats
path: root/test/earray.c
blob: 0663252e18017145f5e91ea5d9b189faac2915a8 (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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Programmer:  Quincey Koziol <koziol@hdfgroup.org>
 *              Tuesday, June 17, 2008
 */
#include "h5test.h"

/*
 * This file needs to access private datatypes from the H5EA package.
 * This file also needs to access the extensible array testing code.
 */
#define H5EA_PACKAGE
#define H5EA_TESTING
#include "H5EApkg.h"		/* Extensible Arrays			*/

/* Other private headers that this test requires */
#include "H5Iprivate.h"		/* IDs			  		*/


/* Local macros */

/* Max. testfile name length */
#define EARRAY_FILENAME_LEN     1024

/* Extensible array creation values */
#define ELMT_SIZE               sizeof(uint64_t)
#define MAX_NELMTS_BITS         32
#define IDX_BLK_ELMTS           4
#define SUP_BLK_MIN_DATA_PTRS   4
#define DATA_BLK_MIN_ELMTS      16

/* Local typedefs */

/* Types of tests to perform */
typedef enum {
    EARRAY_TEST_NORMAL,         /* "Normal" test, with no testing parameters set */
    EARRAY_TEST_REOPEN,         /* Set the reopen_array flag */
    EARRAY_TEST_NTESTS          /* The number of test types, must be last */
} earray_test_type_t;

/* Orders to operate on entries */
typedef enum {
    EARRAY_DIR_FORWARD,          /* Insert objects from 0 -> nobjs */
    EARRAY_DIR_RANDOM,           /* Insert objects randomly from 0 - nobjs */
    EARRAY_DIR_CYCLIC,           /* Insert every n'th object cyclicly: 0, n, 2n, 3n, ..., nobjs/n, 1+nobjs/n, 1+n+nobjs/n, 1+2n+nobjs/n, ..., nobjs */
    EARRAY_DIR_REVERSE,          /* Insert objects from nobjs -> 0 */
    EARRAY_DIR_INWARD,           /* Insert objects from outside to in: 0, nobjs, 1, nobjs-1, 2, nobjs-2, ..., nobjs/2 */
    EARRAY_DIR_OUTWARD,          /* Insert objects from inside to out: nobjs/2, (nobjs/2)-1, (nobjs/2)+1, ..., 0, nobjs */
    EARRAY_DIR_NDIRS             /* The number of different insertion orders, must be last */
} earray_test_dir_t;

/* Whether to compress data blocks */
typedef enum {
    EARRAY_TEST_NO_COMPRESS,    /* Don't compress data blocks */
    EARRAY_TEST_COMPRESS,       /* Compress data blocks */
    EARRAY_TEST_COMP_N          /* The number of different ways to test compressing array blocks, must be last */
} earray_test_comp_t;

/* Testing parameters */
typedef struct earray_test_param_t {
    earray_test_type_t reopen_array;    /* Whether to re-open the array during the test */
    earray_test_comp_t comp;            /* Whether to compress the blocks or not */
} earray_test_param_t;

/* Extensible array state information */
typedef struct earray_state_t {
    hsize_t     nsuper_blks;            /* # of super blocks */
    hsize_t     ndata_blks;             /* # of data blocks */
} earray_state_t;

/* Local variables */
const char *FILENAME[] = {
    "earray",
    NULL
};

/* Filename to use for all tests */
char filename_g[EARRAY_FILENAME_LEN];

/* Empty file size */
h5_stat_size_t empty_size_g;


/* Local prototypes */


/*-------------------------------------------------------------------------
 * Function:	init_cparam
 *
 * Purpose:	Initialize array creation parameter structure 
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 21, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
init_cparam(H5EA_create_t *cparam)
{
    /* Wipe out background */
    HDmemset(cparam, 0, sizeof(*cparam));

    /* General parameters */
    cparam->cls = H5EA_CLS_TEST;
    cparam->raw_elmt_size = ELMT_SIZE;
    cparam->max_nelmts_bits = MAX_NELMTS_BITS;
    cparam->idx_blk_elmts = IDX_BLK_ELMTS;
    cparam->sup_blk_min_data_ptrs = SUP_BLK_MIN_DATA_PTRS;
    cparam->data_blk_min_elmts = DATA_BLK_MIN_ELMTS;

    return(0);
} /* init_cparam() */


/*-------------------------------------------------------------------------
 * Function:	create_file
 *
 * Purpose:	Create file and retrieve pointer to internal file object
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
create_file(hid_t fapl, hid_t *file, H5F_t **f)
{
    /* Create the file to work on */
    if((*file = H5Fcreate(filename_g, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        FAIL_STACK_ERROR

    /* Get a pointer to the internal file object */
    if(NULL == (*f = (H5F_t *)H5I_object(*file)))
        FAIL_STACK_ERROR

    /* Success */
    return(0);

error:
    return(-1);
} /* create_file() */


/*-------------------------------------------------------------------------
 * Function:	check_stats
 *
 * Purpose:	Verify stats for an extensible array
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 21, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
check_stats(const H5EA_t *ea, const earray_state_t *state)
{
    H5EA_stat_t earray_stats;           /* Statistics about the array */

    /* Get statistics for extensible array and verify they are correct */
    if(H5EA_get_stats(ea, &earray_stats) < 0)
        FAIL_STACK_ERROR

    /* Compare information */
    if(earray_stats.nsuper_blks != state->nsuper_blks) {
        HDfprintf(stdout, "earray_stats.nsuper_blks = %Hu, state->nsuper_blks = %Hu\n", earray_stats.nsuper_blks, state->nsuper_blks);
        TEST_ERROR
    } /* end if */
    if(earray_stats.ndata_blks != state->ndata_blks) {
        HDfprintf(stdout, "earray_stats.ndata_blks = %Hu, state->ndata_blks = %Hu\n", earray_stats.ndata_blks, state->ndata_blks);
        TEST_ERROR
    } /* end if */

    /* All tests passed */
    return(0);

error:
    return(-1);
} /* check_stats() */


/*-------------------------------------------------------------------------
 * Function:	reopen_file
 *
 * Purpose:	Perform common "re-open" operations on file & array for testing
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
reopen_file(hid_t *file, H5F_t **f, hid_t fapl, hid_t dxpl,
    H5EA_t **ea, haddr_t ea_addr, const H5EA_class_t *ea_cls,
    const earray_test_param_t *tparam)
{
    /* Check for closing & re-opening the array */
    /* (actually will close & re-open the file as well) */
    if(tparam->reopen_array) {
        /* Close array, if given */
        if(ea) {
            if(H5EA_close(*ea, dxpl) < 0)
                FAIL_STACK_ERROR
            *ea = NULL;
        } /* end if */

        /* Close file */
        if(H5Fclose(*file) < 0)
            FAIL_STACK_ERROR
        *file = (-1);
        *f = NULL;

        /* Re-open the file */
        if((*file = H5Fopen(filename_g, H5F_ACC_RDWR, fapl)) < 0)
            FAIL_STACK_ERROR

        /* Get a pointer to the internal file object */
        if(NULL == (*f = (H5F_t *)H5I_object(*file)))
            FAIL_STACK_ERROR

        /* Re-open array, if given */
        if(ea) {
            if(NULL == (*ea = H5EA_open(*f, dxpl, ea_addr, ea_cls)))
                FAIL_STACK_ERROR
        } /* end if */
    } /* end if */

    /* Success */
    return(0);

error:
    return(-1);
} /* reopen_file() */


/*-------------------------------------------------------------------------
 * Function:	create_array
 *
 * Purpose:	Create an extensible array and perform initial checks
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
create_array(H5F_t *f, hid_t dxpl, const H5EA_create_t *cparam, 
    H5EA_t **ea, haddr_t *ea_addr)
{
    hsize_t     nelmts;                 /* Number of elements in array */
    earray_state_t state;               /* State of extensible array */

    /* Create array */
    if(NULL == (*ea = H5EA_create(f, dxpl, cparam)))
        FAIL_STACK_ERROR

    /* Check status of array */
    nelmts = (hsize_t)ULLONG_MAX;
    if(H5EA_get_nelmts(*ea, &nelmts) < 0)
        FAIL_STACK_ERROR
    if(nelmts > 0)
        TEST_ERROR
    if(H5EA_get_addr(*ea, ea_addr) < 0)
        FAIL_STACK_ERROR
    if(!H5F_addr_defined(*ea_addr))
        TEST_ERROR
    HDmemset(&state, 0, sizeof(state));
    if(check_stats(*ea, &state))
        TEST_ERROR

    /* Success */
    return(0);

error:
    return(-1);
} /* create_array() */


/*-------------------------------------------------------------------------
 * Function:	verify_cparam
 *
 * Purpose:	Verify creation parameters are correct
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
verify_cparam(const H5EA_t *ea, const H5EA_create_t *cparam)
{
    H5EA_create_t test_cparam;          /* Creation parameters for array */

    /* Retrieve creation parameters */
    HDmemset(&test_cparam, 0, sizeof(H5EA_create_t));
    if(H5EA_get_cparam_test(ea, &test_cparam) < 0)
        FAIL_STACK_ERROR

    /* Verify creation parameters */
    if(H5EA_cmp_cparam_test(cparam, &test_cparam))
        TEST_ERROR

    /* Success */
    return(0);

error:
    return(-1);
} /* verify_cparam() */


/*-------------------------------------------------------------------------
 * Function:	shutdown
 *
 * Purpose:	Close array, delete array, close file and verify that file
 *              is empty size
 *
 * Return:	Success:	0
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static int
shutdown(hid_t file, H5F_t *f, H5EA_t *ea, haddr_t ea_addr)
{
    h5_stat_size_t file_size;           /* File size, after deleting array */

    /* Close the extensible array */
    if(H5EA_close(ea, H5P_DATASET_XFER_DEFAULT) < 0)
        FAIL_STACK_ERROR

#ifdef QAK
HDfprintf(stderr, "ea_addr = %a\n", ea_addr);
H5Fflush(file, H5F_SCOPE_GLOBAL);
HDsystem("cp earray.h5 earray.h5.save");
#endif /* QAK */

    /* Delete array */
    if(H5EA_delete(f, H5P_DATASET_XFER_DEFAULT, ea_addr) < 0)
        FAIL_STACK_ERROR

    /* Close the file */
    if(H5Fclose(file) < 0)
        FAIL_STACK_ERROR


    /* Get the size of the file */
    if((file_size = h5_get_file_size(filename_g)) < 0)
        TEST_ERROR

    /* Verify the file is correct size */
    if(file_size != empty_size_g)
        TEST_ERROR

    /* Success */
    return(0);

error:
    return(-1);
} /* shutdown() */


/*-------------------------------------------------------------------------
 * Function:	test_create
 *
 * Purpose:	Test creating extensible array
 *
 * Return:	Success: 0
 *		Failure: 1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August  7, 2008
 *
 *-------------------------------------------------------------------------
 */
static unsigned
test_create(hid_t fapl, H5EA_create_t *cparam, earray_test_param_t UNUSED *tparam)
{
    hid_t	file = -1;              /* File ID */
    H5F_t	*f = NULL;              /* Internal file object pointer */
    H5EA_t      *ea = NULL;             /* Extensible array wrapper */
    haddr_t     ea_addr = HADDR_UNDEF;  /* Array address in file */

    /* Create file & retrieve pointer to internal file object */
    if(create_file(fapl, &file, &f) < 0)
        TEST_ERROR

    /*
     * Display testing message
     */
    TESTING("invalid extensible array creation parameters");

#ifndef NDEBUG
{
    H5EA_create_t test_cparam;          /* Creation parameters for array */

    /* Set invalid element size */
    HDmemcpy(&test_cparam, cparam, sizeof(test_cparam));
    test_cparam.raw_elmt_size = 0;
    H5E_BEGIN_TRY {
        ea = H5EA_create(f, H5P_DATASET_XFER_DEFAULT, &test_cparam);
    } H5E_END_TRY;
    if(ea) {
        /* Close opened extensible array */
        H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    /* Set invalid max. # of elements bits */
    HDmemcpy(&test_cparam, cparam, sizeof(test_cparam));
    test_cparam.max_nelmts_bits = 0;
    H5E_BEGIN_TRY {
        ea = H5EA_create(f, H5P_DATASET_XFER_DEFAULT, &test_cparam);
    } H5E_END_TRY;
    if(ea) {
        /* Close opened extensible array */
        H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    HDmemcpy(&test_cparam, cparam, sizeof(test_cparam));
    test_cparam.max_nelmts_bits = 65;
    H5E_BEGIN_TRY {
        ea = H5EA_create(f, H5P_DATASET_XFER_DEFAULT, &test_cparam);
    } H5E_END_TRY;
    if(ea) {
        /* Close opened extensible array */
        H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    /* Set invalid min. # of data block pointers in super blocks */
    HDmemcpy(&test_cparam, cparam, sizeof(test_cparam));
    test_cparam.sup_blk_min_data_ptrs = 0;
    H5E_BEGIN_TRY {
        ea = H5EA_create(f, H5P_DATASET_XFER_DEFAULT, &test_cparam);
    } H5E_END_TRY;
    if(ea) {
        /* Close opened extensible array */
        H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    /* Set invalid min. # of elements per data block */
    HDmemcpy(&test_cparam, cparam, sizeof(test_cparam));
    test_cparam.data_blk_min_elmts = 0;
    H5E_BEGIN_TRY {
        ea = H5EA_create(f, H5P_DATASET_XFER_DEFAULT, &test_cparam);
    } H5E_END_TRY;
    if(ea) {
        /* Close opened extensible array */
        H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    PASSED()
}
#else /* NDEBUG */
    SKIPPED();
    puts("    Not tested when assertions are disabled");
#endif /* NDEBUG */

    /*
     * Display testing message
     */
    TESTING("extensible array creation");

    /* Create array */
    if(create_array(f, H5P_DATASET_XFER_DEFAULT, cparam, &ea, &ea_addr) < 0)
        TEST_ERROR

    PASSED()

    /* Verify the creation parameters */
    TESTING("verify array creation parameters");

    /* Verify the creation parameters */
    if(verify_cparam(ea, cparam) < 0)
        TEST_ERROR

    /* Close array, delete array, close file & verify file is empty */
    if(shutdown(file, f, ea, ea_addr) < 0)
        TEST_ERROR

    /* All tests passed */
    PASSED()

    return 0;

error:
    H5E_BEGIN_TRY {
        if(ea)
            H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);
	H5Fclose(file);
    } H5E_END_TRY;

    return 1;
} /* end test_create() */


/*-------------------------------------------------------------------------
 * Function:	test_reopen
 *
 * Purpose:	Create & reopen an extensible array
 *
 * Return:	Success:	0
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static unsigned
test_reopen(hid_t fapl, H5EA_create_t *cparam, earray_test_param_t *tparam)
{
    hid_t	file = -1;              /* File ID */
    H5F_t	*f = NULL;              /* Internal file object pointer */
    H5EA_t      *ea = NULL;             /* Extensible array wrapper */
    haddr_t     ea_addr = HADDR_UNDEF;  /* Array address in file */

    /* Create file & retrieve pointer to internal file object */
    if(create_file(fapl, &file, &f) < 0)
        TEST_ERROR

    /*
     * Display testing message
     */
    TESTING("create, close & reopen extensible array");

    /* Create array */
    if(create_array(f, H5P_DATASET_XFER_DEFAULT, cparam, &ea, &ea_addr) < 0)
        TEST_ERROR

    /* Close the extensible array */
    if(H5EA_close(ea, H5P_DATASET_XFER_DEFAULT) < 0)
        FAIL_STACK_ERROR

    /* Check for closing & re-opening the file */
    if(reopen_file(&file, &f, fapl, H5P_DATASET_XFER_DEFAULT, NULL, HADDR_UNDEF, NULL, tparam) < 0)
        TEST_ERROR

    /* Re-open the array */
    if(NULL == (ea = H5EA_open(f, H5P_DATASET_XFER_DEFAULT, ea_addr, cparam->cls)))
        FAIL_STACK_ERROR

    /* Verify the creation parameters */
    if(verify_cparam(ea, cparam) < 0)
        TEST_ERROR

    /* Close array, delete array, close file & verify file is empty */
    if(shutdown(file, f, ea, ea_addr) < 0)
        TEST_ERROR

    /* All tests passed */
    PASSED()

    return 0;

error:
    H5E_BEGIN_TRY {
        if(ea)
            H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);
	H5Fclose(file);
    } H5E_END_TRY;

    return 1;
} /* test_reopen() */


/*-------------------------------------------------------------------------
 * Function:	test_open_twice
 *
 * Purpose:	Open an extensible array twice
 *
 * Return:	Success:	0
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static unsigned
test_open_twice(hid_t fapl, H5EA_create_t *cparam, earray_test_param_t *tparam)
{
    hid_t	file = -1;              /* File ID */
    hid_t	file2 = -1;             /* File ID */
    H5F_t	*f = NULL;              /* Internal file object pointer */
    H5F_t	*f2 = NULL;             /* Internal file object pointer */
    H5EA_t      *ea = NULL;             /* Extensible array wrapper */
    H5EA_t      *ea2 = NULL;            /* Extensible array wrapper */
    haddr_t     ea_addr = HADDR_UNDEF;  /* Array address in file */

    /* Create file & retrieve pointer to internal file object */
    if(create_file(fapl, &file, &f) < 0)
        TEST_ERROR

    /*
     * Display testing message
     */
    TESTING("open extensible array twice");

    /* Create array */
    if(create_array(f, H5P_DATASET_XFER_DEFAULT, cparam, &ea, &ea_addr) < 0)
        TEST_ERROR

    /* Open the array again, through the first file handle */
    if(NULL == (ea2 = H5EA_open(f, H5P_DATASET_XFER_DEFAULT, ea_addr, cparam->cls)))
        FAIL_STACK_ERROR

    /* Verify the creation parameters */
    if(verify_cparam(ea, cparam) < 0)
        TEST_ERROR
    if(verify_cparam(ea2, cparam) < 0)
        TEST_ERROR

    /* Close the second extensible array wrapper */
    if(H5EA_close(ea2, H5P_DATASET_XFER_DEFAULT) < 0)
        FAIL_STACK_ERROR
    ea2 = NULL;

    /* Check for closing & re-opening the file */
    if(reopen_file(&file, &f, fapl, H5P_DATASET_XFER_DEFAULT, &ea, ea_addr, cparam->cls, tparam) < 0)
        TEST_ERROR

    /* Re-open the file */
    if((file2 = H5Freopen(file)) < 0)
        FAIL_STACK_ERROR

    /* Get a pointer to the internal file object */
    if(NULL == (f2 = (H5F_t *)H5I_object(file2)))
        FAIL_STACK_ERROR

    /* Open the extensible array through the second file handle */
    if(NULL == (ea2 = H5EA_open(f2, H5P_DATASET_XFER_DEFAULT, ea_addr, cparam->cls)))
        FAIL_STACK_ERROR

    /* Verify the creation parameters */
    if(verify_cparam(ea, cparam) < 0)
        TEST_ERROR

    /* Close the first extensible array wrapper */
    if(H5EA_close(ea, H5P_DATASET_XFER_DEFAULT) < 0)
        FAIL_STACK_ERROR
    ea = NULL;

    /* Close the first file */
    /* (close before second file, to detect error on internal array header's
     *  shared file information)
     */
    if(H5Fclose(file) < 0)
        FAIL_STACK_ERROR

    /* Close array, delete array, close file & verify file is empty */
    if(shutdown(file2, f2, ea2, ea_addr) < 0)
        TEST_ERROR

    /* All tests passed */
    PASSED()

    return 0;

error:
    H5E_BEGIN_TRY {
        if(ea)
            H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);
        if(ea2)
            H5EA_close(ea2, H5P_DATASET_XFER_DEFAULT);
	H5Fclose(file);
	H5Fclose(file2);
    } H5E_END_TRY;

    return 1;
} /* test_open_twice() */


/*-------------------------------------------------------------------------
 * Function:	test_delete_open
 *
 * Purpose:	Delete opened extensible array (& open deleted array)
 *
 * Return:	Success:	0
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static unsigned
test_delete_open(hid_t fapl, H5EA_create_t *cparam, earray_test_param_t *tparam)
{
    hid_t	file = -1;              /* File ID */
    H5F_t	*f = NULL;              /* Internal file object pointer */
    H5EA_t      *ea = NULL;             /* Extensible array wrapper */
    H5EA_t      *ea2 = NULL;            /* Extensible array wrapper */
    haddr_t     ea_addr = HADDR_UNDEF;  /* Array address in file */
    h5_stat_size_t file_size;           /* File size, after deleting array */

    /* Create file & retrieve pointer to internal file object */
    if(create_file(fapl, &file, &f) < 0)
        TEST_ERROR

    /*
     * Display testing message
     */
    TESTING("deleting open extensible array");

    /* Create array */
    if(create_array(f, H5P_DATASET_XFER_DEFAULT, cparam, &ea, &ea_addr) < 0)
        TEST_ERROR

    /* Open the array again */
    if(NULL == (ea2 = H5EA_open(f, H5P_DATASET_XFER_DEFAULT, ea_addr, cparam->cls)))
        FAIL_STACK_ERROR

    /* Request that the array be deleted */
    if(H5EA_delete(f, H5P_DATASET_XFER_DEFAULT, ea_addr) < 0)
        FAIL_STACK_ERROR

    /* Verify the creation parameters */
    if(verify_cparam(ea, cparam) < 0)
        TEST_ERROR
    if(verify_cparam(ea2, cparam) < 0)
        TEST_ERROR

    /* Close the second extensible array wrapper */
    if(H5EA_close(ea2, H5P_DATASET_XFER_DEFAULT) < 0)
        FAIL_STACK_ERROR
    ea2 = NULL;

    /* Try re-opening the array again (should fail, as array will be deleted) */
    H5E_BEGIN_TRY {
        ea2 = H5EA_open(f, H5P_DATASET_XFER_DEFAULT, ea_addr, cparam->cls);
    } H5E_END_TRY;
    if(ea2) {
        /* Close opened array */
        H5EA_close(ea2, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    /* Close the first extensible array wrapper */
    if(H5EA_close(ea, H5P_DATASET_XFER_DEFAULT) < 0)
        FAIL_STACK_ERROR
    ea = NULL;

    /* Check for closing & re-opening the file */
    if(reopen_file(&file, &f, fapl, H5P_DATASET_XFER_DEFAULT, NULL, HADDR_UNDEF, NULL, tparam) < 0)
        TEST_ERROR

    /* Try re-opening the array again (should fail, as array is now deleted) */
    H5E_BEGIN_TRY {
        ea = H5EA_open(f, H5P_DATASET_XFER_DEFAULT, ea_addr, cparam->cls);
    } H5E_END_TRY;
    if(ea) {
        /* Close opened array */
        H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);

        /* Indicate error */
        TEST_ERROR
    } /* end if */

    /* Close the file */
    if(H5Fclose(file) < 0)
        FAIL_STACK_ERROR

    /* Get the size of the file */
    if((file_size = h5_get_file_size(filename_g)) < 0)
        TEST_ERROR

    /* Verify the file is correct size */
    if(file_size != empty_size_g)
        TEST_ERROR

    /* All tests passed */
    PASSED()

    return 0;

error:
    H5E_BEGIN_TRY {
        if(ea)
            H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);
        if(ea2)
            H5EA_close(ea2, H5P_DATASET_XFER_DEFAULT);
	H5Fclose(file);
    } H5E_END_TRY;

    return 1;
} /* test_delete_open() */


/*-------------------------------------------------------------------------
 * Function:	test_set_first
 *
 * Purpose:	Set first element in extensible array
 *
 * Return:	Success:	0
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, August 28, 2008
 *
 *-------------------------------------------------------------------------
 */
static unsigned
test_set_first(hid_t fapl, H5EA_create_t *cparam, earray_test_param_t *tparam)
{
    hid_t	file = -1;              /* File ID */
    H5F_t	*f = NULL;              /* Internal file object pointer */
    H5EA_t      *ea = NULL;             /* Extensible array wrapper */
    earray_state_t state;               /* State of extensible array */
    uint64_t    welmt;                  /* Element to write */
    uint64_t    relmt;                  /* Element to read */
    hsize_t     nelmts;                 /* Highest element written in array */
    haddr_t     ea_addr = HADDR_UNDEF;  /* Array address in file */

    /*
     * Display testing message
     */
    TESTING("setting first element of array");

    /* Check for elements in index block */
    if(cparam->idx_blk_elmts > 0) {
        /* Create file & retrieve pointer to internal file object */
        if(create_file(fapl, &file, &f) < 0)
            TEST_ERROR

        /* Create array */
        if(create_array(f, H5P_DATASET_XFER_DEFAULT, cparam, &ea, &ea_addr) < 0)
            TEST_ERROR

        /* Verify the creation parameters */
        if(verify_cparam(ea, cparam) < 0)
            TEST_ERROR

        /* Check for closing & re-opening the file */
        if(reopen_file(&file, &f, fapl, H5P_DATASET_XFER_DEFAULT, &ea, ea_addr, cparam->cls, tparam) < 0)
            TEST_ERROR

        /* Verify high-water # of elements written */
        nelmts = (hsize_t)ULLONG_MAX;
        if(H5EA_get_nelmts(ea, &nelmts) < 0)
            FAIL_STACK_ERROR
        if(nelmts != 0)
            TEST_ERROR

        /* Verify array state */
        HDmemset(&state, 0, sizeof(state));
        if(check_stats(ea, &state))
            TEST_ERROR

        /* Retrieve first element of array (not set yet) */
        relmt = (uint64_t)0;
        if(H5EA_get(ea, H5P_DATASET_XFER_DEFAULT, (hsize_t)0, &relmt) < 0)
            FAIL_STACK_ERROR

        /* Verify first element is fill value for array */
        if(relmt != H5EA_TEST_FILL)
            TEST_ERROR

        /* Set first element of array */
        welmt = (uint64_t)7;
        if(H5EA_set(ea, H5P_DATASET_XFER_DEFAULT, (hsize_t)0, &welmt) < 0)
            FAIL_STACK_ERROR

        /* Verify high-water # of elements written */
        nelmts = (hsize_t)ULLONG_MAX;
        if(H5EA_get_nelmts(ea, &nelmts) < 0)
            FAIL_STACK_ERROR
        if(nelmts != 1)
            TEST_ERROR

        /* Verify array state */
        HDmemset(&state, 0, sizeof(state));
        if(check_stats(ea, &state))
            TEST_ERROR

        /* Retrieve first element of array (set now) */
        relmt = (uint64_t)0;
        if(H5EA_get(ea, H5P_DATASET_XFER_DEFAULT, (hsize_t)0, &relmt) < 0)
            FAIL_STACK_ERROR

        /* Verify first element is value written */
        if(relmt != welmt)
            TEST_ERROR

        /* Close array, delete array, close file & verify file is empty */
        if(shutdown(file, f, ea, ea_addr) < 0)
            TEST_ERROR

        /* All tests passed */
        PASSED()
    } /* end if */
    else {
        SKIPPED();
        puts("    No elements stored in index block");
    } /* end else */

    return 0;

error:
    H5E_BEGIN_TRY {
        if(ea)
            H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);
	H5Fclose(file);
    } H5E_END_TRY;

    return 1;
} /* test_set_first() */


/*-------------------------------------------------------------------------
 * Function:	test_set_iblock
 *
 * Purpose:	Set all elements in extensible array's index block
 *
 * Return:	Success:	0
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Thursday, September 11, 2008
 *
 *-------------------------------------------------------------------------
 */
static unsigned
test_set_iblock(hid_t fapl, H5EA_create_t *cparam, earray_test_param_t *tparam)
{
    hid_t	file = -1;              /* File ID */
    H5F_t	*f = NULL;              /* Internal file object pointer */
    H5EA_t      *ea = NULL;             /* Extensible array wrapper */
    earray_state_t state;               /* State of extensible array */
    uint64_t    welmt;                  /* Element to write */
    uint64_t    relmt;                  /* Element to read */
    hsize_t     nelmts;                 /* Highest element written in array */
    haddr_t     ea_addr = HADDR_UNDEF;  /* Array address in file */

    /*
     * Display testing message
     */
    TESTING("setting index block elements of array");

    /* Check for elements in index block */
    if(cparam->idx_blk_elmts > 0) {
        unsigned u;             /* Local index variable */

        /* Create file & retrieve pointer to internal file object */
        if(create_file(fapl, &file, &f) < 0)
            TEST_ERROR

        /* Create array */
        if(create_array(f, H5P_DATASET_XFER_DEFAULT, cparam, &ea, &ea_addr) < 0)
            TEST_ERROR

        /* Verify the creation parameters */
        if(verify_cparam(ea, cparam) < 0)
            TEST_ERROR

        /* Check for closing & re-opening the file */
        if(reopen_file(&file, &f, fapl, H5P_DATASET_XFER_DEFAULT, &ea, ea_addr, cparam->cls, tparam) < 0)
            TEST_ERROR

        /* Verify high-water # of elements written */
        nelmts = (hsize_t)ULLONG_MAX;
        if(H5EA_get_nelmts(ea, &nelmts) < 0)
            FAIL_STACK_ERROR
        if(nelmts != 0)
            TEST_ERROR

        /* Verify array state */
        HDmemset(&state, 0, sizeof(state));
        if(check_stats(ea, &state))
            TEST_ERROR

        /* Retrieve elements of array in index block (not set yet) */
        for(u = 0; u < cparam->idx_blk_elmts; u++) {
            relmt = (uint64_t)0;
            if(H5EA_get(ea, H5P_DATASET_XFER_DEFAULT, (hsize_t)u, &relmt) < 0)
                FAIL_STACK_ERROR

            /* Verify first element is fill value for array */
            if(relmt != H5EA_TEST_FILL)
                TEST_ERROR
        } /* end for */

        /* Set elements of array in index block */
        for(u = 0; u < cparam->idx_blk_elmts; u++) {
            welmt = (uint64_t)(7 + u);
            if(H5EA_set(ea, H5P_DATASET_XFER_DEFAULT, (hsize_t)u, &welmt) < 0)
                FAIL_STACK_ERROR

            /* Verify high-water # of elements written */
            nelmts = (hsize_t)ULLONG_MAX;
            if(H5EA_get_nelmts(ea, &nelmts) < 0)
                FAIL_STACK_ERROR
            if(nelmts != (u + 1))
                TEST_ERROR

            /* Verify array state */
            HDmemset(&state, 0, sizeof(state));
            if(check_stats(ea, &state))
                TEST_ERROR

            /* Retrieve first element of array (set now) */
            relmt = (uint64_t)0;
            if(H5EA_get(ea, H5P_DATASET_XFER_DEFAULT, (hsize_t)u, &relmt) < 0)
                FAIL_STACK_ERROR

            /* Verify first element is value written */
            if(relmt != welmt)
                TEST_ERROR
        } /* end for */

        /* Close array, delete array, close file & verify file is empty */
        if(shutdown(file, f, ea, ea_addr) < 0)
            TEST_ERROR

        /* All tests passed */
        PASSED()
    } /* end if */
    else {
        SKIPPED();
        puts("    No elements stored in index block");
    } /* end else */

    return 0;

error:
    H5E_BEGIN_TRY {
        if(ea)
            H5EA_close(ea, H5P_DATASET_XFER_DEFAULT);
	H5Fclose(file);
    } H5E_END_TRY;

    return 1;
} /* test_set_iblock() */


/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:	Test the extensible array code
 *
 * Return:	Success: 0
 *		Failure: 1
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, June 17, 2008
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    H5EA_create_t cparam;               /* Creation parameters for extensible array */
    earray_test_param_t tparam;         /* Testing parameters */
    earray_test_type_t curr_test;       /* Current test being worked on */
    hid_t	fapl = -1;              /* File access property list for data files */
    unsigned	nerrors = 0;            /* Cumulative error count */
    int		ExpressMode;            /* Test express value */
    const char *envval;                 /* File Driver value from environment */

    /* Reset library */
    h5_reset();
    fapl = h5_fileaccess();
    ExpressMode = GetTestExpress();
    if(ExpressMode > 1)
	printf("***Express test mode on.  Some tests may be skipped\n");

    if(NULL == (envval = HDgetenv("HDF5_DRIVER")))
        envval = "nomatch";

    if(HDstrcmp(envval, "core") && HDstrcmp(envval, "split") && HDstrcmp(envval, "multi") && 
            HDstrcmp(envval, "family")) {
        /* Set the filename to use for this test (dependent on fapl) */
        h5_fixname(FILENAME[0], fapl, filename_g, sizeof(filename_g));


        /* Create an empty file to retrieve size */
        {
            hid_t	file;              /* File ID */

            if((file = H5Fcreate(filename_g, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
                FAIL_STACK_ERROR

            /* Close file */
            if(H5Fclose(file) < 0)
                FAIL_STACK_ERROR

            /* Get the size of a file w/no array */
            if((empty_size_g = h5_get_file_size(filename_g)) < 0)
                TEST_ERROR
        }


        /* Initialize extensible array creation parameters */
        init_cparam(&cparam);

        /* Iterate over the testing parameters */
        for(curr_test = EARRAY_TEST_NORMAL; curr_test < EARRAY_TEST_NTESTS; curr_test++) {

            /* Clear the testing parameters */
            HDmemset(&tparam, 0, sizeof(tparam));

            /* Set appropriate testing parameters for each test */
            switch(curr_test) {
                /* "Normal" testing parameters */
                case EARRAY_TEST_NORMAL:
                    puts("Testing with normal parameters");
                    break;

                /* "Re-open array" testing parameters */
                case EARRAY_TEST_REOPEN:
                    puts("Testing with reopen array flag set");
                    tparam.reopen_array = EARRAY_TEST_REOPEN;
                    break;

                /* An unknown test? */
                default:
                    goto error;
            } /* end switch */

            /* Basic capability tests */
            nerrors += test_create(fapl, &cparam, &tparam);
            nerrors += test_reopen(fapl, &cparam, &tparam);
            nerrors += test_open_twice(fapl, &cparam, &tparam);
            nerrors += test_delete_open(fapl, &cparam, &tparam);

            /* Basic capacity tests */
            nerrors += test_set_first(fapl, &cparam, &tparam);
            nerrors += test_set_iblock(fapl, &cparam, &tparam);
        } /* end for */

        if(nerrors)
            goto error;
        puts("All extensible array tests passed.");
    } /* end if(HDstrcmp(envval=="...")) */
    else
        printf("All extensible array tests skipped - Incompatible with current Virtual File Driver\n");

    /* Clean up file used */
    h5_cleanup(FILENAME, fapl);

    return 0;

error:
    puts("*** TESTS FAILED ***");

    H5E_BEGIN_TRY {
	H5Pclose(fapl);
    } H5E_END_TRY;

    return 1;
} /* end main() */