tstStateSetIO.cpp

./Core/io/tests/tstStateSetIO.cpp

#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include "ScaleUtils/IO/Utils.h"
#include "Origen/Core/config.h"
#include "Nemesis/gtest/nemesis_gtest.hh"
class StateSetIOTester : public ::testing::TestWithParam<const char*>
{
protected:
void TearDown() { Origen::printTimingReport( std::cout ); }
};
TEST_P( StateSetIOTester, RoundTrip )
{
ScaleUtils::IO::DB opts;
std::string file = "test." + std::string( GetParam() ) + ".f71";
ScaleUtils::IO::removeFile( file );
opts.set( "fileFormat", GetParam() );
ASSERT_TRUE( io.save( set, file, opts ) );
opts.set( "fileFormat", "" ); // use trial and error to get format
ASSERT_TRUE( io.load( new_set, file, opts ) );
EXPECT_EQ( set, new_set );
}
INSTANTIATE_TEST_CASE_P( StateSetTesterFormats,
::testing::Values( "s62b", "bof" ) );
TEST_F( StateSetIOTester, AppendStream )
{
std::ofstream ofs;
std::remove( "testOriginalPlus1.f71" );
std::remove( "testAppend.f71" );
bool pass;
// write the original+1
{
ScaleUtils::IO::DB opts;
opts.prepareBinaryFileWrite( ofs, "testOriginalPlus1.f71" );
opts.set( "fileFormat", "bof" );
set.add_states( set.states_at( 1 ) );
pass = io.write( set, ofs, opts );
EXPECT_TRUE( pass );
ofs.close();
}
// write it in two steps with append
{
ScaleUtils::IO::DB opts;
opts.prepareBinaryFileWrite( ofs, "testAppend.f71" );
opts.set( "fileFormat", "bof" );
pass = io.write( set, ofs, opts );
EXPECT_TRUE( pass );
// write an extra piece
{
s2.add_states( set.states_at( 2 ) );
opts.set( "append", true );
pass = io.write( s2, ofs, opts );
EXPECT_TRUE( pass );
}
ofs.close();
}
// load appended state written to disk
{
ScaleUtils::IO::DB opts;
ASSERT_TRUE( io.load( set2, "testAppend.f71", opts ) );
}
// now states should be equal
set.add_states( set.states_at( 2 ) );
// compare for equality
EXPECT_EQ( set, set2 );
}
TEST_F( StateSetIOTester, AppendFile )
{
ASSERT_EQ( 44, set.states_size() );
std::remove( "testOriginal.f71" );
std::remove( "testAppend.f71" );
// add data copies so that write takes longer in order to test whether the
// append time increases with additional data copies
size_t nadd = 10;
for( size_t i = 0; i < nadd; ++i ) set.add_states( set.states_at( 1 ) );
ASSERT_EQ( 54, set.states_size() );
// save the original with copies
{
SCOPED_TIMER( "single write" );
ScaleUtils::IO::DB opts;
opts.set( "appendExisting", true );
bool pass = saveStateSet( set, "testOriginal.f71", opts );
EXPECT_TRUE( pass );
}
// save it in two steps with append
{
SCOPED_TIMER( "using append" );
ScaleUtils::IO::DB opts;
opts.set( "appendExisting", true );
bool pass = saveStateSet( set, "testAppend.f71", opts );
EXPECT_TRUE( pass );
// write an extra piece
{
SCOPED_TIMER( "append time only" );
s2.add_states( set.states_at( 2 ) ); // last state
opts.set( "appendExisting", true );
pass = saveStateSet( s2, "testAppend.f71", opts );
EXPECT_TRUE( pass );
}
}
// clear all opts
Origen::SP_StateSet appended_set;
{
SCOPED_TIMER( "load time" );
ScaleUtils::IO::DB opts;
// load appended state written to disk
appended_set = Origen::SP_StateSet(
Origen::loadStateSet( "testAppend.f71", opts ) );
ASSERT_FALSE( appended_set == nullptr );
ASSERT_EQ( 55, appended_set->states_size() );
}
// now states should be equal
set.add_states( set.states_at( 2 ) ); // last state
ASSERT_EQ( 55, set.states_size() );
// compare for equality
EXPECT_TRUE( set == *appended_set ); //<<set.to_string();
{
std::ofstream ofs( "appended_set.txt" );
ofs << appended_set->to_string() << std::endl;
}
{
std::ofstream ofs( "set.txt" );
ofs << set.to_string() << std::endl;
}
}