Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / smart_ptr / test / allocate_shared_array_noinit_test.cpp
1 /*
2 Copyright 2012-2015 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
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>
13
14 template<class T>
15 struct creator {
16     typedef T value_type;
17
18     template<class U>
19     struct rebind {
20         typedef creator<U> other;
21     };
22
23     creator() { }
24
25     template<class U>
26     creator(const creator<U>&) { }
27
28     T* allocate(std::size_t size) {
29         return static_cast<T*>(::operator new(sizeof(T) * size));
30     }
31
32     void deallocate(T* ptr, std::size_t) {
33         ::operator delete(ptr);
34     }
35 };
36
37 template<class T, class U>
38 inline bool
39 operator==(const creator<T>&, const creator<U>&)
40 {
41     return true;
42 }
43
44 template<class T, class U>
45 inline bool
46 operator!=(const creator<T>&, const creator<U>&)
47 {
48     return false;
49 }
50
51 class type {
52 public:
53     static unsigned instances;
54
55     type()
56         : value_(0.0) {
57         ++instances;
58     }
59
60     ~type() {
61         --instances;
62     }
63
64     void set(long double value) {
65         value_ = value;
66     }
67
68     long double get() const {
69         return value_;
70     }
71
72 private:
73     type(const type&);
74     type& operator=(const type&);
75
76     long double value_;
77 };
78
79 unsigned type::instances = 0;
80
81 int main()
82 {
83     {
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));
90     }
91     {
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));
98     }
99     {
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));
106     }
107     {
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));
114     }
115     {
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));
123     }
124     {
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));
131     }
132     {
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));
140     }
141     {
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));
149     }
150     {
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;
159         result.reset();
160         BOOST_TEST(type::instances == 0);
161     }
162     {
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;
171         result.reset();
172         BOOST_TEST(type::instances == 0);
173     }
174     {
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;
183         result.reset();
184         BOOST_TEST(type::instances == 0);
185     }
186     {
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;
195         result.reset();
196         BOOST_TEST(type::instances == 0);
197     }
198     {
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;
208         result.reset();
209         BOOST_TEST(type::instances == 0);
210     }
211     {
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;
221         result.reset();
222         BOOST_TEST(type::instances == 0);
223     }
224     {
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;
234         result.reset();
235         BOOST_TEST(type::instances == 0);
236     }
237     {
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;
247         result.reset();
248         BOOST_TEST(type::instances == 0);
249     }
250     return boost::report_errors();
251 }