123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- // 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 <boost/type_erasure/any.hpp>
- #include <boost/type_erasure/tuple.hpp>
- #include <boost/type_erasure/builtin.hpp>
- #include <boost/type_erasure/operators.hpp>
- #include <boost/type_erasure/any_cast.hpp>
- #include <boost/mpl/vector.hpp>
- #define BOOST_TEST_MAIN
- #include <boost/test/unit_test.hpp>
- using namespace boost::type_erasure;
- template<class T = _self>
- struct common : ::boost::mpl::vector<
- destructible<T>,
- copy_constructible<T>,
- typeid_<T>
- > {};
- BOOST_AUTO_TEST_CASE(test_same)
- {
- typedef ::boost::mpl::vector<common<>, add_assignable<> > test_concept;
- any<test_concept> x(1);
- any<test_concept> y(2);
- any<test_concept>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<int>(x), 3);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- BOOST_AUTO_TEST_CASE(test_int1)
- {
- typedef ::boost::mpl::vector<common<>, add_assignable<_self, int> > test_concept;
- any<test_concept> x(1);
- any<test_concept>& z(x += 2);
- BOOST_CHECK_EQUAL(any_cast<int>(x), 3);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- BOOST_AUTO_TEST_CASE(test_int2)
- {
- typedef ::boost::mpl::vector<common<>, add_assignable<int, _self> > test_concept;
- int x = 1;
- any<test_concept> y(2);
- int& z(x += y);
- BOOST_CHECK_EQUAL(x, 3);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- BOOST_AUTO_TEST_CASE(test_mixed)
- {
- typedef ::boost::mpl::vector<common<_a>, common<_b>, add_assignable<_a, _b> > test_concept;
- tuple<test_concept, _a, _b> t(1.0, 2);
- any<test_concept, _a> x(get<0>(t));
- any<test_concept, _b> y(get<1>(t));
- any<test_concept, _a>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<double>(x), 3.0);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- BOOST_AUTO_TEST_CASE(test_overload)
- {
- typedef ::boost::mpl::vector<
- common<_a>,
- common<_b>,
- add_assignable<_a>,
- add_assignable<_a, int>,
- add_assignable<double, _a>,
- add_assignable<_b>,
- add_assignable<_b, int>,
- add_assignable<double, _b>,
- add_assignable<_a, _b>
- > test_concept;
- tuple<test_concept, _a, _b> t(1.0, 2);
- {
- any<test_concept, _a> x(get<0>(t));
- any<test_concept, _a> y(get<0>(t));
- any<test_concept, _a>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<double>(x), 2.0);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- {
- any<test_concept, _a> x(get<0>(t));
- int y = 5;
- any<test_concept, _a>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<double>(x), 6.0);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- {
- double x = 11;
- any<test_concept, _a> y(get<0>(t));
- double& z(x += y);
- BOOST_CHECK_EQUAL(x, 12);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- {
- any<test_concept, _b> x(get<1>(t));
- any<test_concept, _b> y(get<1>(t));
- any<test_concept, _b>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<int>(x), 4);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- {
- any<test_concept, _b> x(get<1>(t));
- int y = 5;
- any<test_concept, _b>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<int>(x), 7);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- {
- double x = 11;
- any<test_concept, _b> y(get<1>(t));
- double& z(x += y);
- BOOST_CHECK_EQUAL(x, 13);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- {
- any<test_concept, _a> x(get<0>(t));
- any<test_concept, _b> y(get<1>(t));
- any<test_concept, _a>& z(x += y);
- BOOST_CHECK_EQUAL(any_cast<double>(x), 3.0);
- BOOST_CHECK_EQUAL(&x, &z);
- }
- }
|