From 8e27c898881ac22fb61737edc769fa3cf5372d4e Mon Sep 17 00:00:00 2001 From: Binh-Minh Ribler Date: Mon, 1 Jan 2007 21:17:36 -0500 Subject: [svn-r13093] Purpose: Adding test Description: Added variable-length string tests. Platforms tested AIX 5.1 (copper) SunOS 5.8 64-bit (sol) Linux 2.6 (kagiso) --- c++/test/Makefile.am | 2 +- c++/test/Makefile.in | 5 +- c++/test/tattr.cpp | 2 +- c++/test/testhdf5.cpp | 3 +- c++/test/tvlstr.cpp | 802 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 809 insertions(+), 5 deletions(-) create mode 100644 c++/test/tvlstr.cpp diff --git a/c++/test/Makefile.am b/c++/test/Makefile.am index bd04acf..61933f5 100644 --- a/c++/test/Makefile.am +++ b/c++/test/Makefile.am @@ -38,7 +38,7 @@ LDADD=$(LIBH5CPP) $(LIBH5TEST) $(LIBHDF5) dsets_SOURCES=dsets.cpp h5cpputil.cpp testhdf5_SOURCES=testhdf5.cpp tattr.cpp tfile.cpp th5s.cpp trefer.cpp \ - h5cpputil.cpp + tvlstr.cpp h5cpputil.cpp # Tell conclude.am that these are C++ tests. HDF_CXX=yes diff --git a/c++/test/Makefile.in b/c++/test/Makefile.in index 23348f7..920739f 100644 --- a/c++/test/Makefile.in +++ b/c++/test/Makefile.in @@ -74,7 +74,7 @@ dsets_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \ $(am__DEPENDENCIES_3) am_testhdf5_OBJECTS = testhdf5.$(OBJEXT) tattr.$(OBJEXT) \ tfile.$(OBJEXT) th5s.$(OBJEXT) trefer.$(OBJEXT) \ - h5cpputil.$(OBJEXT) + tvlstr.$(OBJEXT) h5cpputil.$(OBJEXT) testhdf5_OBJECTS = $(am_testhdf5_OBJECTS) testhdf5_LDADD = $(LDADD) testhdf5_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \ @@ -346,7 +346,7 @@ TEST_PROG = dsets testhdf5 LDADD = $(LIBH5CPP) $(LIBH5TEST) $(LIBHDF5) dsets_SOURCES = dsets.cpp h5cpputil.cpp testhdf5_SOURCES = testhdf5.cpp tattr.cpp tfile.cpp th5s.cpp trefer.cpp \ - h5cpputil.cpp + tvlstr.cpp h5cpputil.cpp # Tell conclude.am that these are C++ tests. @@ -428,6 +428,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tfile.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/th5s.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/trefer.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tvlstr.Po@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ if $(CXXCOMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \ diff --git a/c++/test/tattr.cpp b/c++/test/tattr.cpp index df9309c..08eef83 100644 --- a/c++/test/tattr.cpp +++ b/c++/test/tattr.cpp @@ -1104,7 +1104,7 @@ test_attr_dtype_shared(void) DataSet *dset2 = new DataSet (fid1.openDataSet(DSET1_NAME)); // Open attribute - Attribute *attr2 = new Attribute (dset.openAttribute(ATTR1_NAME)); + Attribute *attr2 = new Attribute (dset2->openAttribute(ATTR1_NAME)); // Read data from the attribute attr2->read(PredType::NATIVE_INT, &rdata); diff --git a/c++/test/testhdf5.cpp b/c++/test/testhdf5.cpp index 04f00a8..f157000 100644 --- a/c++/test/testhdf5.cpp +++ b/c++/test/testhdf5.cpp @@ -77,11 +77,12 @@ main(int argc, char *argv[]) AddTest("attr", test_attr, cleanup_attr, "Attributes", NULL); // testing reference functionalities in trefer.cpp AddTest("reference", test_reference, cleanup_reference, "References", NULL); + // testing variable-length strings in tvlstr.cpp + AddTest("vlstrings", test_vlstrings, cleanup_vlstrings, "Variable-Length Strings", NULL); /* Comment out tests that are not done yet. - BMR, Feb 2001 AddTest("select", test_select, cleanup_select, "Selections", NULL); AddTest("time", test_time, cleanup_time, "Time Datatypes", NULL); AddTest("vltypes", test_vltypes, cleanup_vltypes, "Variable-Length Datatypes", NULL); - AddTest("vlstrings", test_vlstrings, cleanup_vlstrings, "Variable-Length Strings", NULL); AddTest("iterate", test_iterate, cleanup_iterate, "Group & Attribute Iteration", NULL); AddTest("array", test_array, cleanup_array, "Array Datatypes", NULL); AddTest("genprop", test_genprop, cleanup_genprop, "Generic Properties", NULL); diff --git a/c++/test/tvlstr.cpp b/c++/test/tvlstr.cpp new file mode 100644 index 0000000..145c4c8 --- /dev/null +++ b/c++/test/tvlstr.cpp @@ -0,0 +1,802 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright by The HDF Group (THG). * + * 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://www.hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/*********************************************************** +* +* Test program: tvlstr +* +* Test the variable length string functionality +* +*************************************************************/ + +#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 + +const H5std_string DATAFILE("tvlstr.h5"); +const H5std_string DATAFILE2("tvlstr2.h5"); + +// 1-D dataset with fixed dimensions +const int SPACE1_RANK = 1; +const hsize_t SPACE1_DIM1 = 4; + +// 2-D dataset with fixed dimensions +const int SPACE2_RANK = 2; +const hsize_t SPACE2_DIM1 = 10; +const hsize_t SPACE2_DIM2 = 10; + +const H5std_string VLSTR_TYPE("vl_string_type"); + +// Definitions for the VL re-writing test +const int REWRITE_NDATASETS = 32; + +/*********************************************************** +* +* Test program: tvlstr +* +* Test the Variable-Length String functionality +* +*************************************************************/ + +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. +** +****************************************************************/ +void *test_vlstr_alloc_custom(size_t size, void *info) +{ + void *ret_value=NULL; // Pointer to return + int *mem_used=(int *)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. +** +****************************************************************/ +void test_vlstr_free_custom(void *_mem, void *info) +{ + unsigned char *mem; + int *mem_used=(int *)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 +} + +/**************************************************************** +** +** test_vlstrings_basic(): Test basic VL string code. +** Tests simple VL string I/O +** +****************************************************************/ +static void +test_vlstrings_basic(void) +{ + 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 + MESSAGE(5, ("Testing Basic VL String Functionality\n")); + + H5File* file1 = NULL; + try { + // Create file. + file1 = new H5File (DATAFILE, 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("Dataset1", tid1, sid1)); + + // Write dataset to disk. + dataset.write(wdata, tid1); + + // Create H5S_SCALAR data space. + DataSpace dataspace; + + DataSet dataset2(file1->createDataSet("Dataset2", tid1, dataspace)); + + char *wdata2 = (char*)HDcalloc(65534, sizeof(char)); + HDmemset(wdata2, 'A', 65533); + + dataset2.write(&wdata2, tid1); + + dataspace.close(); + dataset2.close(); + HDfree(wdata2); + + // Change to the custom memory allocation routines for reading VL string. + DSetMemXferPropList xfer; + int 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. + int str_used; // String data in memory + hsize_t i; // counting variable + for (i=0,str_used=0; iclose(); + } // end try + + // Catch all exceptions. + catch (Exception E) + { + issue_fail_msg(E.getCFuncName(), __LINE__, __FILE__, E.getCDetailMsg()); + if (file1 != NULL) // clean up + delete file1; + } +} // end test_vlstrings_basic() + +/**************************************************************** +** +** test_vlstrings_special(): Test VL string code for special +** string cases, nil and zero-sized. +** +****************************************************************/ +static void +test_vlstrings_special(void) +{ + 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. + MESSAGE(5, ("Testing Special VL Strings\n")); + + try { + // Create file. + H5File file1(DATAFILE, 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 dataset before writing data. + dataset.read(rdata, tid1); + + // Check data read in. + hsize_t i; // counting variable + for (i=0; iopenStrType(VLSTR_TYPE); + + // Close datatype and file. + vlstr_type.close(); + file1->close(); + + // Open file. + file1 = new H5File(DATAFILE, H5F_ACC_RDWR); + + //fid = H5Fopen(DATAFILE.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); + + // Open the variable-length string datatype just created + vlstr_type.setId((file1->openStrType(VLSTR_TYPE)).getId()); + //tid_vlstr = H5Topen(fid, VLSTR_TYPE.c_str()); + + // Verify character set and padding + cset = vlstr_type.getCset(); + verify_val(cset, H5T_CSET_ASCII, "StrType::getCset", __LINE__, __FILE__); + //cset = H5Tget_cset(tid_vlstr); + pad = vlstr_type.getStrpad(); + //pad = H5Tget_strpad(tid_vlstr); + verify_val(pad, H5T_STR_NULLPAD, "StrType::getStrpad", __LINE__, __FILE__); + + // Close datatype and file + vlstr_type.close(); + file1->close(); + } // end try + + // Catch all exceptions. + catch (Exception E) + { + issue_fail_msg(E.getCFuncName(), __LINE__, __FILE__, E.getCDetailMsg()); + } +} // end test_vlstring_type() + +/**************************************************************** +** +** test_compact_vlstring(): Test code for storing VL strings in +** compact datasets. +** +****************************************************************/ +static void +test_compact_vlstring(void) +{ + const char *wdata[SPACE1_DIM1] = {"one", "two", "three", "four"}; + char *rdata[SPACE1_DIM1]; // Information read in + + // Output message about test being performed + MESSAGE(5, ("Testing VL Strings in compact dataset\n")); + + try { + // Create file + H5File file1(DATAFILE, 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 + dataset.write(wdata, tid1); + + // Read dataset from disk + dataset.read(rdata, tid1); + + // Compare data read in + hsize_t i; + for (i=0; i