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