1 // allocate_local_shared_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 
main()13 int 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/smart_ptr/local_shared_ptr.hpp>
22 #include <boost/weak_ptr.hpp>
23 #include <cstddef>
24 
25 class X
26 {
27 private:
28 
29     X( X const & );
30     X & operator=( X const & );
31 
operator new(std::size_t n)32     void * operator new( std::size_t n )
33     {
34         // lack of this definition causes link errors on Comeau C++
35         BOOST_ERROR( "private X::new called" );
36         return ::operator new( n );
37     }
38 
operator delete(void * p)39     void operator delete( void * p )
40     {
41         // lack of this definition causes link errors on MSVC
42         BOOST_ERROR( "private X::delete called" );
43         ::operator delete( p );
44     }
45 
46 public:
47 
48     static int instances;
49 
50     int v;
51 
X(int a1=0,int a2=0,int a3=0,int a4=0,int a5=0,int a6=0,int a7=0,int a8=0,int a9=0)52     explicit X( int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0, int a5 = 0, int a6 = 0, int a7 = 0, int a8 = 0, int a9 = 0 ): v( a1+a2+a3+a4+a5+a6+a7+a8+a9 )
53     {
54         ++instances;
55     }
56 
~X()57     ~X()
58     {
59         --instances;
60     }
61 };
62 
63 int X::instances = 0;
64 
main()65 int main()
66 {
67     {
68         boost::local_shared_ptr< int > pi = boost::allocate_local_shared< int >( std::allocator<int>() );
69 
70         BOOST_TEST( pi.get() != 0 );
71         BOOST_TEST( *pi == 0 );
72     }
73 
74     {
75         boost::local_shared_ptr< int > pi = boost::allocate_local_shared_noinit< int >( std::allocator<int>() );
76 
77         BOOST_TEST( pi.get() != 0 );
78     }
79 
80     {
81         boost::local_shared_ptr< int > pi = boost::allocate_local_shared< int >( std::allocator<int>(), 5 );
82 
83         BOOST_TEST( pi.get() != 0 );
84         BOOST_TEST( *pi == 5 );
85     }
86 
87     BOOST_TEST( X::instances == 0 );
88 
89     {
90         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>() );
91         boost::weak_ptr<X> wp( pi );
92 
93         BOOST_TEST( X::instances == 1 );
94         BOOST_TEST( pi.get() != 0 );
95         BOOST_TEST( pi->v == 0 );
96 
97         pi.reset();
98 
99         BOOST_TEST( X::instances == 0 );
100     }
101 
102     {
103         boost::local_shared_ptr< X > pi = boost::allocate_local_shared_noinit< X >( std::allocator<void>() );
104         boost::weak_ptr<X> wp( pi );
105 
106         BOOST_TEST( X::instances == 1 );
107         BOOST_TEST( pi.get() != 0 );
108         BOOST_TEST( pi->v == 0 );
109 
110         pi.reset();
111 
112         BOOST_TEST( X::instances == 0 );
113     }
114 
115     {
116         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1 );
117         boost::weak_ptr<X> wp( pi );
118 
119         BOOST_TEST( X::instances == 1 );
120         BOOST_TEST( pi.get() != 0 );
121         BOOST_TEST( pi->v == 1 );
122 
123         pi.reset();
124 
125         BOOST_TEST( X::instances == 0 );
126     }
127 
128     {
129         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2 );
130         boost::weak_ptr<X> wp( pi );
131 
132         BOOST_TEST( X::instances == 1 );
133         BOOST_TEST( pi.get() != 0 );
134         BOOST_TEST( pi->v == 1+2 );
135 
136         pi.reset();
137 
138         BOOST_TEST( X::instances == 0 );
139     }
140 
141     {
142         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3 );
143         boost::weak_ptr<X> wp( pi );
144 
145         BOOST_TEST( X::instances == 1 );
146         BOOST_TEST( pi.get() != 0 );
147         BOOST_TEST( pi->v == 1+2+3 );
148 
149         pi.reset();
150 
151         BOOST_TEST( X::instances == 0 );
152     }
153 
154     {
155         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4 );
156         boost::weak_ptr<X> wp( pi );
157 
158         BOOST_TEST( X::instances == 1 );
159         BOOST_TEST( pi.get() != 0 );
160         BOOST_TEST( pi->v == 1+2+3+4 );
161 
162         pi.reset();
163 
164         BOOST_TEST( X::instances == 0 );
165     }
166 
167     {
168         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5 );
169         boost::weak_ptr<X> wp( pi );
170 
171         BOOST_TEST( X::instances == 1 );
172         BOOST_TEST( pi.get() != 0 );
173         BOOST_TEST( pi->v == 1+2+3+4+5 );
174 
175         pi.reset();
176 
177         BOOST_TEST( X::instances == 0 );
178     }
179 
180     {
181         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6 );
182         boost::weak_ptr<X> wp( pi );
183 
184         BOOST_TEST( X::instances == 1 );
185         BOOST_TEST( pi.get() != 0 );
186         BOOST_TEST( pi->v == 1+2+3+4+5+6 );
187 
188         pi.reset();
189 
190         BOOST_TEST( X::instances == 0 );
191     }
192 
193     {
194         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
195         boost::weak_ptr<X> wp( pi );
196 
197         BOOST_TEST( X::instances == 1 );
198         BOOST_TEST( pi.get() != 0 );
199         BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
200 
201         pi.reset();
202 
203         BOOST_TEST( X::instances == 0 );
204     }
205 
206     {
207         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
208         boost::weak_ptr<X> wp( pi );
209 
210         BOOST_TEST( X::instances == 1 );
211         BOOST_TEST( pi.get() != 0 );
212         BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
213 
214         pi.reset();
215 
216         BOOST_TEST( X::instances == 0 );
217     }
218 
219     {
220         boost::local_shared_ptr< X > pi = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
221         boost::weak_ptr<X> wp( pi );
222 
223         BOOST_TEST( X::instances == 1 );
224         BOOST_TEST( pi.get() != 0 );
225         BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
226 
227         pi.reset();
228 
229         BOOST_TEST( X::instances == 0 );
230     }
231 
232     return boost::report_errors();
233 }
234 
235 #endif
236