1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/ptr_container/src/serialization.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,229 @@
1.4 +//
1.5 +// Boost.Pointer Container
1.6 +//
1.7 +// Copyright Thorsten Ottosen 2003-2005. Use, modification and
1.8 +// distribution is subject to the Boost Software License, Version
1.9 +// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1.10 +// http://www.boost.org/LICENSE_1_0.txt)
1.11 +//
1.12 +// For more information, see http://www.boost.org/libs/ptr_container/
1.13 +//
1.14 +
1.15 +#include <boost/test/unit_test.hpp>
1.16 +#include <boost/archive/text_oarchive.hpp>
1.17 +#include <boost/archive/text_iarchive.hpp>
1.18 +#include <boost/ptr_container/ptr_vector.hpp>
1.19 +#include <boost/ptr_container/ptr_deque.hpp>
1.20 +#include <boost/ptr_container/ptr_list.hpp>
1.21 +#include <boost/ptr_container/ptr_array.hpp>
1.22 +#include <boost/ptr_container/ptr_set.hpp>
1.23 +#include <boost/ptr_container/ptr_map.hpp>
1.24 +#include <boost/serialization/export.hpp>
1.25 +#include <boost/serialization/base_object.hpp>
1.26 +#include <boost/serialization/utility.hpp>
1.27 +#include <boost/serialization/string.hpp>
1.28 +#include <fstream>
1.29 +#include <string>
1.30 +
1.31 +//
1.32 +// serialization helper: we can't save a non-const object
1.33 +//
1.34 +template< class T >
1.35 +inline T const& as_const( T const& r )
1.36 +{
1.37 + return r;
1.38 +}
1.39 +
1.40 +//
1.41 +// class hierarchy
1.42 +//
1.43 +struct Base
1.44 +{
1.45 + friend class boost::serialization::access;
1.46 +
1.47 + int i;
1.48 +
1.49 +
1.50 + template< class Archive >
1.51 + void serialize( Archive& ar, const unsigned int version )
1.52 + {
1.53 + ar & i;
1.54 + }
1.55 +
1.56 + Base() : i(42)
1.57 + { }
1.58 +
1.59 + Base( int i ) : i(i)
1.60 + { }
1.61 +
1.62 + virtual ~Base()
1.63 + { }
1.64 +};
1.65 +
1.66 +inline bool operator<( const Base& l, const Base& r )
1.67 +{
1.68 + return l.i < r.i;
1.69 +}
1.70 +
1.71 +struct Derived : Base
1.72 +{
1.73 + int i2;
1.74 +
1.75 + template< class Archive >
1.76 + void serialize( Archive& ar, const unsigned int version )
1.77 + {
1.78 + ar & boost::serialization::base_object<Base>( *this );
1.79 + ar & i2;
1.80 + }
1.81 +
1.82 + Derived() : Base(42), i2(42)
1.83 + { }
1.84 +
1.85 + explicit Derived( int i2 ) : Base(0), i2(i2)
1.86 + { }
1.87 +};
1.88 +
1.89 +BOOST_CLASS_EXPORT_GUID( Derived, "Derived" )
1.90 +
1.91 +//
1.92 +// test of containers
1.93 +//
1.94 +//
1.95 +
1.96 +template< class C, class T >
1.97 +void add( C& c, T* r, unsigned n )
1.98 +{
1.99 + c.insert( c.end(), r );
1.100 +}
1.101 +
1.102 +template< class U, class T >
1.103 +void add( boost::ptr_array<U,2>& c, T* r, unsigned n )
1.104 +{
1.105 + c.replace( n, r );
1.106 +}
1.107 +
1.108 +template< class Cont >
1.109 +void test_serialization_helper()
1.110 +{
1.111 + Cont vec;
1.112 + add( vec, new Base( -1 ), 0u );
1.113 + add( vec, new Derived( 1 ), 1u );
1.114 +
1.115 + std::ofstream ofs("filename");
1.116 + boost::archive::text_oarchive oa(ofs);
1.117 + oa << as_const(vec);
1.118 + ofs.close();
1.119 +
1.120 +
1.121 +
1.122 + std::ifstream ifs("filename", std::ios::binary);
1.123 + boost::archive::text_iarchive ia(ifs);
1.124 + Cont vec2;
1.125 + ia >> vec2;
1.126 + ifs.close();
1.127 +
1.128 + BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
1.129 + BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 );
1.130 + BOOST_CHECK_EQUAL( (*--vec2.end()).i, 0 );
1.131 + typename Cont::iterator i = vec2.end();
1.132 + --i;
1.133 + Derived* d = dynamic_cast<Derived*>( &*i );
1.134 + BOOST_CHECK_EQUAL( d->i2, 1 );
1.135 +}
1.136 +
1.137 +template< class Map >
1.138 +void test_serialization_map_helper()
1.139 +{
1.140 + Map m;
1.141 + std::string key1("key1"), key2("key2");
1.142 + m.insert( key1, new Base( -1 ) );
1.143 + m.insert( key2, new Derived( 1 ) );
1.144 + BOOST_CHECK_EQUAL( m.size(), 2u );
1.145 +
1.146 + std::ofstream ofs("filename");
1.147 + boost::archive::text_oarchive oa(ofs);
1.148 + oa << as_const(m);
1.149 + ofs.close();
1.150 +
1.151 +
1.152 +
1.153 + std::ifstream ifs("filename", std::ios::binary);
1.154 + boost::archive::text_iarchive ia(ifs);
1.155 + Map m2;
1.156 + ia >> m2;
1.157 + ifs.close();
1.158 +
1.159 + BOOST_CHECK_EQUAL( m.size(), m2.size() );
1.160 + BOOST_CHECK_EQUAL( m2.find(key1)->second->i, -1 );
1.161 + BOOST_CHECK_EQUAL( m2.find(key2)->second->i, 0 );
1.162 + typename Map::iterator i = m2.find(key2);
1.163 + Derived* d = dynamic_cast<Derived*>( i->second );
1.164 + BOOST_CHECK_EQUAL( d->i2, 1 );
1.165 +
1.166 +}
1.167 +
1.168 +//
1.169 +// basic test of hierarchy
1.170 +//
1.171 +void test_hierarchy()
1.172 +{
1.173 + Base* p = new Derived();
1.174 + std::ofstream ofs("filename");
1.175 + boost::archive::text_oarchive oa(ofs);
1.176 + oa << as_const(p);
1.177 + ofs.close();
1.178 +
1.179 +
1.180 + Base* d = 0;
1.181 + std::ifstream ifs("filename", std::ios::binary);
1.182 + boost::archive::text_iarchive ia(ifs);
1.183 + ia >> d;
1.184 + ifs.close();
1.185 +
1.186 + BOOST_CHECK_EQUAL( p->i, d->i );
1.187 + BOOST_CHECK( p != d );
1.188 + BOOST_CHECK( dynamic_cast<Derived*>( d ) );
1.189 + delete p;
1.190 + delete d;
1.191 +}
1.192 +
1.193 +//
1.194 +// test initializer
1.195 +//
1.196 +void test_serialization()
1.197 +{
1.198 + test_hierarchy();
1.199 + test_serialization_helper< boost::ptr_deque<Base> >();
1.200 + test_serialization_helper< boost::ptr_list<Base> >();
1.201 + test_serialization_helper< boost::ptr_vector<Base> >();
1.202 + test_serialization_helper< boost::ptr_array<Base,2> >();
1.203 +
1.204 + test_serialization_helper< boost::ptr_set<Base> >();
1.205 + test_serialization_helper< boost::ptr_multiset<Base> >();
1.206 +
1.207 + test_serialization_map_helper< boost::ptr_map<std::string,Base> >();
1.208 +
1.209 +//
1.210 +// GCC hangs when calling find() on a multimap!
1.211 +//
1.212 +//#if !BOOST_WORKAROUND(BOOST_MPL_CFG_GCC, BOOST_TESTED_AT(0x0300))
1.213 +
1.214 + test_serialization_map_helper< boost::ptr_multimap<std::string,Base> >();
1.215 +
1.216 +//#endif
1.217 +
1.218 +}
1.219 +
1.220 +
1.221 +using boost::unit_test::test_suite;
1.222 +
1.223 +test_suite* init_unit_test_suite( int argc, char* argv[] )
1.224 +{
1.225 + test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
1.226 +
1.227 + test->add( BOOST_TEST_CASE( &test_serialization ) );
1.228 +
1.229 + return test;
1.230 +}
1.231 +
1.232 +