sl@0
|
1 |
//
|
sl@0
|
2 |
// Boost.Pointer Container
|
sl@0
|
3 |
//
|
sl@0
|
4 |
// Copyright Thorsten Ottosen 2003-2005. Use, modification and
|
sl@0
|
5 |
// distribution is subject to the Boost Software License, Version
|
sl@0
|
6 |
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
sl@0
|
7 |
// http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
8 |
//
|
sl@0
|
9 |
// For more information, see http://www.boost.org/libs/ptr_container/
|
sl@0
|
10 |
//
|
sl@0
|
11 |
|
sl@0
|
12 |
#include <boost/test/unit_test.hpp>
|
sl@0
|
13 |
#include <boost/archive/text_oarchive.hpp>
|
sl@0
|
14 |
#include <boost/archive/text_iarchive.hpp>
|
sl@0
|
15 |
#include <boost/ptr_container/ptr_vector.hpp>
|
sl@0
|
16 |
#include <boost/ptr_container/ptr_deque.hpp>
|
sl@0
|
17 |
#include <boost/ptr_container/ptr_list.hpp>
|
sl@0
|
18 |
#include <boost/ptr_container/ptr_array.hpp>
|
sl@0
|
19 |
#include <boost/ptr_container/ptr_set.hpp>
|
sl@0
|
20 |
#include <boost/ptr_container/ptr_map.hpp>
|
sl@0
|
21 |
#include <boost/serialization/export.hpp>
|
sl@0
|
22 |
#include <boost/serialization/base_object.hpp>
|
sl@0
|
23 |
#include <boost/serialization/utility.hpp>
|
sl@0
|
24 |
#include <boost/serialization/string.hpp>
|
sl@0
|
25 |
#include <fstream>
|
sl@0
|
26 |
#include <string>
|
sl@0
|
27 |
|
sl@0
|
28 |
//
|
sl@0
|
29 |
// serialization helper: we can't save a non-const object
|
sl@0
|
30 |
//
|
sl@0
|
31 |
template< class T >
|
sl@0
|
32 |
inline T const& as_const( T const& r )
|
sl@0
|
33 |
{
|
sl@0
|
34 |
return r;
|
sl@0
|
35 |
}
|
sl@0
|
36 |
|
sl@0
|
37 |
//
|
sl@0
|
38 |
// class hierarchy
|
sl@0
|
39 |
//
|
sl@0
|
40 |
struct Base
|
sl@0
|
41 |
{
|
sl@0
|
42 |
friend class boost::serialization::access;
|
sl@0
|
43 |
|
sl@0
|
44 |
int i;
|
sl@0
|
45 |
|
sl@0
|
46 |
|
sl@0
|
47 |
template< class Archive >
|
sl@0
|
48 |
void serialize( Archive& ar, const unsigned int version )
|
sl@0
|
49 |
{
|
sl@0
|
50 |
ar & i;
|
sl@0
|
51 |
}
|
sl@0
|
52 |
|
sl@0
|
53 |
Base() : i(42)
|
sl@0
|
54 |
{ }
|
sl@0
|
55 |
|
sl@0
|
56 |
Base( int i ) : i(i)
|
sl@0
|
57 |
{ }
|
sl@0
|
58 |
|
sl@0
|
59 |
virtual ~Base()
|
sl@0
|
60 |
{ }
|
sl@0
|
61 |
};
|
sl@0
|
62 |
|
sl@0
|
63 |
inline bool operator<( const Base& l, const Base& r )
|
sl@0
|
64 |
{
|
sl@0
|
65 |
return l.i < r.i;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
struct Derived : Base
|
sl@0
|
69 |
{
|
sl@0
|
70 |
int i2;
|
sl@0
|
71 |
|
sl@0
|
72 |
template< class Archive >
|
sl@0
|
73 |
void serialize( Archive& ar, const unsigned int version )
|
sl@0
|
74 |
{
|
sl@0
|
75 |
ar & boost::serialization::base_object<Base>( *this );
|
sl@0
|
76 |
ar & i2;
|
sl@0
|
77 |
}
|
sl@0
|
78 |
|
sl@0
|
79 |
Derived() : Base(42), i2(42)
|
sl@0
|
80 |
{ }
|
sl@0
|
81 |
|
sl@0
|
82 |
explicit Derived( int i2 ) : Base(0), i2(i2)
|
sl@0
|
83 |
{ }
|
sl@0
|
84 |
};
|
sl@0
|
85 |
|
sl@0
|
86 |
BOOST_CLASS_EXPORT_GUID( Derived, "Derived" )
|
sl@0
|
87 |
|
sl@0
|
88 |
//
|
sl@0
|
89 |
// test of containers
|
sl@0
|
90 |
//
|
sl@0
|
91 |
//
|
sl@0
|
92 |
|
sl@0
|
93 |
template< class C, class T >
|
sl@0
|
94 |
void add( C& c, T* r, unsigned n )
|
sl@0
|
95 |
{
|
sl@0
|
96 |
c.insert( c.end(), r );
|
sl@0
|
97 |
}
|
sl@0
|
98 |
|
sl@0
|
99 |
template< class U, class T >
|
sl@0
|
100 |
void add( boost::ptr_array<U,2>& c, T* r, unsigned n )
|
sl@0
|
101 |
{
|
sl@0
|
102 |
c.replace( n, r );
|
sl@0
|
103 |
}
|
sl@0
|
104 |
|
sl@0
|
105 |
template< class Cont >
|
sl@0
|
106 |
void test_serialization_helper()
|
sl@0
|
107 |
{
|
sl@0
|
108 |
Cont vec;
|
sl@0
|
109 |
add( vec, new Base( -1 ), 0u );
|
sl@0
|
110 |
add( vec, new Derived( 1 ), 1u );
|
sl@0
|
111 |
|
sl@0
|
112 |
std::ofstream ofs("filename");
|
sl@0
|
113 |
boost::archive::text_oarchive oa(ofs);
|
sl@0
|
114 |
oa << as_const(vec);
|
sl@0
|
115 |
ofs.close();
|
sl@0
|
116 |
|
sl@0
|
117 |
|
sl@0
|
118 |
|
sl@0
|
119 |
std::ifstream ifs("filename", std::ios::binary);
|
sl@0
|
120 |
boost::archive::text_iarchive ia(ifs);
|
sl@0
|
121 |
Cont vec2;
|
sl@0
|
122 |
ia >> vec2;
|
sl@0
|
123 |
ifs.close();
|
sl@0
|
124 |
|
sl@0
|
125 |
BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
|
sl@0
|
126 |
BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 );
|
sl@0
|
127 |
BOOST_CHECK_EQUAL( (*--vec2.end()).i, 0 );
|
sl@0
|
128 |
typename Cont::iterator i = vec2.end();
|
sl@0
|
129 |
--i;
|
sl@0
|
130 |
Derived* d = dynamic_cast<Derived*>( &*i );
|
sl@0
|
131 |
BOOST_CHECK_EQUAL( d->i2, 1 );
|
sl@0
|
132 |
}
|
sl@0
|
133 |
|
sl@0
|
134 |
template< class Map >
|
sl@0
|
135 |
void test_serialization_map_helper()
|
sl@0
|
136 |
{
|
sl@0
|
137 |
Map m;
|
sl@0
|
138 |
std::string key1("key1"), key2("key2");
|
sl@0
|
139 |
m.insert( key1, new Base( -1 ) );
|
sl@0
|
140 |
m.insert( key2, new Derived( 1 ) );
|
sl@0
|
141 |
BOOST_CHECK_EQUAL( m.size(), 2u );
|
sl@0
|
142 |
|
sl@0
|
143 |
std::ofstream ofs("filename");
|
sl@0
|
144 |
boost::archive::text_oarchive oa(ofs);
|
sl@0
|
145 |
oa << as_const(m);
|
sl@0
|
146 |
ofs.close();
|
sl@0
|
147 |
|
sl@0
|
148 |
|
sl@0
|
149 |
|
sl@0
|
150 |
std::ifstream ifs("filename", std::ios::binary);
|
sl@0
|
151 |
boost::archive::text_iarchive ia(ifs);
|
sl@0
|
152 |
Map m2;
|
sl@0
|
153 |
ia >> m2;
|
sl@0
|
154 |
ifs.close();
|
sl@0
|
155 |
|
sl@0
|
156 |
BOOST_CHECK_EQUAL( m.size(), m2.size() );
|
sl@0
|
157 |
BOOST_CHECK_EQUAL( m2.find(key1)->second->i, -1 );
|
sl@0
|
158 |
BOOST_CHECK_EQUAL( m2.find(key2)->second->i, 0 );
|
sl@0
|
159 |
typename Map::iterator i = m2.find(key2);
|
sl@0
|
160 |
Derived* d = dynamic_cast<Derived*>( i->second );
|
sl@0
|
161 |
BOOST_CHECK_EQUAL( d->i2, 1 );
|
sl@0
|
162 |
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
//
|
sl@0
|
166 |
// basic test of hierarchy
|
sl@0
|
167 |
//
|
sl@0
|
168 |
void test_hierarchy()
|
sl@0
|
169 |
{
|
sl@0
|
170 |
Base* p = new Derived();
|
sl@0
|
171 |
std::ofstream ofs("filename");
|
sl@0
|
172 |
boost::archive::text_oarchive oa(ofs);
|
sl@0
|
173 |
oa << as_const(p);
|
sl@0
|
174 |
ofs.close();
|
sl@0
|
175 |
|
sl@0
|
176 |
|
sl@0
|
177 |
Base* d = 0;
|
sl@0
|
178 |
std::ifstream ifs("filename", std::ios::binary);
|
sl@0
|
179 |
boost::archive::text_iarchive ia(ifs);
|
sl@0
|
180 |
ia >> d;
|
sl@0
|
181 |
ifs.close();
|
sl@0
|
182 |
|
sl@0
|
183 |
BOOST_CHECK_EQUAL( p->i, d->i );
|
sl@0
|
184 |
BOOST_CHECK( p != d );
|
sl@0
|
185 |
BOOST_CHECK( dynamic_cast<Derived*>( d ) );
|
sl@0
|
186 |
delete p;
|
sl@0
|
187 |
delete d;
|
sl@0
|
188 |
}
|
sl@0
|
189 |
|
sl@0
|
190 |
//
|
sl@0
|
191 |
// test initializer
|
sl@0
|
192 |
//
|
sl@0
|
193 |
void test_serialization()
|
sl@0
|
194 |
{
|
sl@0
|
195 |
test_hierarchy();
|
sl@0
|
196 |
test_serialization_helper< boost::ptr_deque<Base> >();
|
sl@0
|
197 |
test_serialization_helper< boost::ptr_list<Base> >();
|
sl@0
|
198 |
test_serialization_helper< boost::ptr_vector<Base> >();
|
sl@0
|
199 |
test_serialization_helper< boost::ptr_array<Base,2> >();
|
sl@0
|
200 |
|
sl@0
|
201 |
test_serialization_helper< boost::ptr_set<Base> >();
|
sl@0
|
202 |
test_serialization_helper< boost::ptr_multiset<Base> >();
|
sl@0
|
203 |
|
sl@0
|
204 |
test_serialization_map_helper< boost::ptr_map<std::string,Base> >();
|
sl@0
|
205 |
|
sl@0
|
206 |
//
|
sl@0
|
207 |
// GCC hangs when calling find() on a multimap!
|
sl@0
|
208 |
//
|
sl@0
|
209 |
//#if !BOOST_WORKAROUND(BOOST_MPL_CFG_GCC, BOOST_TESTED_AT(0x0300))
|
sl@0
|
210 |
|
sl@0
|
211 |
test_serialization_map_helper< boost::ptr_multimap<std::string,Base> >();
|
sl@0
|
212 |
|
sl@0
|
213 |
//#endif
|
sl@0
|
214 |
|
sl@0
|
215 |
}
|
sl@0
|
216 |
|
sl@0
|
217 |
|
sl@0
|
218 |
using boost::unit_test::test_suite;
|
sl@0
|
219 |
|
sl@0
|
220 |
test_suite* init_unit_test_suite( int argc, char* argv[] )
|
sl@0
|
221 |
{
|
sl@0
|
222 |
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
|
sl@0
|
223 |
|
sl@0
|
224 |
test->add( BOOST_TEST_CASE( &test_serialization ) );
|
sl@0
|
225 |
|
sl@0
|
226 |
return test;
|
sl@0
|
227 |
}
|
sl@0
|
228 |
|
sl@0
|
229 |
|