]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/test/shared_ptr_basic_test.cpp
19c2ac9cb1615d556c67e2f7591983b4b7488a3b
1 #include <boost/config.hpp>
3 #if defined(BOOST_MSVC)
5 #pragma warning(disable: 4786) // identifier truncated in debug info
6 #pragma warning(disable: 4710) // function not inlined
7 #pragma warning(disable: 4711) // function selected for automatic inline expansion
8 #pragma warning(disable: 4514) // unreferenced inline removed
9 #pragma warning(disable: 4355) // 'this' : used in base member initializer list
11 #if (BOOST_MSVC >= 1310)
12 #pragma warning(disable: 4675) // resolved overload found with Koenig lookup
17 #if defined(__GNUC__) && __GNUC__ > 4
18 # pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
22 // shared_ptr_basic_test.cpp
24 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
26 // Distributed under the Boost Software License, Version 1.0. (See
27 // accompanying file LICENSE_1_0.txt or copy at
28 // http://www.boost.org/LICENSE_1_0.txt)
31 #include <boost/core/lightweight_test.hpp>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/weak_ptr.hpp>
45 ~X() // virtual destructor deliberately omitted
50 virtual int id() const
58 X
& operator= (X
const &);
73 virtual int id() const
81 Y
& operator= (Y
const &);
90 void release_object(int * p
)
92 BOOST_TEST(p
== &cnt
);
96 template<class T
> void test_is_X(boost::shared_ptr
<T
> const & p
)
98 BOOST_TEST(p
->id() == 1);
99 BOOST_TEST((*p
).id() == 1);
102 template<class T
> void test_is_X(boost::weak_ptr
<T
> const & p
)
104 BOOST_TEST(p
.get() != 0);
105 BOOST_TEST(p
.get()->id() == 1);
108 template<class T
> void test_is_Y(boost::shared_ptr
<T
> const & p
)
110 BOOST_TEST(p
->id() == 2);
111 BOOST_TEST((*p
).id() == 2);
114 template<class T
> void test_is_Y(boost::weak_ptr
<T
> const & p
)
116 boost::shared_ptr
<T
> q
= p
.lock();
117 BOOST_TEST(q
.get() != 0);
118 BOOST_TEST(q
->id() == 2);
121 template<class T
> void test_eq(T
const & a
, T
const & b
)
124 BOOST_TEST(!(a
!= b
));
125 BOOST_TEST(!(a
< b
));
126 BOOST_TEST(!(b
< a
));
129 template<class T
> void test_ne(T
const & a
, T
const & b
)
131 BOOST_TEST(!(a
== b
));
133 BOOST_TEST(a
< b
|| b
< a
);
134 BOOST_TEST(!(a
< b
&& b
< a
));
137 template<class T
, class U
> void test_shared(boost::weak_ptr
<T
> const & a
, boost::weak_ptr
<U
> const & b
)
139 BOOST_TEST(!(a
< b
));
140 BOOST_TEST(!(b
< a
));
143 template<class T
, class U
> void test_nonshared(boost::weak_ptr
<T
> const & a
, boost::weak_ptr
<U
> const & b
)
145 BOOST_TEST(a
< b
|| b
< a
);
146 BOOST_TEST(!(a
< b
&& b
< a
));
149 template<class T
, class U
> void test_eq2(T
const & a
, U
const & b
)
152 BOOST_TEST(!(a
!= b
));
155 template<class T
, class U
> void test_ne2(T
const & a
, U
const & b
)
157 BOOST_TEST(!(a
== b
));
161 template<class T
> void test_is_zero(boost::shared_ptr
<T
> const & p
)
164 BOOST_TEST(p
.get() == 0);
167 template<class T
> void test_is_nonzero(boost::shared_ptr
<T
> const & p
)
169 // p? true: false is used to test p in a boolean context.
170 // BOOST_TEST(p) is not guaranteed to test the conversion,
171 // as the macro might test !!p instead.
172 BOOST_TEST(p
? true: false);
173 BOOST_TEST(p
.get() != 0);
178 using namespace boost
;
181 shared_ptr
<X
> p(new Y
);
182 shared_ptr
<X
> p2(new X
);
195 #if !defined( BOOST_NO_RTTI )
196 shared_ptr
<Y
> p3
= dynamic_pointer_cast
<Y
>(p
);
197 shared_ptr
<Y
> p4
= dynamic_pointer_cast
<Y
>(p2
);
202 BOOST_TEST(p
.use_count() == 2);
203 BOOST_TEST(p2
.use_count() == 1);
204 BOOST_TEST(p3
.use_count() == 2);
211 shared_ptr
<void> p5(p
);
218 BOOST_TEST(!wp1
.expired());
219 BOOST_TEST(wp1
.use_count() != 0);
223 #if !defined( BOOST_NO_RTTI )
230 #if !defined( BOOST_NO_RTTI )
235 BOOST_TEST(p5
.use_count() == 1);
237 BOOST_TEST(wp1
.expired());
238 BOOST_TEST(wp1
.use_count() == 0);
242 shared_ptr
<X
> sp1(wp1
);
243 BOOST_ERROR("shared_ptr<X> sp1(wp1) failed to throw");
245 catch(boost::bad_weak_ptr
const &)
249 test_is_zero(wp1
.lock());
251 weak_ptr
<X
> wp2
= static_pointer_cast
<X
>(p5
);
253 BOOST_TEST(wp2
.use_count() == 1);
255 test_nonshared(wp1
, wp2
);
257 // Scoped to not affect the subsequent use_count() tests.
259 shared_ptr
<X
> sp2(wp2
);
260 test_is_nonzero(wp2
.lock());
263 #if !defined( BOOST_NO_RTTI )
264 weak_ptr
<Y
> wp3
= dynamic_pointer_cast
<Y
>(wp2
.lock());
266 BOOST_TEST(wp3
.use_count() == 1);
267 test_shared(wp2
, wp3
);
269 weak_ptr
<X
> wp4(wp3
);
271 BOOST_TEST(wp4
.use_count() == 1);
272 test_shared(wp2
, wp4
);
276 test_is_zero(wp1
.lock());
278 #if !defined( BOOST_NO_RTTI )
284 BOOST_TEST(wp1
.use_count() == 1);
285 test_shared(wp1
, wp2
);
294 BOOST_TEST(wp1
.use_count() == 0);
295 BOOST_TEST(wp2
.use_count() == 0);
296 #if !defined( BOOST_NO_RTTI )
297 BOOST_TEST(wp3
.use_count() == 0);
300 // Test operator< stability for std::set< weak_ptr<> >
301 // Thanks to Joe Gottman for pointing this out
303 BOOST_TEST(b1
== (wp1
< wp5
));
304 BOOST_TEST(b2
== (wp5
< wp1
));
307 // note that both get_object and release_object deal with int*
308 shared_ptr
<void> p6(get_object(), release_object
);
312 BOOST_TEST(cnt
== 0);
314 return boost::report_errors();