]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/test/allocate_shared_array_test.cpp
2 Copyright 2012-2015 Glen Joseph Fernandes
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/align/is_aligned.hpp>
9 #include <boost/core/lightweight_test.hpp>
10 #include <boost/smart_ptr/make_shared.hpp>
11 #include <boost/smart_ptr/weak_ptr.hpp>
12 #include <boost/type_traits/alignment_of.hpp>
14 template<class T
= void>
20 typedef creator
<U
> other
;
26 creator(const creator
<U
>&) { }
28 T
* allocate(std::size_t size
) {
29 return static_cast<T
*>(::operator new(sizeof(T
) * size
));
32 void deallocate(T
* ptr
, std::size_t) {
33 ::operator delete(ptr
);
37 template<class T
, class U
>
39 operator==(const creator
<T
>&, const creator
<U
>&)
44 template<class T
, class U
>
46 operator!=(const creator
<T
>&, const creator
<U
>&)
53 static unsigned instances
;
64 void set(long double value
) {
68 long double get() const {
74 type
& operator=(const type
&);
79 unsigned type::instances
= 0;
84 boost::shared_ptr
<int[]> result
=
85 boost::allocate_shared
<int[]>(creator
<int>(), 3);
86 BOOST_TEST(result
.get() != 0);
87 BOOST_TEST(result
.use_count() == 1);
88 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
89 boost::alignment_of
<int>::value
));
90 BOOST_TEST(result
[0] == 0);
91 BOOST_TEST(result
[1] == 0);
92 BOOST_TEST(result
[2] == 0);
95 boost::shared_ptr
<int[3]> result
=
96 boost::allocate_shared
<int[3]>(creator
<int>());
97 BOOST_TEST(result
.get() != 0);
98 BOOST_TEST(result
.use_count() == 1);
99 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
100 boost::alignment_of
<int>::value
));
101 BOOST_TEST(result
[0] == 0);
102 BOOST_TEST(result
[1] == 0);
103 BOOST_TEST(result
[2] == 0);
106 boost::shared_ptr
<int[][2]> result
=
107 boost::allocate_shared
<int[][2]>(creator
<>(), 2);
108 BOOST_TEST(result
.get() != 0);
109 BOOST_TEST(result
.use_count() == 1);
110 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
111 boost::alignment_of
<int>::value
));
112 BOOST_TEST(result
[0][0] == 0);
113 BOOST_TEST(result
[0][1] == 0);
114 BOOST_TEST(result
[1][0] == 0);
115 BOOST_TEST(result
[1][1] == 0);
118 boost::shared_ptr
<int[2][2]> result
=
119 boost::allocate_shared
<int[2][2]>(creator
<>());
120 BOOST_TEST(result
.get() != 0);
121 BOOST_TEST(result
.use_count() == 1);
122 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
123 boost::alignment_of
<int>::value
));
124 BOOST_TEST(result
[0][0] == 0);
125 BOOST_TEST(result
[0][1] == 0);
126 BOOST_TEST(result
[1][0] == 0);
127 BOOST_TEST(result
[1][1] == 0);
130 boost::shared_ptr
<const int[]> result
=
131 boost::allocate_shared
<const int[]>(creator
<>(), 3);
132 BOOST_TEST(result
.get() != 0);
133 BOOST_TEST(result
.use_count() == 1);
134 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
135 boost::alignment_of
<int>::value
));
136 BOOST_TEST(result
[0] == 0);
137 BOOST_TEST(result
[1] == 0);
138 BOOST_TEST(result
[2] == 0);
141 boost::shared_ptr
<const int[3]> result
=
142 boost::allocate_shared
<const int[3]>(creator
<>());
143 BOOST_TEST(result
.get() != 0);
144 BOOST_TEST(result
.use_count() == 1);
145 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
146 boost::alignment_of
<int>::value
));
147 BOOST_TEST(result
[0] == 0);
148 BOOST_TEST(result
[1] == 0);
149 BOOST_TEST(result
[2] == 0);
152 boost::shared_ptr
<const int[][2]> result
=
153 boost::allocate_shared
<const int[][2]>(creator
<>(), 2);
154 BOOST_TEST(result
.get() != 0);
155 BOOST_TEST(result
.use_count() == 1);
156 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
157 boost::alignment_of
<int>::value
));
158 BOOST_TEST(result
[0][0] == 0);
159 BOOST_TEST(result
[0][1] == 0);
160 BOOST_TEST(result
[1][0] == 0);
161 BOOST_TEST(result
[1][1] == 0);
164 boost::shared_ptr
<const int[2][2]> result
=
165 boost::allocate_shared
<const int[2][2]>(creator
<>());
166 BOOST_TEST(result
.get() != 0);
167 BOOST_TEST(result
.use_count() == 1);
168 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
169 boost::alignment_of
<int>::value
));
170 BOOST_TEST(result
[0][0] == 0);
171 BOOST_TEST(result
[0][1] == 0);
172 BOOST_TEST(result
[1][0] == 0);
173 BOOST_TEST(result
[1][1] == 0);
176 boost::shared_ptr
<type
[]> result
=
177 boost::allocate_shared
<type
[]>(creator
<type
>(), 3);
178 BOOST_TEST(result
.get() != 0);
179 BOOST_TEST(result
.use_count() == 1);
180 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
181 boost::alignment_of
<type
>::value
));
182 BOOST_TEST(type::instances
== 3);
183 boost::weak_ptr
<type
[]> w1
= result
;
185 BOOST_TEST(type::instances
== 0);
188 boost::shared_ptr
<type
[3]> result
=
189 boost::allocate_shared
<type
[3]>(creator
<type
>());
190 BOOST_TEST(result
.get() != 0);
191 BOOST_TEST(result
.use_count() == 1);
192 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
193 boost::alignment_of
<type
>::value
));
194 BOOST_TEST(type::instances
== 3);
195 boost::weak_ptr
<type
[3]> w1
= result
;
197 BOOST_TEST(type::instances
== 0);
200 boost::shared_ptr
<type
[][2]> result
=
201 boost::allocate_shared
<type
[][2]>(creator
<>(), 2);
202 BOOST_TEST(result
.get() != 0);
203 BOOST_TEST(result
.use_count() == 1);
204 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
205 boost::alignment_of
<type
>::value
));
206 BOOST_TEST(type::instances
== 4);
208 BOOST_TEST(type::instances
== 0);
211 boost::shared_ptr
<type
[2][2]> result
=
212 boost::allocate_shared
<type
[2][2]>(creator
<>());
213 BOOST_TEST(result
.get() != 0);
214 BOOST_TEST(result
.use_count() == 1);
215 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
216 boost::alignment_of
<type
>::value
));
217 BOOST_TEST(type::instances
== 4);
219 BOOST_TEST(type::instances
== 0);
222 boost::shared_ptr
<const type
[]> result
=
223 boost::allocate_shared
<const type
[]>(creator
<>(), 3);
224 BOOST_TEST(result
.get() != 0);
225 BOOST_TEST(result
.use_count() == 1);
226 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
227 boost::alignment_of
<type
>::value
));
228 BOOST_TEST(type::instances
== 3);
230 BOOST_TEST(type::instances
== 0);
233 boost::shared_ptr
<const type
[3]> result
=
234 boost::allocate_shared
<const type
[3]>(creator
<>());
235 BOOST_TEST(result
.get() != 0);
236 BOOST_TEST(result
.use_count() == 1);
237 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
238 boost::alignment_of
<type
>::value
));
239 BOOST_TEST(type::instances
== 3);
241 BOOST_TEST(type::instances
== 0);
244 boost::shared_ptr
<const type
[][2]> result
=
245 boost::allocate_shared
<const type
[][2]>(creator
<>(), 2);
246 BOOST_TEST(result
.get() != 0);
247 BOOST_TEST(result
.use_count() == 1);
248 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
249 boost::alignment_of
<type
>::value
));
250 BOOST_TEST(type::instances
== 4);
252 BOOST_TEST(type::instances
== 0);
255 boost::shared_ptr
<const type
[2][2]> result
=
256 boost::allocate_shared
<const type
[2][2]>(creator
<>());
257 BOOST_TEST(result
.get() != 0);
258 BOOST_TEST(result
.use_count() == 1);
259 BOOST_TEST(boost::alignment::is_aligned(result
.get(),
260 boost::alignment_of
<type
>::value
));
261 BOOST_TEST(type::instances
== 4);
263 BOOST_TEST(type::instances
== 0);
265 return boost::report_errors();