/**************************************************************************** * NCSA HDF * * Software Development Group * * National Center for Supercomputing Applications * * University of Illinois at Urbana-Champaign * * 605 E. Springfield, Champaign IL 61820 * * * * For conditions of distribution and use, see the accompanying * * hdf/COPYING file. * * * ****************************************************************************/ #ifdef RCSID static char RcsId[] = "$Revision$"; #endif /* $Id$ */ /*********************************************************** * * Test program: tattr * * Test the attribute functionality * *************************************************************/ #include #include #define FILE "tattr.h5" #define ATTR_NAME_LEN 16 #define ATTR_MAX_DIMS 7 /* 3-D dataset with fixed dimensions */ #define SPACE1_NAME "Space1" #define SPACE1_RANK 3 #define SPACE1_DIM1 3 #define SPACE1_DIM2 15 #define SPACE1_DIM3 13 /* Group Information */ #define GROUP1_NAME "/Group1" /* Attribute Rank & Dimensions */ #define ATTR1_NAME "Attr1" #define ATTR1_RANK 1 #define ATTR1_DIM1 3 int32 attr_data1[ATTR1_DIM1]={512,-234,98123}; /* Test data for 1st attribute */ #define ATTR2_NAME "Attr2" #define ATTR2_RANK 2 #define ATTR2_DIM1 2 #define ATTR2_DIM2 2 int32 attr_data2[ATTR2_DIM1][ATTR2_DIM2]={{7614,-416},{197814,-3}}; /* Test data for 2nd attribute */ #define ATTR3_NAME "Attr3" #define ATTR3_RANK 3 #define ATTR3_DIM1 2 #define ATTR3_DIM2 2 #define ATTR3_DIM3 2 double attr_data3[ATTR3_DIM1][ATTR3_DIM2][ATTR3_DIM3]={{{2.3,-26.1},{0.123,-10.0}},{{981724.2,-0.91827},{2.0,23.0}}}; /* Test data for 3rd attribute */ #define ATTR4_NAME "Attr4" #define ATTR4_RANK 2 #define ATTR4_DIM1 2 #define ATTR4_DIM2 2 #define ATTR4_FIELDNAME1 "i" #define ATTR4_FIELDNAME2 "d" #define ATTR4_FIELDNAME3 "c" size_t attr4_field1_off=0; size_t attr4_field2_off=0; size_t attr4_field3_off=0; struct attr4_struct { int32 i; double d; char c; } attr_data4[ATTR4_DIM1][ATTR4_DIM2]={{{3,-26.1,'d'},{-100000, 0.123,'3'}}, {{-23,981724.2,'Q'},{0,2.0,'\n'}}}; /* Test data for 4th attribute */ int attr_op1(hid_t loc_id, const char *name, void *op_data); /**************************************************************** ** ** test_attr_basic_write(): Test basic H5A (attribute) code. ** Tests integer attributes on both datasets and groups ** ****************************************************************/ static void test_attr_basic_write(void) { hid_t fid1; /* HDF5 File IDs */ hid_t dataset; /* Dataset ID */ hid_t group; /* Group ID */ hid_t sid1,sid2; /* Dataspace ID */ hid_t attr; /* Attribute ID */ hsize_t dims1[] = {SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3}; hsize_t dims2[] = {ATTR1_DIM1}; hsize_t dims3[] = {ATTR2_DIM1,ATTR2_DIM2}; herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(5, ("Testing Basic Attribute Functions\n")); /* Create file */ fid1 = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); CHECK(fid1, FAIL, "H5Fcreate"); /* Create dataspace for dataset */ sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL); CHECK(sid1, FAIL, "H5Screate_simple"); /* Create a dataset */ dataset=H5Dcreate(fid1,"Dataset1",H5T_NATIVE_UINT8,sid1,H5P_DEFAULT); /* Create dataspace for attribute */ sid2 = H5Screate_simple(ATTR1_RANK, dims2, NULL); CHECK(sid2, FAIL, "H5Screate_simple"); /* Create an attribute for the dataset */ attr=H5Acreate(dataset,ATTR1_NAME,H5T_NATIVE_INT32,sid2,H5P_DEFAULT); CHECK(attr, FAIL, "H5Acreate"); /* Try to create the same attribute again (should fail) */ ret=H5Acreate(dataset,ATTR1_NAME,H5T_NATIVE_INT32,sid2,H5P_DEFAULT); VERIFY(ret, FAIL, "H5Acreate"); /* Write attribute information */ ret=H5Awrite(attr,H5T_NATIVE_INT32,&attr_data1); CHECK(ret, FAIL, "H5Awrite"); /* Close attribute */ ret=H5Aclose(attr); CHECK(ret, FAIL, "H5Aclose"); ret = H5Sclose(sid1); CHECK(ret, FAIL, "H5Sclose"); ret = H5Sclose(sid2); CHECK(ret, FAIL, "H5Sclose"); /* Close Dataset */ ret = H5Dclose(dataset); CHECK(ret, FAIL, "H5Dclose"); /* Create group */ group = H5Gcreate(fid1, GROUP1_NAME, 0); CHECK(group, FAIL, "H5Gcreate"); /* Create dataspace for attribute */ sid2 = H5Screate_simple(ATTR2_RANK, dims3, NULL); CHECK(sid2, FAIL, "H5Screate_simple"); /* Create an attribute for the group */ attr=H5Acreate(group,ATTR2_NAME,H5T_NATIVE_INT32,sid2,H5P_DEFAULT); CHECK(attr, FAIL, "H5Acreate"); /* Try to create the same attribute again (should fail) */ ret=H5Acreate(group,ATTR2_NAME,H5T_NATIVE_INT32,sid2,H5P_DEFAULT); VERIFY(ret, FAIL, "H5Acreate"); /* Write attribute information */ ret=H5Awrite(attr,H5T_NATIVE_INT32,&attr_data2); CHECK(ret, FAIL, "H5Awrite"); /* Close attribute */ ret=H5Aclose(attr); CHECK(ret, FAIL, "H5Aclose"); /* Close Attribute dataspace */ ret = H5Sclose(sid2); CHECK(ret, FAIL, "H5Sclose"); /* Close Group */ ret = H5Gclose(group); CHECK(ret, FAIL, "H5Gclose"); /* Close file */ ret = H5Fclose(fid1); CHECK(ret, FAIL, "H5Fclose"); } /* test_attr_basic_write() */ /**************************************************************** ** ** test_attr_basic_read(): Test basic H5A (attribute) code. ** ****************************************************************/ static void test_attr_basic_read(void) { hid_t fid1; /* HDF5 File IDs */ hid_t dataset; /* Dataset ID */ hid_t group; /* Group ID */ hid_t attr; /* Attribute ID */ int32 read_data1[ATTR1_DIM1]={0}; /* Buffer for reading 1st attribute */ int32 read_data2[ATTR2_DIM1][ATTR2_DIM2]={{0}}; /* Buffer for reading 2nd attribute */ int i,j; herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(5, ("Testing Basic Attribute Functions\n")); /* Create file */ fid1 = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(fid1, FAIL, "H5Fopen"); /* Open the dataset */ dataset=H5Dopen(fid1,"Dataset1"); /* Verify the correct number of attributes */ ret=H5Anum_attrs(dataset); VERIFY(ret, 1, "H5Anum_attrs"); /* Open an attribute for the dataset */ attr=H5Aopen_name(dataset,ATTR1_NAME); CHECK(attr, FAIL, "H5Aopen_name"); /* Read attribute information */ ret=H5Aread(attr,H5T_NATIVE_INT32,&read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i