diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2005-02-21 04:23:17 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2005-02-21 04:23:17 (GMT) |
commit | 95f6839c65d827a6433ea04a6cc0c0f23eaadd9e (patch) | |
tree | ea66818c193a9657c6f153d11fe5201d9f2c4e9b | |
parent | 6200b0981ed86754e7b3c9e4f5f51ceaa777631a (diff) | |
download | hdf5-95f6839c65d827a6433ea04a6cc0c0f23eaadd9e.zip hdf5-95f6839c65d827a6433ea04a6cc0c0f23eaadd9e.tar.gz hdf5-95f6839c65d827a6433ea04a6cc0c0f23eaadd9e.tar.bz2 |
[svn-r10055] Purpose: Clean up tests
Description:
+ replaced "goto error" with throw exceptions
+ properly cleanup dynamically allocated memory in failure cases,
for in some cases, the execution continues on after the failures
were reported.
Platforms tested:
Linux 2.4 (heping)
SunOS 5.8 64-bit (sol)
-rw-r--r-- | c++/test/dsets.cpp | 280 |
1 files changed, 146 insertions, 134 deletions
diff --git a/c++/test/dsets.cpp b/c++/test/dsets.cpp index 3f3be4b..5aa2032 100644 --- a/c++/test/dsets.cpp +++ b/c++/test/dsets.cpp @@ -21,7 +21,6 @@ These routines are in the test directory of the C library: h5_reset() -- in h5test.c, resets the library by closing it h5_fileaccess() -- in h5test.c, returns a file access template - h5_cleanup() -- in h5test.c, cleanup temporary test files ***************************************************************************/ @@ -275,7 +274,8 @@ test_simple_io( H5File& file) for (j = 0; j < 200; j++) { int status = check_values (i, j, points[i][j], check[i][j]); - if (status == -1) goto error; + if (status == -1) + throw Exception("DataSet::read"); } delete [] tconv_buf; @@ -284,13 +284,16 @@ test_simple_io( H5File& file) } // end try // catch all dataset, space, plist exceptions - catch (Exception E) { goto error; } - - error: - // cleaning up - if (tconv_buf) - delete [] tconv_buf; - return -1; + catch (Exception E) + { + cerr << " FAILED" << endl; + cerr << " <<< " << E.getDetailMsg() << " >>>" << endl << endl; + + // clean up and return with failure + if (tconv_buf) + delete [] tconv_buf; + return -1; + } } /*------------------------------------------------------------------------- @@ -352,12 +355,11 @@ test_tconv( H5File& file) in[4*i+2]!=out[4*i+1] || in[4*i+3]!=out[4*i+0]) { - H5_FAILED(); - cerr << " Read with byte order conversion failed." << endl; - goto error; + throw Exception("DataSet::read", "Read with byte order conversion failed"); } } + // clean up and return with success delete [] out; delete [] in; cerr << " PASSED" << endl; @@ -365,12 +367,16 @@ test_tconv( H5File& file) } // end try // catch all dataset and space exceptions - catch (Exception E) { goto error; } - - error: - delete [] out; - delete [] in; - return -1; + catch (Exception E) + { + cerr << " FAILED" << endl; + cerr << " <<< " << E.getDetailMsg() << " >>>" << endl << endl; + + // clean up and return with failure + delete [] out; + delete [] in; + return -1; + } } /* This message derives from H5Z */ @@ -448,7 +454,7 @@ test_compression(H5File& file) } } char* tconv_buf = new char [1000]; - + DataSet* dataset = NULL; try { const hsize_t size[2] = {100, 200}; @@ -468,8 +474,6 @@ test_compression(H5File& file) dscreatplist.setChunk (2, chunk_size); dscreatplist.setDeflate (6); - DataSet* dataset; - #ifdef H5_HAVE_FILTER_DEFLATE TESTING("compression (setup)"); @@ -494,7 +498,7 @@ test_compression(H5File& file) cerr << " Read a non-zero value." << endl; cerr << " At index " << (unsigned long)i << "," << (unsigned long)j << endl; - goto error; + throw Exception("test_compression", "Failed in uninitialized read"); } } } @@ -533,7 +537,8 @@ test_compression(H5File& file) for (j = 0; j < size[1]; j++) { int status = check_values (i, j, points[i][j], check[i][j]); - if (status == -1) goto error; + if (status == -1) + throw Exception("test_compression", "Failed in read"); } PASSED(); @@ -564,7 +569,8 @@ test_compression(H5File& file) for (j = 0; j < size[1]; j++) { int status = check_values (i, j, points[i][j], check[i][j]); - if (status == -1) goto error; + if (status == -1) + throw Exception("test_compression", "Failed in modify"); } PASSED(); @@ -577,6 +583,7 @@ test_compression(H5File& file) */ TESTING("compression (re-open)"); + // close this dataset delete dataset; dataset = new DataSet (file.openDataSet (DSET_COMPRESS_NAME)); @@ -587,7 +594,8 @@ test_compression(H5File& file) for (j = 0; j < size[1]; j++) { int status = check_values (i, j, points[i][j], check[i][j]); - if (status == -1) goto error; + if (status == -1) + throw Exception("test_compression", "Failed in re-open"); } PASSED(); @@ -624,12 +632,13 @@ test_compression(H5File& file) cerr << " At original: " << (int)points[hs_offset[0]+i][hs_offset[1]+j] << endl; cerr << " At returned: " << (int)check[hs_offset[0]+i][hs_offset[1]+j] << endl; - goto error; + throw Exception("test_compression", "Failed in partial I/O"); } - } - } + } // for j + } // for i delete dataset; + dataset = NULL; PASSED(); @@ -647,11 +656,14 @@ test_compression(H5File& file) TESTING("compression (app-defined method)"); #ifdef H5_WANT_H5_V1_4_COMPAT - if (H5Zregister (H5Z_FILTER_BOGUS, "bogus", bogus)<0) goto error; + if (H5Zregister (H5Z_FILTER_BOGUS, "bogus", bogus)<0) + throw Exception("test_compression", "Failed in app-defined method"); #else /* H5_WANT_H5_V1_4_COMPAT */ - if (H5Zregister (H5Z_BOGUS)<0) goto error; + if (H5Zregister (H5Z_BOGUS)<0) + throw Exception("test_compression", "Failed in app-defined method"); #endif /* H5_WANT_H5_V1_4_COMPAT */ - if (H5Pset_filter (dscreatplist.getId(), H5Z_FILTER_BOGUS, 0, 0, NULL)<0) goto error; + if (H5Pset_filter (dscreatplist.getId(), H5Z_FILTER_BOGUS, 0, 0, NULL)<0) + throw Exception("test_compression", "Failed in app-defined method"); dscreatplist.setFilter (H5Z_FILTER_BOGUS, 0, 0, NULL); DataSpace space2 (2, size, NULL); @@ -666,7 +678,7 @@ test_compression(H5File& file) { int status = check_values (i, j, points[i][j], check[i][j]); if (status == -1) - goto error; + throw Exception("test_compression", "Failed in app-defined method"); } PASSED(); @@ -681,13 +693,18 @@ test_compression(H5File& file) } // end try // catch all dataset, file, space, and plist exceptions - catch (Exception E) { goto error; } - - error: - // cleaning up - if (tconv_buf) - delete [] tconv_buf; - return -1; + catch (Exception E) + { + cerr << " FAILED" << endl; + cerr << " <<< " << E.getDetailMsg() << " >>>" << endl << endl; + + // clean up and return with failure + if (dataset != NULL) + delete dataset; + if (tconv_buf) + delete [] tconv_buf; + return -1; + } } /*------------------------------------------------------------------------- @@ -714,6 +731,8 @@ test_multiopen (H5File& file) { TESTING("multi-open with extending"); + + DataSpace* space = NULL; try { // Create a dataset creation property list @@ -725,7 +744,7 @@ test_multiopen (H5File& file) // Create a simple data space with unlimited size static hsize_t max_size[1] = {H5S_UNLIMITED}; - DataSpace* space = new DataSpace (1, cur_size, max_size); + space = new DataSpace (1, cur_size, max_size); // Create first dataset DataSet dset1 = file.createDataSet ("multiopen", PredType::NATIVE_INT, *space, dcpl); @@ -735,6 +754,7 @@ test_multiopen (H5File& file) // Relieve the dataspace delete space; + space = NULL; // Extend the dimensionality of the first dataset cur_size[0] = 20; @@ -747,13 +767,12 @@ test_multiopen (H5File& file) space->getSimpleExtentDims (tmp_size); if (cur_size[0]!=tmp_size[0]) { - H5_FAILED(); cerr << " Got " << (int)tmp_size[0] << " instead of " << (int)cur_size[0] << "!" << endl; - delete space; - goto error; + throw Exception("test_multiopen", "Failed in multi-open with extending"); } + // clean up and return with success delete space; PASSED(); return 0; @@ -761,10 +780,14 @@ test_multiopen (H5File& file) // catch all dataset, file, space, and plist exceptions catch (Exception E) - { goto error; } + { + cerr << " FAILED" << endl; + cerr << " <<< " << E.getDetailMsg() << " >>>" << endl << endl; - error: - return -1; + if (space != NULL) + delete space; + return -1; + } } @@ -787,9 +810,10 @@ test_multiopen (H5File& file) static herr_t test_types(H5File& file) { - size_t i; - TESTING("various datatypes"); + + size_t i; + DataSet* dset = NULL; try { // Create a group in the file that was passed in from the caller @@ -824,97 +848,94 @@ test_types(H5File& file) another_int_type = new_int_type; DataSpace space (1, &nelmts); - DataSet* dset = new DataSet(grp.createDataSet("bitfield_1", type, space)); + dset = new DataSet(grp.createDataSet("bitfield_1", type, space)); // Fill buffer for (i=0; i<sizeof buf; i++) buf[i] = (unsigned char)0xff ^ (unsigned char)i; - // Write data from buf using all default dataspaces and property - // list; if writing fails, deallocate dset and return. - try { dset->write (buf, type); } - catch(DataSetIException E) - { - delete dset; - goto error; - } - delete dset; + // Write data from buf using all default dataspaces and property + // list + dset->write (buf, type); + // no failure in bitfield_1, close this dataset + delete dset; } // end try block of bitfield_1 // catch exceptions thrown in try block of bitfield_1 - catch (Exception E) { - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; - } + catch (Exception E) + { + cerr << " FAILED" << endl; + cerr << " <<< " << "bitfield_1: " << E.getFuncName() + << " - " << E.getDetailMsg() << " >>>" << endl << endl; + if (dset != NULL) + delete dset; + return -1; + } /* bitfield_2 */ nelmts = sizeof(buf)/2; try { // bitfield_2 block type.copy (PredType::STD_B16LE); DataSpace space (1, &nelmts); - DataSet* dset = new DataSet(grp.createDataSet("bitfield_2", type, space)); + dset = new DataSet(grp.createDataSet("bitfield_2", type, space)); // Fill buffer for (i=0; i<sizeof(buf); i++) buf[i] = (unsigned char)0xff ^ (unsigned char)i; - // Write data from buf using all default dataspaces and property - // list; if writing fails, deallocate dset and return. - try { dset->write (buf, type); } - catch(DataSetIException E) - { - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - delete dset; - goto error; - } - delete dset; + // Write data from buf using all default dataspaces and property + // list; if writing fails, deallocate dset and return. + dset->write (buf, type); + + // no failure in bitfield_2, close this dataset and reset for + // variable reuse + delete dset; + dset = NULL; } // end try block of bitfield_2 // catch exceptions thrown in try block of bitfield_2 catch (Exception E) { - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; - } + cerr << " FAILED" << endl; + cerr << " <<< " << "bitfield_2: " << E.getFuncName() + << " - " << E.getDetailMsg() << " >>>" << endl << endl; + if (dset != NULL) + delete dset; + throw E; // propagate the exception + } /* opaque_1 */ - DataType* optype = new DataType(H5T_OPAQUE, 1); + DataType* optype = NULL; try { // opaque_1 block + optype = new DataType(H5T_OPAQUE, 1); nelmts = sizeof(buf); DataSpace space (1, &nelmts); optype->setTag ("testing 1-byte opaque type"); - DataSet* dset = new DataSet(grp.createDataSet("opaque_1", *optype, space)); + dset = new DataSet(grp.createDataSet("opaque_1", *optype, space)); // Fill buffer for (i=0; i<sizeof buf; i++) buf[i] = (unsigned char)0xff ^ (unsigned char)i; - // Write data from buf using all default dataspaces and property - // list; if writing fails, deallocate dset and return. - try { dset->write (buf, *optype); } - catch(DataSetIException E) - { - delete dset; - goto error; - } - delete dset; - delete optype; + // Write data from buf using all default dataspaces and property + // list; if writing fails, deallocate dset and return. + dset->write (buf, *optype); + + // no failure in opaque_1 + delete dset; dset = NULL; + delete optype; optype = NULL; } // end try block of opaque_1 // catch exceptions thrown in try block of opaque_1 catch (DataSetIException E) { - delete optype; - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; - } - catch (Exception E) { - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; + cerr << " FAILED" << endl; + cerr << " <<< " << "opaque_1: " << E.getFuncName() + << " - " << E.getDetailMsg() << " >>>" << endl << endl; + if (dset != NULL) + delete dset; + if (optype != NULL) + delete optype; + throw E; // propagate the exception } /* opaque_2 */ @@ -923,47 +944,41 @@ test_types(H5File& file) DataSpace space (1, &nelmts); optype = new DataType(H5T_OPAQUE, 4); optype->setTag ("testing 4-byte opaque type"); - DataSet* dset = new DataSet(grp.createDataSet("opaque_2", *optype, space)); + dset = new DataSet(grp.createDataSet("opaque_2", *optype, space)); // Fill buffer for (i=0; i<sizeof(buf); i++) buf[i] = (unsigned char)0xff ^ (unsigned char)i; - // Write data from buf using all default dataspaces and property - // list; if writing fails, deallocate dset and return. - try { dset->write (buf, *optype); } - catch(DataSetIException E) - { - delete dset; - goto error; - } - delete dset; - delete optype; + // Write data from buf using all default dataspaces and property + // list; if writing fails, deallocate dset and return. + dset->write (buf, *optype); + + // no failure in opaque_1 + delete dset; dset = NULL; + delete optype; optype = NULL; } //end try block of opaque_2 - catch (DataSetIException E) { - delete optype; - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; - } + + // catch exceptions thrown in try block of opaque_2 catch (Exception E) { - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; + cerr << " FAILED" << endl; + cerr << " <<< " << "opaque_2: " << E.getFuncName() + << " - " << E.getDetailMsg() << " >>>" << endl << endl; + if (dset != NULL) + delete dset; + if (optype != NULL) + delete optype; + throw E; // propagate the exception } PASSED(); return 0; } // end top try block - catch (Exception E) { // Group and DataType exceptions - cerr << "Failure in " << E.getFuncName() << " - " - << E.getDetailMsg() << endl; - goto error; + catch (Exception E) + { + return -1; } - - error: - return -1; } /*------------------------------------------------------------------------- @@ -971,9 +986,9 @@ test_types(H5File& file) * * Purpose: Tests the dataset interface (H5D) * - * Return: Success: exit(0) + * Return: Success: 0 * - * Failure: exit(1) + * Failure: -1 * * Programmer: Binh-Minh Ribler (using C version) * Friday, January 5, 2001 @@ -983,6 +998,8 @@ test_types(H5File& file) * - moved h5_cleanup to outside of try block because * dataset.h5 cannot be removed until "file" is out of * scope and dataset.h5 is closed. + * Feb 20, 05: + * - cleanup_dsets took care of the cleanup now. * *------------------------------------------------------------------------- */ @@ -1018,11 +1035,6 @@ main(void) nerrors += test_compression(file)<0 ?1:0; nerrors += test_multiopen (file)<0 ?1:0; nerrors += test_types(file)<0 ?1:0; - - // increment the ref count of this property list so that the - // property list id won't be closed when fapl goes out of scope. - // This is a bad hack, but I want to use existing routine h5_cleanup! - fapl.incRefCount(); } catch (Exception E) { |