sl@0: // sl@0: // Boost.Pointer Container sl@0: // sl@0: // Copyright Thorsten Ottosen 2003-2005. Use, modification and sl@0: // distribution is subject to the Boost Software License, Version sl@0: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: // sl@0: // For more information, see http://www.boost.org/libs/ptr_container/ sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // sl@0: // serialization helper: we can't save a non-const object sl@0: // sl@0: template< class T > sl@0: inline T const& as_const( T const& r ) sl@0: { sl@0: return r; sl@0: } sl@0: sl@0: // sl@0: // class hierarchy sl@0: // sl@0: struct Base sl@0: { sl@0: friend class boost::serialization::access; sl@0: sl@0: int i; sl@0: sl@0: sl@0: template< class Archive > sl@0: void serialize( Archive& ar, const unsigned int version ) sl@0: { sl@0: ar & i; sl@0: } sl@0: sl@0: Base() : i(42) sl@0: { } sl@0: sl@0: Base( int i ) : i(i) sl@0: { } sl@0: sl@0: virtual ~Base() sl@0: { } sl@0: }; sl@0: sl@0: inline bool operator<( const Base& l, const Base& r ) sl@0: { sl@0: return l.i < r.i; sl@0: } sl@0: sl@0: struct Derived : Base sl@0: { sl@0: int i2; sl@0: sl@0: template< class Archive > sl@0: void serialize( Archive& ar, const unsigned int version ) sl@0: { sl@0: ar & boost::serialization::base_object( *this ); sl@0: ar & i2; sl@0: } sl@0: sl@0: Derived() : Base(42), i2(42) sl@0: { } sl@0: sl@0: explicit Derived( int i2 ) : Base(0), i2(i2) sl@0: { } sl@0: }; sl@0: sl@0: BOOST_CLASS_EXPORT_GUID( Derived, "Derived" ) sl@0: sl@0: // sl@0: // test of containers sl@0: // sl@0: // sl@0: sl@0: template< class C, class T > sl@0: void add( C& c, T* r, unsigned n ) sl@0: { sl@0: c.insert( c.end(), r ); sl@0: } sl@0: sl@0: template< class U, class T > sl@0: void add( boost::ptr_array& c, T* r, unsigned n ) sl@0: { sl@0: c.replace( n, r ); sl@0: } sl@0: sl@0: template< class Cont > sl@0: void test_serialization_helper() sl@0: { sl@0: Cont vec; sl@0: add( vec, new Base( -1 ), 0u ); sl@0: add( vec, new Derived( 1 ), 1u ); sl@0: sl@0: std::ofstream ofs("filename"); sl@0: boost::archive::text_oarchive oa(ofs); sl@0: oa << as_const(vec); sl@0: ofs.close(); sl@0: sl@0: sl@0: sl@0: std::ifstream ifs("filename", std::ios::binary); sl@0: boost::archive::text_iarchive ia(ifs); sl@0: Cont vec2; sl@0: ia >> vec2; sl@0: ifs.close(); sl@0: sl@0: BOOST_CHECK_EQUAL( vec.size(), vec2.size() ); sl@0: BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 ); sl@0: BOOST_CHECK_EQUAL( (*--vec2.end()).i, 0 ); sl@0: typename Cont::iterator i = vec2.end(); sl@0: --i; sl@0: Derived* d = dynamic_cast( &*i ); sl@0: BOOST_CHECK_EQUAL( d->i2, 1 ); sl@0: } sl@0: sl@0: template< class Map > sl@0: void test_serialization_map_helper() sl@0: { sl@0: Map m; sl@0: std::string key1("key1"), key2("key2"); sl@0: m.insert( key1, new Base( -1 ) ); sl@0: m.insert( key2, new Derived( 1 ) ); sl@0: BOOST_CHECK_EQUAL( m.size(), 2u ); sl@0: sl@0: std::ofstream ofs("filename"); sl@0: boost::archive::text_oarchive oa(ofs); sl@0: oa << as_const(m); sl@0: ofs.close(); sl@0: sl@0: sl@0: sl@0: std::ifstream ifs("filename", std::ios::binary); sl@0: boost::archive::text_iarchive ia(ifs); sl@0: Map m2; sl@0: ia >> m2; sl@0: ifs.close(); sl@0: sl@0: BOOST_CHECK_EQUAL( m.size(), m2.size() ); sl@0: BOOST_CHECK_EQUAL( m2.find(key1)->second->i, -1 ); sl@0: BOOST_CHECK_EQUAL( m2.find(key2)->second->i, 0 ); sl@0: typename Map::iterator i = m2.find(key2); sl@0: Derived* d = dynamic_cast( i->second ); sl@0: BOOST_CHECK_EQUAL( d->i2, 1 ); sl@0: sl@0: } sl@0: sl@0: // sl@0: // basic test of hierarchy sl@0: // sl@0: void test_hierarchy() sl@0: { sl@0: Base* p = new Derived(); sl@0: std::ofstream ofs("filename"); sl@0: boost::archive::text_oarchive oa(ofs); sl@0: oa << as_const(p); sl@0: ofs.close(); sl@0: sl@0: sl@0: Base* d = 0; sl@0: std::ifstream ifs("filename", std::ios::binary); sl@0: boost::archive::text_iarchive ia(ifs); sl@0: ia >> d; sl@0: ifs.close(); sl@0: sl@0: BOOST_CHECK_EQUAL( p->i, d->i ); sl@0: BOOST_CHECK( p != d ); sl@0: BOOST_CHECK( dynamic_cast( d ) ); sl@0: delete p; sl@0: delete d; sl@0: } sl@0: sl@0: // sl@0: // test initializer sl@0: // sl@0: void test_serialization() sl@0: { sl@0: test_hierarchy(); sl@0: test_serialization_helper< boost::ptr_deque >(); sl@0: test_serialization_helper< boost::ptr_list >(); sl@0: test_serialization_helper< boost::ptr_vector >(); sl@0: test_serialization_helper< boost::ptr_array >(); sl@0: sl@0: test_serialization_helper< boost::ptr_set >(); sl@0: test_serialization_helper< boost::ptr_multiset >(); sl@0: sl@0: test_serialization_map_helper< boost::ptr_map >(); sl@0: sl@0: // sl@0: // GCC hangs when calling find() on a multimap! sl@0: // sl@0: //#if !BOOST_WORKAROUND(BOOST_MPL_CFG_GCC, BOOST_TESTED_AT(0x0300)) sl@0: sl@0: test_serialization_map_helper< boost::ptr_multimap >(); sl@0: sl@0: //#endif sl@0: sl@0: } sl@0: sl@0: sl@0: using boost::unit_test::test_suite; sl@0: sl@0: test_suite* init_unit_test_suite( int argc, char* argv[] ) sl@0: { sl@0: test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" ); sl@0: sl@0: test->add( BOOST_TEST_CASE( &test_serialization ) ); sl@0: sl@0: return test; sl@0: } sl@0: sl@0: