1 // allocate_shared_esft_test.cpp
3 // Copyright 2007-2009 Peter Dimov
5 // Distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/make_shared.hpp>
11 #include <boost/shared_ptr.hpp>
12 #include <boost/enable_shared_from_this.hpp>
15 class X: public boost::enable_shared_from_this<X>
20 X & operator=( X const & );
26 explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
41 BOOST_TEST( X::instances == 0 );
44 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>() );
45 BOOST_TEST( X::instances == 1 );
49 boost::shared_ptr< X > qx = px->shared_from_this();
51 BOOST_TEST( px == qx );
52 BOOST_TEST( !( px < qx ) && !( qx < px ) );
55 BOOST_TEST( X::instances == 1 );
57 catch( boost::bad_weak_ptr const& )
59 BOOST_ERROR( "px->shared_from_this() failed" );
63 BOOST_TEST( X::instances == 0 );
66 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1 );
67 BOOST_TEST( X::instances == 1 );
71 boost::shared_ptr< X > qx = px->shared_from_this();
73 BOOST_TEST( px == qx );
74 BOOST_TEST( !( px < qx ) && !( qx < px ) );
77 BOOST_TEST( X::instances == 1 );
79 catch( boost::bad_weak_ptr const& )
81 BOOST_ERROR( "px->shared_from_this() failed" );
85 BOOST_TEST( X::instances == 0 );
88 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2 );
89 BOOST_TEST( X::instances == 1 );
93 boost::shared_ptr< X > qx = px->shared_from_this();
95 BOOST_TEST( px == qx );
96 BOOST_TEST( !( px < qx ) && !( qx < px ) );
99 BOOST_TEST( X::instances == 1 );
101 catch( boost::bad_weak_ptr const& )
103 BOOST_ERROR( "px->shared_from_this() failed" );
107 BOOST_TEST( X::instances == 0 );
110 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3 );
111 BOOST_TEST( X::instances == 1 );
115 boost::shared_ptr< X > qx = px->shared_from_this();
117 BOOST_TEST( px == qx );
118 BOOST_TEST( !( px < qx ) && !( qx < px ) );
121 BOOST_TEST( X::instances == 1 );
123 catch( boost::bad_weak_ptr const& )
125 BOOST_ERROR( "px->shared_from_this() failed" );
129 BOOST_TEST( X::instances == 0 );
132 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4 );
133 BOOST_TEST( X::instances == 1 );
137 boost::shared_ptr< X > qx = px->shared_from_this();
139 BOOST_TEST( px == qx );
140 BOOST_TEST( !( px < qx ) && !( qx < px ) );
143 BOOST_TEST( X::instances == 1 );
145 catch( boost::bad_weak_ptr const& )
147 BOOST_ERROR( "px->shared_from_this() failed" );
151 BOOST_TEST( X::instances == 0 );
154 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5 );
155 BOOST_TEST( X::instances == 1 );
159 boost::shared_ptr< X > qx = px->shared_from_this();
161 BOOST_TEST( px == qx );
162 BOOST_TEST( !( px < qx ) && !( qx < px ) );
165 BOOST_TEST( X::instances == 1 );
167 catch( boost::bad_weak_ptr const& )
169 BOOST_ERROR( "px->shared_from_this() failed" );
173 BOOST_TEST( X::instances == 0 );
176 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6 );
177 BOOST_TEST( X::instances == 1 );
181 boost::shared_ptr< X > qx = px->shared_from_this();
183 BOOST_TEST( px == qx );
184 BOOST_TEST( !( px < qx ) && !( qx < px ) );
187 BOOST_TEST( X::instances == 1 );
189 catch( boost::bad_weak_ptr const& )
191 BOOST_ERROR( "px->shared_from_this() failed" );
195 BOOST_TEST( X::instances == 0 );
198 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
199 BOOST_TEST( X::instances == 1 );
203 boost::shared_ptr< X > qx = px->shared_from_this();
205 BOOST_TEST( px == qx );
206 BOOST_TEST( !( px < qx ) && !( qx < px ) );
209 BOOST_TEST( X::instances == 1 );
211 catch( boost::bad_weak_ptr const& )
213 BOOST_ERROR( "px->shared_from_this() failed" );
217 BOOST_TEST( X::instances == 0 );
220 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
221 BOOST_TEST( X::instances == 1 );
225 boost::shared_ptr< X > qx = px->shared_from_this();
227 BOOST_TEST( px == qx );
228 BOOST_TEST( !( px < qx ) && !( qx < px ) );
231 BOOST_TEST( X::instances == 1 );
233 catch( boost::bad_weak_ptr const& )
235 BOOST_ERROR( "px->shared_from_this() failed" );
239 BOOST_TEST( X::instances == 0 );
242 boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
243 BOOST_TEST( X::instances == 1 );
247 boost::shared_ptr< X > qx = px->shared_from_this();
249 BOOST_TEST( px == qx );
250 BOOST_TEST( !( px < qx ) && !( qx < px ) );
253 BOOST_TEST( X::instances == 1 );
255 catch( boost::bad_weak_ptr const& )
257 BOOST_ERROR( "px->shared_from_this() failed" );
261 BOOST_TEST( X::instances == 0 );
263 return boost::report_errors();