adding units tests for #44

This commit is contained in:
Shane Grant 2014-01-16 15:15:11 -08:00
parent ce009d9c9a
commit d5e813aa56

View File

@ -1011,6 +1011,140 @@ BOOST_AUTO_TEST_CASE( json_multimap )
test_multimap<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}
// ######################################################################
struct MemoryCycle
{
MemoryCycle() = default;
MemoryCycle( int v ) :
value( v )
{ }
int value;
std::shared_ptr<MemoryCycle> ptr;
bool operator==( MemoryCycle const & other ) const
{
return value == other.value && ptr == other.ptr;
}
template <class Archive>
void serialize( Archive & ar )
{
ar( value, ptr );
}
};
std::ostream& operator<<(std::ostream& os, MemoryCycle const & s)
{
os << "[value: " << s.value << " ptr: " << s.ptr << "]";
return os;
}
class MemoryCycleLoadAndAllocate
{
public:
MemoryCycleLoadAndAllocate( int v ) :
value( v )
{ }
MemoryCycleLoadAndAllocate( int v,
std::shared_ptr<MemoryCycleLoadAndAllocate> p ) :
value( v ),
ptr( p )
{ }
bool operator==( MemoryCycleLoadAndAllocate const & other ) const
{
return value == other.value && ptr == other.ptr;
}
template <class Archive>
void serialize( Archive & ar )
{
ar( value, ptr );
}
template <class Archive>
static MemoryCycleLoadAndAllocate * load_and_allocate( Archive & ar )
{
int value;
std::shared_ptr<MemoryCycleLoadAndAllocate> ptr;
ar( value, ptr );
return new MemoryCycleLoadAndAllocate( value, ptr );
}
int value;
std::shared_ptr<MemoryCycleLoadAndAllocate> ptr;
};
std::ostream& operator<<(std::ostream& os, MemoryCycleLoadAndAllocate const & s)
{
os << "[value: " << s.value << " ptr: " << s.ptr << "]";
return os;
}
template <class IArchive, class OArchive>
void test_memory_cycles()
{
std::random_device rd;
std::mt19937 gen(rd());
for(int ii=0; ii<100; ++ii)
{
auto o_ptr1 = std::make_shared<MemoryCycle>( random_value<int>(gen) );
o_ptr1->ptr = o_ptr1;
auto o_ptr2 = std::make_shared<MemoryCycleLoadAndAllocate>( random_value<int>(gen) );
o_ptr2->ptr = o_ptr2;
std::ostringstream os;
{
OArchive oar(os);
oar( o_ptr1 );
oar( o_ptr2 );
}
decltype(o_ptr1) i_ptr1;
decltype(o_ptr2) i_ptr2;
std::istringstream is(os.str());
{
IArchive iar(is);
iar( i_ptr1 );
iar( i_ptr2 );
}
BOOST_CHECK_EQUAL( o_ptr1->value, i_ptr1->value );
BOOST_CHECK_EQUAL( i_ptr1.get(), i_ptr1->ptr.get() );
BOOST_CHECK_EQUAL( o_ptr2->value, i_ptr2->value );
BOOST_CHECK_EQUAL( i_ptr2.get(), i_ptr2->ptr.get() );
}
}
BOOST_AUTO_TEST_CASE( binary_memory_cycles )
{
test_memory_cycles<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
}
BOOST_AUTO_TEST_CASE( portable_binary_memory_cycles )
{
test_memory_cycles<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
}
BOOST_AUTO_TEST_CASE( xml_memory_cycles )
{
test_memory_cycles<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
}
BOOST_AUTO_TEST_CASE( json_memory_cycles )
{
test_memory_cycles<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
}
// ######################################################################
template <class IArchive, class OArchive>
void test_memory()