/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /*********************************************************** * * Test program: tattr * * Test the attribute functionality * *************************************************************/ /* JAMES: try writing a second value to an existing shared attribute. * Does modifying attributes work? */ #include "testhdf5.h" #include "h5test.h" #include "hdf5.h" /* * This file needs to access private information from the H5G package. * This file also needs to access the group testing code. */ #define H5O_PACKAGE #define H5O_TESTING #include "H5Opkg.h" /* Object headers */ #define FILENAME "tattr.h5" #define NAME_BUF_SIZE 1024 #define ATTR_NAME_LEN 16 #define ATTR_MAX_DIMS 7 #define ATTR_TMP_NAME "temp_name" /* 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 /* Dataset Information */ #define DSET1_NAME "Dataset1" #define DSET2_NAME "Dataset2" /* Group Information */ #define GROUP1_NAME "/Group1" /* Named Datatype Information */ #define TYPE1_NAME "/Type" /* Attribute Rank & Dimensions */ #define ATTR1_NAME "Attr1" #define ATTR1_RANK 1 #define ATTR1_DIM1 3 int attr_data1[ATTR1_DIM1]={512,-234,98123}; /* Test data for 1st attribute */ /* rank & dimensions for another attribute */ #define ATTR1A_NAME "Attr1_a" int attr_data1a[ATTR1_DIM1]={256,11945,-22107}; #define ATTR2_NAME "Attr2" #define ATTR2_RANK 2 #define ATTR2_DIM1 2 #define ATTR2_DIM2 2 int 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 { int 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 */ #define ATTR5_NAME "Attr5" #define ATTR5_RANK 0 float attr_data5=(float)-5.123; /* Test data for 5th attribute */ #ifndef QAK herr_t 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(hid_t fapl) { 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, attr2; /* Attribute ID */ hsize_t attr_size; /* storage size for attribute */ ssize_t attr_name_size; /* size of attribute name */ char *attr_name=NULL; /* name of attribute */ hsize_t dims1[] = {SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3}; hsize_t dims2[] = {ATTR1_DIM1}; hsize_t dims3[] = {ATTR2_DIM1,ATTR2_DIM2}; int read_data1[ATTR1_DIM1]={0}; /* Buffer for reading 1st attribute */ int i; herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(5, ("Testing Basic Scalar Attribute Writing Functions\n")); /* Create file */ fid1 = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); 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,DSET1_NAME,H5T_NATIVE_UCHAR,sid1,H5P_DEFAULT); CHECK(dataset, FAIL, "H5Dcreate"); /* Create dataspace for attribute */ sid2 = H5Screate_simple(ATTR1_RANK, dims2, NULL); CHECK(sid2, FAIL, "H5Screate_simple"); /* Try to create an attribute on the file (should fail) */ ret=H5Acreate(fid1,ATTR1_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); VERIFY(ret, FAIL, "H5Acreate"); /* Create an attribute for the dataset */ attr=H5Acreate(dataset,ATTR1_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); CHECK(attr, FAIL, "H5Acreate"); /* Try to create the same attribute again (should fail) */ ret=H5Acreate(dataset,ATTR1_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); VERIFY(ret, FAIL, "H5Acreate"); /* Write attribute information */ ret=H5Awrite(attr,H5T_NATIVE_INT,attr_data1); CHECK(ret, FAIL, "H5Awrite"); /* Create an another attribute for the dataset */ attr2=H5Acreate(dataset,ATTR1A_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); CHECK(attr, FAIL, "H5Acreate"); /* Write attribute information */ ret=H5Awrite(attr2,H5T_NATIVE_INT,attr_data1a); CHECK(ret, FAIL, "H5Awrite"); /* Check storage size for attribute */ attr_size=H5Aget_storage_size(attr); VERIFY(attr_size, (ATTR1_DIM1*sizeof(int)), "H5A_get_storage_size"); /* Read attribute information immediately, without closing attribute */ ret=H5Aread(attr,H5T_NATIVE_INT,read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i0) attr_name = (char*)HDcalloc((size_t)(attr_name_size+1), sizeof(char)); ret=(herr_t)H5Aget_name(attr, (size_t)(attr_name_size+1), attr_name); CHECK(ret, FAIL, "H5Aget_name"); ret=HDstrcmp(attr_name, ATTR_TMP_NAME); VERIFY(ret, 0, "HDstrcmp"); if(attr_name) HDfree(attr_name); /* Read attribute information immediately, without closing attribute */ ret=H5Aread(attr,H5T_NATIVE_INT,read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i0) attr_name = (char*)HDcalloc((size_t)(attr_name_size+1), sizeof(char)); ret=(herr_t)H5Aget_name(attr2, (size_t)(attr_name_size+1), attr_name); CHECK(ret, FAIL, "H5Aget_name"); ret=HDstrcmp(attr_name, ATTR1A_NAME); VERIFY(ret, 0, "HDstrcmp"); if(attr_name) HDfree(attr_name); /* Read attribute information immediately, without closing attribute */ ret=H5Aread(attr2,H5T_NATIVE_INT,read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i= min_dense; u--) { /* Delete attribute */ sprintf(attrname, "attr %02u", u); ret = H5Adelete(dataset, attrname); CHECK(ret, FAIL, "H5Adelete"); } /* end for */ /* Check on dataset's attribute storage status */ is_dense = H5O_is_attr_dense_test(dataset); VERIFY(is_dense, TRUE, "H5O_is_attr_dense_test"); /* Delete one more attribute, which should cause reversion to compact storage */ sprintf(attrname, "attr %02u", u); ret = H5Adelete(dataset, attrname); CHECK(ret, FAIL, "H5Adelete"); /* Close Dataset */ ret = H5Dclose(dataset); CHECK(ret, FAIL, "H5Dclose"); /* Close file */ ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); } /* test_attr_dense_delete() */ /**************************************************************** ** ** test_attr(): Main H5A (attribute) testing routine. ** ****************************************************************/ void test_attr(void) { hid_t fapl = (-1), fapl2 = (-1); /* File access property lists */ hid_t fcpl = (-1), fcpl2 = (-1); /* File creation property lists */ hbool_t new_format; /* Whether to use the new format or not */ hbool_t use_shared; /* Whether to use shared attributes or not */ herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(5, ("Testing Attributes\n")); /* Create a default file access property list */ fapl = H5Pcreate(H5P_FILE_ACCESS); CHECK(fapl, FAIL, "H5Pcreate"); /* Copy the file access property list */ fapl2 = H5Pcopy(fapl); CHECK(fapl2, FAIL, "H5Pcopy"); /* Set the "use the latest version of the format" flag for creating objects in the file */ ret = H5Pset_latest_format(fapl2, TRUE); CHECK(ret, FAIL, "H5Pset_latest_format"); #ifndef QAK /* Loop over using new group format */ for(new_format = FALSE; new_format <= TRUE; new_format++) { hid_t my_fapl; /* Set the FAPL for the type of format */ if(new_format) { MESSAGE(7, ("testing with new file format\n")); my_fapl = fapl2; } /* end if */ else { MESSAGE(7, ("testing with old file format\n")); my_fapl = fapl; } /* end else */ /* These next two tests use the same file information */ test_attr_basic_write(my_fapl); /* Test basic H5A writing code */ test_attr_basic_read(my_fapl); /* Test basic H5A reading code */ test_attr_flush(my_fapl); /* Test H5A I/O in the presence of H5Fflush calls */ /* This next test uses the same file information */ test_attr_plist(my_fapl); /* Test attribute property lists */ /* These next two tests use the same file information */ test_attr_compound_write(my_fapl); /* Test complex datatype H5A writing code */ test_attr_compound_read(my_fapl); /* Test complex datatype H5A reading code */ /* These next two tests use the same file information */ test_attr_scalar_write(my_fapl); /* Test scalar dataspace H5A writing code */ test_attr_scalar_read(my_fapl); /* Test scalar dataspace H5A reading code */ /* These next four tests use the same file information */ test_attr_mult_write(my_fapl); /* Test H5A writing code for multiple attributes */ test_attr_mult_read(my_fapl); /* Test H5A reading code for multiple attributes */ test_attr_iterate(my_fapl); /* Test H5A iterator code */ test_attr_delete(my_fapl); /* Test H5A code for deleting attributes */ /* This next test use the same file information */ test_attr_dtype_shared(my_fapl); /* Test using shared dataypes in attributes */ } /* end for */ /* Create a default file creation property list */ fcpl = H5Pcreate(H5P_FILE_CREATE); CHECK(fcpl, FAIL, "H5Pcreate"); /* Copy the file creation property list */ fcpl2 = H5Pcopy(fcpl); CHECK(fcpl2, FAIL, "H5Pcopy"); /* Make attributes > 1 byte shared (i.e. all of them :-) */ ret = H5Pset_shared_mesg_nindexes(fcpl2, (unsigned)1); CHECK_I(ret, "H5Pset_shared_mesg_nindexes"); ret = H5Pset_shared_mesg_index(fcpl2, (unsigned)1, H5O_MESG_ATTR_FLAG, (unsigned)1); CHECK_I(ret, "H5Pset_shared_mesg_index"); /* Tests on "new format" attribute storage */ /* Loop over using shared attributes */ for(use_shared = FALSE; use_shared <= TRUE; use_shared++) { hid_t my_fcpl; /* Set the FCPL for shared or not */ if(use_shared) { MESSAGE(7, ("testing with shared attributes\n")); my_fcpl = fcpl2; } /* end if */ else { MESSAGE(7, ("testing without shared attributes\n")); my_fcpl = fcpl; } /* end else */ test_attr_dense_create(my_fcpl, fapl2); /* Test dense attribute storage creation */ test_attr_dense_open(my_fcpl, fapl2); /* Test opening attributes in dense storage */ test_attr_dense_delete(my_fcpl, fapl2); /* Test deleting attributes in dense storage */ } /* end for */ #else /* QAK */ HDfprintf(stderr, "Uncomment tests!\n"); #endif /* QAK */ /* Close FCPLs */ ret = H5Pclose(fcpl); CHECK(ret, FAIL, "H5Pclose"); ret = H5Pclose(fcpl2); CHECK(ret, FAIL, "H5Pclose"); /* Close FAPLs */ ret = H5Pclose(fapl); CHECK(ret, FAIL, "H5Pclose"); ret = H5Pclose(fapl2); CHECK(ret, FAIL, "H5Pclose"); } /* test_attr() */ /*------------------------------------------------------------------------- * Function: cleanup_attr * * Purpose: Cleanup temporary test files * * Return: none * * Programmer: Albert Cheng * July 2, 1998 * * Modifications: * *------------------------------------------------------------------------- */ void cleanup_attr(void) { remove(FILENAME); }