summaryrefslogtreecommitdiffstats
path: root/tools/h5copy/h5copygentest.c
blob: cce63c8747a27d38fa96ba3fc0f6125f8363130c (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Generate the binary hdf5 file for the h5copy tests
 */
#include "hdf5.h"
#include "H5private.h"

/* HDF file names */
#define HDF_FILE1        "h5copytst.h5"
#define HDF_FILE2        "h5copy_ref.h5"
#define HDF_EXT_SRC_FILE "h5copy_extlinks_src.h5"
#define HDF_EXT_TRG_FILE "h5copy_extlinks_trg.h5"

/* objects in HDF_FILE1 */
#define DATASET_SIMPLE     "simple"
#define DATASET_CHUNK      "chunk"
#define DATASET_COMPACT    "compact"
#define DATASET_COMPOUND   "compound"
#define DATASET_COMPRESSED "compressed"
#define DATASET_NAMED_VL   "named_vl"
#define DATASET_NESTED_VL  "nested_vl"
#define DATASET_ATTR       "dset_attr"
#define ATTR               "attr"
#define GROUP_EMPTY        "grp_empty"
#define GROUP_DATASETS     "grp_dsets"
#define GROUP_NESTED       "grp_nested"
#define GROUP_ATTR         "grp_attr"

/* Obj reference */
#define OBJ_REF_DS  "Dset1"
#define OBJ_REF_GRP "Group"
/* Region reference */
#define REG_REF_DS1 "Dset_REGREF"
#define REG_REF_DS2 "Dset2"

/*-------------------------------------------------------------------------
 * Function:    gent_simple
 *
 * Purpose:     Generate a simple dataset in LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_simple(hid_t loc_id)
{
    hid_t   sid, did;
    hsize_t dims[1] = {6};
    int     buf[6]  = {1, 2, 3, 4, 5, 6};

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_SIMPLE, H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Sclose(sid);
    H5Dclose(did);
}

/*-------------------------------------------------------------------------
 * Function:    gent_chunked
 *
 * Purpose:     Generate a chunked dataset in LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_chunked(hid_t loc_id)
{
    hid_t   sid, did, pid;
    hsize_t dims[1]       = {6};
    hsize_t chunk_dims[1] = {2};
    int     buf[6]        = {1, 2, 3, 4, 5, 6};

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create property plist */
    pid = H5Pcreate(H5P_DATASET_CREATE);
    H5Pset_chunk(pid, 1, chunk_dims);

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_CHUNK, H5T_NATIVE_INT, sid, H5P_DEFAULT, pid, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Sclose(sid);
    H5Dclose(did);
    H5Pclose(pid);
}

/*-------------------------------------------------------------------------
 * Function:    gent_compact
 *
 * Purpose:     Generate a compact dataset in LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_compact(hid_t loc_id)
{
    hid_t   sid, did, pid;
    hsize_t dims[1] = {6};
    int     buf[6]  = {1, 2, 3, 4, 5, 6};

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create property plist  */
    pid = H5Pcreate(H5P_DATASET_CREATE);
    H5Pset_layout(pid, H5D_COMPACT);

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_COMPACT, H5T_NATIVE_INT, sid, H5P_DEFAULT, pid, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Sclose(sid);
    H5Dclose(did);
    H5Pclose(pid);
}

/*-------------------------------------------------------------------------
 * Function:    gent_compound
 *
 * Purpose:     Generate a compound dataset in LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_compound(hid_t loc_id)
{
    typedef struct s_t {
        char str1[20];
        char str2[20];
    } s_t;
    hid_t   sid, did, tid_c, tid_s;
    hsize_t dims[1] = {2};
    s_t     buf[2]  = {{"str1", "str2"}, {"str3", "str4"}};

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create a compound type */
    tid_c = H5Tcreate(H5T_COMPOUND, sizeof(s_t));
    tid_s = H5Tcopy(H5T_C_S1);
    H5Tset_size(tid_s, 20);

    H5Tinsert(tid_c, "str1", HOFFSET(s_t, str1), tid_s);
    H5Tinsert(tid_c, "str2", HOFFSET(s_t, str2), tid_s);

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_COMPOUND, tid_c, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, tid_c, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Sclose(sid);
    H5Dclose(did);
    H5Tclose(tid_c);
    H5Tclose(tid_s);
}

