summaryrefslogtreecommitdiffstats
path: root/c++/test/tfile.cpp
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2013-09-29 04:34:31 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2013-09-29 04:34:31 (GMT)
commit5478de764594d5c30bf8cd53076892f0aa60543c (patch)
tree558d9e524e5f2e84026ebdfeb9960a5b8962c06d /c++/test/tfile.cpp
parent252ed47f9b0333a112a49a5b23a1cc4a5e108b1e (diff)
downloadhdf5-5478de764594d5c30bf8cd53076892f0aa60543c.zip
hdf5-5478de764594d5c30bf8cd53076892f0aa60543c.tar.gz
hdf5-5478de764594d5c30bf8cd53076892f0aa60543c.tar.bz2
[svn-r24216] Purpose: Merged changes from trunk
Description from trunk, r22836 In this bug, H5File doesn't have the ability to create attribute. The following changes will provide that functionality and several others that were also missing: - Added an abstract class H5Location in between IdComponent and H5Object. - New class structure of IdComponent, H5Location, H5Object, H5File IdComponent | H5Location / \ H5Object H5File - Wrappers in H5Object were moved to H5Location because the related C functions take either file, group, dataset, or named datatype ID. - Added wrapper for H5Rget_obj_type2 - Added tests for file attributes and H5Rget_obj_type2 wrapper Description from trunk, r22845 Fixed miscellaneous inconsistencies and typos, which also took care of the failure in Packet Table test on daily test today. Description from trunk, r24143 Fixed comments, documentation, and mis-matched DOXYGEN_SHOULD_SKIP_THIS pairs. Description from trunk, r24188 - The failure in daily test was caused by missing initialization of member "id" in a few constructors. This is now fixed. - Added two overloaded H5Location::setComment - Improved some error reporting in H5Location - Improved error reporting in tests Description from trunk, r24189 Changed header guards from single underscore to double underscore. Platforms tested: SunOS 5.11 (emu) Linux/32 2.6 (jam) Linux/64 2.6 (koala)/PGI compilers
Diffstat (limited to 'c++/test/tfile.cpp')
-rw-r--r--c++/test/tfile.cpp113
1 files changed, 112 insertions, 1 deletions
diff --git a/c++/test/tfile.cpp b/c++/test/tfile.cpp
index 765168c..65b7f11 100644
--- a/c++/test/tfile.cpp
+++ b/c++/test/tfile.cpp
@@ -140,7 +140,7 @@ static void test_file_create()
// Test create with H5F_ACC_TRUNC. This will truncate the existing file.
file1 = new H5File (FILE1, H5F_ACC_TRUNC);
- // Try to truncate first file again. This should fail because file1
+ // Try to create first file again. This should fail because file1
// is the same file and is currently open. Skip it on OpenVMS because
// it creates another version of the file.
#ifndef H5_HAVE_FILE_VERSIONS
@@ -492,6 +492,116 @@ static void test_file_name()
} // test_file_name()
+#define NUM_OBJS 4
+#define NUM_ATTRS 3
+const int RANK1 = 1;
+const int ATTR1_DIM1 = 3;
+const H5std_string FILE5("tfattrs.h5");
+const H5std_string FATTR1_NAME ("file attribute 1");
+const H5std_string FATTR2_NAME ("file attribute 2");
+int fattr_data[ATTR1_DIM1]={512,-234,98123}; /* Test data for file attribute */
+int dattr_data[ATTR1_DIM1]={256,-123,1000}; /* Test data for dataset attribute */
+static void test_file_attribute()
+{
+ int rdata[ATTR1_DIM1];
+ int i;
+
+ // Output message about test being performed
+ SUBTEST("File Attribute");
+
+ H5std_string file_name;
+ try {
+ // Create a file using default properties.
+ H5File file5(FILE5, H5F_ACC_TRUNC);
+
+ // Create the data space
+ hsize_t dims[RANK1] = {ATTR1_DIM1};
+ DataSpace space(RANK1, dims);
+
+ // Create two attributes for the file
+ Attribute fattr1(file5.createAttribute(FATTR1_NAME, PredType::NATIVE_FLOAT, space));
+ Attribute fattr2(file5.createAttribute(FATTR2_NAME, PredType::NATIVE_INT, space));
+
+ fattr2.write(PredType::NATIVE_INT, fattr_data);
+
+ try {
+ // Try to create the same attribute again (should fail)
+ Attribute fattr_dup(file5.createAttribute(FATTR2_NAME, PredType::NATIVE_INT, space));
+ // Should FAIL but didn't, so throw an invalid action exception
+ throw InvalidActionException("H5File createAttribute", "Attempted to create an existing attribute.");
+ }
+ catch( AttributeIException E ) // catch creating existing attribute
+ {} // do nothing, FAIL expected
+
+ // Create a new dataset
+ DataSet dataset(file5.createDataSet (DSETNAME, PredType::NATIVE_INT, space));
+
+ // Create an attribute for the dataset
+ Attribute dattr(dataset.createAttribute(DATTRNAME, PredType::NATIVE_INT, space));
+
+ // Write data to the second file attribute
+ dattr.write(PredType::NATIVE_INT, dattr_data);
+
+ // Test flushing out the data from the attribute object
+ dattr.flush(H5F_SCOPE_GLOBAL);
+
+ // Get and verify the number of all objects in the file
+ // Current: 1 file, 2 file attr, 1 ds, and 1 ds attr.
+ ssize_t num_objs = file5.getObjCount(H5F_OBJ_ALL);
+ verify_val(num_objs, 5, "H5File::getObjCount", __LINE__, __FILE__);
+
+ num_objs = file5.getObjCount(H5F_OBJ_GROUP);
+ verify_val(num_objs, 0, "H5File::getObjCount(H5F_OBJ_GROUP)", __LINE__, __FILE__);
+ num_objs = file5.getObjCount(H5F_OBJ_DATASET);
+ verify_val(num_objs, 1, "H5File::getObjCount(H5F_OBJ_DATASET)", __LINE__, __FILE__);
+ num_objs = file5.getObjCount(H5F_OBJ_ATTR);
+ verify_val(num_objs, 3, "H5File::getObjCount(H5F_OBJ_ATTR)", __LINE__, __FILE__);
+ num_objs = file5.getObjCount(H5F_OBJ_DATATYPE);
+ verify_val(num_objs, 0, "H5File::getObjCount(H5F_OBJ_DATATYPE)", __LINE__, __FILE__);
+ num_objs = file5.getObjCount(H5F_OBJ_FILE);
+ verify_val(num_objs, 1, "H5File::getObjCount(H5F_OBJ_FILE)", __LINE__, __FILE__);
+
+ // Get the file name using the attributes
+ H5std_string fname = fattr1.getFileName();
+ verify_val(fname, FILE5, "H5File::getFileName()", __LINE__, __FILE__);
+
+ fname.clear();
+ fname = dattr.getFileName();
+ verify_val(fname, FILE5, "H5File::getFileName()", __LINE__, __FILE__);
+
+ // Get the class of a file attribute's datatype
+ H5T_class_t atclass = fattr1.getTypeClass();
+ verify_val(atclass, H5T_FLOAT, "Attribute::getTypeClass()", __LINE__, __FILE__);
+
+ // Get and verify the number of attributes attached to a file
+ int n_attrs = file5.getNumAttrs();
+ verify_val(n_attrs, 2, "H5File::getNumAttrs()", __LINE__, __FILE__);
+
+ // Get and verify the number of attributes attached to a dataset
+ n_attrs = 0;
+ n_attrs = dataset.getNumAttrs();
+ verify_val(n_attrs, 1, "DataSet::getNumAttrs()", __LINE__, __FILE__);
+
+ // Read back attribute's data
+ HDmemset(rdata, 0, sizeof(rdata));
+ dattr.read(PredType::NATIVE_INT, rdata);
+ /* Check results */
+ for (i = 0; i < ATTR1_DIM1; i++) {
+ if (rdata[i] != dattr_data[i]) {
+ H5_FAILED();
+ cerr << endl;
+ cerr << "element [" << i << "] is " << rdata[i] <<
+ "but should have been " << dattr_data[i] << endl;
+ }
+ }
+ PASSED();
+ } // end of try block
+
+ catch (Exception E) {
+ issue_fail_msg("test_file_name()", __LINE__, __FILE__, E.getCDetailMsg());
+ }
+} // test_file_attribute()
+
/*-------------------------------------------------------------------------
* Function: test_file
*
@@ -518,6 +628,7 @@ void test_file()
test_file_open(); // Test file opening
test_file_size(); // Test file size
test_file_name(); // Test getting file's name
+ test_file_attribute(); // Test file attribute feature
} // test_file()