NetCDF4 C++ API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Test_NcString.cpp

Test of class NcString.

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include <iostream>
using std::cout;
using std::endl;
#include <memory>
#include <sstream>
#include "netcdf4"
using namespace netcdf;
#include "AssertEx.h"
//-----------------------------------------------------------------------------
// configuration
char const * const ncFileName = "Test_NcString.nc";
char const * const varNames[] = { "strings", "oneString", "emptyString" };
//-----------------------------------------------------------------------------
std::vector< std::string > & strVec,
short const numStrings)
{
if ( numStrings > 0 )
{
strVec.resize( numStrings );
for ( short i = 0; i < numStrings; ++i )
{
std::ostringstream os;
os << "string " << i << ":";
for ( short n = 0; n <= i; ++n )
os << " " << n;
strVec[i] = os.str();
}
}
}
//-----------------------------------------------------------------------------
std::auto_ptr<NcFile> CreateNcFile (
std::string const & fileName )
{
std::auto_ptr<NcFile> pNcFile (
new NcFile ( fileName, NcFile::Replace, NcFile::NetCDF4 ) );
NcDim strDim = pNcFile->AddDim ( "count" );
Assert(!strDim.IsNull());
NcVar strVar = pNcFile->AddVar ( varNames[0], ncString, strDim );
Assert(!strVar.IsNull());
NcVar varScalarStr = pNcFile->AddVar ( varNames[1], ncString );
Assert(!varScalarStr.IsNull());
NcVar varEmptyStr = pNcFile->AddVar ( varNames[2], ncString );
Assert(!varScalarStr.IsNull());
pNcFile->ToDataMode();
return pNcFile;
}
//-----------------------------------------------------------------------------
void AddData (
NcFile & ncFile,
std::vector< std::string > const & strVec,
std::string const & oneStr)
{
NcVar var = ncFile.GetVar(varNames[0]);
Assert(!var.IsNull());
var.Put( strVec );
var = ncFile.GetVar(varNames[1]);
Assert(!var.IsNull());
var.Put (oneStr);
var = ncFile.GetVar(varNames[2]);
Assert(!var.IsNull());
std::string emptyStr;
var.Put (emptyStr);
}
//-----------------------------------------------------------------------------
void ReadData (
NcFile & ncFile,
std::vector< std::string > & strVec,
std::string & oneStr)
{
NcVar var = ncFile.GetVar(varNames[0]);
Assert(!var.IsNull());
var.Get( strVec );
var = ncFile.GetVar(varNames[1]);
Assert(!var.IsNull());
var.Get( oneStr );
var = ncFile.GetVar(varNames[2]);
Assert(!var.IsNull());
std::string emptyStr;
var.Get (emptyStr);
}
//-----------------------------------------------------------------------------
bool ReadAndCompare_StringAt ( // returns true if compared ok else false
NcFile & nc,
std::vector< std::string > const & strVec)
{
NcVar var = nc.GetVar(varNames[0]);
Assert(!var.IsNull());
std::string str;
var.Get( 1, str ); // read 2nd string
// compare
bool ok = ( str == strVec[1] );
return ok;
}
//-----------------------------------------------------------------------------
std::string const & Test ( bool const result )
{
static std::string const strs[] = { "passed", "failed" };
return ( result ? strs[0] : strs[1] );
}
//-----------------------------------------------------------------------------
int main ()
{
cout << "Test of class NcString I/O." << endl;
try
{
cout << "Making string array..." << endl;
std::size_t const numStrings = 5;
std::vector< std::string > strVec;
MakeStringVecWithData ( strVec, numStrings );
cout << "Creating file: " << ncFileName << endl;
std::auto_ptr<NcFile> pNcFile = CreateNcFile (ncFileName);
cout << "Add data to file..." << endl;
std::string const oneStr = "Do wah ditty";
AddData( *pNcFile, strVec, oneStr );
cout << "Read data from file..." << endl;
std::vector< std::string > strVecIn;
std::string oneStrIn;
ReadData( *pNcFile, strVecIn, oneStrIn );
// are values equal?
bool areEqual = strVec.size() == strVecIn.size();
if ( areEqual )
areEqual = std::equal( strVec.begin(), strVec.end(), strVecIn.begin() );
cout << "variable vector<string>: original == input ? "
<< ( areEqual ? "yes" : "no" )
<< endl;
cout << "variable scalar string: original == input ? "
<< ( (oneStr == oneStrIn) ? "yes" : "no" )
<< endl;
cout << " read string[1] : " << Test( ReadAndCompare_StringAt(*pNcFile, strVec) ) << endl;
}
catch (std::exception const & e)
{
cout << "Exception: " << e.what() << endl;
}
catch (...)
{
cout << "Error: unknown error." << endl;
}
cout << "\n all done!" << endl;
return 0;
}
//-----------------------------------------------------------------------------