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/move/detail/type_traits.hpp> |
16 | #include <boost/core/lightweight_test.hpp> |
17 | |
18 | ////////////////////////////////////////////// |
19 | // |
20 | // The initial implementation of these tests |
21 | // was written by Howard Hinnant. |
22 | // |
23 | // These test were later refactored grouping |
24 | // and porting them to Boost.Move. |
25 | // |
26 | // Many thanks to Howard for releasing his C++03 |
27 | // unique_ptr implementation with such detailed |
28 | // test cases. |
29 | // |
30 | ////////////////////////////////////////////// |
31 | |
32 | #include "unique_ptr_test_utils_beg.hpp" |
33 | |
34 | namespace bml = ::boost::movelib; |
35 | namespace bmupmu = ::boost::move_upmu; |
36 | |
37 | //////////////////////////////// |
38 | // unique_ptr_pointer_type |
39 | //////////////////////////////// |
40 | namespace unique_ptr_pointer_type { |
41 | |
42 | struct Deleter |
43 | { |
44 | struct pointer {}; |
45 | }; |
46 | |
47 | // Test unique_ptr::pointer type |
48 | void test() |
49 | { |
50 | //Single unique_ptr |
51 | { |
52 | typedef bml::unique_ptr<int> P; |
53 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, int*>::value)); |
54 | } |
55 | { |
56 | typedef bml::unique_ptr<int, Deleter> P; |
57 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value)); |
58 | } |
59 | //Unbounded array unique_ptr |
60 | { |
61 | typedef bml::unique_ptr<int[]> P; |
62 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, int*>::value)); |
63 | } |
64 | { |
65 | typedef bml::unique_ptr<int[], Deleter> P; |
66 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value)); |
67 | } |
68 | //Bounded array unique_ptr |
69 | { |
70 | typedef bml::unique_ptr<int[5]> P; |
71 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, int*>::value)); |
72 | } |
73 | { |
74 | typedef bml::unique_ptr<int[5], Deleter> P; |
75 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value)); |
76 | } |
77 | //Unbounded array of bounded array unique_ptr |
78 | { |
79 | typedef int int_5_t [5]; |
80 | typedef bml::unique_ptr<int_5_t[]> P; |
81 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, int_5_t*>::value)); |
82 | } |
83 | { |
84 | typedef int int_5_t [5]; |
85 | typedef bml::unique_ptr<int_5_t[], Deleter> P; |
86 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value)); |
87 | } |
88 | } |
89 | |
90 | } //namespace unique_ptr_pointer_type { |
91 | |
92 | //////////////////////////////// |
93 | // unique_ptr_deleter_type |
94 | //////////////////////////////// |
95 | namespace unique_ptr_deleter_type { |
96 | |
97 | struct Deleter |
98 | {}; |
99 | |
100 | // Test unique_ptr::deleter type |
101 | void test() |
102 | { |
103 | //Single unique_ptr |
104 | { |
105 | typedef bml::unique_ptr<int> P; |
106 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, bml::default_delete<int> >::value)); |
107 | } |
108 | { |
109 | typedef bml::unique_ptr<int, Deleter> P; |
110 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, Deleter >::value)); |
111 | } |
112 | //Unbounded array unique_ptr |
113 | { |
114 | typedef bml::unique_ptr<int[]> P; |
115 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, bml::default_delete<int[]> >::value)); |
116 | } |
117 | { |
118 | typedef bml::unique_ptr<int[], Deleter> P; |
119 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, Deleter >::value)); |
120 | } |
121 | //Bounded array unique_ptr |
122 | { |
123 | typedef bml::unique_ptr<int[2]> P; |
124 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, bml::default_delete<int[2]> >::value)); |
125 | } |
126 | { |
127 | typedef bml::unique_ptr<int[2], Deleter> P; |
128 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, Deleter >::value)); |
129 | } |
130 | } |
131 | |
132 | } //namespace unique_ptr_deleter_type { |
133 | |
134 | //////////////////////////////// |
135 | // unique_ptr_element_type |
136 | //////////////////////////////// |
137 | namespace unique_ptr_element_type { |
138 | |
139 | // Test unique_ptr::deleter type |
140 | void test() |
141 | { |
142 | //Single unique_ptr |
143 | { |
144 | typedef bml::unique_ptr<const int> P; |
145 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::element_type, const int>::value)); |
146 | } |
147 | //Unbounded array unique_ptr |
148 | { |
149 | typedef bml::unique_ptr<const int[]> P; |
150 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::element_type, const int>::value)); |
151 | } |
152 | //Bounded array unique_ptr |
153 | { |
154 | typedef bml::unique_ptr<const int[2]> P; |
155 | BOOST_MOVE_STATIC_ASSERT((bmupmu::is_same<P::element_type, const int>::value)); |
156 | } |
157 | } |
158 | |
159 | } //namespace unique_ptr_element_type { |
160 | |
161 | //////////////////////////////// |
162 | // unique_ptr_construct_assign_traits |
163 | //////////////////////////////// |
164 | |
165 | namespace unique_ptr_construct_assign_traits { |
166 | |
167 | void test() |
168 | { |
169 | typedef bml::unique_ptr<int> unique_ptr_t; |
170 | //Even if BOOST_MOVE_TT_CXX11_IS_COPY_CONSTRUCTIBLE is not defined |
171 | //boost::unique_ptr shall work with boost::movelib traits |
172 | BOOST_MOVE_STATIC_ASSERT(!(boost::move_detail::is_copy_constructible<unique_ptr_t>::value)); |
173 | //Even if BOOST_MOVE_TT_CXX11_IS_COPY_ASSIGNABLE is not defined |
174 | //boost::unique_ptr shall work with boost::movelib traits |
175 | BOOST_MOVE_STATIC_ASSERT(!(boost::move_detail::is_copy_assignable<unique_ptr_t>::value)); |
176 | //Important traits for containers like boost::vector |
177 | BOOST_MOVE_STATIC_ASSERT(!(boost::move_detail::is_trivially_copy_constructible<unique_ptr_t>::value)); |
178 | BOOST_MOVE_STATIC_ASSERT(!(boost::move_detail::is_trivially_copy_assignable<unique_ptr_t>::value)); |
179 | } |
180 | |
181 | } //namespace unique_ptr_construct_assign_traits { |
182 | |
183 | //////////////////////////////// |
184 | // main |
185 | //////////////////////////////// |
186 | |
187 | int main() |
188 | { |
189 | //General |
190 | unique_ptr_pointer_type::test(); |
191 | unique_ptr_deleter_type::test(); |
192 | unique_ptr_element_type::test(); |
193 | unique_ptr_construct_assign_traits::test(); |
194 | |
195 | //Test results |
196 | return boost::report_errors(); |
197 | } |
198 | |
199 | #include "unique_ptr_test_utils_end.hpp" |
200 | |