/*-------------------------------------------------------------------------
 * Function:    gent_compressed
 *
 * Purpose:     Generate a compressed dataset in LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_compressed(hid_t loc_id)
{
    hid_t   sid, did, pid;
    hsize_t dims[1]       = {6};
    hsize_t chunk_dims[1] = {2};
    int     buf[6]        = {1, 2, 3, 4, 5, 6};

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create property plist for chunk*/
    pid = H5Pcreate(H5P_DATASET_CREATE);
    H5Pset_chunk(pid, 1, chunk_dims);

    /* set the deflate filter */
#if defined(H5_HAVE_FILTER_DEFLATE)
    H5Pset_deflate(pid, 1);
#endif

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_COMPRESSED, H5T_NATIVE_INT, sid, H5P_DEFAULT, pid, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Sclose(sid);
    H5Dclose(did);
    H5Pclose(pid);
}

/*-------------------------------------------------------------------------
 * Function:    gent_named_vl
 *
 * Purpose:     Generate a variable length named datatype for a dataset in
                LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_named_vl(hid_t loc_id)
{
    hid_t   sid, did, tid;
    hsize_t dims[1] = {2};
    hvl_t   buf[2];

    /* allocate and initialize VL dataset to write */
    buf[0].len           = 1;
    buf[0].p             = HDmalloc(1 * sizeof(int));
    ((int *)buf[0].p)[0] = 1;
    buf[1].len           = 2;
    buf[1].p             = HDmalloc(2 * sizeof(int));
    ((int *)buf[1].p)[0] = 2;
    ((int *)buf[1].p)[1] = 3;

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create datatype */
    tid = H5Tvlen_create(H5T_NATIVE_INT);

    /* create named datatype */
    H5Tcommit2(loc_id, "vl", tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_NAMED_VL, tid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, buf);
    H5Sclose(sid);
    H5Dclose(did);
    H5Tclose(tid);
}

/*-------------------------------------------------------------------------
 * Function:    gent_nested_vl
 *
 * Purpose:     Generate a nested variable length dataset in LOC_ID
 *
 *-------------------------------------------------------------------------
 */
