123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- // Unit test for boost::any.
- //
- // See http://www.boost.org for most recent version, including documentation.
- //
- // Copyright Antony Polukhin, 2013-2019.
- //
- // Distributed under the Boost
- // Software License, Version 1.0. (See accompanying file
- // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
- #include <cstdlib>
- #include <string>
- #include <utility>
- #include <boost/any.hpp>
- #include "test.hpp"
- #include <boost/move/move.hpp>
- #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
- int main()
- {
- return EXIT_SUCCESS;
- }
- #else
- namespace any_tests
- {
- typedef test<const char *, void (*)()> test_case;
- typedef const test_case * test_case_iterator;
- extern const test_case_iterator begin, end;
- }
- int main()
- {
- using namespace any_tests;
- tester<test_case_iterator> test_suite(begin, end);
- return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE;
- }
- namespace any_tests // test suite
- {
- void test_move_construction();
- void test_move_assignment();
- void test_copy_construction();
- void test_copy_assignment();
-
- void test_move_construction_from_value();
- void test_move_assignment_from_value();
- void test_copy_construction_from_value();
- void test_copy_assignment_from_value();
- void test_construction_from_const_any_rv();
- void test_cast_to_rv();
-
- const test_case test_cases[] =
- {
- { "move construction of any", test_move_construction },
- { "move assignment of any", test_move_assignment },
- { "copy construction of any", test_copy_construction },
- { "copy assignment of any", test_copy_assignment },
- { "move construction from value", test_move_construction_from_value },
- { "move assignment from value", test_move_assignment_from_value },
- { "copy construction from value", test_copy_construction_from_value },
- { "copy assignment from value", test_copy_assignment_from_value },
- { "constructing from const any&&", test_construction_from_const_any_rv },
- { "casting to rvalue reference", test_cast_to_rv }
- };
- const test_case_iterator begin = test_cases;
- const test_case_iterator end =
- test_cases + (sizeof test_cases / sizeof *test_cases);
-
- class move_copy_conting_class {
- public:
- static unsigned int moves_count;
- static unsigned int copy_count;
- move_copy_conting_class(){}
- move_copy_conting_class(move_copy_conting_class&& /*param*/) {
- ++ moves_count;
- }
- move_copy_conting_class& operator=(move_copy_conting_class&& /*param*/) {
- ++ moves_count;
- return *this;
- }
- move_copy_conting_class(const move_copy_conting_class&) {
- ++ copy_count;
- }
- move_copy_conting_class& operator=(const move_copy_conting_class& /*param*/) {
- ++ copy_count;
- return *this;
- }
- };
- unsigned int move_copy_conting_class::moves_count = 0;
- unsigned int move_copy_conting_class::copy_count = 0;
- }
- namespace any_tests // test definitions
- {
- using namespace boost;
- void test_move_construction()
- {
- any value0 = move_copy_conting_class();
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- any value(boost::move(value0));
- check(value0.empty(), "moved away value is empty");
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- check_equal(
- move_copy_conting_class::copy_count, 0u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- }
- void test_move_assignment()
- {
- any value0 = move_copy_conting_class();
- any value = move_copy_conting_class();
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- value = boost::move(value0);
- check(value0.empty(), "moved away is empty");
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- check_equal(
- move_copy_conting_class::copy_count, 0u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- }
- void test_copy_construction()
- {
- any value0 = move_copy_conting_class();
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- any value(value0);
- check_false(value0.empty(), "copied value is not empty");
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- check_equal(
- move_copy_conting_class::copy_count, 1u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- }
- void test_copy_assignment()
- {
- any value0 = move_copy_conting_class();
- any value = move_copy_conting_class();
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- value = value0;
- check_false(value0.empty(), "copied value is not empty");
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- check_equal(
- move_copy_conting_class::copy_count, 1u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- }
- void test_move_construction_from_value()
- {
- move_copy_conting_class value0;
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
- any value(boost::move(value0));
- #else
- any value(value0);
- #endif
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
-
- #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
- check_equal(
- move_copy_conting_class::copy_count, 0u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 1u,
- "checking move counts");
- #endif
- }
- void test_move_assignment_from_value()
- {
- move_copy_conting_class value0;
- any value;
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
- value = boost::move(value0);
- #else
- value = value0;
- #endif
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
- check_equal(
- move_copy_conting_class::copy_count, 0u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 1u,
- "checking move counts");
- #endif
- }
- void test_copy_construction_from_value()
- {
- move_copy_conting_class value0;
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- any value(value0);
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- check_equal(
- move_copy_conting_class::copy_count, 1u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- }
- void test_copy_assignment_from_value()
- {
- move_copy_conting_class value0;
- any value;
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- value = value0;
- check_false(value.empty(), "empty");
- check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
- check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
- check_equal(
- move_copy_conting_class::copy_count, 1u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- }
- const any helper_method() {
- return true;
- }
- const bool helper_method1() {
- return true;
- }
- void test_construction_from_const_any_rv()
- {
- any values[] = {helper_method(), helper_method1() };
- (void)values;
- }
- void test_cast_to_rv()
- {
- move_copy_conting_class value0;
- any value;
- value = value0;
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- move_copy_conting_class value1 = any_cast<move_copy_conting_class&&>(value);
- check_equal(
- move_copy_conting_class::copy_count, 0u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 1u,
- "checking move counts");
- (void)value1;
- /* Following code shall fail to compile
- const any cvalue = value0;
- move_copy_conting_class::copy_count = 0;
- move_copy_conting_class::moves_count = 0;
- move_copy_conting_class value2 = any_cast<move_copy_conting_class&&>(cvalue);
- check_equal(
- move_copy_conting_class::copy_count, 1u,
- "checking copy counts");
- check_equal(
- move_copy_conting_class::moves_count, 0u,
- "checking move counts");
- (void)value2;
- */
- }
-
- }
- #endif
|