Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-06-05 13:42:19


Frank Birbacher:
> Robert Ramey schrieb:
>> If someone really want's to make portable_binary_archive an "officially
>> supported archive". Then some way of testing is required. This would
>> require generating output on one platform and reading on another. This
>> is
>> something that I couldn't figure out how to make BJAM do.
>
> Well, you could just instead check whether the produced output is the
> same everywhere. I thought of calculating the md5 sum and checking
> against that. That would make a good unit test. Or am I missing something?

This is a good way to test the output part; the input can be tested by
trying to read a pre-written archive (possibly embedded in the test source).
I made a similar test once when I was playing with shared_ptr serialization:

#include <boost/serialization/shared_ptr_132.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/weak_ptr.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/export.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <iostream>

struct A
{
    int x_;

 template<class Archive>
    void serialize( Archive & ar, unsigned )
 {
        ar & BOOST_SERIALIZATION_NVP( x_ );
    }

    static int instances;

 explicit A( int x = 0 ): x_( x )
 {
  ++instances;
 }

 virtual ~A()
 {
  --instances;
 }

 bool operator==( const A & rhs ) const
 {
        return x_ == rhs.x_;
    }
};

int A::instances;

struct B: public A
{
    int y_;

 template<class Archive>
    void serialize( Archive & ar, unsigned )
 {
        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( A );
        ar & BOOST_SERIALIZATION_NVP( y_ );
    }

 static int instances;

 explicit B( int x = 0, int y = 0 ): A( x ), y_( y )
 {
  ++instances;
 }

    ~B()
 {
  --instances;
 }

 bool operator==( const B & rhs ) const
 {
        return x_ == rhs.x_ && y_ == rhs.y_;
    }
};

int B::instances;

BOOST_SHARED_POINTER_EXPORT( B )

template<class Ar> void write_132( Ar & ar )
{
 boost_132::shared_ptr<B> p1( new B( 701, 801 ) );
 boost_132::shared_ptr<A> p2( p1 );
 boost_132::shared_ptr<A> p3( new B( 703, 803 ) );
 boost_132::shared_ptr<A> p4( new A( 704 ) );
 boost_132::shared_ptr<A> p5( p4 );

 ar << BOOST_SERIALIZATION_NVP( p1 );
 ar << BOOST_SERIALIZATION_NVP( p2 );
 ar << BOOST_SERIALIZATION_NVP( p3 );
 ar << BOOST_SERIALIZATION_NVP( p4 );
 ar << BOOST_SERIALIZATION_NVP( p5 );
}

template<class Ar> void write_135( Ar & ar )
{
 boost::shared_ptr<B> p1( new B( 701, 801 ) );
 boost::shared_ptr<A> p2( p1 );
 boost::shared_ptr<A> p3( new B( 703, 803 ) );
 boost::shared_ptr<A> p4( new A( 704 ) );
 boost::shared_ptr<A> p5( p4 );

 ar << BOOST_SERIALIZATION_NVP( p1 );
 ar << BOOST_SERIALIZATION_NVP( p2 );
 ar << BOOST_SERIALIZATION_NVP( p3 );
 ar << BOOST_SERIALIZATION_NVP( p4 );
 ar << BOOST_SERIALIZATION_NVP( p5 );
}

#include <boost/detail/lightweight_test.hpp>

template<class Ar> void read( Ar & ar )
{
 boost::shared_ptr<B> p1; // ( new B( 701, 801 ) );
 boost::shared_ptr<A> p2; // ( p1 );
 boost::shared_ptr<A> p3; // ( new B( 703, 803 ) );
 boost::shared_ptr<A> p4; // ( new A( 704 ) );
 boost::shared_ptr<A> p5; // ( p4 );

 ar >> BOOST_SERIALIZATION_NVP( p1 );

 BOOST_TEST( p1 && *p1 == B( 701, 801 ) );

 ar >> BOOST_SERIALIZATION_NVP( p2 );

 BOOST_TEST( p1 == p2 );
 BOOST_TEST( !( p1 < p2 ) && !( p2 < p1 ) );

 ar >> BOOST_SERIALIZATION_NVP( p3 );

 BOOST_TEST( p3 && dynamic_cast<B*>( p3.get() ) && dynamic_cast<B&>( *p3 )
== B( 703, 803 ) );

 ar >> BOOST_SERIALIZATION_NVP( p4 );

 BOOST_TEST( p4 && *p4 == A( 704 ) );

 ar >> BOOST_SERIALIZATION_NVP( p5 );

 BOOST_TEST( p4 == p5 );
 BOOST_TEST( !( p4 < p5 ) && !( p5 < p4 ) );
}

char const * data_132 = "22 serialization::archive 4 0 0 2 1 0 0 1 0 1 701
801 0 0 1 1 0 2 2 0 0 0 2 0 1 2 2 3 4 703 803 1 5 2 3 3 6 704 6 1 0 7 3 6 3
6 6 7";
char const * data_135 = "22 serialization::archive 4 0 1 1 1 0 0 1 0 1 701
801 0 1 1 0 1 2 3 703 803 2 4 704 2 4";

#include <boost/archive/text_iarchive.hpp>
#include <sstream>

int main()
{
 //{
 // boost::archive::text_oarchive ar( std::cout );
 // write_132( ar );
 // std::cout << "\n--\n\n";
 //}

 //{
 // boost::archive::text_oarchive ar( std::cout );
 // write_135( ar );
 // std::cout << "\n--\n\n";
 //}

 {
  std::istringstream is( data_132 );
  boost::archive::text_iarchive ar( is );
  read( ar );
 }

 {
  std::istringstream is( data_135 );
  boost::archive::text_iarchive ar( is );
  read( ar );
 }

 return boost::report_errors();
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net