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