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>
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_noinit<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));
92 boost::shared_ptr<int[3]> result =
93 boost::allocate_shared_noinit<int[3]>(creator<int>());
94 BOOST_TEST(result.get() != 0);
95 BOOST_TEST(result.use_count() == 1);
96 BOOST_TEST(boost::alignment::is_aligned(result.get(),
97 boost::alignment_of<int>::value));
100 boost::shared_ptr<int[][2]> result =
101 boost::allocate_shared_noinit<int[][2]>(creator<int>(), 2);
102 BOOST_TEST(result.get() != 0);
103 BOOST_TEST(result.use_count() == 1);
104 BOOST_TEST(boost::alignment::is_aligned(result.get(),
105 boost::alignment_of<int>::value));
108 boost::shared_ptr<int[2][2]> result =
109 boost::allocate_shared_noinit<int[2][2]>(creator<int>());
110 BOOST_TEST(result.get() != 0);
111 BOOST_TEST(result.use_count() == 1);
112 BOOST_TEST(boost::alignment::is_aligned(result.get(),
113 boost::alignment_of<int>::value));
116 boost::shared_ptr<const int[]> result =
117 boost::allocate_shared_noinit<const
118 int[]>(creator<int>(), 3);
119 BOOST_TEST(result.get() != 0);
120 BOOST_TEST(result.use_count() == 1);
121 BOOST_TEST(boost::alignment::is_aligned(result.get(),
122 boost::alignment_of<int>::value));
125 boost::shared_ptr<const int[3]> result =
126 boost::allocate_shared_noinit<const int[3]>(creator<int>());
127 BOOST_TEST(result.get() != 0);
128 BOOST_TEST(result.use_count() == 1);
129 BOOST_TEST(boost::alignment::is_aligned(result.get(),
130 boost::alignment_of<int>::value));
133 boost::shared_ptr<const int[][2]> result =
134 boost::allocate_shared_noinit<const
135 int[][2]>(creator<int>(), 2);
136 BOOST_TEST(result.get() != 0);
137 BOOST_TEST(result.use_count() == 1);
138 BOOST_TEST(boost::alignment::is_aligned(result.get(),
139 boost::alignment_of<int>::value));
142 boost::shared_ptr<const int[2][2]> result =
143 boost::allocate_shared_noinit<const
144 int[2][2]>(creator<int>());
145 BOOST_TEST(result.get() != 0);
146 BOOST_TEST(result.use_count() == 1);
147 BOOST_TEST(boost::alignment::is_aligned(result.get(),
148 boost::alignment_of<int>::value));
151 boost::shared_ptr<type[]> result =
152 boost::allocate_shared_noinit<type[]>(creator<type>(), 3);
153 BOOST_TEST(result.get() != 0);
154 BOOST_TEST(result.use_count() == 1);
155 BOOST_TEST(boost::alignment::is_aligned(result.get(),
156 boost::alignment_of<type>::value));
157 BOOST_TEST(type::instances == 3);
158 boost::weak_ptr<type[]> other = result;
160 BOOST_TEST(type::instances == 0);
163 boost::shared_ptr<type[3]> result =
164 boost::allocate_shared_noinit<type[3]>(creator<type>());
165 BOOST_TEST(result.get() != 0);
166 BOOST_TEST(result.use_count() == 1);
167 BOOST_TEST(boost::alignment::is_aligned(result.get(),
168 boost::alignment_of<type>::value));
169 BOOST_TEST(type::instances == 3);
170 boost::weak_ptr<type[3]> other = result;
172 BOOST_TEST(type::instances == 0);
175 boost::shared_ptr<type[][2]> result =
176 boost::allocate_shared_noinit<type[][2]>(creator<type>(), 2);
177 BOOST_TEST(result.get() != 0);
178 BOOST_TEST(result.use_count() == 1);
179 BOOST_TEST(boost::alignment::is_aligned(result.get(),
180 boost::alignment_of<type>::value));
181 BOOST_TEST(type::instances == 4);
182 boost::weak_ptr<type[][2]> other = result;
184 BOOST_TEST(type::instances == 0);
187 boost::shared_ptr<type[2][2]> result =
188 boost::allocate_shared_noinit<type[2][2]>(creator<type>());
189 BOOST_TEST(result.get() != 0);
190 BOOST_TEST(result.use_count() == 1);
191 BOOST_TEST(boost::alignment::is_aligned(result.get(),
192 boost::alignment_of<type>::value));
193 BOOST_TEST(type::instances == 4);
194 boost::weak_ptr<type[2][2]> other = result;
196 BOOST_TEST(type::instances == 0);
199 boost::shared_ptr<const type[]> result =
200 boost::allocate_shared_noinit<const
201 type[]>(creator<type>(), 3);
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 == 3);
207 boost::weak_ptr<const type[]> other = result;
209 BOOST_TEST(type::instances == 0);
212 boost::shared_ptr<const type[3]> result =
213 boost::allocate_shared_noinit<const
214 type[3]>(creator<type>());
215 BOOST_TEST(result.get() != 0);
216 BOOST_TEST(result.use_count() == 1);
217 BOOST_TEST(boost::alignment::is_aligned(result.get(),
218 boost::alignment_of<type>::value));
219 BOOST_TEST(type::instances == 3);
220 boost::weak_ptr<const type[3]> other = result;
222 BOOST_TEST(type::instances == 0);
225 boost::shared_ptr<const type[][2]> result =
226 boost::allocate_shared_noinit<const
227 type[][2]>(creator<type>(), 2);
228 BOOST_TEST(result.get() != 0);
229 BOOST_TEST(result.use_count() == 1);
230 BOOST_TEST(boost::alignment::is_aligned(result.get(),
231 boost::alignment_of<type>::value));
232 BOOST_TEST(type::instances == 4);
233 boost::weak_ptr<const type[][2]> other = result;
235 BOOST_TEST(type::instances == 0);
238 boost::shared_ptr<const type[2][2]> result =
239 boost::allocate_shared_noinit<const
240 type[2][2]>(creator<type>());
241 BOOST_TEST(result.get() != 0);
242 BOOST_TEST(result.use_count() == 1);
243 BOOST_TEST(boost::alignment::is_aligned(result.get(),
244 boost::alignment_of<type>::value));
245 BOOST_TEST(type::instances == 4);
246 boost::weak_ptr<const type[2][2]> other = result;
248 BOOST_TEST(type::instances == 0);
250 return boost::report_errors();