]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/test/sp_array_test.cpp
4 // Copyright (c) 2012 Peter Dimov
6 // Distributed under the Boost Software License, Version 1.0.
7 // See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt
11 #include <boost/shared_ptr.hpp>
12 #include <boost/weak_ptr.hpp>
13 #include <boost/enable_shared_from_this.hpp>
14 #include <boost/core/lightweight_test.hpp>
18 class X
: public boost::enable_shared_from_this
< X
>
22 static int allocations
;
35 void* operator new[]( std::size_t n
)
38 return ::operator new[]( n
);
41 void operator delete[]( void* p
)
44 ::operator delete[]( p
);
50 X
& operator=( X
const& );
53 int X::allocations
= 0;
56 template< class T
> class array_deleter
62 void operator()( T
* p
) const
70 template< class Y
> void operator()( Y
* p
) const;
73 template< class T
> int array_deleter
< T
>::calls
= 0;
77 BOOST_TEST( X::allocations
== 0 );
78 BOOST_TEST( X::instances
== 0 );
81 boost::shared_ptr
<X
[]> px
;
84 BOOST_TEST( X::allocations
== 0 );
85 BOOST_TEST( X::instances
== 0 );
87 boost::shared_ptr
<X
[]> px2( new X
[ 3 ] );
92 px2
[0].shared_from_this();
93 BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
95 catch( boost::bad_weak_ptr
const& )
100 BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
103 BOOST_TEST( X::allocations
== 1 );
104 BOOST_TEST( X::instances
== 3 );
108 BOOST_TEST( &rx
== px2
.get() );
111 boost::shared_ptr
<X
const[]> px3( px2
);
112 BOOST_TEST( px3
== px2
);
113 BOOST_TEST( !( px2
< px3
) && !( px3
< px2
) );
116 X
const & rx
= px3
[ 1 ];
117 BOOST_TEST( &rx
== px3
.get() + 1 );
122 BOOST_TEST( px3
== px2
);
123 BOOST_TEST( !( px2
< px3
) && !( px3
< px2
) );
125 boost::shared_ptr
<X
volatile[]> px4( px2
);
126 BOOST_TEST( px4
== px2
);
127 BOOST_TEST( !( px2
< px4
) && !( px4
< px2
) );
130 X
volatile & rx
= px4
[ 2 ];
131 BOOST_TEST( &rx
== px4
.get() + 2 );
136 BOOST_TEST( px4
== px2
);
137 BOOST_TEST( !( px2
< px4
) && !( px4
< px2
) );
139 boost::shared_ptr
<void> px5( px2
);
140 BOOST_TEST( px5
== px2
);
141 BOOST_TEST( !( px2
< px5
) && !( px5
< px2
) );
145 BOOST_TEST( px5
== px2
);
146 BOOST_TEST( !( px2
< px5
) && !( px5
< px2
) );
148 boost::weak_ptr
<X
[]> wp( px
);
149 BOOST_TEST( wp
.lock() == px
);
151 boost::weak_ptr
<X
[]> wp2( px2
);
152 BOOST_TEST( wp2
.lock() == px2
);
156 BOOST_TEST( wp2
.lock() == px2
);
158 boost::weak_ptr
<X
const[]> wp3( px2
);
159 BOOST_TEST( wp3
.lock() == px2
);
163 BOOST_TEST( wp3
.lock() == px2
);
165 boost::weak_ptr
<X
volatile[]> wp4( px2
);
166 BOOST_TEST( wp4
.lock() == px2
);
170 BOOST_TEST( wp4
.lock() == px2
);
172 boost::weak_ptr
<void> wp5( px2
);
173 BOOST_TEST( wp5
.lock() == px2
);
177 BOOST_TEST( wp5
.lock() == px2
);
181 BOOST_TEST( X::allocations
== 1 );
182 BOOST_TEST( X::instances
== 3 );
188 BOOST_TEST( X::allocations
== 0 );
189 BOOST_TEST( X::instances
== 0 );
191 BOOST_TEST( wp2
.lock() == 0 );
192 BOOST_TEST( wp3
.lock() == 0 );
193 BOOST_TEST( wp4
.lock() == 0 );
194 BOOST_TEST( wp5
.lock() == 0 );
197 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
200 std::unique_ptr
<X
[]> px( new X
[ 4 ] );
201 BOOST_TEST( X::allocations
== 1 );
202 BOOST_TEST( X::instances
== 4 );
204 boost::shared_ptr
<X
[]> px2( std::move( px
) );
205 BOOST_TEST( X::allocations
== 1 );
206 BOOST_TEST( X::instances
== 4 );
207 BOOST_TEST( px
.get() == 0 );
211 px2
[0].shared_from_this();
212 BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
214 catch( boost::bad_weak_ptr
const& )
219 BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
224 BOOST_TEST( X::allocations
== 0 );
225 BOOST_TEST( X::instances
== 0 );
229 std::unique_ptr
<X
[]> px( new X
[ 4 ] );
230 BOOST_TEST( X::allocations
== 1 );
231 BOOST_TEST( X::instances
== 4 );
233 boost::shared_ptr
<X
[]> px2
;
234 px2
= std::move( px
);
235 BOOST_TEST( X::allocations
== 1 );
236 BOOST_TEST( X::instances
== 4 );
237 BOOST_TEST( px
.get() == 0 );
241 px2
[0].shared_from_this();
242 BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
244 catch( boost::bad_weak_ptr
const& )
249 BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
254 BOOST_TEST( X::allocations
== 0 );
255 BOOST_TEST( X::instances
== 0 );
261 boost::shared_ptr
<X
[]> px( new X
[ 5 ], array_deleter
< X
>() );
262 BOOST_TEST( X::allocations
== 1 );
263 BOOST_TEST( X::instances
== 5 );
267 px
[0].shared_from_this();
268 BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
270 catch( boost::bad_weak_ptr
const& )
275 BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
280 BOOST_TEST( X::allocations
== 0 );
281 BOOST_TEST( X::instances
== 0 );
282 BOOST_TEST( array_deleter
< X
>::calls
== 1 );
286 boost::shared_ptr
<X
[]> px( new X
[ 6 ], array_deleter
< X
>(), std::allocator
< X
>() );
287 BOOST_TEST( X::allocations
== 1 );
288 BOOST_TEST( X::instances
== 6 );
292 px
[0].shared_from_this();
293 BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
295 catch( boost::bad_weak_ptr
const& )
300 BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
305 BOOST_TEST( X::allocations
== 0 );
306 BOOST_TEST( X::instances
== 0 );
307 BOOST_TEST( array_deleter
< X
>::calls
== 2 );
310 return boost::report_errors();