1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Howard Hinnant 2009
4 // (C) Copyright Ion Gaztanaga 2014-2014.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // See http://www.boost.org/libs/move for documentation.
11 //
12 //////////////////////////////////////////////////////////////////////////////
13 #include <boost/move/utility_core.hpp>
14 #include <boost/move/unique_ptr.hpp>
15 #include <boost/static_assert.hpp>
16 #include <boost/move/adl_move_swap.hpp>
17 #include <boost/core/lightweight_test.hpp>
18
19 //////////////////////////////////////////////
20 //
21 // The initial implementation of these tests
22 // was written by Howard Hinnant.
23 //
24 // These test were later refactored grouping
25 // and porting them to Boost.Move.
26 //
27 // Many thanks to Howard for releasing his C++03
28 // unique_ptr implementation with such detailed
29 // test cases.
30 //
31 //////////////////////////////////////////////
32
33 #include "unique_ptr_test_utils_beg.hpp"
34
35 namespace bml = ::boost::movelib;
36
37 ////////////////////////////////
38 // unique_ptr_modifiers_release
39 ////////////////////////////////
40
41 namespace unique_ptr_modifiers_release{
42
test()43 void test()
44 {
45 //Single unique_ptr
46 {
47 bml::unique_ptr<int> p(new int(3));
48 int* i = p.get();
49 int* j = p.release();
50 BOOST_TEST(p.get() == 0);
51 BOOST_TEST(i == j);
52 p.reset(j);
53 }
54 //Unbounded array unique_ptr
55 {
56 bml::unique_ptr<int[]> p(new int[2]);
57 int* i = p.get();
58 int* j = p.release();
59 BOOST_TEST(p.get() == 0);
60 BOOST_TEST(i == j);
61 p.reset(j);
62 }
63 //Bounded array unique_ptr
64 {
65 bml::unique_ptr<int[2]> p(new int[2]);
66 int* i = p.get();
67 int* j = p.release();
68 BOOST_TEST(p.get() == 0);
69 BOOST_TEST(i == j);
70 p.reset(j);
71 }
72 }
73
74 } //namespace unique_ptr_modifiers_release{
75
76 ////////////////////////////////
77 // unique_ptr_modifiers_reset
78 ////////////////////////////////
79
80 namespace unique_ptr_modifiers_reset{
81
test()82 void test()
83 {
84 //Single unique_ptr
85 {
86 reset_counters();
87 { //reset()
88 bml::unique_ptr<A> p(new A);
89 BOOST_TEST(A::count == 1);
90 A* i = p.get();
91 (void)i;
92 p.reset();
93 BOOST_TEST(A::count == 0);
94 BOOST_TEST(p.get() == 0);
95 }
96 BOOST_TEST(A::count == 0);
97 { //reset(p)
98 bml::unique_ptr<A> p(new A);
99 BOOST_TEST(A::count == 1);
100 A* i = p.get();
101 (void)i;
102 p.reset(new A);
103 BOOST_TEST(A::count == 1);
104 }
105 BOOST_TEST(A::count == 0);
106 { //reset(0)
107 bml::unique_ptr<A> p(new A);
108 BOOST_TEST(A::count == 1);
109 A* i = p.get();
110 (void)i;
111 p.reset(0);
112 BOOST_TEST(A::count == 0);
113 BOOST_TEST(p.get() == 0);
114 }
115 BOOST_TEST(A::count == 0);
116 }
117 //Unbounded array unique_ptr
118 {
119 reset_counters();
120 { //reset()
121 bml::unique_ptr<A[]> p(new A[2]);
122 BOOST_TEST(A::count == 2);
123 A* i = p.get();
124 (void)i;
125 p.reset();
126 BOOST_TEST(A::count == 0);
127 BOOST_TEST(p.get() == 0);
128 }
129 BOOST_TEST(A::count == 0);
130 { //reset(p)
131 bml::unique_ptr<A[]> p(new A[2]);
132 BOOST_TEST(A::count == 2);
133 A* i = p.get();
134 (void)i;
135 p.reset(new A[3]);
136 BOOST_TEST(A::count == 3);
137 }
138 BOOST_TEST(A::count == 0);
139 { //reset(0)
140 bml::unique_ptr<A[]> p(new A[2]);
141 BOOST_TEST(A::count == 2);
142 A* i = p.get();
143 (void)i;
144 p.reset(0);
145 BOOST_TEST(A::count == 0);
146 BOOST_TEST(p.get() == 0);
147 }
148 BOOST_TEST(A::count == 0);
149 }
150 {
151 //Bounded array unique_ptr
152 reset_counters();
153 { //reset()
154 bml::unique_ptr<A[2]> p(new A[2]);
155 BOOST_TEST(A::count == 2);
156 A* i = p.get();
157 (void)i;
158 p.reset();
159 BOOST_TEST(A::count == 0);
160 BOOST_TEST(p.get() == 0);
161 }
162 BOOST_TEST(A::count == 0);
163 { //reset(p)
164 bml::unique_ptr<A[2]> p(new A[2]);
165 BOOST_TEST(A::count == 2);
166 A* i = p.get();
167 (void)i;
168 p.reset(new A[3]);
169 BOOST_TEST(A::count == 3);
170 }
171 BOOST_TEST(A::count == 0);
172 { //reset(0)
173 bml::unique_ptr<A[2]> p(new A[2]);
174 BOOST_TEST(A::count == 2);
175 A* i = p.get();
176 (void)i;
177 p.reset(0);
178 BOOST_TEST(A::count == 0);
179 BOOST_TEST(p.get() == 0);
180 }
181 BOOST_TEST(A::count == 0);
182 }
183 }
184
185 } //namespace unique_ptr_modifiers_reset{
186
187 ////////////////////////////////
188 // unique_ptr_modifiers_reset_convert
189 ////////////////////////////////
190
191 namespace unique_ptr_modifiers_reset_convert{
192
test()193 void test()
194 {
195 //Single unique_ptr
196 reset_counters();
197 {
198 bml::unique_ptr<A> p(new A);
199 BOOST_TEST(A::count == 1);
200 BOOST_TEST(B::count == 0);
201 A* i = p.get();
202 (void)i;
203 p.reset(new B);
204 BOOST_TEST(A::count == 1);
205 BOOST_TEST(B::count == 1);
206 }
207 BOOST_TEST(A::count == 0);
208 BOOST_TEST(B::count == 0);
209 {
210 bml::unique_ptr<A> p(new B);
211 BOOST_TEST(A::count == 1);
212 BOOST_TEST(B::count == 1);
213 A* i = p.get();
214 (void)i;
215 p.reset(new B);
216 BOOST_TEST(A::count == 1);
217 BOOST_TEST(B::count == 1);
218 }
219 BOOST_TEST(A::count == 0);
220 BOOST_TEST(B::count == 0);
221 //Unbounded array unique_ptr
222 reset_counters();
223 {
224 bml::unique_ptr<const volatile A[2]> p(new const A[2]);
225 BOOST_TEST(A::count == 2);
226 const volatile A* i = p.get();
227 (void)i;
228 p.reset(new volatile A[3]);
229 BOOST_TEST(A::count == 3);
230 }
231 BOOST_TEST(A::count == 0);
232 {
233 bml::unique_ptr<const A[2]> p(new A[2]);
234 BOOST_TEST(A::count == 2);
235 const A* i = p.get();
236 (void)i;
237 p.reset(new const A[3]);
238 BOOST_TEST(A::count == 3);
239 }
240 BOOST_TEST(A::count == 0);
241 //Bounded array unique_ptr
242 reset_counters();
243 {
244 bml::unique_ptr<const volatile A[2]> p(new const A[2]);
245 BOOST_TEST(A::count == 2);
246 const volatile A* i = p.get();
247 (void)i;
248 p.reset(new volatile A[3]);
249 BOOST_TEST(A::count == 3);
250 }
251 BOOST_TEST(A::count == 0);
252 {
253 bml::unique_ptr<const A[2]> p(new A[2]);
254 BOOST_TEST(A::count == 2);
255 const A* i = p.get();
256 (void)i;
257 p.reset(new const A[3]);
258 BOOST_TEST(A::count == 3);
259 }
260 BOOST_TEST(A::count == 0);
261 }
262
263 } //unique_ptr_modifiers_reset_convert
264
265
266 ////////////////////////////////
267 // unique_ptr_modifiers
268 ////////////////////////////////
269
270 namespace unique_ptr_modifiers_swap{
271
272 // test swap
273
test()274 void test()
275 {
276 //Single unique_ptr
277 reset_counters();
278 {
279 A* p1 = new A(1);
280 move_constr_deleter<A> d1(1);
281 bml::unique_ptr<A, move_constr_deleter<A> > s1(p1, ::boost::move(d1));
282 A* p2 = new A(2);
283 move_constr_deleter<A> d2(2);
284 bml::unique_ptr<A, move_constr_deleter<A> > s2(p2, ::boost::move(d2));
285 BOOST_TEST(s1.get() == p1);
286 BOOST_TEST(*s1 == A(1));
287 BOOST_TEST(s1.get_deleter().state() == 1);
288 BOOST_TEST(s2.get() == p2);
289 BOOST_TEST(*s2 == A(2));
290 BOOST_TEST(s2.get_deleter().state() == 2);
291 boost::adl_move_swap(s1, s2);
292 BOOST_TEST(s1.get() == p2);
293 BOOST_TEST(*s1 == A(2));
294 BOOST_TEST(s1.get_deleter().state() == 2);
295 BOOST_TEST(s2.get() == p1);
296 BOOST_TEST(*s2 == A(1));
297 BOOST_TEST(s2.get_deleter().state() == 1);
298 }
299 //Unbounded array unique_ptr
300 reset_counters();
301 {
302 A* p1 = new A[2];
303 p1[0].set(1);
304 p1[1].set(2);
305 move_constr_deleter<A[]> d1(1);
306 bml::unique_ptr<A[], move_constr_deleter<A[]> > s1(p1, ::boost::move(d1));
307 A* p2 = new A[2];
308 p2[0].set(3);
309 p2[1].set(4);
310 move_constr_deleter<A[]> d2(2);
311 bml::unique_ptr<A[], move_constr_deleter<A[]> > s2(p2, ::boost::move(d2));
312 BOOST_TEST(s1.get() == p1);
313 BOOST_TEST(s1[0] == A(1));
314 BOOST_TEST(s1[1] == A(2));
315 BOOST_TEST(s1.get_deleter().state() == 1);
316 BOOST_TEST(s2.get() == p2);
317 BOOST_TEST(s2[0] == A(3));
318 BOOST_TEST(s2[1] == A(4));
319 BOOST_TEST(s2.get_deleter().state() == 2);
320 swap(s1, s2);
321 BOOST_TEST(s1.get() == p2);
322 BOOST_TEST(s1[0] == A(3));
323 BOOST_TEST(s1[1] == A(4));
324 BOOST_TEST(s1.get_deleter().state() == 2);
325 BOOST_TEST(s2.get() == p1);
326 BOOST_TEST(s2[0] == A(1));
327 BOOST_TEST(s2[1] == A(2));
328 BOOST_TEST(s2.get_deleter().state() == 1);
329 }
330 //Bounded array unique_ptr
331 reset_counters();
332 {
333 A* p1 = new A[2];
334 p1[0].set(1);
335 p1[1].set(2);
336 move_constr_deleter<A[2]> d1(1);
337 bml::unique_ptr<A[2], move_constr_deleter<A[2]> > s1(p1, ::boost::move(d1));
338 A* p2 = new A[2];
339 p2[0].set(3);
340 p2[1].set(4);
341 move_constr_deleter<A[2]> d2(2);
342 bml::unique_ptr<A[2], move_constr_deleter<A[2]> > s2(p2, ::boost::move(d2));
343 BOOST_TEST(s1.get() == p1);
344 BOOST_TEST(s1[0] == A(1));
345 BOOST_TEST(s1[1] == A(2));
346 BOOST_TEST(s1.get_deleter().state() == 1);
347 BOOST_TEST(s2.get() == p2);
348 BOOST_TEST(s2[0] == A(3));
349 BOOST_TEST(s2[1] == A(4));
350 BOOST_TEST(s2.get_deleter().state() == 2);
351 swap(s1, s2);
352 BOOST_TEST(s1.get() == p2);
353 BOOST_TEST(s1[0] == A(3));
354 BOOST_TEST(s1[1] == A(4));
355 BOOST_TEST(s1.get_deleter().state() == 2);
356 BOOST_TEST(s2.get() == p1);
357 BOOST_TEST(s2[0] == A(1));
358 BOOST_TEST(s2[1] == A(2));
359 BOOST_TEST(s2.get_deleter().state() == 1);
360 }
361 }
362
363 } //namespace unique_ptr_modifiers_swap{
364
365 ////////////////////////////////
366 // main
367 ////////////////////////////////
main()368 int main()
369 {
370 //Modifiers
371 unique_ptr_modifiers_release::test();
372 unique_ptr_modifiers_reset::test();
373 unique_ptr_modifiers_reset_convert::test();
374 unique_ptr_modifiers_swap::test();
375
376 //Test results
377 return boost::report_errors();
378
379 }
380
381 #include "unique_ptr_test_utils_end.hpp"
382