1// make_local_shared_esft_test.cpp
2//
3// Copyright 2007-2009, 2017 Peter Dimov
4//
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
8
9#include <boost/config.hpp>
10
11#if defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) || defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES )
12
13int main()
14{
15}
16
17#else
18
19#include <boost/core/lightweight_test.hpp>
20#include <boost/smart_ptr/make_local_shared.hpp>
21#include <boost/shared_ptr.hpp>
22#include <boost/enable_shared_from_this.hpp>
23
24class X: public boost::enable_shared_from_this<X>
25{
26private:
27
28 X( X const & );
29 X & operator=( X const & );
30
31public:
32
33 static int instances;
34
35 explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
36 {
37 ++instances;
38 }
39
40 ~X()
41 {
42 --instances;
43 }
44};
45
46int X::instances = 0;
47
48int main()
49{
50 BOOST_TEST( X::instances == 0 );
51
52 {
53 boost::shared_ptr< X > px = boost::make_local_shared< X >();
54 BOOST_TEST( px.use_count() == 1 );
55 BOOST_TEST( X::instances == 1 );
56
57 try
58 {
59 boost::shared_ptr< X > qx = px->shared_from_this();
60
61 BOOST_TEST( px == qx );
62 BOOST_TEST( !( px < qx ) && !( qx < px ) );
63
64 px.reset();
65 BOOST_TEST( X::instances == 1 );
66 }
67 catch( boost::bad_weak_ptr const& )
68 {
69 BOOST_ERROR( "px->shared_from_this() failed" );
70 }
71 }
72
73 BOOST_TEST( X::instances == 0 );
74
75 {
76 boost::shared_ptr< X > px = boost::make_local_shared_noinit< X >();
77 BOOST_TEST( px.use_count() == 1 );
78 BOOST_TEST( X::instances == 1 );
79
80 try
81 {
82 boost::shared_ptr< X > qx = px->shared_from_this();
83
84 BOOST_TEST( px == qx );
85 BOOST_TEST( !( px < qx ) && !( qx < px ) );
86
87 px.reset();
88 BOOST_TEST( X::instances == 1 );
89 }
90 catch( boost::bad_weak_ptr const& )
91 {
92 BOOST_ERROR( "px->shared_from_this() failed" );
93 }
94 }
95
96 BOOST_TEST( X::instances == 0 );
97
98 {
99 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1 );
100 BOOST_TEST( px.use_count() == 1 );
101 BOOST_TEST( X::instances == 1 );
102
103 try
104 {
105 boost::shared_ptr< X > qx = px->shared_from_this();
106
107 BOOST_TEST( px == qx );
108 BOOST_TEST( !( px < qx ) && !( qx < px ) );
109
110 px.reset();
111 BOOST_TEST( X::instances == 1 );
112 }
113 catch( boost::bad_weak_ptr const& )
114 {
115 BOOST_ERROR( "px->shared_from_this() failed" );
116 }
117 }
118
119 BOOST_TEST( X::instances == 0 );
120
121 {
122 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2 );
123 BOOST_TEST( px.use_count() == 1 );
124 BOOST_TEST( X::instances == 1 );
125
126 try
127 {
128 boost::shared_ptr< X > qx = px->shared_from_this();
129
130 BOOST_TEST( px == qx );
131 BOOST_TEST( !( px < qx ) && !( qx < px ) );
132
133 px.reset();
134 BOOST_TEST( X::instances == 1 );
135 }
136 catch( boost::bad_weak_ptr const& )
137 {
138 BOOST_ERROR( "px->shared_from_this() failed" );
139 }
140 }
141
142 BOOST_TEST( X::instances == 0 );
143
144 {
145 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3 );
146 BOOST_TEST( px.use_count() == 1 );
147 BOOST_TEST( X::instances == 1 );
148
149 try
150 {
151 boost::shared_ptr< X > qx = px->shared_from_this();
152
153 BOOST_TEST( px == qx );
154 BOOST_TEST( !( px < qx ) && !( qx < px ) );
155
156 px.reset();
157 BOOST_TEST( X::instances == 1 );
158 }
159 catch( boost::bad_weak_ptr const& )
160 {
161 BOOST_ERROR( "px->shared_from_this() failed" );
162 }
163 }
164
165 BOOST_TEST( X::instances == 0 );
166
167 {
168 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3, args: 4 );
169 BOOST_TEST( px.use_count() == 1 );
170 BOOST_TEST( X::instances == 1 );
171
172 try
173 {
174 boost::shared_ptr< X > qx = px->shared_from_this();
175
176 BOOST_TEST( px == qx );
177 BOOST_TEST( !( px < qx ) && !( qx < px ) );
178
179 px.reset();
180 BOOST_TEST( X::instances == 1 );
181 }
182 catch( boost::bad_weak_ptr const& )
183 {
184 BOOST_ERROR( "px->shared_from_this() failed" );
185 }
186 }
187
188 BOOST_TEST( X::instances == 0 );
189
190 {
191 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3, args: 4, args: 5 );
192 BOOST_TEST( px.use_count() == 1 );
193 BOOST_TEST( X::instances == 1 );
194
195 try
196 {
197 boost::shared_ptr< X > qx = px->shared_from_this();
198
199 BOOST_TEST( px == qx );
200 BOOST_TEST( !( px < qx ) && !( qx < px ) );
201
202 px.reset();
203 BOOST_TEST( X::instances == 1 );
204 }
205 catch( boost::bad_weak_ptr const& )
206 {
207 BOOST_ERROR( "px->shared_from_this() failed" );
208 }
209 }
210
211 BOOST_TEST( X::instances == 0 );
212
213 {
214 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3, args: 4, args: 5, args: 6 );
215 BOOST_TEST( px.use_count() == 1 );
216 BOOST_TEST( X::instances == 1 );
217
218 try
219 {
220 boost::shared_ptr< X > qx = px->shared_from_this();
221
222 BOOST_TEST( px == qx );
223 BOOST_TEST( !( px < qx ) && !( qx < px ) );
224
225 px.reset();
226 BOOST_TEST( X::instances == 1 );
227 }
228 catch( boost::bad_weak_ptr const& )
229 {
230 BOOST_ERROR( "px->shared_from_this() failed" );
231 }
232 }
233
234 BOOST_TEST( X::instances == 0 );
235
236 {
237 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3, args: 4, args: 5, args: 6, args: 7 );
238 BOOST_TEST( px.use_count() == 1 );
239 BOOST_TEST( X::instances == 1 );
240
241 try
242 {
243 boost::shared_ptr< X > qx = px->shared_from_this();
244
245 BOOST_TEST( px == qx );
246 BOOST_TEST( !( px < qx ) && !( qx < px ) );
247
248 px.reset();
249 BOOST_TEST( X::instances == 1 );
250 }
251 catch( boost::bad_weak_ptr const& )
252 {
253 BOOST_ERROR( "px->shared_from_this() failed" );
254 }
255 }
256
257 BOOST_TEST( X::instances == 0 );
258
259 {
260 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3, args: 4, args: 5, args: 6, args: 7, args: 8 );
261 BOOST_TEST( px.use_count() == 1 );
262 BOOST_TEST( X::instances == 1 );
263
264 try
265 {
266 boost::shared_ptr< X > qx = px->shared_from_this();
267
268 BOOST_TEST( px == qx );
269 BOOST_TEST( !( px < qx ) && !( qx < px ) );
270
271 px.reset();
272 BOOST_TEST( X::instances == 1 );
273 }
274 catch( boost::bad_weak_ptr const& )
275 {
276 BOOST_ERROR( "px->shared_from_this() failed" );
277 }
278 }
279
280 BOOST_TEST( X::instances == 0 );
281
282 {
283 boost::shared_ptr< X > px = boost::make_local_shared< X >( args: 1, args: 2, args: 3, args: 4, args: 5, args: 6, args: 7, args: 8, args: 9 );
284 BOOST_TEST( px.use_count() == 1 );
285 BOOST_TEST( X::instances == 1 );
286
287 try
288 {
289 boost::shared_ptr< X > qx = px->shared_from_this();
290
291 BOOST_TEST( px == qx );
292 BOOST_TEST( !( px < qx ) && !( qx < px ) );
293
294 px.reset();
295 BOOST_TEST( X::instances == 1 );
296 }
297 catch( boost::bad_weak_ptr const& )
298 {
299 BOOST_ERROR( "px->shared_from_this() failed" );
300 }
301 }
302
303 BOOST_TEST( X::instances == 0 );
304
305 return boost::report_errors();
306}
307
308#endif
309

source code of boost/libs/smart_ptr/test/make_local_shared_esft_test.cpp