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 | class type { |
15 | public: |
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 | |
35 | private: |
36 | type(const type&); |
37 | type& operator=(const type&); |
38 | |
39 | long double value_; |
40 | }; |
41 | |
42 | unsigned type::instances = 0; |
43 | |
44 | int main() |
45 | { |
46 | { |
47 | boost::shared_ptr<int[]> result = |
48 | boost::make_shared_noinit<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 | } |
54 | { |
55 | boost::shared_ptr<int[3]> result = |
56 | boost::make_shared_noinit<int[3]>(); |
57 | BOOST_TEST(result.get() != 0); |
58 | BOOST_TEST(result.use_count() == 1); |
59 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
60 | boost::alignment_of<int>::value)); |
61 | } |
62 | { |
63 | boost::shared_ptr<int[][2]> result = |
64 | boost::make_shared_noinit<int[][2]>(size: 2); |
65 | BOOST_TEST(result.get() != 0); |
66 | BOOST_TEST(result.use_count() == 1); |
67 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
68 | boost::alignment_of<int>::value)); |
69 | } |
70 | { |
71 | boost::shared_ptr<int[2][2]> result = |
72 | boost::make_shared_noinit<int[2][2]>(); |
73 | BOOST_TEST(result.get() != 0); |
74 | BOOST_TEST(result.use_count() == 1); |
75 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
76 | boost::alignment_of<int>::value)); |
77 | } |
78 | { |
79 | boost::shared_ptr<const int[]> result = |
80 | boost::make_shared_noinit<const int[]>(size: 3); |
81 | BOOST_TEST(result.get() != 0); |
82 | BOOST_TEST(result.use_count() == 1); |
83 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
84 | boost::alignment_of<int>::value)); |
85 | } |
86 | { |
87 | boost::shared_ptr<const int[3]> result = |
88 | boost::make_shared_noinit<const int[3]>(); |
89 | BOOST_TEST(result.get() != 0); |
90 | BOOST_TEST(result.use_count() == 1); |
91 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
92 | boost::alignment_of<int>::value)); |
93 | } |
94 | { |
95 | boost::shared_ptr<const int[][2]> result = |
96 | boost::make_shared_noinit<const int[][2]>(size: 2); |
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 | } |
102 | { |
103 | boost::shared_ptr<const int[2][2]> result = |
104 | boost::make_shared_noinit<const int[2][2]>(); |
105 | BOOST_TEST(result.get() != 0); |
106 | BOOST_TEST(result.use_count() == 1); |
107 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
108 | boost::alignment_of<int>::value)); |
109 | } |
110 | { |
111 | boost::shared_ptr<type[]> result = |
112 | boost::make_shared_noinit<type[]>(size: 3); |
113 | BOOST_TEST(result.get() != 0); |
114 | BOOST_TEST(result.use_count() == 1); |
115 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
116 | boost::alignment_of<type>::value)); |
117 | BOOST_TEST(type::instances == 3); |
118 | boost::weak_ptr<type[]> other = result; |
119 | result.reset(); |
120 | BOOST_TEST(type::instances == 0); |
121 | } |
122 | { |
123 | boost::shared_ptr<type[3]> result = |
124 | boost::make_shared_noinit<type[3]>(); |
125 | BOOST_TEST(result.get() != 0); |
126 | BOOST_TEST(result.use_count() == 1); |
127 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
128 | boost::alignment_of<type>::value)); |
129 | BOOST_TEST(type::instances == 3); |
130 | boost::weak_ptr<type[3]> other = result; |
131 | result.reset(); |
132 | BOOST_TEST(type::instances == 0); |
133 | } |
134 | { |
135 | boost::shared_ptr<type[][2]> result = |
136 | boost::make_shared_noinit<type[][2]>(size: 2); |
137 | BOOST_TEST(result.get() != 0); |
138 | BOOST_TEST(result.use_count() == 1); |
139 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
140 | boost::alignment_of<type>::value)); |
141 | BOOST_TEST(type::instances == 4); |
142 | boost::weak_ptr<type[][2]> other = result; |
143 | result.reset(); |
144 | BOOST_TEST(type::instances == 0); |
145 | } |
146 | { |
147 | boost::shared_ptr<type[2][2]> result = |
148 | boost::make_shared_noinit<type[2][2]>(); |
149 | BOOST_TEST(result.get() != 0); |
150 | BOOST_TEST(result.use_count() == 1); |
151 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
152 | boost::alignment_of<type>::value)); |
153 | BOOST_TEST(type::instances == 4); |
154 | boost::weak_ptr<type[2][2]> other = result; |
155 | result.reset(); |
156 | BOOST_TEST(type::instances == 0); |
157 | } |
158 | { |
159 | boost::shared_ptr<const type[]> result = |
160 | boost::make_shared_noinit<const type[]>(size: 3); |
161 | BOOST_TEST(result.get() != 0); |
162 | BOOST_TEST(result.use_count() == 1); |
163 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
164 | boost::alignment_of<type>::value)); |
165 | BOOST_TEST(type::instances == 3); |
166 | boost::weak_ptr<const type[]> other = result; |
167 | result.reset(); |
168 | BOOST_TEST(type::instances == 0); |
169 | } |
170 | { |
171 | boost::shared_ptr<const type[3]> result = |
172 | boost::make_shared_noinit<const type[3]>(); |
173 | BOOST_TEST(result.get() != 0); |
174 | BOOST_TEST(result.use_count() == 1); |
175 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
176 | boost::alignment_of<type>::value)); |
177 | BOOST_TEST(type::instances == 3); |
178 | boost::weak_ptr<const type[3]> other = result; |
179 | result.reset(); |
180 | BOOST_TEST(type::instances == 0); |
181 | } |
182 | { |
183 | boost::shared_ptr<const type[][2]> result = |
184 | boost::make_shared_noinit<const type[][2]>(size: 2); |
185 | BOOST_TEST(result.get() != 0); |
186 | BOOST_TEST(result.use_count() == 1); |
187 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
188 | boost::alignment_of<type>::value)); |
189 | BOOST_TEST(type::instances == 4); |
190 | boost::weak_ptr<const type[][2]> other = result; |
191 | result.reset(); |
192 | BOOST_TEST(type::instances == 0); |
193 | } |
194 | { |
195 | boost::shared_ptr<const type[2][2]> result = |
196 | boost::make_shared_noinit<const type[2][2]>(); |
197 | BOOST_TEST(result.get() != 0); |
198 | BOOST_TEST(result.use_count() == 1); |
199 | BOOST_TEST(boost::alignment::is_aligned(result.get(), |
200 | boost::alignment_of<type>::value)); |
201 | BOOST_TEST(type::instances == 4); |
202 | boost::weak_ptr<const type[2][2]> other = result; |
203 | result.reset(); |
204 | BOOST_TEST(type::instances == 0); |
205 | } |
206 | return boost::report_errors(); |
207 | } |
208 | |