diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2018-05-10 18:04:28 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2018-05-10 18:04:28 (GMT) |
commit | c32b7387083d574bfa5fc31fffc45cbf21b6a1f0 (patch) | |
tree | 665af0c032ce6dda6d110da87e4a66d05b016bb2 /c++/test | |
parent | ffd98d490f04952e01629c6378c4528459db9f9a (diff) | |
download | hdf5-c32b7387083d574bfa5fc31fffc45cbf21b6a1f0.zip hdf5-c32b7387083d574bfa5fc31fffc45cbf21b6a1f0.tar.gz hdf5-c32b7387083d574bfa5fc31fffc45cbf21b6a1f0.tar.bz2 |
C++ API improvement
Description:
- Reorganized some exception classes to reflect the HDF5 object
hierarchy and provide extendibility.
DataSetIException -> LocationException -> Exception
DataTypeIException -> LocationException -> Exception
GroupIException -> LocationException -> Exception
AttributeIException -> LocationException -> Exception
FileIException -> GroupIException -> LocationException -> Exception
- Added throwException() to these subclasses and use it in H5Location
and H5Object member functions to throw an exception that is specific
to the object that invokes the member function. Applications that
catch the base exception can continue to do the same.
- Many cleanup for inconsistencies in comments/headers.
Platforms tested:
Linux/64 (jelly)
Linux/64 (platypus)
Darwin (osx1010test)
Diffstat (limited to 'c++/test')
-rw-r--r-- | c++/test/dsets.cpp | 19 | ||||
-rw-r--r-- | c++/test/tattr.cpp | 12 | ||||
-rw-r--r-- | c++/test/tfile.cpp | 14 | ||||
-rw-r--r-- | c++/test/tlinks.cpp | 20 | ||||
-rw-r--r-- | c++/test/tobject.cpp | 45 |
5 files changed, 72 insertions, 38 deletions
diff --git a/c++/test/dsets.cpp b/c++/test/dsets.cpp index 8d2618b..0a187ef 100644 --- a/c++/test/dsets.cpp +++ b/c++/test/dsets.cpp @@ -68,6 +68,8 @@ static size_t filter_bogus(unsigned int flags, size_t cd_nelmts, * *------------------------------------------------------------------------- */ +const H5std_string DSET_COMMENT ("This is a dataset"); +const H5std_string NON_EXISTING_DSET ("does_not_exist"); static herr_t test_create( H5File& file) { @@ -83,13 +85,12 @@ test_create( H5File& file) DataSpace space (2, dims, NULL); // Create a dataset using the default dataset creation properties. - // We're not sure what they are, so we won't check. dataset = new DataSet (file.createDataSet (DSET_DEFAULT_NAME, PredType::NATIVE_DOUBLE, space)); // Add a comment to the dataset - file.setComment (DSET_DEFAULT_NAME, "This is a dataset"); + file.setComment (DSET_DEFAULT_NAME, DSET_COMMENT); // Close the dataset delete dataset; @@ -120,7 +121,7 @@ test_create( H5File& file) // Get and verify the comment from this dataset, using // H5std_string getComment(const H5std_string& name, <buf_size=0, by default>) H5std_string comment = file.getComment(DSET_DEFAULT_NAME); - verify_val(comment, "This is a dataset", "DataSet::getComment", __LINE__, __FILE__); + verify_val(comment, DSET_COMMENT, "DataSet::getComment", __LINE__, __FILE__); // Close the dataset when accessing is completed delete dataset; @@ -132,24 +133,24 @@ test_create( H5File& file) // exception is not thrown for this action by openDataSet, then // display failure information and throw an exception. try { - dataset = new DataSet (file.openDataSet( "does_not_exist" )); + dataset = new DataSet (file.openDataSet(NON_EXISTING_DSET)); // continuation here, that means no exception has been thrown throw InvalidActionException("H5File::openDataSet", "Attempted to open a non-existent dataset"); } - catch (FileIException& E ) // catching creating non-existent dataset + catch (FileIException& E ) // catching opening non-existent dataset {} // do nothing, exception expected - // Create a new dataset that uses chunked storage instead of the default - // layout. + // Create a new dataset that uses chunked storage instead of the + // default layout. DSetCreatPropList create_parms; - hsize_t csize[2]; + hsize_t csize[2]; csize[0] = 5; csize[1] = 100; create_parms.setChunk( 2, csize ); dataset = new DataSet (file.createDataSet - (DSET_CHUNKED_NAME, PredType::NATIVE_DOUBLE, space, create_parms)); + (DSET_CHUNKED_NAME, PredType::NATIVE_DOUBLE, space, create_parms)); // Note: this one has no error message in C when failure occurs? // clean up and return with success diff --git a/c++/test/tattr.cpp b/c++/test/tattr.cpp index d97d478..25b5ff8 100644 --- a/c++/test/tattr.cpp +++ b/c++/test/tattr.cpp @@ -653,7 +653,8 @@ static void test_attr_compound_read() verify_val((long)dims[1], (long)ATTR4_DIM2, "DataSpace::getSimpleExtentDims",__LINE__, __FILE__); // Get the class of the datatype that is used by attr - H5T_class_t type_class = attr.getTypeClass(); + H5T_class_t type_class; + type_class = attr.getTypeClass(); // Verify that the type is of compound datatype verify_val(type_class, H5T_COMPOUND, "Attribute::getTypeClass", __LINE__, __FILE__); @@ -1289,6 +1290,7 @@ static void test_attr_dtype_shared() // Retrieve and verify information about the type H5O_info_t oinfo; + dtype.getObjectInfo(TYPE1_NAME, &oinfo); fid1.getObjectInfo(TYPE1_NAME, &oinfo); if (oinfo.type != H5O_TYPE_NAMED_DATATYPE) TestErrPrintf("Line %d: object type wrong!\n", __LINE__); @@ -1402,6 +1404,14 @@ static void test_attr_dtype_shared() PASSED(); } // end try block + catch (DataTypeIException& E) + { + issue_fail_msg("test_attr_dtype_shared()", __LINE__, __FILE__, E.getCDetailMsg()); + } + catch (FileIException& E) + { + issue_fail_msg("test_attr_dtype_shared()", __LINE__, __FILE__, E.getCDetailMsg()); + } catch (Exception& E) { issue_fail_msg("test_attr_dtype_shared()", __LINE__, __FILE__, E.getCDetailMsg()); diff --git a/c++/test/tfile.cpp b/c++/test/tfile.cpp index 23cf280..d5278d5 100644 --- a/c++/test/tfile.cpp +++ b/c++/test/tfile.cpp @@ -497,15 +497,10 @@ static void test_file_name() comp_type.getFileName(); verify_val(file_name, FILE4, "CompType::getFileName", __LINE__, __FILE__); - /* file4.close(); -file4.openFile(FILE4, H5F_ACC_RDWR); - */ // Get the file's version information. H5F_info_t finfo; - file4.getFileInfo(finfo); // there's no C test for H5Fget_info - /* cerr << "file4: super_ext_size = " << finfo.super_ext_size << endl; - */ - //verify_val(finfo.sohm.hdr_size, 0, "H5File::getFileInfo", __LINE__, __FILE__); + file4.getFileInfo(finfo); + verify_val(finfo.sohm.hdr_size, 0, "H5File::getFileInfo", __LINE__, __FILE__); PASSED(); } // end of try block @@ -642,6 +637,11 @@ static void test_file_attribute() PASSED(); } // end of try block + // Catch creating existing attribute + catch (AttributeIException& E) + {} // do nothing, exception expected + + // Catch all other exceptions catch (Exception& E) { issue_fail_msg("test_file_attribute()", __LINE__, __FILE__, E.getCDetailMsg()); diff --git a/c++/test/tlinks.cpp b/c++/test/tlinks.cpp index b38ed39..356bc1b 100644 --- a/c++/test/tlinks.cpp +++ b/c++/test/tlinks.cpp @@ -326,11 +326,9 @@ static const char *FILENAME[] = { * Purpose: Test building a file with assorted links. * * Return: Success: 0 - * * Failure: -1 * - * Programmer: Binh-Minh Ribler - * October 16, 2009 + * October, 2009 * *------------------------------------------------------------------------- */ @@ -369,7 +367,6 @@ static void test_basic_links(hid_t fapl_id, hbool_t new_format) // Because these are not implemented in the C++ API yet, they are // used so CommonFG::getLinkval can be tested. - // Create a hard link if(H5Lcreate_hard( file_id, "dset1", H5L_SAME_LOC, "grp1/hard1", H5P_DEFAULT, H5P_DEFAULT) < 0) @@ -433,7 +430,7 @@ static void test_basic_links(hid_t fapl_id, hbool_t new_format) { issue_fail_msg("test_basic_links()", __LINE__, __FILE__, E.getCDetailMsg()); } -} +} // test_basic_links /*------------------------------------------------------------------------- * Function: test_num_links @@ -441,12 +438,9 @@ static void test_basic_links(hid_t fapl_id, hbool_t new_format) * Purpose: Test setting and getting limit of number of links * * Return: Success: 0 - * * Failure: -1 * - * Programmer: Binh-Minh Ribler - * Mar, 2017 - * + * March, 2017 *------------------------------------------------------------------------- */ static void test_num_links(hid_t fapl_id, hbool_t new_format) @@ -535,9 +529,12 @@ void test_links() test_basic_links(my_fapl_id, new_format); test_num_links(my_fapl_id, new_format); #if 0 + // these tests are from the C test links.c and left here for future // implementation of H5L API - nerrors += test_basic_links(fapl_id, new_format) < 0 ? 1 : 0; + test_move(my_fapl_id, new_format); + test_copy(my_fapl_id, new_format); + test_lcpl(my_fapl_id, new_format); nerrors += cklinks(my_fapl, new_format) < 0 ? 1 : 0; nerrors += new_links(my_fapl, new_format) < 0 ? 1 : 0; nerrors += ck_new_links(my_fapl, new_format) < 0 ? 1 : 0; @@ -545,9 +542,6 @@ void test_links() nerrors += toomany(my_fapl, new_format) < 0 ? 1 : 0; /* Test new H5L link creation routine */ - nerrors += test_lcpl(my_fapl, new_format); - nerrors += test_move(my_fapl, new_format); - nerrors += test_copy(my_fapl, new_format); nerrors += test_move_preserves(my_fapl, new_format); #ifndef H5_NO_DEPRECATED_SYMBOLS nerrors += test_deprec(my_fapl, new_format); diff --git a/c++/test/tobject.cpp b/c++/test/tobject.cpp index 54d62f2..b5e9ff0 100644 --- a/c++/test/tobject.cpp +++ b/c++/test/tobject.cpp @@ -73,6 +73,19 @@ static void test_get_objname() Group grp1_1 = grp1.createGroup(GROUP1_1, 0); Group grp1_2 = grp1.createGroup(GROUP1_2, 0); + // Attempted to create a same group to generate a failure, which should + // be caught with sub-class exception clause, if available. + try { + Group grp1_2 = grp1.createGroup(GROUP1_2, 0); + } + catch (GroupIException& E) + {} // do nothing, exception expected + catch (Exception& E) + { + cerr << "Exception should have been caught by the previous catch" << endl; + issue_fail_msg("test_get_objname", __LINE__, __FILE__); + } + // Get part of the group's name, random length using // ssize_t getObjName(char* comment, size_t buf_size) @@ -302,6 +315,7 @@ static void test_get_objtype() *------------------------------------------------------------------------- */ const H5std_string GROUPNAME("group"); +const H5std_string NOGROUPNAME("non-existent-group"); const H5std_string DTYPENAME("group/datatype"); const H5std_string DTYPENAME_INGRP("datatype"); const H5std_string DSETNAME("dataset"); @@ -323,22 +337,25 @@ static void test_open_object_header() // Create a group in the root group Group grp(file1.createGroup(GROUPNAME)); - grp.close(); // Commit the type inside the file IntType dtype(PredType::NATIVE_INT); dtype.commit(file1, DTYPENAME); dtype.close(); - // Create a new dataset + // Create a new dataset in the file dims[0] = DIM0; dims[1] = DIM1; DataSpace dspace(RANK, dims); DataSet dset(file1.createDataSet(DSETNAME, PredType::NATIVE_INT, dspace)); - // Close dataset and dataspace + // Create a dataset in the group + DataSet dsingrp(grp.createDataSet(DSET_IN_GRP1, PredType::NATIVE_INT, dspace)); + + // Close dataset, dataspace, and group dset.close(); dspace.close(); + grp.close(); // Now make sure that openObjId can open all three types of objects hid_t obj_grp = file1.openObjId(GROUPNAME); @@ -357,12 +374,12 @@ static void test_open_object_header() Group grp2(obj_grp); hsize_t num_objs = grp2.getNumObjs(); - verify_val(num_objs, 1, "H5Gget_info", __LINE__, __FILE__); - // There should be one object, the datatype + verify_val(num_objs, 2, "H5Gget_info", __LINE__, __FILE__); // Close datatype object opened from the file H5Location::closeObjId(obj_dtype); + // Do a few things using the dset object identifier dset.setId(obj_dset); dspace = dset.getSpace(); bool is_simple = dspace.isSimple(); @@ -371,6 +388,7 @@ static void test_open_object_header() // Open datatype object from the group obj_dtype = grp2.openObjId(DTYPENAME_INGRP); + // Do a few things using the datatype object identifier dtype.setId(obj_dtype); H5T_class_t type_class = dtype.getClass(); verify_val(type_class, H5T_INTEGER, "H5Tget_class", __LINE__, __FILE__); @@ -384,11 +402,24 @@ static void test_open_object_header() // Try doing something with group, the ID should still work num_objs = grp2.getNumObjs(); - verify_val(num_objs, 1, "H5Gget_info", __LINE__, __FILE__); + verify_val(num_objs, 2, "H5Gget_info", __LINE__, __FILE__); // Close the cloned group grp2.close(); + // Attempted to open a non-existing group, which should + // be caught with sub-class exception clause, if available. + try { + Group grp3 = dsingrp.openObjId(NOGROUPNAME); + } + catch (DataSetIException& E) + {} // do nothing, exception expected and caught correctly + catch (Exception& E) + { + cerr << "Exception should have been caught by the previous catch" << endl; + issue_fail_msg("test_get_objname", __LINE__, __FILE__); + } + PASSED(); } // end of try block // catch invalid action exception @@ -401,7 +432,6 @@ static void test_open_object_header() // catch all other exceptions catch (Exception& E) { - cerr << " in Exception" << endl; issue_fail_msg("test_file_name()", __LINE__, __FILE__, E.getCDetailMsg()); } } /* test_open_object_header() */ @@ -454,7 +484,6 @@ static void test_is_valid() // catch all other exceptions catch (Exception& E) { - cerr << " in catch " << endl; issue_fail_msg("test_get_objtype", __LINE__, __FILE__, E.getCDetailMsg()); } } // test_is_valid |