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