1 //  allocate_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/detail/lightweight_test.hpp>
10 #include <boost/make_shared.hpp>
11 #include <boost/shared_ptr.hpp>
12 #include <boost/enable_shared_from_this.hpp>
13 #include <memory>
14 
15 class X: public boost::enable_shared_from_this<X>
16 {
17 private:
18 
19     X( X const & );
20     X & operator=( X const & );
21 
22 public:
23 
24     static int instances;
25 
X(int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0)26     explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
27     {
28         ++instances;
29     }
30 
~X()31     ~X()
32     {
33         --instances;
34     }
35 };
36 
37 int X::instances = 0;
38 
main()39 int main()
40 {
41     BOOST_TEST( X::instances == 0 );
42 
43     {
44         boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>() );
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::allocate_shared< X >( std::allocator<void>(), 1 );
67         BOOST_TEST( X::instances == 1 );
68 
69         try
70         {
71             boost::shared_ptr< X > qx = px->shared_from_this();
72 
73             BOOST_TEST( px == qx );
74             BOOST_TEST( !( px < qx ) && !( qx < px ) );
75 
76             px.reset();
77             BOOST_TEST( X::instances == 1 );
78         }
79         catch( boost::bad_weak_ptr const& )
80         {
81             BOOST_ERROR( "px->shared_from_this() failed" );
82         }
83     }
84 
85     BOOST_TEST( X::instances == 0 );
86 
87     {
88         boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2 );
89         BOOST_TEST( X::instances == 1 );
90 
91         try
92         {
93             boost::shared_ptr< X > qx = px->shared_from_this();
94 
95             BOOST_TEST( px == qx );
96             BOOST_TEST( !( px < qx ) && !( qx < px ) );
97 
98             px.reset();
99             BOOST_TEST( X::instances == 1 );
100         }
101         catch( boost::bad_weak_ptr const& )
102         {
103             BOOST_ERROR( "px->shared_from_this() failed" );
104         }
105     }
106 
107     BOOST_TEST( X::instances == 0 );
108 
109     {
110         boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3 );
111         BOOST_TEST( X::instances == 1 );
112 
113         try
114         {
115             boost::shared_ptr< X > qx = px->shared_from_this();
116 
117             BOOST_TEST( px == qx );
118             BOOST_TEST( !( px < qx ) && !( qx < px ) );
119 
120             px.reset();
121             BOOST_TEST( X::instances == 1 );
122         }
123         catch( boost::bad_weak_ptr const& )
124         {
125             BOOST_ERROR( "px->shared_from_this() failed" );
126         }
127     }
128 
129     BOOST_TEST( X::instances == 0 );
130 
131     {
132         boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4 );
133         BOOST_TEST( X::instances == 1 );
134 
135         try
136         {
137             boost::shared_ptr< X > qx = px->shared_from_this();
138 
139             BOOST_TEST( px == qx );
140             BOOST_TEST( !( px < qx ) && !( qx < px ) );
141 
142             px.reset();
143             BOOST_TEST( X::instances == 1 );
144         }
145         catch( boost::bad_weak_ptr const& )
146         {
147             BOOST_ERROR( "px->shared_from_this() failed" );
148         }
149     }
150 
151     BOOST_TEST( X::instances == 0 );
152 
153     {
154         boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5 );
155         BOOST_TEST( X::instances == 1 );
156 
157         try
158         {
159             boost::shared_ptr< X > qx = px->shared_from_this();
160 
161             BOOST_TEST( px == qx );
162             BOOST_TEST( !( px < qx ) && !( qx < px ) );
163 
164             px.reset();
165             BOOST_TEST( X::instances == 1 );
166         }
167         catch( boost::bad_weak_ptr const& )
168         {
169             BOOST_ERROR( "px->shared_from_this() failed" );
170         }
171     }
172 
173     BOOST_TEST( X::instances == 0 );
174 
175     {
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 );
178 
179         try
180         {
181             boost::shared_ptr< X > qx = px->shared_from_this();
182 
183             BOOST_TEST( px == qx );
184             BOOST_TEST( !( px < qx ) && !( qx < px ) );
185 
186             px.reset();
187             BOOST_TEST( X::instances == 1 );
188         }
189         catch( boost::bad_weak_ptr const& )
190         {
191             BOOST_ERROR( "px->shared_from_this() failed" );
192         }
193     }
194 
195     BOOST_TEST( X::instances == 0 );
196 
197     {
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 );
200 
201         try
202         {
203             boost::shared_ptr< X > qx = px->shared_from_this();
204 
205             BOOST_TEST( px == qx );
206             BOOST_TEST( !( px < qx ) && !( qx < px ) );
207 
208             px.reset();
209             BOOST_TEST( X::instances == 1 );
210         }
211         catch( boost::bad_weak_ptr const& )
212         {
213             BOOST_ERROR( "px->shared_from_this() failed" );
214         }
215     }
216 
217     BOOST_TEST( X::instances == 0 );
218 
219     {
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 );
222 
223         try
224         {
225             boost::shared_ptr< X > qx = px->shared_from_this();
226 
227             BOOST_TEST( px == qx );
228             BOOST_TEST( !( px < qx ) && !( qx < px ) );
229 
230             px.reset();
231             BOOST_TEST( X::instances == 1 );
232         }
233         catch( boost::bad_weak_ptr const& )
234         {
235             BOOST_ERROR( "px->shared_from_this() failed" );
236         }
237     }
238 
239     BOOST_TEST( X::instances == 0 );
240 
241     {
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 );
244 
245         try
246         {
247             boost::shared_ptr< X > qx = px->shared_from_this();
248 
249             BOOST_TEST( px == qx );
250             BOOST_TEST( !( px < qx ) && !( qx < px ) );
251 
252             px.reset();
253             BOOST_TEST( X::instances == 1 );
254         }
255         catch( boost::bad_weak_ptr const& )
256         {
257             BOOST_ERROR( "px->shared_from_this() failed" );
258         }
259     }
260 
261     BOOST_TEST( X::instances == 0 );
262 
263     return boost::report_errors();
264 }
265