adding tests for load_and_allocate

relates #44
This commit is contained in:
Shane Grant
2014-01-22 16:33:56 -08:00
parent 9d4f51ee10
commit 6f7ca3ea99
3 changed files with 136 additions and 7 deletions

View File

@@ -1145,6 +1145,135 @@ BOOST_AUTO_TEST_CASE( json_memory_cycles )
test_memory_cycles<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}
// ######################################################################
struct OneLA
{
OneLA( int xx ) : x( xx ) {}
int x;
template <class Archive>
void serialize( Archive & ar )
{ ar( x ); }
template <class Archive>
static void load_and_allocate( Archive & ar, cereal::allocate<OneLA> & allocate )
{
int xx;
ar( xx );
allocate( xx );
}
bool operator==( OneLA const & other ) const
{ return x == other.x; }
};
std::ostream& operator<<(std::ostream& os, OneLA const & s)
{
os << "[" << s.x << "]";
return os;
}
struct TwoLA
{
TwoLA( int xx ) : x( xx ) {}
int x;
template <class Archive>
void serialize( Archive & ar )
{ ar( x ); }
bool operator==( TwoLA const & other ) const
{ return x == other.x; }
};
std::ostream& operator<<(std::ostream& os, TwoLA const & s)
{
os << "[" << s.x << "]";
return os;
}
namespace cereal
{
template <>
struct LoadAndAllocate<TwoLA>
{
template <class Archive>
static void load_and_allocate( Archive & ar, cereal::allocate<TwoLA> & allocate )
{
int xx;
ar( xx );
allocate( xx );
}
};
}
template <class IArchive, class OArchive>
void test_memory_load_allocate()
{
std::random_device rd;
std::mt19937 gen(rd());
for(int ii=0; ii<100; ++ii)
{
auto o_shared1 = std::make_shared<OneLA>( random_value<int>(gen) );
auto o_shared2 = std::make_shared<TwoLA>( random_value<int>(gen) );
std::unique_ptr<OneLA> o_unique1( new OneLA( random_value<int>(gen) ) );
std::unique_ptr<TwoLA> o_unique2( new TwoLA( random_value<int>(gen) ) );
std::ostringstream os;
{
OArchive oar(os);
oar( o_shared1 );
oar( o_shared2 );
oar( o_unique1 );
oar( o_unique2 );
}
decltype(o_shared1) i_shared1;
decltype(o_shared2) i_shared2;
decltype(o_unique1) i_unique1;
decltype(o_unique2) i_unique2;
std::istringstream is(os.str());
{
IArchive iar(is);
iar( i_shared1 );
iar( i_shared2 );
iar( i_unique1 );
iar( i_unique2 );
}
BOOST_CHECK_EQUAL( *o_shared1, *i_shared1 );
BOOST_CHECK_EQUAL( *o_shared2, *i_shared2 );
BOOST_CHECK_EQUAL( *o_unique1, *i_unique1 );
BOOST_CHECK_EQUAL( *o_unique2, *i_unique2 );
}
}
BOOST_AUTO_TEST_CASE( binary_memory_load_allocate )
{
test_memory_load_allocate<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
}
BOOST_AUTO_TEST_CASE( portable_binary_memory_load_allocate )
{
test_memory_load_allocate<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
}
BOOST_AUTO_TEST_CASE( xml_memory_load_allocate )
{
test_memory_load_allocate<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
}
BOOST_AUTO_TEST_CASE( json_memory_load_allocate )
{
test_memory_load_allocate<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}
// ######################################################################
template <class IArchive, class OArchive>
void test_memory()