diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2009-07-27 04:00:17 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2009-07-27 04:00:17 (GMT) |
commit | c1ad3676c4b56b80663d271ac367a65cc41c39ab (patch) | |
tree | 42f19502941ff1d02d45b92a7f8a0a3f6945002e | |
parent | 405ea51bb7728dc696e29694b22e81dc4b6ce2c1 (diff) | |
download | hdf5-c1ad3676c4b56b80663d271ac367a65cc41c39ab.zip hdf5-c1ad3676c4b56b80663d271ac367a65cc41c39ab.tar.gz hdf5-c1ad3676c4b56b80663d271ac367a65cc41c39ab.tar.bz2 |
[svn-r17238] Purpose: Fix bug and improve readability
Description:
- Revised DataSet::write to pass in correct string buffer
- Added member function DataSet::getInMemDataSize() to simplify
getting the dataset's data size in memory.
- Added private functions for reading fixed- and variable-length
string data: p_read_fixed_len and p_read_variable_len.
- Added tests to write/read array of strings to datasets.
Platforms tested:
Linux/32 2.6 (jam)
FreeBSD/64 6.3 (liberty)
SunOS 5.10 (linew)
-rw-r--r-- | c++/src/H5AbstractDs.cpp | 5 | ||||
-rw-r--r-- | c++/src/H5AbstractDs.h | 3 | ||||
-rw-r--r-- | c++/src/H5Attribute.cpp | 14 | ||||
-rw-r--r-- | c++/src/H5Attribute.h | 6 | ||||
-rw-r--r-- | c++/src/H5DataSet.cpp | 203 | ||||
-rw-r--r-- | c++/src/H5DataSet.h | 9 | ||||
-rw-r--r-- | c++/test/tvlstr.cpp | 369 |
7 files changed, 432 insertions, 177 deletions
diff --git a/c++/src/H5AbstractDs.cpp b/c++/src/H5AbstractDs.cpp index a61cc88..19eeb33 100644 --- a/c++/src/H5AbstractDs.cpp +++ b/c++/src/H5AbstractDs.cpp @@ -25,6 +25,11 @@ #include "H5CommonFG.h" #include "H5Alltypes.h" +#include <iostream> // remove when done + + using std::cerr; + using std::endl; + #ifndef H5_NO_NAMESPACE namespace H5 { #endif diff --git a/c++/src/H5AbstractDs.h b/c++/src/H5AbstractDs.h index c98e5e1..1d04d6c 100644 --- a/c++/src/H5AbstractDs.h +++ b/c++/src/H5AbstractDs.h @@ -51,6 +51,9 @@ class H5_DLLCPP AbstractDs { StrType getStrType() const; VarLenType getVarLenType() const; + // Gets the size in memory of this abstract dataset. + virtual size_t getInMemDataSize() const = 0; + // Gets the dataspace of this abstract dataset - pure virtual. virtual DataSpace getSpace() const = 0; diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp index b1ab73e..6de5e63 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -198,35 +198,37 @@ void Attribute::read(const DataType& mem_type, H5std_string& strg) const //-------------------------------------------------------------------------- size_t Attribute::getInMemDataSize() const { + char *func = "Attribute::getInMemDataSize"; + // Get the data type of this attribute hid_t mem_type_id = H5Aget_type(id); - if (mem_type_id <= 0) + if( mem_type_id < 0 ) { - throw AttributeIException("Attribute::getInMemDataSize", "H5Aget_type failed"); + throw AttributeIException(func, "H5Aget_type failed"); } // Get the data type's size hid_t native_type = H5Tget_native_type(mem_type_id, H5T_DIR_DEFAULT); if (native_type < 0) { - throw AttributeIException("Attribute::getInMemDataSize", "H5Tget_native_type failed"); + throw AttributeIException(func, "H5Tget_native_type failed"); } size_t type_size = H5Tget_size(native_type); if (type_size == 0) { - throw AttributeIException("Attribute::getInMemDataSize", "H5Tget_size failed"); + throw AttributeIException(func, "H5Tget_size failed"); } // Get number of elements of the attribute hid_t space_id = H5Aget_space(id); if (space_id < 0) { - throw AttributeIException("Attribute::getInMemDataSize", "H5Aget_space failed"); + throw AttributeIException(func, "H5Aget_space failed"); } hssize_t num_elements = H5Sget_simple_extent_npoints(space_id); if (num_elements < 0) { - throw AttributeIException("Attribute::getInMemDataSize", "H5Sget_simple_extent_npoints failed"); + throw AttributeIException(func, "H5Sget_simple_extent_npoints failed"); } // Calculate and return the size of the data diff --git a/c++/src/H5Attribute.h b/c++/src/H5Attribute.h index abece10..284e5bc 100644 --- a/c++/src/H5Attribute.h +++ b/c++/src/H5Attribute.h @@ -38,10 +38,10 @@ class H5_DLLCPP Attribute : public AbstractDs, public IdComponent { virtual DataSpace getSpace() const; // Returns the amount of storage size required for this attribute. - hsize_t getStorageSize() const; + virtual hsize_t getStorageSize() const; // Returns the in memory size of this attribute's data. - size_t getInMemDataSize() const; + virtual size_t getInMemDataSize() const; // Reads data from this attribute. void read( const DataType& mem_type, void *buf ) const; @@ -82,7 +82,7 @@ class H5_DLLCPP Attribute : public AbstractDs, public IdComponent { // sub-types virtual hid_t p_get_type() const; - // Reads variable or fixed len strings + // Reads variable or fixed len strings from this attribute. void p_read_variable_len(const DataType& mem_type, H5std_string& strg) const; void p_read_fixed_len(const DataType& mem_type, H5std_string& strg) const; diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp index 494d3ea..cddb5d4 100644 --- a/c++/src/H5DataSet.cpp +++ b/c++/src/H5DataSet.cpp @@ -37,6 +37,7 @@ #include "H5File.h" #include "H5Attribute.h" #include "H5DataSet.h" +#include "H5private.h" // for HDfree #ifndef H5_NO_NAMESPACE namespace H5 { @@ -220,6 +221,53 @@ hsize_t DataSet::getStorageSize() const } //-------------------------------------------------------------------------- +// Function: DataSet::getInMemDataSize +///\brief Gets the size in memory of the dataset's data. +///\return Size of data (in memory) +///\exception H5::DataSetIException +// Programmer Binh-Minh Ribler - Apr 2009 +//-------------------------------------------------------------------------- +size_t DataSet::getInMemDataSize() const +{ + char *func = "DataSet::getInMemDataSize"; + + // Get the data type of this dataset + hid_t mem_type_id = H5Dget_type(id); + if( mem_type_id < 0 ) + { + throw DataSetIException(func, "H5Dget_type failed"); + } + + // Get the data type's size + hid_t native_type = H5Tget_native_type(mem_type_id, H5T_DIR_DEFAULT); + if (native_type < 0) + { + throw DataSetIException(func, "H5Tget_native_type failed"); + } + size_t type_size = H5Tget_size(native_type); + if (type_size == 0) + { + throw DataSetIException(func, "H5Tget_size failed"); + } + + // Get number of elements of the dataset + hid_t space_id = H5Dget_space(id); // first get its data space + if (space_id < 0) + { + throw DataSetIException(func, "H5Dget_space failed"); + } + hssize_t num_elements = H5Sget_simple_extent_npoints(space_id); + if (num_elements < 0) + { + throw DataSetIException(func, "H5Sget_simple_extent_npoints failed"); + } + + // Calculate and return the size of the data + size_t data_size = type_size * num_elements; + return(data_size); +} + +//-------------------------------------------------------------------------- // Function: DataSet::getOffset ///\brief Returns the address of this dataset in the file. ///\return Address of dataset @@ -364,20 +412,45 @@ void DataSet::read( void* buf, const DataType& mem_type, const DataSpace& mem_sp // Function: DataSet::read ///\brief This is an overloaded member function, provided for convenience. /// It takes a reference to a \c std::string for the buffer. +///\param buf - IN: Buffer for read data +///\param mem_type - IN: Memory datatype +///\param mem_space - IN: Memory dataspace +///\param file_space - IN: Dataset's dataspace in the file +///\param xfer_plist - IN: Transfer property list for this I/O operation +///\exception H5::DataSetIException // Programmer Binh-Minh Ribler - 2000 -//-------------------------------------------------------------------------- -void DataSet::read( H5std_string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const +// Modification +// Jul 2009 +// Follow the change to Attribute::read and use the following +// private functions to read datasets with fixed- and +// variable-length string: +// DataSet::p_read_fixed_len and +// DataSet::p_read_variable_len +//-------------------------------------------------------------------------- +void DataSet::read(H5std_string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist) const { - // Allocate C character string for reading - size_t size = mem_type.getSize(); - char* strg_C = new char[size+1]; // temporary C-string for C API + // Check if this dataset has variable-len string or fixed-len string and + // proceed appropriately. + htri_t is_variable_len = H5Tis_variable_str(mem_type.getId()); + if (is_variable_len < 0) + { + throw DataSetIException("DataSet::read", "H5Tis_variable_str failed"); + } - // Use the overloaded member to read - read(strg_C, mem_type, mem_space, file_space, xfer_plist); + // Obtain identifiers for C API + hid_t mem_type_id = mem_type.getId(); + hid_t mem_space_id = mem_space.getId(); + hid_t file_space_id = file_space.getId(); + hid_t xfer_plist_id = xfer_plist.getId(); - // Get the String and clean up - strg = strg_C; - delete []strg_C; + if (!is_variable_len) // only allocate for fixed-len string + { + p_read_fixed_len(mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg); + } + else + { + p_read_variable_len(mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg); + } } //-------------------------------------------------------------------------- @@ -416,15 +489,46 @@ void DataSet::write( const void* buf, const DataType& mem_type, const DataSpace& ///\brief This is an overloaded member function, provided for convenience. /// It takes a reference to a \c std::string for the buffer. // Programmer Binh-Minh Ribler - 2000 +// Modification +// Jul 2009 +// Modified to pass the buffer into H5Dwrite properly depending +// whether the dataset has variable- or fixed-length string. //-------------------------------------------------------------------------- void DataSet::write( const H5std_string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const { - // Convert string to C-string - const char* strg_C; - strg_C = strg.c_str(); // strg_C refers to the contents of strg as a C-str + // Check if this attribute has variable-len string or fixed-len string and + // proceed appropriately. + htri_t is_variable_len = H5Tis_variable_str(mem_type.getId()); + if (is_variable_len < 0) + { + throw DataSetIException("DataSet::write", "H5Tis_variable_str failed"); + } + + // Obtain identifiers for C API + hid_t mem_type_id = mem_type.getId(); + hid_t mem_space_id = mem_space.getId(); + hid_t file_space_id = file_space.getId(); + hid_t xfer_plist_id = xfer_plist.getId(); + + // Convert string to C-string + const char* strg_C; + strg_C = strg.c_str(); // strg_C refers to the contents of strg as a C-str + herr_t ret_value = 0; - // Use the overloaded member - write(strg_C, mem_type, mem_space, file_space, xfer_plist); + // Pass string in differently depends on variable or fixed length + if (!is_variable_len) + { + ret_value = H5Dwrite( id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg_C ); + } + else + { + // passing string argument by address + ret_value = H5Dwrite( id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, &strg_C ); + } + if (ret_value < 0) + { + throw DataSetIException("DataSet::write", "H5Dwrite failed"); + } } //-------------------------------------------------------------------------- @@ -586,7 +690,74 @@ hid_t DataSet::getId() const } //-------------------------------------------------------------------------- -// Function: DataSet::p_setId +// Function: DataSet::p_read_fixed_len (private) +// brief Reads a fixed length \a H5std_string from an dataset. +// param mem_type - IN: DataSet datatype (in memory) +// param strg - IN: Buffer for read string +// exception H5::DataSetIException +// Programmer Binh-Minh Ribler - Jul, 2009 +// Modification +// Jul 2009 +// Added in follow to the change in Attribute::read +//-------------------------------------------------------------------------- +void DataSet::p_read_fixed_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const +{ + // Only allocate for fixed-len string. + + // Get the size of the dataset's data + size_t attr_size = getInMemDataSize(); + + // If there is data, allocate buffer and read it. + if (attr_size > 0) + { + char *strg_C = NULL; + + strg_C = new char [(size_t)attr_size+1]; + herr_t ret_value = H5Dread(id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg_C); + + if( ret_value < 0 ) + { + delete []strg_C; // de-allocate for fixed-len string + throw DataSetIException("DataSet::read", "H5Dread failed for fixed length string"); + } + + // Get string from the C char* and release resource allocated locally + strg = strg_C; + delete []strg_C; + } +} + +//-------------------------------------------------------------------------- +// Function: DataSet::p_read_variable_len (private) +// brief Reads a variable length \a H5std_string from an dataset. +// param mem_type - IN: DataSet datatype (in memory) +// param strg - IN: Buffer for read string +// exception H5::DataSetIException +// Programmer Binh-Minh Ribler - Jul, 2009 +// Modification +// Jul 2009 +// Added in follow to the change in Attribute::read +//-------------------------------------------------------------------------- +void DataSet::p_read_variable_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const +{ + // Prepare and call C API to read dataset. + char *strg_C; + + // Read dataset, no allocation for variable-len string; C library will + herr_t ret_value = H5Dread(id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, &strg_C); + + if( ret_value < 0 ) + { + throw DataSetIException("DataSet::read", "H5Dread failed for variable length string"); + } + + // Get string from the C char* and release resource allocated by C API + strg = strg_C; + HDfree(strg_C); +} + +//-------------------------------------------------------------------------- +// Function: DataSet::p_setId (private) ///\brief Sets the identifier of this object to a new value. /// ///\exception H5::IdComponentException when the attempt to close the HDF5 diff --git a/c++/src/H5DataSet.h b/c++/src/H5DataSet.h index f26d775..3d9183d 100644 --- a/c++/src/H5DataSet.h +++ b/c++/src/H5DataSet.h @@ -49,7 +49,10 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs { void getSpaceStatus(H5D_space_status_t& status) const; // Returns the amount of storage size required for this dataset. - hsize_t getStorageSize() const; + virtual hsize_t getStorageSize() const; + + // Returns the in memory size of this attribute's data. + virtual size_t getInMemDataSize() const; // Returns the number of bytes required to store VL data. hsize_t getVlenBufSize( DataType& type, DataSpace& space ) const; @@ -113,6 +116,10 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs { // sub-types virtual hid_t p_get_type() const; + // Reads variable or fixed len strings from this dataset. + void p_read_fixed_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const; + void p_read_variable_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const; + protected: // Sets the dataset id. virtual void p_setId(const hid_t new_id); diff --git a/c++/test/tvlstr.cpp b/c++/test/tvlstr.cpp index 0abab1b..01033be 100644 --- a/c++/test/tvlstr.cpp +++ b/c++/test/tvlstr.cpp @@ -18,7 +18,6 @@ tvlstr.cpp - HDF5 C++ testing the Variable-Length String functionality EXTERNAL ROUTINES/VARIABLES: - These routines are in the test directory of the C library: ***************************************************************************/ @@ -52,7 +51,7 @@ const H5std_string FILENAME("tvlstr.h5"); const int SPACE1_RANK = 1; const hsize_t SPACE1_DIM1 = 4; -// Utility functions +// Utility functions - not used now, later though. void *test_vlstr_alloc_custom(size_t size, void *info); void test_vlstr_free_custom(void *mem, void *info); @@ -119,9 +118,9 @@ void test_vlstr_free_custom(void *_mem, void *info) } /*------------------------------------------------------------------------- - * Function: test_vlstrings_basic + * Function: test_vlstring_dataset * - * Purpose: Test simple VL string I/O. + * Purpose: Test writing/reading VL strings on datasets. * * Return: None * @@ -130,98 +129,171 @@ void test_vlstr_free_custom(void *_mem, void *info) * *------------------------------------------------------------------------- */ -static void test_vlstrings_basic() +// String for testing datasets +static char *dynstring_ds_write=NULL; +static char stastring_ds_write[1]={'A'}; + +// Info for a string dataset +const H5std_string DSET1_NAME("String_ds"); +const H5std_string DSET1_DATA("String Dataset"); + +static void test_vlstring_dataset() +{ + // Output message about test being performed + SUBTEST("Testing VL String on Datasets"); + + try { + // Open the file + H5File file1(FILENAME, H5F_ACC_TRUNC); + + // Create a datatype to refer to. + StrType vlst(0, H5T_VARIABLE); + + // Open the root group. + Group root = file1.openGroup("/"); + + // Create dataspace for the dataset. + DataSpace ds_space (H5S_SCALAR); + + // Create an dataset in the root group. + DataSet dset1 = root.createDataSet(DSET1_NAME, vlst, ds_space); + + // Write data to the dataset. + dset1.write(DSET1_DATA, vlst); + + // Read and verify the dataset string as a string of chars. + char *string_ds_check; + dset1.read(&string_ds_check, vlst); + if(HDstrcmp(string_ds_check, DSET1_DATA.c_str())!=0) + TestErrPrintf("Line %d: Attribute data different: DSET1_DATA=%s,string_ds_check=%s\n",__LINE__, DSET1_DATA.c_str(), string_ds_check); + + HDfree(string_ds_check); // note: no need for std::string test + + // Read and verify the dataset string as an std::string. + H5std_string read_str; + dset1.read(read_str, vlst); + if (read_str != DSET1_DATA) + TestErrPrintf("Line %d: Attribute data different: DSET1_DATA=%s,read_str=%s\n",__LINE__, DSET1_DATA.c_str(), read_str.c_str()); + + // Close the dataset. + dset1.close(); + + // Test scalar type dataset with 1 value. + dset1 = root.createDataSet("test_scalar_small", vlst, ds_space); + + dynstring_ds_write = (char*)HDcalloc(1, sizeof(char)); + HDmemset(dynstring_ds_write, 'A', 1); + + // Write data to the dataset, then read it back. + dset1.write(&dynstring_ds_write, vlst); + dset1.read(&string_ds_check, vlst); + + // Verify data read. + if(HDstrcmp(string_ds_check,dynstring_ds_write)!=0) + TestErrPrintf("VL string datasets don't match!, dynstring_ds_write=%s, string_ds_check=%s\n",dynstring_ds_write,string_ds_check); + HDfree(string_ds_check); + dset1.close(); + + // Open dataset DSET1_NAME again. + dset1 = root.openDataSet(DSET1_NAME); + + // Close dataset and file + dset1.close(); + file1.close(); + + PASSED(); + } // end try block + + // Catch all exceptions. + catch (Exception E) { + issue_fail_msg("test_vlstring_dataset()", __LINE__, __FILE__, E.getCDetailMsg()); + } +} // test_vlstring_dataset() + +/*------------------------------------------------------------------------- + * Function: test_vlstring_array_dataset + * + * Purpose: Test writing/reading VL string array to/from datasets. + * + * Return: None + * + * Programmer: Binh-Minh Ribler + * July, 2009 + * + *------------------------------------------------------------------------- + */ +const H5std_string DSSTRARR_NAME("StringArray_dset"); +static void test_vlstring_array_dataset() { - const char *wdata[SPACE1_DIM1]= { - "Four score and seven years ago our forefathers brought forth on this continent a new nation,", - "conceived in liberty and dedicated to the proposition that all men are created equal.", - "Now we are engaged in a great civil war,", - "testing whether that nation or any nation so conceived and so dedicated can long endure." + const char *string_ds_array[SPACE1_DIM1]= { + "Line 1", "Line 2", "Line 3", "Line 4" }; // Information to write // Output message about test being performed - SUBTEST("Testing Basic VL String Functionality"); + SUBTEST("Testing VL String Array on Datasets"); - H5File* file1 = NULL; + H5File* file1; try { // Create file. - file1 = new H5File (FILENAME, H5F_ACC_TRUNC); + file1 = new H5File (FILENAME, H5F_ACC_RDWR); - // Create dataspace for datasets. - hsize_t dims1[] = {SPACE1_DIM1}; - DataSpace sid1(SPACE1_RANK, dims1); + // Create dataspace for datasets. + hsize_t dims1[] = {SPACE1_DIM1}; + DataSpace ds_space(SPACE1_RANK, dims1); // Create a datatype to refer to. - StrType tid1(0, H5T_VARIABLE); + StrType vlst(0, H5T_VARIABLE); // Create and write a dataset. - DataSet dataset(file1->createDataSet("Dataset1", tid1, sid1)); - dataset.write(wdata, tid1); + DataSet dataset(file1->createDataSet(DSSTRARR_NAME, vlst, ds_space)); + dataset.write(string_ds_array, vlst); + + // Read and verify the dataset using strings of chars as buffer. + // Note: reading by array of H5std_string doesn't work yet. + char *string_ds_check[SPACE1_DIM1]; + dataset.read(string_ds_check, vlst); + + int ii; + for (ii = 0; ii < SPACE1_DIM1; ii++) + { + if(HDstrcmp(string_ds_check[ii], string_ds_array[ii])!=0) + TestErrPrintf("Line %d: Dataset data different: written=%s,read=%s\n",__LINE__, string_ds_array[ii], string_ds_check[ii]); + + HDfree(string_ds_check[ii]); + } + + // Close objects that are no longer needed. + dataset.close(); + ds_space.close(); + + // + // Test with scalar data space. + // // Create H5S_SCALAR data space. DataSpace scalar_space; // Create and write another dataset. - DataSet dataset2(file1->createDataSet("Dataset2", tid1, scalar_space)); + DataSet dataset2(file1->createDataSet("Dataset2", vlst, scalar_space)); char *wdata2 = (char*)HDcalloc(65534, sizeof(char)); HDmemset(wdata2, 'A', 65533); - dataset2.write(&wdata2, tid1); + dataset2.write(&wdata2, vlst); + + char *rdata2 = (char*)HDcalloc(65534, sizeof(char)); + HDmemset(rdata2, 0, 65533); + dataset2.read(&rdata2, vlst); + if (HDstrcmp(wdata2, rdata2)!=0) + TestErrPrintf("Line %d: Dataset data different: written=%s,read=%s\n",__LINE__, wdata2, rdata2); // Release resources from second dataset operation. scalar_space.close(); dataset2.close(); HDfree(wdata2); - - // Change to the custom memory allocation routines for reading - // VL string. - DSetMemXferPropList xfer; - size_t mem_used = 0; // Memory used during allocation - xfer.setVlenMemManager(test_vlstr_alloc_custom, &mem_used, test_vlstr_free_custom, &mem_used); - - // Make certain the correct amount of memory will be used. - hsize_t vlsize = dataset.getVlenBufSize(tid1, sid1); - - // Count the actual number of bytes used by the strings. - size_t str_used; // String data in memory - hsize_t i; // counting variable - for (i=0,str_used=0; i<SPACE1_DIM1; i++) - str_used+=HDstrlen(wdata[i])+1; - - // Compare against the strings actually written. - verify_val((int)vlsize,str_used,"DataSet::getVlenBufSize", __LINE__, __FILE__); - - // Read dataset from disk. - char *rdata[SPACE1_DIM1]; // Data read in - dataset.read(rdata, tid1, DataSpace::ALL, DataSpace::ALL, xfer); - - // Make certain the correct amount of memory has been used. - verify_val(mem_used, str_used, "DataSet::read", __LINE__, __FILE__); - - // Compare data read in. - for(i = 0; i < SPACE1_DIM1; i++) { - size_t wlen = HDstrlen(wdata[i]); - size_t rlen = HDstrlen(rdata[i]); - if(wlen != rlen) { - TestErrPrintf("VL data lengths don't match!, strlen(wdata[%d])=%u, strlen(rdata[%d])=%u\n", (int)i, (unsigned)wlen, (int)i, (unsigned)rlen); - continue; - } // end if - if(HDstrcmp(wdata[i], rdata[i]) != 0) { - TestErrPrintf("VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n", (int)i, wdata[i], (int)i, rdata[i]); - continue; - } // end if - } // end for - - // Reclaim the read VL data. - DataSet::vlenReclaim((void *)rdata, tid1, sid1, xfer); - - // Make certain the VL memory has been freed. - verify_val(mem_used, 0, "DataSet::vlenReclaim", __LINE__, __FILE__); + HDfree(rdata2); // Close objects and file. - dataset.close(); - tid1.close(); - sid1.close(); - xfer.close(); + dataset2.close(); + vlst.close(); file1->close(); PASSED(); @@ -230,11 +302,10 @@ static void test_vlstrings_basic() // Catch all exceptions. catch (Exception E) { - issue_fail_msg("test_vlstrings_basic()", __LINE__, __FILE__, E.getCDetailMsg()); - if (file1 != NULL) // clean up - delete file1; + issue_fail_msg("test_vlstring_array_dataset()", __LINE__, __FILE__, E.getCDetailMsg()); + delete file1; } -} // end test_vlstrings_basic() +} // end test_vlstring_array_dataset() /*------------------------------------------------------------------------- * Function: test_vlstrings_special @@ -267,13 +338,13 @@ static void test_vlstrings_special() DataSpace sid1(SPACE1_RANK, dims1); // Create a datatype to refer to. - StrType tid1(0, H5T_VARIABLE); + StrType vlst(0, H5T_VARIABLE); // Create a dataset. - DataSet dataset(file1.createDataSet("Dataset3", tid1, sid1)); + DataSet dataset(file1.createDataSet("Dataset3", vlst, sid1)); // Read from the dataset before writing data. - dataset.read(rdata, tid1); + dataset.read(rdata, vlst); // Check data read in. hsize_t i; // counting variable @@ -282,8 +353,8 @@ static void test_vlstrings_special() TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]); // Write dataset to disk, then read it back. - dataset.write(wdata, tid1); - dataset.read(rdata, tid1); + dataset.write(wdata, vlst); + dataset.read(rdata, vlst); // Compare data read in. for(i = 0; i < SPACE1_DIM1; i++) { @@ -300,7 +371,7 @@ static void test_vlstrings_special() } // end for // Reclaim the read VL data. - DataSet::vlenReclaim((void *)rdata, tid1, sid1); + DataSet::vlenReclaim((void *)rdata, vlst, sid1); // Close Dataset. dataset.close(); @@ -313,14 +384,14 @@ static void test_vlstrings_special() // dataset. DSetCreatPropList dcpl; char *fill = NULL; // Fill value - dcpl.setFillValue(tid1, &fill); - dataset = file1.createDataSet("Dataset4", tid1, sid1, dcpl); + dcpl.setFillValue(vlst, &fill); + dataset = file1.createDataSet("Dataset4", vlst, sid1, dcpl); // Close dataset creation property list. dcpl.close(); // Read from dataset before writing data. - dataset.read(rdata, tid1); + dataset.read(rdata, vlst); // Check data read in. for (i=0; i<SPACE1_DIM1; i++) @@ -328,10 +399,10 @@ static void test_vlstrings_special() TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i, rdata[i]); // Try to write nil strings to disk. - dataset.write(wdata2, tid1); + dataset.write(wdata2, vlst); // Read nil strings back from disk. - dataset.read(rdata, tid1); + dataset.read(rdata, vlst); // Check data read in. for (i=0; i<SPACE1_DIM1; i++) @@ -340,7 +411,7 @@ static void test_vlstrings_special() // Close objects and file. dataset.close(); - tid1.close(); + vlst.close(); sid1.close(); file1.close(); @@ -370,7 +441,7 @@ const H5std_string VLSTR_TYPE("vl_string_type"); static void test_vlstring_type() { // Output message about test being performed. - SUBTEST("Testing VL String type"); + SUBTEST("Testing VL String Type"); H5File* file1 = NULL; try { @@ -378,55 +449,55 @@ static void test_vlstring_type() file1 = new H5File(FILENAME, H5F_ACC_RDWR); // Create a datatype to refer to. - StrType vlstr_type(PredType::C_S1); + StrType vlst(PredType::C_S1); // Change padding and verify it. - vlstr_type.setStrpad(H5T_STR_NULLPAD); - H5T_str_t pad = vlstr_type.getStrpad(); + vlst.setStrpad(H5T_STR_NULLPAD); + H5T_str_t pad = vlst.getStrpad(); verify_val(pad, H5T_STR_NULLPAD, "StrType::getStrpad", __LINE__, __FILE__); // Convert to variable-length string. - vlstr_type.setSize(H5T_VARIABLE); + vlst.setSize(H5T_VARIABLE); // Check if datatype is VL string. - H5T_class_t type_class = vlstr_type.getClass(); + H5T_class_t type_class = vlst.getClass(); verify_val(type_class, H5T_STRING, "DataType::getClass", __LINE__, __FILE__); - bool is_variable_str = vlstr_type.isVariableStr(); + bool is_variable_str = vlst.isVariableStr(); verify_val(is_variable_str, TRUE, "DataType::isVariableStr", __LINE__, __FILE__); // Check default character set and padding. - H5T_cset_t cset = vlstr_type.getCset(); + H5T_cset_t cset = vlst.getCset(); verify_val(cset, H5T_CSET_ASCII, "StrType::getCset", __LINE__, __FILE__); - pad = vlstr_type.getStrpad(); + pad = vlst.getStrpad(); verify_val(pad, H5T_STR_NULLPAD, "StrType::getStrpad", __LINE__, __FILE__); // Commit variable-length string datatype to storage. - vlstr_type.commit(*file1, VLSTR_TYPE); + vlst.commit(*file1, VLSTR_TYPE); // Close datatype. - vlstr_type.close(); + vlst.close(); // Try opening datatype again. - vlstr_type = file1->openStrType(VLSTR_TYPE); + vlst = file1->openStrType(VLSTR_TYPE); // Close datatype and file. - vlstr_type.close(); + vlst.close(); file1->close(); // Open file. file1 = new H5File(FILENAME, H5F_ACC_RDWR); // Open the variable-length string datatype just created - vlstr_type = file1->openStrType(VLSTR_TYPE); + vlst = file1->openStrType(VLSTR_TYPE); // Verify character set and padding - cset = vlstr_type.getCset(); + cset = vlst.getCset(); verify_val(cset, H5T_CSET_ASCII, "StrType::getCset", __LINE__, __FILE__); - pad = vlstr_type.getStrpad(); + pad = vlst.getStrpad(); verify_val(pad, H5T_STR_NULLPAD, "StrType::getStrpad", __LINE__, __FILE__); // Close datatype and file - vlstr_type.close(); + vlst.close(); file1->close(); PASSED(); @@ -436,6 +507,7 @@ static void test_vlstring_type() catch (Exception E) { issue_fail_msg("test_vlstring_type()", __LINE__, __FILE__, E.getCDetailMsg()); + delete file1; } } // end test_vlstring_type() @@ -454,7 +526,7 @@ static void test_vlstring_type() static void test_compact_vlstring() { // Output message about test being performed - SUBTEST("Testing VL Strings in compact dataset"); + SUBTEST("Testing VL Strings on Compact Dataset"); try { // Create file @@ -465,22 +537,22 @@ static void test_compact_vlstring() DataSpace sid1(SPACE1_RANK, dims1); // Create a datatype to refer to - StrType tid1(0, H5T_VARIABLE); + StrType vlst(0, H5T_VARIABLE); // Create dataset create property list and set layout DSetCreatPropList plist; plist.setLayout(H5D_COMPACT); // Create a dataset - DataSet dataset(file1.createDataSet("Dataset5", tid1, sid1, plist)); + DataSet dataset(file1.createDataSet("Dataset5", vlst, sid1, plist)); // Write dataset to disk const char *wdata[SPACE1_DIM1] = {"one", "two", "three", "four"}; - dataset.write(wdata, tid1); + dataset.write(wdata, vlst); // Read dataset from disk char *rdata[SPACE1_DIM1]; // Information read in - dataset.read(rdata, tid1); + dataset.read(rdata, vlst); // Compare data read in hsize_t i; @@ -496,11 +568,11 @@ static void test_compact_vlstring() } // end for // Reclaim the read VL data - DataSet::vlenReclaim((void *)rdata, tid1, sid1); + DataSet::vlenReclaim((void *)rdata, vlst, sid1); // Close objects and file dataset.close(); - tid1.close(); + vlst.close(); sid1.close(); plist.close(); file1.close(); @@ -516,9 +588,9 @@ static void test_compact_vlstring() } // test_compact_vlstrings /*------------------------------------------------------------------------- - * Function: test_write_vl_string_attribute + * Function: test_vlstring_attribute * - * Purpose: Test writing VL strings as attributes. + * Purpose: Test writing/reading VL strings on attributes. * * Return: None * @@ -534,17 +606,17 @@ static char *string_att_write=NULL; const H5std_string ATTRSTR_NAME("String_attr"); const H5std_string ATTRSTR_DATA("String Attribute"); -static void test_write_vl_string_attribute() +static void test_vlstring_attribute() { // Output message about test being performed - SUBTEST("Testing writing VL String as attributes"); + SUBTEST("Testing VL String on Attributes"); try { // Open the file H5File file1(FILENAME, H5F_ACC_RDWR); // Create a datatype to refer to. - StrType tid1(0, H5T_VARIABLE); + StrType vlst(0, H5T_VARIABLE); // Open the root group. Group root = file1.openGroup("/"); @@ -553,14 +625,14 @@ static void test_write_vl_string_attribute() DataSpace att_space (H5S_SCALAR); // Create an attribute for the root group. - Attribute gr_attr = root.createAttribute(ATTRSTR_NAME, tid1, att_space); + Attribute gr_attr = root.createAttribute(ATTRSTR_NAME, vlst, att_space); // Write data to the attribute. - gr_attr.write(tid1, ATTRSTR_DATA); + gr_attr.write(vlst, ATTRSTR_DATA); // Read and verify the attribute string as a string of chars. char *string_att_check; - gr_attr.read(tid1, &string_att_check); + gr_attr.read(vlst, &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); @@ -568,7 +640,7 @@ static void test_write_vl_string_attribute() // Read and verify the attribute string as an std::string. H5std_string read_str; - gr_attr.read(tid1, read_str); + gr_attr.read(vlst, 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()); @@ -576,28 +648,22 @@ static void test_write_vl_string_attribute() gr_attr.close(); // Test creating a "large" sized string attribute - gr_attr = root.createAttribute("test_scalar_large", tid1, att_space); + gr_attr = root.createAttribute("test_scalar_large", vlst, att_space); string_att_write = (char*)HDcalloc(8192, sizeof(char)); HDmemset(string_att_write, 'A', 8191); // Write data to the attribute, then read it back. - gr_attr.write(tid1, &string_att_write); - gr_attr.read(tid1, &string_att_check); + gr_attr.write(vlst, &string_att_write); + gr_attr.read(vlst, &string_att_check); // Verify data read. if(HDstrcmp(string_att_check,string_att_write)!=0) TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check); - HDfree(string_att_check); - gr_attr.close(); - - // Open attribute ATTRSTR_NAME again. - gr_attr = root.openAttribute(ATTRSTR_NAME); - // The attribute string written is freed below, in the - // test_read_vl_string_attribute() test - - // Close attribute and file + // Release resources. + HDfree(string_att_check); + HDfree(string_att_write); gr_attr.close(); file1.close(); @@ -606,9 +672,9 @@ static void test_write_vl_string_attribute() // Catch all exceptions. catch (Exception E) { - issue_fail_msg("test_string_attr()", __LINE__, __FILE__, E.getCDetailMsg()); + issue_fail_msg("test_vlstring_attribute()", __LINE__, __FILE__, E.getCDetailMsg()); } -} // test_string_attr() +} // test_vlstring_attribute() /*------------------------------------------------------------------------- * Function: test_read_vl_string_attribute @@ -633,7 +699,7 @@ static void test_read_vl_string_attribute() H5File file1(FILENAME, H5F_ACC_RDONLY); // Create a datatype to refer to. - StrType tid1(0, H5T_VARIABLE); + StrType vlst(0, H5T_VARIABLE); // Open the root group and its attribute named ATTRSTR_NAME. Group root = file1.openGroup("/"); @@ -641,7 +707,7 @@ static void test_read_vl_string_attribute() // Test reading "normal" sized string attribute char *string_att_check; - att.read(tid1, &string_att_check); + att.read(vlst, &string_att_check); if(HDstrcmp(string_att_check,ATTRSTR_DATA.c_str())!=0) TestErrPrintf("VL string attributes don't match!, string_att=%s, string_att_check=%s\n",ATTRSTR_DATA.c_str(),string_att_check); HDfree(string_att_check); @@ -649,7 +715,7 @@ static void test_read_vl_string_attribute() // Test reading "large" sized string attribute att = root.openAttribute("test_scalar_large"); - att.read(tid1, &string_att_check); + att.read(vlst, &string_att_check); if(HDstrcmp(string_att_check,string_att_write)!=0) TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check); HDfree(string_att_check); @@ -657,7 +723,7 @@ static void test_read_vl_string_attribute() // Close objects and file. att.close(); - tid1.close(); + vlst.close(); root.close(); file1.close(); @@ -672,7 +738,7 @@ static void test_read_vl_string_attribute() /*------------------------------------------------------------------------- - * Function: test_vl_stringarray_attribute + * Function: test_vlstring_array_attribute * * Purpose: Test writing/reading VL string array to/from attributes. * @@ -685,21 +751,21 @@ static void test_read_vl_string_attribute() */ const H5std_string ATTRSTRARR_NAME("StringArray_attr"); -static void test_vl_stringarray_attribute() +static void test_vlstring_array_attribute() { const char *string_att_array[SPACE1_DIM1]= { "Line 1", "Line 2", "Line 3", "Line 4" }; // Information to write // Output message about test being performed - SUBTEST("Testing writing/reading VL String Array on attribute"); + SUBTEST("Testing VL String Array on Attributes"); try { // Open the file H5File file1(FILENAME, H5F_ACC_RDWR); // Create a datatype to refer to. - StrType tid1(0, H5T_VARIABLE); + StrType vlst(0, H5T_VARIABLE); // Open the root group. Group root = file1.openGroup("/"); @@ -709,15 +775,15 @@ static void test_vl_stringarray_attribute() DataSpace att_space(SPACE1_RANK, dims1); // Create an attribute for the root group. - Attribute gr_attr = root.createAttribute(ATTRSTRARR_NAME, tid1, att_space); + Attribute gr_attr = root.createAttribute(ATTRSTRARR_NAME, vlst, att_space); // Write data to the attribute. - gr_attr.write(tid1, string_att_array); + gr_attr.write(vlst, string_att_array); // Read and verify the attribute string as a string of chars. // Note: reading by array of H5std_string doesn't work yet. char *string_att_check[SPACE1_DIM1]; - gr_attr.read(tid1, &string_att_check); + gr_attr.read(vlst, &string_att_check); int ii; for (ii = 0; ii < SPACE1_DIM1; ii++) @@ -737,9 +803,9 @@ static void test_vl_stringarray_attribute() // Catch all exceptions. catch (Exception E) { - issue_fail_msg("test_string_attr()", __LINE__, __FILE__, E.getCDetailMsg()); + issue_fail_msg("test_vlstring_array_attribute()", __LINE__, __FILE__, E.getCDetailMsg()); } -} // test_vl_stringarray_attribute() +} // test_vlstring_array_attribute() /* Helper routine for test_vl_rewrite() */ static void write_scalar_dset(H5File& file, DataType& type, DataSpace& space, @@ -885,17 +951,18 @@ void test_vlstrings() // These tests use the same file // Test basic VL string datatype - test_vlstrings_basic(); + test_vlstring_dataset(); test_vlstrings_special(); test_vlstring_type(); test_compact_vlstring(); // Test using VL strings in attributes - test_write_vl_string_attribute(); - test_read_vl_string_attribute(); + test_vlstring_attribute(); + // test_read_vl_string_attribute(); - // Test using VL string array in attributes - test_vl_stringarray_attribute(); + // Test using VL string array in attributes and datasets + test_vlstring_array_attribute(); + test_vlstring_array_dataset(); // Test writing VL datasets in files with lots of unlinking test_vl_rewrite(); |