1/*
2Copyright 2012-2015 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed 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
14class type {
15public:
16 static unsigned instances;
17
18 type()
19 : value_(0.0) {
20 ++instances;
21 }
22
23 ~type() {
24 --instances;
25 }
26
27 void set(long double value) {
28 value_ = value;
29 }
30
31 long double get() const {
32 return value_;
33 }
34
35private:
36 type(const type&);
37 type& operator=(const type&);
38
39 long double value_;
40};
41
42unsigned type::instances = 0;
43
44int main()
45{
46 {
47 boost::shared_ptr<int[]> result =
48 boost::make_shared<int[]>(size: 3);
49 BOOST_TEST(result.get() != 0);
50 BOOST_TEST(result.use_count() == 1);
51 BOOST_TEST(boost::alignment::is_aligned(result.get(),
52 boost::alignment_of<int>::value));
53 BOOST_TEST(result[0] == 0);
54 BOOST_TEST(result[1] == 0);
55 BOOST_TEST(result[2] == 0);
56 }
57 {
58 boost::shared_ptr<int[3]> result =
59 boost::make_shared<int[3]>();
60 BOOST_TEST(result.get() != 0);
61 BOOST_TEST(result.use_count() == 1);
62 BOOST_TEST(boost::alignment::is_aligned(result.get(),
63 boost::alignment_of<int>::value));
64 BOOST_TEST(result[0] == 0);
65 BOOST_TEST(result[1] == 0);
66 BOOST_TEST(result[2] == 0);
67 }
68 {
69 boost::shared_ptr<int[][2]> result =
70 boost::make_shared<int[][2]>(size: 2);
71 BOOST_TEST(result.get() != 0);
72 BOOST_TEST(result.use_count() == 1);
73 BOOST_TEST(boost::alignment::is_aligned(result.get(),
74 boost::alignment_of<int>::value));
75 BOOST_TEST(result[0][0] == 0);
76 BOOST_TEST(result[0][1] == 0);
77 BOOST_TEST(result[1][0] == 0);
78 BOOST_TEST(result[1][1] == 0);
79 }
80 {
81 boost::shared_ptr<int[2][2]> result =
82 boost::make_shared<int[2][2]>();
83 BOOST_TEST(result.get() != 0);
84 BOOST_TEST(result.use_count() == 1);
85 BOOST_TEST(boost::alignment::is_aligned(result.get(),
86 boost::alignment_of<int>::value));
87 BOOST_TEST(result[0][0] == 0);
88 BOOST_TEST(result[0][1] == 0);
89 BOOST_TEST(result[1][0] == 0);
90 BOOST_TEST(result[1][1] == 0);
91 }
92 {
93 boost::shared_ptr<const int[]> result =
94 boost::make_shared<const int[]>(size: 3);
95 BOOST_TEST(result.get() != 0);
96 BOOST_TEST(result.use_count() == 1);
97 BOOST_TEST(boost::alignment::is_aligned(result.get(),
98 boost::alignment_of<int>::value));
99 BOOST_TEST(result[0] == 0);
100 BOOST_TEST(result[1] == 0);
101 BOOST_TEST(result[2] == 0);
102 }
103 {
104 boost::shared_ptr<const int[3]> result =
105 boost::make_shared<const int[3]>();
106 BOOST_TEST(result.get() != 0);
107 BOOST_TEST(result.use_count() == 1);
108 BOOST_TEST(boost::alignment::is_aligned(result.get(),
109 boost::alignment_of<int>::value));
110 BOOST_TEST(result[0] == 0);
111 BOOST_TEST(result[1] == 0);
112 BOOST_TEST(result[2] == 0);
113 }
114 {
115 boost::shared_ptr<const int[][2]> result =
116 boost::make_shared<const int[][2]>(size: 2);
117 BOOST_TEST(result.get() != 0);
118 BOOST_TEST(result.use_count() == 1);
119 BOOST_TEST(boost::alignment::is_aligned(result.get(),
120 boost::alignment_of<int>::value));
121 BOOST_TEST(result[0][0] == 0);
122 BOOST_TEST(result[0][1] == 0);
123 BOOST_TEST(result[1][0] == 0);
124 BOOST_TEST(result[1][1] == 0);
125 }
126 {
127 boost::shared_ptr<const int[2][2]> result =
128 boost::make_shared<const int[2][2]>();
129 BOOST_TEST(result.get() != 0);
130 BOOST_TEST(result.use_count() == 1);
131 BOOST_TEST(boost::alignment::is_aligned(result.get(),
132 boost::alignment_of<int>::value));
133 BOOST_TEST(result[0][0] == 0);
134 BOOST_TEST(result[0][1] == 0);
135 BOOST_TEST(result[1][0] == 0);
136 BOOST_TEST(result[1][1] == 0);
137 }
138 {
139 boost::shared_ptr<type[]> result =
140 boost::make_shared<type[]>(size: 3);
141 BOOST_TEST(result.get() != 0);
142 BOOST_TEST(result.use_count() == 1);
143 BOOST_TEST(boost::alignment::is_aligned(result.get(),
144 boost::alignment_of<type>::value));
145 BOOST_TEST(type::instances == 3);
146 boost::weak_ptr<type[]> w1 = result;
147 result.reset();
148 BOOST_TEST(type::instances == 0);
149 }
150 {
151 boost::shared_ptr<type[3]> result =
152 boost::make_shared<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[3]> w1 = result;
159 result.reset();
160 BOOST_TEST(type::instances == 0);
161 }
162 {
163 boost::shared_ptr<type[][2]> result =
164 boost::make_shared<type[][2]>(size: 2);
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 == 4);
170 result.reset();
171 BOOST_TEST(type::instances == 0);
172 }
173 {
174 boost::shared_ptr<type[2][2]> result =
175 boost::make_shared<type[2][2]>();
176 BOOST_TEST(result.get() != 0);
177 BOOST_TEST(result.use_count() == 1);
178 BOOST_TEST(boost::alignment::is_aligned(result.get(),
179 boost::alignment_of<type>::value));
180 BOOST_TEST(type::instances == 4);
181 result.reset();
182 BOOST_TEST(type::instances == 0);
183 }
184 {
185 boost::shared_ptr<const type[]> result =
186 boost::make_shared<const type[]>(size: 3);
187 BOOST_TEST(result.get() != 0);
188 BOOST_TEST(result.use_count() == 1);
189 BOOST_TEST(boost::alignment::is_aligned(result.get(),
190 boost::alignment_of<type>::value));
191 BOOST_TEST(type::instances == 3);
192 result.reset();
193 BOOST_TEST(type::instances == 0);
194 }
195 {
196 boost::shared_ptr<const type[3]> result =
197 boost::make_shared<const type[3]>();
198 BOOST_TEST(result.get() != 0);
199 BOOST_TEST(result.use_count() == 1);
200 BOOST_TEST(boost::alignment::is_aligned(result.get(),
201 boost::alignment_of<type>::value));
202 BOOST_TEST(type::instances == 3);
203 result.reset();
204 BOOST_TEST(type::instances == 0);
205 }
206 {
207 boost::shared_ptr<const type[][2]> result =
208 boost::make_shared<const type[][2]>(size: 2);
209 BOOST_TEST(result.get() != 0);
210 BOOST_TEST(result.use_count() == 1);
211 BOOST_TEST(boost::alignment::is_aligned(result.get(),
212 boost::alignment_of<type>::value));
213 BOOST_TEST(type::instances == 4);
214 result.reset();
215 BOOST_TEST(type::instances == 0);
216 }
217 {
218 boost::shared_ptr<const type[2][2]> result =
219 boost::make_shared<const type[2][2]>();
220 BOOST_TEST(result.get() != 0);
221 BOOST_TEST(result.use_count() == 1);
222 BOOST_TEST(boost::alignment::is_aligned(result.get(),
223 boost::alignment_of<type>::value));
224 BOOST_TEST(type::instances == 4);
225 result.reset();
226 BOOST_TEST(type::instances == 0);
227 }
228 return boost::report_errors();
229}
230

source code of boost/libs/smart_ptr/test/make_shared_array_test.cpp