1 // Unit test for boost::any.
3 // See http://www.boost.org for most recent version, including documentation.
5 // Copyright Antony Polukhin, 2013-2014.
7 // Distributed under the Boost
8 // Software License, Version 1.0. (See accompanying file
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
15 #include "boost/any.hpp"
17 #include <boost/move/move.hpp>
19 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
30 typedef test
<const char *, void (*)()> test_case
;
31 typedef const test_case
* test_case_iterator
;
33 extern const test_case_iterator begin
, end
;
38 using namespace any_tests
;
39 tester
<test_case_iterator
> test_suite(begin
, end
);
40 return test_suite() ? EXIT_SUCCESS
: EXIT_FAILURE
;
43 namespace any_tests
// test suite
45 void test_move_construction();
46 void test_move_assignment();
47 void test_copy_construction();
48 void test_copy_assignment();
50 void test_move_construction_from_value();
51 void test_move_assignment_from_value();
52 void test_copy_construction_from_value();
53 void test_copy_assignment_from_value();
54 void test_construction_from_const_any_rv();
55 void test_cast_to_rv();
58 const test_case test_cases
[] =
60 { "move construction of any", test_move_construction
},
61 { "move assignment of any", test_move_assignment
},
62 { "copy construction of any", test_copy_construction
},
63 { "copy assignment of any", test_copy_assignment
},
65 { "move construction from value", test_move_construction_from_value
},
66 { "move assignment from value", test_move_assignment_from_value
},
67 { "copy construction from value", test_copy_construction_from_value
},
68 { "copy assignment from value", test_copy_assignment_from_value
},
69 { "constructing from const any&&", test_construction_from_const_any_rv
},
70 { "casting to rvalue reference", test_cast_to_rv
}
73 const test_case_iterator begin
= test_cases
;
74 const test_case_iterator end
=
75 test_cases
+ (sizeof test_cases
/ sizeof *test_cases
);
78 class move_copy_conting_class
{
80 static unsigned int moves_count
;
81 static unsigned int copy_count
;
83 move_copy_conting_class(){}
84 move_copy_conting_class(move_copy_conting_class
&& /*param*/) {
88 move_copy_conting_class
& operator=(move_copy_conting_class
&& /*param*/) {
93 move_copy_conting_class(const move_copy_conting_class
&) {
96 move_copy_conting_class
& operator=(const move_copy_conting_class
& /*param*/) {
102 unsigned int move_copy_conting_class::moves_count
= 0;
103 unsigned int move_copy_conting_class::copy_count
= 0;
106 namespace any_tests
// test definitions
108 using namespace boost
;
110 void test_move_construction()
112 any value0
= move_copy_conting_class();
113 move_copy_conting_class::copy_count
= 0;
114 move_copy_conting_class::moves_count
= 0;
115 any
value(boost::move(value0
));
117 check(value0
.empty(), "moved away value is empty");
118 check_false(value
.empty(), "empty");
119 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
120 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
122 move_copy_conting_class::copy_count
, 0u,
123 "checking copy counts");
125 move_copy_conting_class::moves_count
, 0u,
126 "checking move counts");
129 void test_move_assignment()
131 any value0
= move_copy_conting_class();
132 any value
= move_copy_conting_class();
133 move_copy_conting_class::copy_count
= 0;
134 move_copy_conting_class::moves_count
= 0;
135 value
= boost::move(value0
);
137 check(value0
.empty(), "moved away is empty");
138 check_false(value
.empty(), "empty");
139 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
140 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
142 move_copy_conting_class::copy_count
, 0u,
143 "checking copy counts");
145 move_copy_conting_class::moves_count
, 0u,
146 "checking move counts");
149 void test_copy_construction()
151 any value0
= move_copy_conting_class();
152 move_copy_conting_class::copy_count
= 0;
153 move_copy_conting_class::moves_count
= 0;
156 check_false(value0
.empty(), "copyed value is not empty");
157 check_false(value
.empty(), "empty");
158 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
159 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
161 move_copy_conting_class::copy_count
, 1u,
162 "checking copy counts");
164 move_copy_conting_class::moves_count
, 0u,
165 "checking move counts");
168 void test_copy_assignment()
170 any value0
= move_copy_conting_class();
171 any value
= move_copy_conting_class();
172 move_copy_conting_class::copy_count
= 0;
173 move_copy_conting_class::moves_count
= 0;
176 check_false(value0
.empty(), "copyied value is not empty");
177 check_false(value
.empty(), "empty");
178 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
179 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
181 move_copy_conting_class::copy_count
, 1u,
182 "checking copy counts");
184 move_copy_conting_class::moves_count
, 0u,
185 "checking move counts");
188 void test_move_construction_from_value()
190 move_copy_conting_class value0
;
191 move_copy_conting_class::copy_count
= 0;
192 move_copy_conting_class::moves_count
= 0;
193 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
194 any
value(boost::move(value0
));
199 check_false(value
.empty(), "empty");
200 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
201 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
203 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
205 move_copy_conting_class::copy_count
, 0u,
206 "checking copy counts");
208 move_copy_conting_class::moves_count
, 1u,
209 "checking move counts");
214 void test_move_assignment_from_value()
216 move_copy_conting_class value0
;
218 move_copy_conting_class::copy_count
= 0;
219 move_copy_conting_class::moves_count
= 0;
220 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
221 value
= boost::move(value0
);
226 check_false(value
.empty(), "empty");
227 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
228 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
230 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
232 move_copy_conting_class::copy_count
, 0u,
233 "checking copy counts");
235 move_copy_conting_class::moves_count
, 1u,
236 "checking move counts");
241 void test_copy_construction_from_value()
243 move_copy_conting_class value0
;
244 move_copy_conting_class::copy_count
= 0;
245 move_copy_conting_class::moves_count
= 0;
248 check_false(value
.empty(), "empty");
249 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
250 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
253 move_copy_conting_class::copy_count
, 1u,
254 "checking copy counts");
256 move_copy_conting_class::moves_count
, 0u,
257 "checking move counts");
260 void test_copy_assignment_from_value()
262 move_copy_conting_class value0
;
264 move_copy_conting_class::copy_count
= 0;
265 move_copy_conting_class::moves_count
= 0;
268 check_false(value
.empty(), "empty");
269 check_equal(value
.type(), typeindex::type_id
<move_copy_conting_class
>(), "type");
270 check_non_null(any_cast
<move_copy_conting_class
>(&value
), "any_cast<move_copy_conting_class>");
273 move_copy_conting_class::copy_count
, 1u,
274 "checking copy counts");
276 move_copy_conting_class::moves_count
, 0u,
277 "checking move counts");
280 const any
helper_method() {
284 const bool helper_method1() {
288 void test_construction_from_const_any_rv()
290 any values
[] = {helper_method(), helper_method1() };
294 void test_cast_to_rv()
296 move_copy_conting_class value0
;
299 move_copy_conting_class::copy_count
= 0;
300 move_copy_conting_class::moves_count
= 0;
302 move_copy_conting_class value1
= any_cast
<move_copy_conting_class
&&>(value
);
305 move_copy_conting_class::copy_count
, 0u,
306 "checking copy counts");
308 move_copy_conting_class::moves_count
, 1u,
309 "checking move counts");
311 /* Following code shall fail to compile
312 const any cvalue = value0;
313 move_copy_conting_class::copy_count = 0;
314 move_copy_conting_class::moves_count = 0;
316 move_copy_conting_class value2 = any_cast<move_copy_conting_class&&>(cvalue);
319 move_copy_conting_class::copy_count, 1u,
320 "checking copy counts");
322 move_copy_conting_class::moves_count, 0u,
323 "checking move counts");