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/core/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 | |
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 | |
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 | |
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 | |
47 | ~X() |
48 | { |
49 | --instances; |
50 | } |
51 | }; |
52 | |
53 | int X::instances = 0; |
54 | |
55 | int main() |
56 | { |
57 | { |
58 | boost::shared_ptr< int > pi = boost::allocate_shared< int >( a: 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_noinit< int >( a: std::allocator<int>() ); |
66 | |
67 | BOOST_TEST( pi.get() != 0 ); |
68 | } |
69 | |
70 | { |
71 | boost::shared_ptr< int > pi = boost::allocate_shared< int >( a: std::allocator<int>(), args: 5 ); |
72 | |
73 | BOOST_TEST( pi.get() != 0 ); |
74 | BOOST_TEST( *pi == 5 ); |
75 | } |
76 | |
77 | BOOST_TEST( X::instances == 0 ); |
78 | |
79 | { |
80 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>() ); |
81 | boost::weak_ptr<X> wp( pi ); |
82 | |
83 | BOOST_TEST( X::instances == 1 ); |
84 | BOOST_TEST( pi.get() != 0 ); |
85 | BOOST_TEST( pi->v == 0 ); |
86 | |
87 | pi.reset(); |
88 | |
89 | BOOST_TEST( X::instances == 0 ); |
90 | } |
91 | |
92 | { |
93 | boost::shared_ptr< X > pi = boost::allocate_shared_noinit< X >( a: std::allocator<void>() ); |
94 | boost::weak_ptr<X> wp( pi ); |
95 | |
96 | BOOST_TEST( X::instances == 1 ); |
97 | BOOST_TEST( pi.get() != 0 ); |
98 | BOOST_TEST( pi->v == 0 ); |
99 | |
100 | pi.reset(); |
101 | |
102 | BOOST_TEST( X::instances == 0 ); |
103 | } |
104 | |
105 | { |
106 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1 ); |
107 | boost::weak_ptr<X> wp( pi ); |
108 | |
109 | BOOST_TEST( X::instances == 1 ); |
110 | BOOST_TEST( pi.get() != 0 ); |
111 | BOOST_TEST( pi->v == 1 ); |
112 | |
113 | pi.reset(); |
114 | |
115 | BOOST_TEST( X::instances == 0 ); |
116 | } |
117 | |
118 | { |
119 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2 ); |
120 | boost::weak_ptr<X> wp( pi ); |
121 | |
122 | BOOST_TEST( X::instances == 1 ); |
123 | BOOST_TEST( pi.get() != 0 ); |
124 | BOOST_TEST( pi->v == 1+2 ); |
125 | |
126 | pi.reset(); |
127 | |
128 | BOOST_TEST( X::instances == 0 ); |
129 | } |
130 | |
131 | { |
132 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3 ); |
133 | boost::weak_ptr<X> wp( pi ); |
134 | |
135 | BOOST_TEST( X::instances == 1 ); |
136 | BOOST_TEST( pi.get() != 0 ); |
137 | BOOST_TEST( pi->v == 1+2+3 ); |
138 | |
139 | pi.reset(); |
140 | |
141 | BOOST_TEST( X::instances == 0 ); |
142 | } |
143 | |
144 | { |
145 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3, args: 4 ); |
146 | boost::weak_ptr<X> wp( pi ); |
147 | |
148 | BOOST_TEST( X::instances == 1 ); |
149 | BOOST_TEST( pi.get() != 0 ); |
150 | BOOST_TEST( pi->v == 1+2+3+4 ); |
151 | |
152 | pi.reset(); |
153 | |
154 | BOOST_TEST( X::instances == 0 ); |
155 | } |
156 | |
157 | { |
158 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3, args: 4, args: 5 ); |
159 | boost::weak_ptr<X> wp( pi ); |
160 | |
161 | BOOST_TEST( X::instances == 1 ); |
162 | BOOST_TEST( pi.get() != 0 ); |
163 | BOOST_TEST( pi->v == 1+2+3+4+5 ); |
164 | |
165 | pi.reset(); |
166 | |
167 | BOOST_TEST( X::instances == 0 ); |
168 | } |
169 | |
170 | { |
171 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3, args: 4, args: 5, args: 6 ); |
172 | boost::weak_ptr<X> wp( pi ); |
173 | |
174 | BOOST_TEST( X::instances == 1 ); |
175 | BOOST_TEST( pi.get() != 0 ); |
176 | BOOST_TEST( pi->v == 1+2+3+4+5+6 ); |
177 | |
178 | pi.reset(); |
179 | |
180 | BOOST_TEST( X::instances == 0 ); |
181 | } |
182 | |
183 | { |
184 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3, args: 4, args: 5, args: 6, args: 7 ); |
185 | boost::weak_ptr<X> wp( pi ); |
186 | |
187 | BOOST_TEST( X::instances == 1 ); |
188 | BOOST_TEST( pi.get() != 0 ); |
189 | BOOST_TEST( pi->v == 1+2+3+4+5+6+7 ); |
190 | |
191 | pi.reset(); |
192 | |
193 | BOOST_TEST( X::instances == 0 ); |
194 | } |
195 | |
196 | { |
197 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3, args: 4, args: 5, args: 6, args: 7, args: 8 ); |
198 | boost::weak_ptr<X> wp( pi ); |
199 | |
200 | BOOST_TEST( X::instances == 1 ); |
201 | BOOST_TEST( pi.get() != 0 ); |
202 | BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 ); |
203 | |
204 | pi.reset(); |
205 | |
206 | BOOST_TEST( X::instances == 0 ); |
207 | } |
208 | |
209 | { |
210 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( a: std::allocator<void>(), args: 1, args: 2, args: 3, args: 4, args: 5, args: 6, args: 7, args: 8, args: 9 ); |
211 | boost::weak_ptr<X> wp( pi ); |
212 | |
213 | BOOST_TEST( X::instances == 1 ); |
214 | BOOST_TEST( pi.get() != 0 ); |
215 | BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 ); |
216 | |
217 | pi.reset(); |
218 | |
219 | BOOST_TEST( X::instances == 0 ); |
220 | } |
221 | |
222 | return boost::report_errors(); |
223 | } |
224 | |