// Boost.TypeErasure library // // Copyright 2011 Steven Watanabe // // 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) // // $Id$ #include #include #include #include #include #include #define BOOST_TEST_MAIN #include using namespace boost::type_erasure; template struct common : ::boost::mpl::vector< copy_constructible, typeid_ > {}; template T as_rvalue(const T& arg) { return arg; } template const T& as_const(const T& arg) { return arg; } BOOST_AUTO_TEST_CASE(test_implicit) { int i = 4; any, const _self&> x = i; BOOST_CHECK_EQUAL(any_cast(&x), &i); } BOOST_AUTO_TEST_CASE(test_from_int_with_binding) { typedef ::boost::mpl::vector > test_concept; int i = 4; any x(i, make_binding > >()); BOOST_CHECK_EQUAL(any_cast(&x), &i); } BOOST_AUTO_TEST_CASE(test_copy) { typedef ::boost::mpl::vector > test_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_convert) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector > dst_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_rebind) { typedef ::boost::mpl::vector > src_concept; typedef ::boost::mpl::vector > dst_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_rebind_and_convert) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector > dst_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_rebind_and_convert_with_binding) { typedef ::boost::mpl::vector, incrementable<> > src_concept; typedef ::boost::mpl::vector > dst_concept; typedef ::boost::mpl::map > map; typedef ::boost::mpl::map > types; binding table(make_binding()); int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x, make_binding()); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z(x, table); BOOST_CHECK_EQUAL(any_cast(&z), &i); any cy(as_const(x), make_binding()); BOOST_CHECK_EQUAL(any_cast(&cy), &i); any cz(as_const(x), table); BOOST_CHECK_EQUAL(any_cast(&cz), &i); } BOOST_AUTO_TEST_CASE(test_copy_from_ref) { typedef ::boost::mpl::vector > test_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_convert_from_ref) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector > dst_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_rebind_from_ref) { typedef ::boost::mpl::vector > src_concept; typedef ::boost::mpl::vector > dst_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_rebind_and_convert_from_ref) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector > dst_concept; int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z = as_rvalue(x); BOOST_CHECK_EQUAL(any_cast(&z), &i); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), &i); } BOOST_AUTO_TEST_CASE(test_rebind_and_convert_with_binding_from_ref) { typedef ::boost::mpl::vector, incrementable<> > src_concept; typedef ::boost::mpl::vector > dst_concept; typedef ::boost::mpl::map > map; typedef ::boost::mpl::map > types; binding table(make_binding()); int i = 4; any x(i); BOOST_CHECK_EQUAL(any_cast(&x), &i); any y(x, make_binding()); BOOST_CHECK_EQUAL(any_cast(&y), &i); any z(x, table); BOOST_CHECK_EQUAL(any_cast(&z), &i); any cy(as_const(x), make_binding()); BOOST_CHECK_EQUAL(any_cast(&cy), &i); any cz(as_const(x), table); BOOST_CHECK_EQUAL(any_cast(&cz), &i); } BOOST_AUTO_TEST_CASE(test_copy_from_value) { typedef ::boost::mpl::vector, typeid_<> > test_concept; any x(4); const int* ip = any_cast(&x); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), ip); BOOST_CHECK_EQUAL(any_cast(any(as_rvalue(x))), 4); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), ip); } BOOST_AUTO_TEST_CASE(test_convert_from_value) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector > dst_concept; any x(4); const int* ip = any_cast(&x); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), ip); BOOST_CHECK_EQUAL(any_cast(any(as_rvalue(x))), 4); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), ip); } BOOST_AUTO_TEST_CASE(test_rebind_from_value) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector, typeid_<_a> > dst_concept; any x(4); const int* ip = any_cast(&x); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), ip); BOOST_CHECK_EQUAL(any_cast(any(as_rvalue(x))), 4); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), ip); } BOOST_AUTO_TEST_CASE(test_rebind_and_convert_from_value) { typedef ::boost::mpl::vector, typeid_<> > src_concept; typedef ::boost::mpl::vector > dst_concept; any x(4); const int* ip = any_cast(&x); any y(x); BOOST_CHECK_EQUAL(any_cast(&y), ip); BOOST_CHECK_EQUAL(any_cast(any(as_rvalue(x))), 4); any w = as_const(x); BOOST_CHECK_EQUAL(any_cast(&w), ip); } BOOST_AUTO_TEST_CASE(test_rebind_and_convert_with_binding_from_value) { typedef ::boost::mpl::vector, incrementable<> > src_concept; typedef ::boost::mpl::vector > dst_concept; typedef ::boost::mpl::map > map; typedef ::boost::mpl::map > types; binding table(make_binding()); any x(4); const int* ip = any_cast(&x); any y(x, make_binding()); BOOST_CHECK_EQUAL(any_cast(&y), ip); any z(x, table); BOOST_CHECK_EQUAL(any_cast(&z), ip); any cy(as_const(x), make_binding()); BOOST_CHECK_EQUAL(any_cast(&cy), ip); any cz(as_const(x), table); BOOST_CHECK_EQUAL(any_cast(&cz), ip); }