/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /***************************************************************************** FILE tattr.cpp - HDF5 C++ testing the functionalities associated with the C attribute interface (H5A) ***************************************************************************/ #ifdef OLD_HEADER_FILENAME #include #else #include #endif #include #ifndef H5_NO_NAMESPACE #ifndef H5_NO_STD using std::cerr; using std::endl; #endif // H5_NO_STD #endif #include "testhdf5.h" // C test header file #include "H5Cpp.h" // C++ API header file #ifndef H5_NO_NAMESPACE using namespace H5; #endif #include "h5cpputil.h" // C++ utilility header file const H5std_string FILENAME("tattr.h5"); const size_t ATTR_MAX_DIMS = 7; const H5std_string ATTR_TMP_NAME("temp_name"); /* 3-D dataset with fixed dimensions */ const H5std_string SPACE1_NAME("Space1"); const int SPACE1_RANK = 3; const int SPACE1_DIM1 = 3; const int SPACE1_DIM2 = 15; const int SPACE1_DIM3 = 13; /* Dataset Information */ const H5std_string DSET1_NAME("Dataset1"); const H5std_string DSET2_NAME("Dataset2"); /* Group Information */ const H5std_string GROUP1_NAME("/Group1"); /* Named Datatype Information */ const H5std_string TYPE1_NAME("/Type"); /* Attribute Rank & Dimensions */ const H5std_string ATTR1_NAME("Attr1"); const int ATTR1_RANK = 1; const int ATTR1_DIM1 = 3; int attr_data1[ATTR1_DIM1]={512,-234,98123}; /* Test data for 1st attribute */ const H5std_string ATTR2_NAME("Attr2"); const int ATTR2_RANK = 2; const int ATTR2_DIM1 = 2; const int ATTR2_DIM2 = 2; int attr_data2[ATTR2_DIM1][ATTR2_DIM2]={{7614,-416},{197814,-3}}; /* Test data for 2nd attribute */ const H5std_string ATTR3_NAME("Attr3"); const int ATTR3_RANK = 3; const hsize_t ATTR3_DIM1 = 2; const hsize_t ATTR3_DIM2 = 2; const hsize_t 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 */ const H5std_string ATTR4_NAME("Attr4"); const int ATTR4_RANK = 2; const hsize_t ATTR4_DIM1 = 2; const hsize_t ATTR4_DIM2 = 2; const H5std_string ATTR4_FIELDNAME1("i"); const H5std_string ATTR4_FIELDNAME2("d"); const H5std_string 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 const H5std_string ATTR5_NAME("Attr5"); const int ATTR5_RANK = 0; float attr_data5 = (float)-5.123; // Test data for 5th attribute /* Info for another attribute */ const H5std_string ATTR1A_NAME("Attr1_a"); int attr_data1a[ATTR1_DIM1]={256,11945,-22107}; /**************************************************************** ** ** test_attr_basic_write(): Test basic write attribute. ** Tests integer attributes on both datasets and groups ** ****************************************************************/ static void test_attr_basic_write(void) { 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; // Output message about test being performed MESSAGE(5, ("Testing Basic Attribute Writing Functions\n")); try { // Create file H5File fid1 (FILENAME, H5F_ACC_TRUNC); // Create dataspace for dataset DataSpace ds_space (SPACE1_RANK, dims1); // Create a dataset DataSet dataset = fid1.createDataSet(DSET1_NAME, PredType::NATIVE_UCHAR, ds_space); // Create dataspace for attribute DataSpace att_space (ATTR1_RANK, dims2); // Create an attribute for the dataset Attribute ds_attr1 = dataset.createAttribute (ATTR1_NAME, PredType::NATIVE_INT, att_space); // Try creating an attribute that already exists. This should fail // since two attributes cannot have the same name. If an exception // is not thrown for this action by createAttribute, then throw an // invalid action exception. try { Attribute invalid_attr = dataset.createAttribute (ATTR1_NAME, PredType::NATIVE_INT, att_space); // continuation here, that means no exception has been thrown throw InvalidActionException("H5File::createDataSet", "Library allowed overwrite of existing dataset"); } catch (AttributeIException E) // catching invalid creating attribute {} // do nothing, exception expected // Write attribute information ds_attr1.write (PredType::NATIVE_INT, attr_data1); // Read attribute information immediately, without closing attribute ds_attr1.read (PredType::NATIVE_INT, read_data1); // Verify values read in for(i=0; iopenAttribute(ATTR1_NAME)); // Read data from the attribute attr2->read(PredType::NATIVE_INT, &rdata); verify_val(data, rdata, "Attribute::read", __LINE__, __FILE__); // Close attribute and dataset delete attr2; delete dset2; // Check reference count on named datatype fid1.getObjinfo(TYPE1_NAME, statbuf); verify_val((int)statbuf.nlink, 3, "DataSet::openAttribute", __LINE__, __FILE__); // Unlink the dataset fid1.unlink(DSET1_NAME); // Check reference count on named datatype fid1.getObjinfo(TYPE1_NAME, statbuf); verify_val((int)statbuf.nlink, 1, "H5File::unlink", __LINE__, __FILE__); // Unlink the named datatype fid1.unlink(TYPE1_NAME); // Close file fid1.close(); // Check size of file filesize=h5_get_file_size(FILENAME.c_str()); verify_val((long)filesize, (long)empty_filesize, "Checking file size", __LINE__, __FILE__); } // end try block catch (Exception E) { issue_fail_msg(E.getCFuncName(), __LINE__, __FILE__, E.getCDetailMsg()); } } /* test_attr_dtype_shared() */ /**************************************************************** ** ** test_string_attr(): Test read/write string attribute. ** Tests string attributes on groups. ** ****************************************************************/ /* Info for a string attribute */ const H5std_string ATTRSTR_NAME("String_attr"); const H5std_string ATTRSTR_DATA("String Attribute"); static void test_string_attr(void) { // Output message about test being performed MESSAGE(5, ("Testing Basic Attribute Writing Functions\n")); try { // Create file H5File fid1(FILENAME, H5F_ACC_RDWR); // Create a variable length string datatype to refer to. StrType type(0, H5T_VARIABLE); // Open the root group. Group root = fid1.openGroup("/"); // Create dataspace for the attribute. DataSpace att_space (H5S_SCALAR); // Create an attribute for the root group. Attribute gr_attr = root.createAttribute(ATTRSTR_NAME, type, att_space); // Write data to the attribute. gr_attr.write(type, ATTRSTR_DATA); // Read and verify the attribute string as a string of chars. char *string_att_check; gr_attr.read(type, &string_att_check); if(HDstrcmp(string_att_check, ATTRSTR_DATA.c_str())!=0) TestErrPrintf("Line %d: Attribute data different: ATTRSTR_DATA=%s,string_att_check=%s\n",__LINE__, ATTRSTR_DATA.c_str(), string_att_check); // Read and verify the attribute string as an std::string. H5std_string read_str; gr_attr.read(type, read_str); if (read_str != ATTRSTR_DATA) TestErrPrintf("Line %d: Attribute data different: ATTRSTR_DATA=%s,read_str=%s\n",__LINE__, ATTRSTR_DATA.c_str(), read_str.c_str()); } // end try block catch (Exception E) { issue_fail_msg(E.getCFuncName(), __LINE__, __FILE__, E.getCDetailMsg()); } } /* test_string_attr() */ /**************************************************************** ** ** test_attr(): Main attribute testing routine. ** ****************************************************************/ #ifdef __cplusplus extern "C" #endif void test_attr(void) { // Output message about test being performed MESSAGE(5, ("Testing Attributes\n")); test_attr_basic_write(); // Test basic H5A writing code test_attr_rename(); // Test renaming attribute test_attr_basic_read(); // Test basic H5A reading code test_attr_compound_write(); // Test complex datatype H5A writing code test_attr_compound_read(); // Test complex datatype H5A reading code test_attr_scalar_write(); // Test scalar dataspace H5A writing code test_attr_scalar_read(); // Test scalar dataspace H5A reading code test_attr_mult_write(); // Test writing multiple attributes test_attr_mult_read(); // Test reading multiple attributes test_attr_delete(); // Test deleting attributes test_attr_dtype_shared(); // Test using shared datatypes in attributes test_string_attr(); // Test read/write string attribute } /* 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.c_str()); }