/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 tvlstr.cpp - HDF5 C++ testing the Variable-Length String functionality EXTERNAL ROUTINES/VARIABLES: These routines are in the test directory of the C library: ***************************************************************************/ #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 // Data file used in most test functions const H5std_string FILENAME("tvlstr.h5"); // 1-D dataset with fixed dimensions const int SPACE1_RANK = 1; const hsize_t SPACE1_DIM1 = 4; // Utility functions void *test_vlstr_alloc_custom(size_t size, void *info); void test_vlstr_free_custom(void *mem, void *info); /**************************************************************** ** ** test_vlstr_alloc_custom(): Test VL datatype custom memory ** allocation routines. This routine just uses malloc to ** allocate the memory and increments the amount of memory ** allocated. It is passed into setVlenMemManager. ** ** Note: exact copy from the C version. ** ****************************************************************/ void *test_vlstr_alloc_custom(size_t size, void *info) { void *ret_value = NULL; // Pointer to return size_t *mem_used = (size_t *)info; // Get the pointer to the memory used size_t extra; // Extra space needed /* * This weird contortion is required on the DEC Alpha to keep the * alignment correct - QAK */ extra = MAX(sizeof(void *),sizeof(size_t)); if((ret_value = HDmalloc(extra + size)) != NULL) { *(size_t *)ret_value = size; *mem_used += size; } // end if ret_value = ((unsigned char *)ret_value) + extra; return(ret_value); } /**************************************************************** ** ** test_vlstr_free_custom(): Test VL datatype custom memory ** allocation routines. This routine just uses free to ** release the memory and decrements the amount of memory ** allocated. It is passed into setVlenMemManager. ** ** Note: exact copy from the C version. ** ****************************************************************/ void test_vlstr_free_custom(void *_mem, void *info) { unsigned char *mem; size_t *mem_used=(size_t *)info; // Get the pointer to the memory used size_t extra; // Extra space needed /* * This weird contortion is required on the DEC Alpha to keep the * alignment correct - QAK */ extra=MAX(sizeof(void *),sizeof(size_t)); if(_mem!=NULL) { mem=((unsigned char *)_mem)-extra; *mem_used-=*(size_t *)mem; HDfree(mem); } // end if } /*------------------------------------------------------------------------- * Function: test_vlstrings_basic * * Purpose: Test simple VL string I/O. * * Return: None * * Programmer: Binh-Minh Ribler (use C version) * January, 2007 * *------------------------------------------------------------------------- */ static void test_vlstrings_basic() { 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." }; // Information to write // Output message about test being performed SUBTEST("Testing Basic VL String Functionality"); H5File* file1 = NULL; try { // Create file. file1 = new H5File (FILENAME, H5F_ACC_TRUNC); // Create dataspace for datasets. hsize_t dims1[] = {SPACE1_DIM1}; DataSpace sid1(SPACE1_RANK, dims1); // Create a datatype to refer to. StrType tid1(0, H5T_VARIABLE); // Create and write a dataset. DataSet dataset(file1->createDataSet("Dataset1", tid1, sid1)); dataset.write(wdata, tid1); // Create H5S_SCALAR data space. DataSpace scalar_space; // Create and write another dataset. DataSet dataset2(file1->createDataSet("Dataset2", tid1, scalar_space)); char *wdata2 = (char*)HDcalloc(65534, sizeof(char)); HDmemset(wdata2, 'A', 65533); dataset2.write(&wdata2, tid1); // 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; iclose(); PASSED(); } // end try // Catch all exceptions. catch (Exception E) { issue_fail_msg("test_vlstrings_basic()", __LINE__, __FILE__, E.getCDetailMsg()); if (file1 != NULL) // clean up delete file1; } } // end test_vlstrings_basic() /*------------------------------------------------------------------------- * Function: test_vlstrings_special * * Purpose: Test VL string code for special string cases, nil and * zero-sized. * * Return: None * * Programmer: Binh-Minh Ribler (use C version) * January, 2007 * *------------------------------------------------------------------------- */ static void test_vlstrings_special() { const char *wdata[SPACE1_DIM1] = {"one", "two", "", "four"}; const char *wdata2[SPACE1_DIM1] = {NULL, NULL, NULL, NULL}; char *rdata[SPACE1_DIM1]; // Information read in // Output message about test being performed. SUBTEST("Testing Special VL Strings"); try { // Create file. H5File file1(FILENAME, H5F_ACC_TRUNC); // Create dataspace for datasets. hsize_t dims1[] = {SPACE1_DIM1}; DataSpace sid1(SPACE1_RANK, dims1); // Create a datatype to refer to. StrType tid1(0, H5T_VARIABLE); // Create a dataset. DataSet dataset(file1.createDataSet("Dataset3", tid1, sid1)); // Read from the dataset before writing data. dataset.read(rdata, tid1); // Check data read in. hsize_t i; // counting variable for(i = 0; i < SPACE1_DIM1; i++) if(rdata[i] != NULL) 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); // 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); // Close Dataset. dataset.close(); /* * Create another dataset to test nil strings. */ // Create the property list and set the fill value for the second // dataset. DSetCreatPropList dcpl; char *fill = NULL; // Fill value dcpl.setFillValue(tid1, &fill); dataset = file1.createDataSet("Dataset4", tid1, sid1, dcpl); // Close dataset creation property list. dcpl.close(); // Read from dataset before writing data. dataset.read(rdata, tid1); // Check data read in. for (i=0; iopenStrType(VLSTR_TYPE); // Close datatype and file. vlstr_type.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); // Verify character set and padding cset = vlstr_type.getCset(); verify_val(cset, H5T_CSET_ASCII, "StrType::getCset", __LINE__, __FILE__); pad = vlstr_type.getStrpad(); verify_val(pad, H5T_STR_NULLPAD, "StrType::getStrpad", __LINE__, __FILE__); // Close datatype and file vlstr_type.close(); file1->close(); PASSED(); } // end try block // Catch all exceptions. catch (Exception E) { issue_fail_msg("test_vlstring_type()", __LINE__, __FILE__, E.getCDetailMsg()); } } // end test_vlstring_type() /*------------------------------------------------------------------------- * Function: test_compact_vlstring * * Purpose: Test storing VL strings in compact datasets. * * Return: None * * Programmer: Binh-Minh Ribler (use C version) * January, 2007 * *------------------------------------------------------------------------- */ static void test_compact_vlstring() { // Output message about test being performed SUBTEST("Testing VL Strings in compact dataset"); try { // Create file H5File file1(FILENAME, H5F_ACC_TRUNC); // Create dataspace for datasets hsize_t dims1[] = {SPACE1_DIM1}; DataSpace sid1(SPACE1_RANK, dims1); // Create a datatype to refer to StrType tid1(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)); // Write dataset to disk const char *wdata[SPACE1_DIM1] = {"one", "two", "three", "four"}; dataset.write(wdata, tid1); // Read dataset from disk char *rdata[SPACE1_DIM1]; // Information read in dataset.read(rdata, tid1); // Compare data read in hsize_t i; for (i=0; i