static void
gent_nested_vl(hid_t loc_id)
{
    hid_t   sid, did, tid1, tid2;
    hsize_t dims[1] = {2};
    hvl_t   buf[2];
    hvl_t * tvl;

    /* allocate and initialize VL dataset to write */
    buf[0].len         = 1;
    buf[0].p           = HDmalloc(1 * sizeof(hvl_t));
    tvl                = (hvl_t *)buf[0].p;
    tvl->p             = HDmalloc(1 * sizeof(int));
    tvl->len           = 1;
    ((int *)tvl->p)[0] = 1;

    buf[1].len         = 1;
    buf[1].p           = HDmalloc(1 * sizeof(hvl_t));
    tvl                = (hvl_t *)buf[1].p;
    tvl->p             = HDmalloc(2 * sizeof(int));
    tvl->len           = 2;
    ((int *)tvl->p)[0] = 2;
    ((int *)tvl->p)[1] = 3;

    /* create dataspace */
    sid = H5Screate_simple(1, dims, NULL);

    /* create datatype */
    tid1 = H5Tvlen_create(H5T_NATIVE_INT);

    /* create nested VL datatype */
    tid2 = H5Tvlen_create(tid1);

    /* create dataset */
    did = H5Dcreate2(loc_id, DATASET_NESTED_VL, tid2, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* write */
    H5Dwrite(did, tid2, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

    /* close */
    H5Dvlen_reclaim(tid2, sid, H5P_DEFAULT, buf);
    H5Sclose(sid);
    H5Dclose(did);
    H5Tclose(tid1);
    H5Tclose(tid2);
}

/*-------------------------------------------------------------------------
 * Function:    gent_att_compound_vlstr
 *
 * Purpose:     Generate a dataset and a group.
 *              Both has an attribute with a compound datatype consisting
 *              of a variable length string
 *
 *-------------------------------------------------------------------------
 */
static void
gent_att_compound_vlstr(hid_t loc_id)
{
    typedef struct { /* Compound structure for the attribute */
        int         i;
        const char *v;
    } s1;
    hsize_t dim[1]     = {1};             /* Dimension size */
    hid_t   sid        = H5I_INVALID_HID; /* Dataspace ID */
    hid_t   tid        = H5I_INVALID_HID; /* Datatype ID */
    hid_t   aid        = H5I_INVALID_HID; /* Attribute ID */
    hid_t   did        = H5I_INVALID_HID; /* Dataset ID */
    hid_t   gid        = H5I_INVALID_HID; /* Group ID */
    hid_t   vl_str_tid = H5I_INVALID_HID; /* Variable length datatype ID */
    hid_t   cmpd_tid   = H5I_INVALID_HID; /* Compound datatype ID */
    hid_t   null_sid   = H5I_INVALID_HID; /* Null dataspace ID */
    s1      buf;                          /* Buffer */

    buf.i = 9;
    buf.v = "ThisIsAString";

    /* Create an integer datatype */
    tid = H5Tcopy(H5T_NATIVE_INT);

    /* Create a variable length string */
    vl_str_tid = H5Tcopy(H5T_C_S1);
    H5Tset_size(vl_str_tid, H5T_VARIABLE);

    /* Create a compound datatype with a variable length string and an integer */
    cmpd_tid = H5Tcreate(H5T_COMPOUND, sizeof(s1));
    H5Tinsert(cmpd_tid, "i", HOFFSET(s1, i), tid);
    H5Tinsert(cmpd_tid, "v", HOFFSET(s1, v), vl_str_tid);

    /* Create a dataset */
    null_sid = H5Screate(H5S_NULL);
    did = H5Dcreate2(loc_id, DATASET_ATTR, H5T_NATIVE_INT, null_sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* Attach an attribute with the compound datatype to the dataset */
    sid = H5Screate_simple(1, dim, dim);
    aid = H5Acreate2(did, ATTR, cmpd_tid, sid, H5P_DEFAULT, H5P_DEFAULT);

    /* Write the attribute */
    buf.i = 9;
    buf.v = "ThisIsAString";
    H5Awrite(aid, cmpd_tid, &buf);

    /* Close the dataset and its attribute */
    H5Dclose(did);
    H5Aclose(aid);

    /* Create a group */
    gid = H5Gcreate2(loc_id, GROUP_ATTR, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* Attach an attribute with the compound datatype to the group */
    aid = H5Acreate2(gid, ATTR, cmpd_tid, sid, H5P_DEFAULT, H5P_DEFAULT);
    H5Awrite(aid, cmpd_tid, &buf);

    /* Close the group and its attribute */
    H5Aclose(aid);
    H5Gclose(gid);

    /* Close dataspaces */
    H5Sclose(sid);
    H5Sclose(null_sid);

    /* Close datatypes */
    H5Tclose(tid);
    H5Tclose(vl_str_tid);
    H5Tclose(cmpd_tid);

} /* gen_att_compound_vlstr() */

/*-------------------------------------------------------------------------
 * Function: gent_datasets
 *
 * Purpose: Generate all datasets in a particular location
 *
 *-------------------------------------------------------------------------
 */
static void
gent_datasets(hid_t loc_id)
{
    gent_simple(loc_id);
    gent_chunked(loc_id);
    gent_compact(loc_id);
    gent_compound(loc_id);
    gent_compressed(loc_id);
    gent_named_vl(loc_id);
    gent_nested_vl(loc_id);
}

/*-------------------------------------------------------------------------
 * Function: gent_empty_group
 *
 * Purpose: Generate an empty group in a location
 *
 *-------------------------------------------------------------------------
 */
static void
gent_empty_group(hid_t loc_id)
{
    hid_t gid;

    /* Create group in location */
    gid = H5Gcreate2(loc_id, GROUP_EMPTY, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* Release resources */
    H5Gclose(gid);
}

/*-------------------------------------------------------------------------
 * Function: gent_nested_datasets
 *
 * Purpose: Generate a group in a location and populate it with the "standard"
 *    datasets
 *
 *-------------------------------------------------------------------------
 */
static void
gent_nested_datasets(hid_t loc_id)
{
    hid_t gid;

    /* Create group in location */
    gid = H5Gcreate2(loc_id, GROUP_DATASETS, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* Add datasets to group created */
    gent_datasets(gid);

    /* Release resources */
    H5Gclose(gid);
}

/*-------------------------------------------------------------------------
 * Function: gent_nested_group
 *
 * Purpose: Generate a group in a location and populate it with another group
 *    containing the "standard" datasets
 *
 *-------------------------------------------------------------------------
 */
static void
gent_nested_group(hid_t loc_id)
{
    hid_t gid;

    /* Create group in location */
    gid = H5Gcreate2(loc_id, GROUP_NESTED, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    /* Add datasets to group created */
    gent_nested_datasets(gid);

    /* Release resources */
    H5Gclose(gid);
}

/*-------------------------------------------------------------------------
 * Function: gen_obj_ref
 *
 * Purpose: Generate object references to dataset and group
 *
 * Programmer: Jonathan Kim (Feb 23, 2010)
 *------------------------------------------------------------------------*/
static herr_t
gen_obj_ref(hid_t loc_id)
{
    hid_t   sid = 0, oid = 0;
    hsize_t dims1[1] = {3};
    hsize_t dims2[1] = {2};
    int     data[3]  = {10, 20, 30};
    int     status;

    /*---------------------
     * create obj references to the previously created objects.
     * Passing -1 as reference is an object.*/
    hobj_ref_t or_data[2]; /* write buffer */
    herr_t     ret = SUCCEED;

    /*--------------
     * add dataset */
    sid = H5Screate_simple(1, dims1, NULL);
    if (sid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    oid = H5Dcreate2(loc_id, OBJ_REF_DS, H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (oid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    status = H5Dwrite(oid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    H5Dclose(oid);
    H5Sclose(sid);

    /*--------------
     * add group  */
    oid = H5Gcreate2(loc_id, OBJ_REF_GRP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (oid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Gcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }
    H5Gclose(oid);

    status = H5Rcreate(&or_data[0], loc_id, OBJ_REF_DS, H5R_OBJECT, (hid_t)-1);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }
    status = H5Rcreate(&or_data[1], loc_id, OBJ_REF_GRP, H5R_OBJECT, (hid_t)-1);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    sid = H5Screate_simple(1, dims2, NULL);
    if (sid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    oid = H5Dcreate2(loc_id, "Dset_OBJREF", H5T_STD_REF_OBJ, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (oid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    status = H5Dwrite(oid, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, or_data);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

out:
    if (oid > 0)
        H5Dclose(oid);
    if (sid > 0)
        H5Sclose(sid);

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: gen_region_ref
 *
 * Purpose: Generate dataset region references
 *
 * Programmer: Jonathan Kim (Feb 23, 2010)
 *------------------------------------------------------------------------*/
static herr_t
gen_region_ref(hid_t loc_id)
{
    hid_t           sid = 0, oid1 = 0, oid2 = 0;
    int             status;
    herr_t          ret          = SUCCEED;
    char            data[3][16]  = {"The quick brown", "fox jumps over ", "the 5 lazy dogs"};
    hsize_t         dims2[2]     = {3, 16};
    hsize_t         coords[4][2] = {{0, 1}, {2, 11}, {1, 0}, {2, 4}};
    hdset_reg_ref_t rr_data[2];
    hsize_t         start[2]  = {0, 0};
    hsize_t         stride[2] = {2, 11};
    hsize_t         count[2]  = {2, 2};
    hsize_t         block[2]  = {1, 3};
    hsize_t         dims1[1]  = {2};

    sid = H5Screate_simple(2, dims2, NULL);
    if (sid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* create normal dataset which is refered */
    oid2 = H5Dcreate2(loc_id, REG_REF_DS2, H5T_STD_I8LE, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (oid2 < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* write values to dataset */
    status = H5Dwrite(oid2, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* select elements space for reference */
    status = H5Sselect_elements(sid, H5S_SELECT_SET, 4, coords[0]);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Sselect_elements failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* create region reference from elements space */
    status = H5Rcreate(&rr_data[0], loc_id, REG_REF_DS2, H5R_DATASET_REGION, sid);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* select hyperslab space for reference */
    status = H5Sselect_hyperslab(sid, H5S_SELECT_SET, start, stride, count, block);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Sselect_hyperslab failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* create region reference from hyperslab space */
    status = H5Rcreate(&rr_data[1], loc_id, REG_REF_DS2, H5R_DATASET_REGION, sid);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Rcreate failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    H5Sclose(sid);

    /* Create dataspace. */
    sid = H5Screate_simple(1, dims1, NULL);
    if (sid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Screate_simple failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* create region reference dataset */
    oid1 = H5Dcreate2(loc_id, REG_REF_DS1, H5T_STD_REF_DSETREG, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (oid1 < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* write data as region references */
    status = H5Dwrite(oid1, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, rr_data);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Dwrite failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

out:
    if (oid1 > 0)
        H5Dclose(oid1);
    if (oid2 > 0)
        H5Dclose(oid2);
    if (sid > 0)
        H5Sclose(sid);

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: Test_Obj_Copy
 *
 * Purpose: Testing with various objects
 *
 *------------------------------------------------------------------------*/
static void
Test_Obj_Copy(void)
{
    hid_t fid = H5I_INVALID_HID; /* File id */

    /* Create source file */
    fid = H5Fcreate(HDF_FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    if (fid < 0) {
        HDfprintf(stderr, "Error: %s> H5Fcreate failed.\n", HDF_FILE1);
        goto out;
    }

    gent_datasets(fid);
    gent_empty_group(fid);
    gent_nested_datasets(fid);
    gent_nested_group(fid);
    gent_att_compound_vlstr(fid);

out:
    /*-----------------------------------------------------------------------
     * Close
     *------------------------------------------------------------------------*/
    if (fid > 0)
        H5Fclose(fid);
}

/*-------------------------------------------------------------------------
 * Function: Test_Ref_Copy
 *
 * Purpose: Testing with various references
 *
 *------------------------------------------------------------------------*/
static void
Test_Ref_Copy(void)
{
    hid_t  fid = 0;
    herr_t status;

    fid = H5Fcreate(HDF_FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    if (fid < 0) {
        HDfprintf(stderr, "Error: %s> H5Fcreate failed.\n", HDF_FILE2);
        goto out;
    }

    /* add object reference */
    status = gen_obj_ref(fid);
    if (status < 0)
        HDfprintf(stderr, "Failed to generate object reference.\n");

    /* add region reference */
    status = gen_region_ref(fid);
    if (status < 0)
        HDfprintf(stderr, "Failed to generate region reference.\n");

out:
    /*-----------------------------------------------------------------------
     * Close
     *------------------------------------------------------------------------*/
    if (fid > 0)
        H5Fclose(fid);
}

/*-------------------------------------------------------------------------
 * Function: gen_extlink_trg
 *
 * Purpose: generate target external link objs
 *
 * Programmer: Jonathan Kim (March 03, 2010)
 *------------------------------------------------------------------------*/
static herr_t
gen_extlink_trg(hid_t loc_id)
{
    hid_t  gid = 0, tid = 0;
    int    status;
    herr_t ret = SUCCEED;

    /*-----------------------------------------------------------------------
     * Groups
     *------------------------------------------------------------------------*/
    /*--------------
     * target file */
    gid = H5Gcreate2(loc_id, "group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (gid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Gcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /*--------------
     * add dataset */
    gent_simple(loc_id);

    /*--------------------
     * add named datatype
     */
    tid    = H5Tcopy(H5T_NATIVE_INT);
    status = H5Tcommit2(loc_id, "datatype", tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Tcommit2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

out:
    if (gid > 0)
        H5Gclose(gid);
    if (tid > 0)
        H5Tclose(tid);

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: gen_extlink_src
 *
 * Purpose: generate source external link objs
 *
 * Programmer: Jonathan Kim (March 03, 2010)
 *------------------------------------------------------------------------*/
static herr_t
gen_extlink_src(hid_t loc_id)
{
    hid_t  gid = 0;
    int    status;
    herr_t ret = SUCCEED;

    /*-----------------------------------------------------------------------
     * Groups
     *------------------------------------------------------------------------*/
    gid = H5Gcreate2(loc_id, "/group_ext", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if (gid < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Gcreate2 failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /*-----------------------------------------------------------------------
     * External links
     *------------------------------------------------------------------------*/
    /* link to dataset */
    status = H5Lcreate_external(HDF_EXT_TRG_FILE, "/simple", gid, "extlink_dset", H5P_DEFAULT, H5P_DEFAULT);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Lcreate_external failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* link to group */
    status = H5Lcreate_external(HDF_EXT_TRG_FILE, "/group", gid, "extlink_grp", H5P_DEFAULT, H5P_DEFAULT);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Lcreate_external failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* link to datatype */
    status =
        H5Lcreate_external(HDF_EXT_TRG_FILE, "/datatype", gid, "extlink_datatype", H5P_DEFAULT, H5P_DEFAULT);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Lcreate_external failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* dangling link - no obj*/
    status = H5Lcreate_external(HDF_EXT_TRG_FILE, "notyet", gid, "extlink_notyet1", H5P_DEFAULT, H5P_DEFAULT);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Lcreate_external failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

    /* dangling link - no file */
    status = H5Lcreate_external("notyet_file.h5", "notyet", gid, "extlink_notyet2", H5P_DEFAULT, H5P_DEFAULT);
    if (status < 0) {
        HDfprintf(stderr, "Error: %s %d> H5Lcreate_external failed.\n", FUNC, __LINE__);
        ret = FAIL;
        goto out;
    }

out:
    if (gid > 0)
        H5Gclose(gid);

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: Test_Extlink_Copy
 *
 * Purpose: gerenate external link files
 *
 *------------------------------------------------------------------------*/
static void
Test_Extlink_Copy(void)
{
    hid_t  fid1 = 0;
    hid_t  fid2 = 0;
    herr_t status;

    fid1 = H5Fcreate(HDF_EXT_SRC_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    if (fid1 < 0) {
        HDfprintf(stderr, "Error: %s> H5Fcreate failed.\n", HDF_EXT_SRC_FILE);
        goto out;
    }

    fid2 = H5Fcreate(HDF_EXT_TRG_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    if (fid2 < 0) {
        HDfprintf(stderr, "Error: %s> H5Fcreate failed.\n", HDF_EXT_TRG_FILE);
        goto out;
    }

    /* add links to source external link file */
    status = gen_extlink_src(fid1);
    if (status < 0)
        HDfprintf(stderr, "Error: %s> gen_extlink_src failed.\n", HDF_EXT_SRC_FILE);

    /* add objs to target external link file */
    status = gen_extlink_trg(fid2);
    if (status < 0)
        HDfprintf(stderr, "Error: %s> gen_extlink_trg failed.\n", HDF_EXT_TRG_FILE);

out:
    /*-----------------------------------------------------------------------
     * Close
     *------------------------------------------------------------------------*/
    if (fid1 > 0)
        H5Fclose(fid1);
    if (fid2 > 0)
        H5Fclose(fid2);
}

/*-------------------------------------------------------------------------
 * Function: main
 *
 *-------------------------------------------------------------------------
 */

int
main(void)
{
    Test_Obj_Copy();
    Test_Ref_Copy();
    Test_Extlink_Copy();

    return 0;
}