]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/ptr_container/test/serialization.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / ptr_container / test / serialization.cpp
1 //
2 // Boost.Pointer Container
3 //
4 // Copyright Thorsten Ottosen 2003-2005. Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // For more information, see http://www.boost.org/libs/ptr_container/
10 //
11
12 #include <boost/config.hpp>
13 #ifdef BOOST_MSVC
14 #pragma warning( disable: 4996 )
15 #endif
16
17 #include <boost/test/unit_test.hpp>
18 #include <boost/archive/text_oarchive.hpp>
19 #include <boost/archive/text_iarchive.hpp>
20 #include <boost/archive/xml_iarchive.hpp>
21 #include <boost/archive/xml_oarchive.hpp>
22 #include <boost/functional/hash.hpp>
23 #include <boost/ptr_container/ptr_container.hpp>
24 #include <boost/ptr_container/serialize_ptr_container.hpp>
25 #include <boost/serialization/export.hpp>
26 #include <boost/serialization/base_object.hpp>
27 #include <boost/serialization/utility.hpp>
28 #include <boost/serialization/string.hpp>
29 #include <fstream>
30 #include <string>
31
32 //
33 // serialization helper: we can't save a non-const object
34 //
35 template< class T >
36 inline T const& as_const( T const& r )
37 {
38 return r;
39 }
40
41 //
42 // used to customize tests for circular_buffer
43 //
44 template< class Cont >
45 struct set_capacity
46 {
47 void operator()( Cont& ) const
48 { }
49 };
50
51 template<class T>
52 struct set_capacity< boost::ptr_circular_buffer<T> >
53 {
54 void operator()( boost::ptr_circular_buffer<T>& c ) const
55 {
56 c.set_capacity( 100u );
57 }
58 };
59
60 //
61 // class hierarchy
62 //
63 struct Base
64 {
65 friend class boost::serialization::access;
66
67 int i;
68
69
70 template< class Archive >
71 void serialize( Archive& ar, const unsigned int /*version*/ )
72 {
73 ar & boost::serialization::make_nvp( "i", i );
74 }
75
76 Base() : i(42)
77 { }
78
79 Base( int i ) : i(i)
80 { }
81
82 virtual ~Base()
83 { }
84 };
85
86 inline bool operator<( const Base& l, const Base& r )
87 {
88 return l.i < r.i;
89 }
90
91 inline bool operator==( const Base& l, const Base& r )
92 {
93 return l.i == r.i;
94 }
95
96 inline std::size_t hash_value( const Base& b )
97 {
98 return boost::hash_value( b.i );
99 }
100
101 struct Derived : Base
102 {
103 int i2;
104
105 template< class Archive >
106 void serialize( Archive& ar, const unsigned int /*version*/ )
107 {
108 ar & boost::serialization::make_nvp( "Base",
109 boost::serialization::base_object<Base>( *this ) );
110 ar & boost::serialization::make_nvp( "i2", i2 );
111 }
112
113 Derived() : Base(42), i2(42)
114 { }
115
116 explicit Derived( int i2 ) : Base(0), i2(i2)
117 { }
118 };
119
120 BOOST_CLASS_EXPORT_GUID( Derived, "Derived" )
121
122 //
123 // test of containers
124 //
125 //
126
127 template< class C, class T >
128 void add( C& c, T* r, unsigned /*n*/ )
129 {
130 c.insert( c.end(), r );
131 }
132
133 template< class U, class T >
134 void add( boost::ptr_array<U,2>& c, T* r, unsigned n )
135 {
136 c.replace( n, r );
137 }
138
139 template< class Cont, class OArchive, class IArchive >
140 void test_serialization_helper()
141 {
142 Cont vec;
143 set_capacity<Cont>()( vec );
144 add( vec, new Base( -1 ), 0u );
145 add( vec, new Derived( 1 ), 1u );
146 BOOST_CHECK_EQUAL( vec.size(), 2u );
147
148 {
149 std::ofstream ofs("filename");
150 OArchive oa(ofs);
151 oa << boost::serialization::make_nvp( "container", as_const(vec) );
152 }
153
154 Cont vec2;
155
156 {
157 std::ifstream ifs("filename", std::ios::binary);
158 IArchive ia(ifs);
159 ia >> boost::serialization::make_nvp( "container", vec2 );
160 }
161
162 BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
163 BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 );
164 BOOST_CHECK_EQUAL( (*++vec2.begin()).i, 0 );
165
166 typename Cont::iterator i = vec2.begin();
167 ++i;
168 Derived* d = dynamic_cast<Derived*>( &*i );
169 BOOST_CHECK_EQUAL( d->i2, 1 );
170
171 }
172
173 template< class Cont, class OArchive, class IArchive >
174 void test_serialization_unordered_set_helper()
175 {
176 Cont vec;
177 set_capacity<Cont>()( vec );
178 add( vec, new Base( -1 ), 0u );
179 add( vec, new Derived( 1 ), 1u );
180 BOOST_CHECK_EQUAL( vec.size(), 2u );
181
182 {
183 std::ofstream ofs("filename");
184 OArchive oa(ofs);
185 oa << boost::serialization::make_nvp( "container", as_const(vec) );
186 }
187
188 Cont vec2;
189
190 {
191 std::ifstream ifs("filename", std::ios::binary);
192 IArchive ia(ifs);
193 ia >> boost::serialization::make_nvp( "container", vec2 );
194 }
195
196 BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
197 BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 );
198 BOOST_CHECK_EQUAL( (*++vec2.begin()).i, 0 );
199 }
200
201 template< class Map, class OArchive, class IArchive >
202 void test_serialization_map_helper()
203 {
204 Map m;
205 std::string key1("key1"), key2("key2");
206 m.insert( key1, new Base( -1 ) );
207 m.insert( key2, new Derived( 1 ) );
208 BOOST_CHECK_EQUAL( m.size(), 2u );
209
210 {
211 std::ofstream ofs("filename");
212 OArchive oa(ofs);
213 oa << boost::serialization::make_nvp( "container", as_const(m) );
214 }
215
216 Map m2;
217
218 {
219 std::ifstream ifs("filename", std::ios::binary);
220 IArchive ia(ifs);
221 ia >> boost::serialization::make_nvp( "container", m2 );
222 }
223
224 BOOST_CHECK_EQUAL( m.size(), m2.size() );
225 BOOST_CHECK_EQUAL( m2.find(key1)->second->i, -1 );
226 BOOST_CHECK_EQUAL( m2.find(key2)->second->i, 0 );
227
228 typename Map::iterator i = m2.find(key2);
229 Derived* d = dynamic_cast<Derived*>( i->second );
230 BOOST_CHECK_EQUAL( d->i2, 1 );
231 }
232
233 //
234 // basic test of hierarchy
235 //
236 void test_hierarchy()
237 {
238 Base* p = new Derived();
239
240 {
241 std::ofstream ofs("filename");
242 boost::archive::text_oarchive oa(ofs);
243 oa << as_const(p);
244 }
245
246 Base* d = 0;
247
248 {
249 std::ifstream ifs("filename", std::ios::binary);
250 boost::archive::text_iarchive ia(ifs);
251 ia >> d;
252 }
253
254 BOOST_CHECK_EQUAL( p->i, d->i );
255 BOOST_CHECK( p != d );
256 BOOST_CHECK( dynamic_cast<Derived*>( d ) );
257 delete p;
258 delete d;
259 }
260
261 //
262 // test initializer
263 //
264 void test_serialization()
265 {
266 test_hierarchy();
267 test_serialization_helper< boost::ptr_deque<Base>,
268 boost::archive::text_oarchive,
269 boost::archive::text_iarchive >();
270 test_serialization_helper< boost::ptr_list<Base>,
271 boost::archive::text_oarchive,
272 boost::archive::text_iarchive>();
273 test_serialization_helper< boost::ptr_vector<Base>,
274 boost::archive::text_oarchive,
275 boost::archive::text_iarchive>();
276 test_serialization_helper< boost::ptr_vector<Base>,
277 boost::archive::xml_oarchive,
278 boost::archive::xml_iarchive>();
279 test_serialization_helper< boost::ptr_circular_buffer<Base>,
280 boost::archive::text_oarchive,
281 boost::archive::text_iarchive>();
282 test_serialization_helper< boost::ptr_circular_buffer<Base>,
283 boost::archive::xml_oarchive,
284 boost::archive::xml_iarchive>();
285 test_serialization_helper< boost::ptr_array<Base,2>,
286 boost::archive::text_oarchive,
287 boost::archive::text_iarchive>();
288 test_serialization_helper< boost::ptr_set<Base>,
289 boost::archive::text_oarchive,
290 boost::archive::text_iarchive>();
291 test_serialization_helper< boost::ptr_multiset<Base>,
292 boost::archive::text_oarchive,
293 boost::archive::text_iarchive>();
294
295 test_serialization_unordered_set_helper< boost::ptr_unordered_set<Base>,
296 boost::archive::text_oarchive,
297 boost::archive::text_iarchive>();
298 test_serialization_unordered_set_helper<boost::ptr_unordered_multiset<Base>,
299 boost::archive::text_oarchive,
300 boost::archive::text_iarchive>();
301
302 test_serialization_map_helper< boost::ptr_map<std::string,Base>,
303 boost::archive::text_oarchive,
304 boost::archive::text_iarchive>();
305 test_serialization_map_helper< boost::ptr_multimap<std::string,Base>,
306 boost::archive::text_oarchive,
307 boost::archive::text_iarchive>();
308
309 test_serialization_map_helper< boost::ptr_map<std::string,Base>,
310 boost::archive::xml_oarchive,
311 boost::archive::xml_iarchive>();
312 test_serialization_map_helper< boost::ptr_multimap<std::string,Base>,
313 boost::archive::xml_oarchive,
314 boost::archive::xml_iarchive>();
315
316 test_serialization_map_helper< boost::ptr_unordered_map<std::string,Base>,
317 boost::archive::text_oarchive,
318 boost::archive::text_iarchive>();
319 test_serialization_map_helper< boost::ptr_unordered_multimap<std::string,Base>,
320 boost::archive::text_oarchive,
321 boost::archive::text_iarchive>();
322
323 test_serialization_map_helper< boost::ptr_unordered_map<std::string,Base>,
324 boost::archive::xml_oarchive,
325 boost::archive::xml_iarchive>();
326 test_serialization_map_helper< boost::ptr_unordered_multimap<std::string,Base>,
327 boost::archive::xml_oarchive,
328 boost::archive::xml_iarchive>();
329
330 }
331
332
333 using boost::unit_test::test_suite;
334
335 test_suite* init_unit_test_suite( int argc, char* argv[] )
336 {
337 test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
338
339 test->add( BOOST_TEST_CASE( &test_serialization ) );
340
341 return test;
342 }