/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /*********************************************************** * * Test program: trefer * * Test the Reference functionality * *************************************************************/ #include "testhdf5.h" #include "hdf5.h" #define FILE1 "trefer1.h5" #define FILE2 "trefer2.h5" #define FILE3 "trefer3.h5" /* 1-D dataset with fixed dimensions */ #define SPACE1_NAME "Space1" #define SPACE1_RANK 1 #define SPACE1_DIM1 4 /* 2-D dataset with fixed dimensions */ #define SPACE2_NAME "Space2" #define SPACE2_RANK 2 #define SPACE2_DIM1 10 #define SPACE2_DIM2 10 /* Larger 1-D dataset with fixed dimensions */ #define SPACE3_NAME "Space3" #define SPACE3_RANK 1 #define SPACE3_DIM1 100 /* Element selection information */ #define POINT1_NPOINTS 10 /* Compound datatype */ typedef struct s1_t { unsigned int a; unsigned int b; float c; } s1_t; #define GROUPNAME "/group" #define GROUPNAME2 "group2" #define GROUPNAME3 "group3" #define DSETNAME "/dset" #define DSETNAME2 "dset2" #define NAME_SIZE 16 /**************************************************************** ** ** test_reference_obj(): Test basic H5R (reference) object reference code. ** Tests references to various kinds of objects ** ****************************************************************/ static void test_reference_obj(void) { hid_t fid1; /* HDF5 File IDs */ hid_t dataset, /* Dataset ID */ dset2; /* Dereferenced dataset ID */ hid_t group; /* Group ID */ hid_t sid1; /* Dataspace ID */ hid_t tid1; /* Datatype ID */ hsize_t dims1[] = {SPACE1_DIM1}; hobj_ref_t *wbuf, /* buffer to write to disk */ *rbuf, /* buffer read from disk */ *tbuf; /* temp. buffer read from disk */ hobj_ref_t nvrbuf[3]={0,100,1000000000}; /* buffer with non-valid refs */ unsigned *tu32; /* Temporary pointer to uint32 data */ int i, j; /* counting variables */ const char *write_comment="Foo!"; /* Comments for group */ char read_comment[10]; herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(5, ("Testing Object Reference Functions\n")); /* Allocate write & read buffers */ wbuf=malloc(MAX(sizeof(unsigned),sizeof(hobj_ref_t))*SPACE1_DIM1); rbuf=malloc(MAX(sizeof(unsigned),sizeof(hobj_ref_t))*SPACE1_DIM1); tbuf=malloc(MAX(sizeof(unsigned),sizeof(hobj_ref_t))*SPACE1_DIM1); /* Create file */ fid1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); CHECK(fid1, FAIL, "H5Fcreate"); /* Create dataspace for datasets */ sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL); CHECK(sid1, FAIL, "H5Screate_simple"); /* Create a group */ group=H5Gcreate(fid1,"Group1",(size_t)-1); CHECK(group, FAIL, "H5Gcreate"); /* Set group's comment */ ret=H5Gset_comment(group,".",write_comment); CHECK(ret, FAIL, "H5Gset_comment"); /* Create a dataset (inside Group1) */ dataset=H5Dcreate(group,"Dataset1",H5T_NATIVE_UINT,sid1,H5P_DEFAULT); CHECK(dataset, FAIL, "H5Dcreate"); for(tu32=(unsigned *)wbuf,i=0; i 0); ret = H5Dclose(did); assert(ret >= 0); ret = H5Gclose(gid); CHECK(ret, FAIL, "H5Gclose"); /* Create dataset */ did = H5Dcreate(fid, DSETNAME, H5T_STD_REF_OBJ, sid, H5P_DEFAULT); CHECK(did, FAIL, "H5Dcreate"); /* Create reference to group */ ret = H5Rcreate(&wref, fid, GROUPNAME, H5R_OBJECT, -1); CHECK(ret, FAIL, "H5Rcreate"); /* Write reference to disk */ ret = H5Dwrite(did, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wref); CHECK(ret, FAIL, "H5Dwrite"); /* Close objects */ ret = H5Dclose(did); CHECK(ret, FAIL, "H5Dclose"); ret = H5Sclose(sid); CHECK(ret, FAIL, "H5Sclose"); ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); /* Re-open file */ fid = H5Fopen(FILE1, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(fid, FAIL, "H5Fopen"); /* Re-open dataset */ did = H5Dopen(fid, DSETNAME); CHECK(did, FAIL, "H5Dopen"); /* Read in the reference */ ret = H5Dread(did, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &rref); CHECK(ret, FAIL, "H5Dread"); /* Dereference to get the group */ gid = H5Rdereference(did, H5R_OBJECT, &rref); CHECK(gid, FAIL, "H5Rdereference"); /* Iterate through objects in dereferenced group */ ret = H5Giterate(gid, ".", NULL, test_deref_iter_op, &count); CHECK(ret, FAIL, "H5Giterate"); /* Various queries on the group opened */ ret = H5Gget_num_objs(gid, &nobjs); CHECK(ret, FAIL, "H5Gget_num_objs"); VERIFY(nobjs, 3, "H5Gget_num_objs"); ret = H5Gget_objname_by_idx(gid, (hsize_t)0, objname, NAME_SIZE); CHECK(ret, FAIL, "H5Gget_objname_by_idx"); VERIFY_STR(objname, DSETNAME2, "H5Gget_objname_by_idx"); objtype = H5Gget_objtype_by_idx(gid, (hsize_t)0); VERIFY(objtype, H5G_DATASET, "H5Gget_objtype_by_idx"); /* Unlink one of the objects in the dereferenced group */ ret = H5Gunlink(gid, GROUPNAME2); CHECK(ret, FAIL, "H5Gunlink"); /* Delete dataset object in dereferenced group (with other dataset still open) */ ret = H5Gunlink(gid, DSETNAME2); assert(ret >= 0); /* Close objects */ ret = H5Dclose(did); CHECK(ret, FAIL, "H5Dclose"); ret = H5Gclose(gid); CHECK(ret, FAIL, "H5Gclose"); ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); } /* test_reference_group() */ /**************************************************************** ** ** test_reference_version(): Test H5R (reference) object reference code. ** Tests version compatibility macro ** ****************************************************************/ static void test_reference_version(void) { hid_t fid = -1; /* File ID */ hid_t gid = -1; /* Group ID */ hobj_ref_t ref; /* Reference */ herr_t ret; /* Create file with a group */ fid = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); CHECK(fid, FAIL, "H5Fcreate"); /* Create group to refer to */ gid = H5Gcreate(fid, GROUPNAME, (size_t)0); CHECK(gid, FAIL, "H5Gcreate"); /* Create reference to group */ ret = H5Rcreate(&ref, fid, GROUPNAME, H5R_OBJECT, -1); CHECK(ret, FAIL, "H5Rcreate"); /* Get type of referenced object (test H5Rget_obj_type1) */ ret = H5Rget_obj_type1(fid,H5R_OBJECT,&ref); VERIFY(ret, H5G_GROUP, "H5Rget_obj_type1"); /* Close objects */ ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); ret = H5Gclose(gid); CHECK(ret, FAIL, "H5Gclose"); } /* test_reference_version() */ /**************************************************************** ** ** test_reference(): Main H5R reference testing routine. ** ****************************************************************/ void test_reference(void) { /* Output message about test being performed */ MESSAGE(5, ("Testing References\n")); test_reference_obj(); /* Test basic H5R object reference code */ test_reference_region(); /* Test basic H5R dataset region reference code */ test_reference_region_1D(); /* Test H5R dataset region reference code for 1-D datasets */ test_reference_obj_deleted(); /* Test H5R object reference code for deleted objects */ test_reference_group(); /* Test operations on dereferenced groups */ test_reference_version(); /* Test version compatibility macro */ } /* test_reference() */ /*------------------------------------------------------------------------- * Function: cleanup_reference * * Purpose: Cleanup temporary test files * * Return: none * * Programmer: Quincey Koziol * September 8, 1998 * * Modifications: * *------------------------------------------------------------------------- */ void cleanup_reference(void) { remove(FILE1); remove(FILE2); remove(FILE3); }