]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // allocate_shared_alloc11_test.cpp |
2 | // | |
3 | // allocate_shared with a minimal C++11 allocator | |
4 | // | |
5 | // Copyright 2007-2009, 2014 Peter Dimov | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. | |
8 | // See accompanying file LICENSE_1_0.txt or copy at | |
9 | // http://www.boost.org/LICENSE_1_0.txt | |
10 | ||
f67539c2 | 11 | #include <boost/core/lightweight_test.hpp> |
7c673cae FG |
12 | #include <boost/make_shared.hpp> |
13 | #include <boost/shared_ptr.hpp> | |
14 | #include <boost/weak_ptr.hpp> | |
15 | #include <boost/config.hpp> | |
16 | #include <cstddef> | |
17 | ||
18 | #if !defined( BOOST_NO_CXX11_ALLOCATOR ) | |
19 | ||
20 | template< class T > class cxx11_allocator | |
21 | { | |
22 | public: | |
23 | ||
24 | typedef T value_type; | |
25 | ||
26 | cxx11_allocator() | |
27 | { | |
28 | } | |
29 | ||
30 | template< class Y > cxx11_allocator( cxx11_allocator<Y> const & ) | |
31 | { | |
32 | } | |
33 | ||
34 | T * allocate( std::size_t n ) | |
35 | { | |
36 | return static_cast< T* >( ::operator new( n * sizeof( T ) ) ); | |
37 | } | |
38 | ||
39 | void deallocate( T * p, std::size_t n ) | |
40 | { | |
41 | ::operator delete( p ); | |
42 | } | |
43 | }; | |
44 | ||
45 | class X | |
46 | { | |
47 | private: | |
48 | ||
49 | X( X const & ); | |
50 | X & operator=( X const & ); | |
51 | ||
52 | void * operator new( std::size_t n ) | |
53 | { | |
54 | BOOST_ERROR( "private X::new called" ); | |
55 | return ::operator new( n ); | |
56 | } | |
57 | ||
58 | void operator delete( void * p ) | |
59 | { | |
60 | BOOST_ERROR( "private X::delete called" ); | |
61 | ::operator delete( p ); | |
62 | } | |
63 | ||
64 | public: | |
65 | ||
66 | static int instances; | |
67 | ||
68 | int v; | |
69 | ||
70 | 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 ) | |
71 | { | |
72 | ++instances; | |
73 | } | |
74 | ||
75 | ~X() | |
76 | { | |
77 | --instances; | |
78 | } | |
79 | }; | |
80 | ||
81 | int X::instances = 0; | |
82 | ||
83 | int main() | |
84 | { | |
85 | { | |
86 | boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>() ); | |
87 | ||
88 | BOOST_TEST( pi.get() != 0 ); | |
89 | BOOST_TEST( *pi == 0 ); | |
90 | } | |
91 | ||
92 | { | |
93 | boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>(), 5 ); | |
94 | ||
95 | BOOST_TEST( pi.get() != 0 ); | |
96 | BOOST_TEST( *pi == 5 ); | |
97 | } | |
98 | ||
99 | BOOST_TEST( X::instances == 0 ); | |
100 | ||
101 | { | |
102 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>() ); | |
103 | boost::weak_ptr<X> wp( pi ); | |
104 | ||
105 | BOOST_TEST( X::instances == 1 ); | |
106 | BOOST_TEST( pi.get() != 0 ); | |
107 | BOOST_TEST( pi->v == 0 ); | |
108 | ||
109 | pi.reset(); | |
110 | ||
111 | BOOST_TEST( X::instances == 0 ); | |
112 | } | |
113 | ||
114 | { | |
115 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1 ); | |
116 | boost::weak_ptr<X> wp( pi ); | |
117 | ||
118 | BOOST_TEST( X::instances == 1 ); | |
119 | BOOST_TEST( pi.get() != 0 ); | |
120 | BOOST_TEST( pi->v == 1 ); | |
121 | ||
122 | pi.reset(); | |
123 | ||
124 | BOOST_TEST( X::instances == 0 ); | |
125 | } | |
126 | ||
127 | { | |
128 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2 ); | |
129 | boost::weak_ptr<X> wp( pi ); | |
130 | ||
131 | BOOST_TEST( X::instances == 1 ); | |
132 | BOOST_TEST( pi.get() != 0 ); | |
133 | BOOST_TEST( pi->v == 1+2 ); | |
134 | ||
135 | pi.reset(); | |
136 | ||
137 | BOOST_TEST( X::instances == 0 ); | |
138 | } | |
139 | ||
140 | { | |
141 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3 ); | |
142 | boost::weak_ptr<X> wp( pi ); | |
143 | ||
144 | BOOST_TEST( X::instances == 1 ); | |
145 | BOOST_TEST( pi.get() != 0 ); | |
146 | BOOST_TEST( pi->v == 1+2+3 ); | |
147 | ||
148 | pi.reset(); | |
149 | ||
150 | BOOST_TEST( X::instances == 0 ); | |
151 | } | |
152 | ||
153 | { | |
154 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4 ); | |
155 | boost::weak_ptr<X> wp( pi ); | |
156 | ||
157 | BOOST_TEST( X::instances == 1 ); | |
158 | BOOST_TEST( pi.get() != 0 ); | |
159 | BOOST_TEST( pi->v == 1+2+3+4 ); | |
160 | ||
161 | pi.reset(); | |
162 | ||
163 | BOOST_TEST( X::instances == 0 ); | |
164 | } | |
165 | ||
166 | { | |
167 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5 ); | |
168 | boost::weak_ptr<X> wp( pi ); | |
169 | ||
170 | BOOST_TEST( X::instances == 1 ); | |
171 | BOOST_TEST( pi.get() != 0 ); | |
172 | BOOST_TEST( pi->v == 1+2+3+4+5 ); | |
173 | ||
174 | pi.reset(); | |
175 | ||
176 | BOOST_TEST( X::instances == 0 ); | |
177 | } | |
178 | ||
179 | { | |
180 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6 ); | |
181 | boost::weak_ptr<X> wp( pi ); | |
182 | ||
183 | BOOST_TEST( X::instances == 1 ); | |
184 | BOOST_TEST( pi.get() != 0 ); | |
185 | BOOST_TEST( pi->v == 1+2+3+4+5+6 ); | |
186 | ||
187 | pi.reset(); | |
188 | ||
189 | BOOST_TEST( X::instances == 0 ); | |
190 | } | |
191 | ||
192 | { | |
193 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7 ); | |
194 | boost::weak_ptr<X> wp( pi ); | |
195 | ||
196 | BOOST_TEST( X::instances == 1 ); | |
197 | BOOST_TEST( pi.get() != 0 ); | |
198 | BOOST_TEST( pi->v == 1+2+3+4+5+6+7 ); | |
199 | ||
200 | pi.reset(); | |
201 | ||
202 | BOOST_TEST( X::instances == 0 ); | |
203 | } | |
204 | ||
205 | { | |
206 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 ); | |
207 | boost::weak_ptr<X> wp( pi ); | |
208 | ||
209 | BOOST_TEST( X::instances == 1 ); | |
210 | BOOST_TEST( pi.get() != 0 ); | |
211 | BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 ); | |
212 | ||
213 | pi.reset(); | |
214 | ||
215 | BOOST_TEST( X::instances == 0 ); | |
216 | } | |
217 | ||
218 | { | |
219 | boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 ); | |
220 | boost::weak_ptr<X> wp( pi ); | |
221 | ||
222 | BOOST_TEST( X::instances == 1 ); | |
223 | BOOST_TEST( pi.get() != 0 ); | |
224 | BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 ); | |
225 | ||
226 | pi.reset(); | |
227 | ||
228 | BOOST_TEST( X::instances == 0 ); | |
229 | } | |
230 | ||
231 | return boost::report_errors(); | |
232 | } | |
233 | ||
234 | #else // !defined( BOOST_NO_CXX11_ALLOCATOR ) | |
235 | ||
236 | int main() | |
237 | { | |
238 | return 0; | |
239 | } | |
240 | ||
241 | #endif |