// 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 #include #include #include #include #define BOOST_TEST_MAIN #include using namespace boost::type_erasure; template struct common : ::boost::mpl::vector< copy_constructible, typeid_ > {}; int f1_val; void f1() { ++f1_val; } int f2_val; int f2() { return ++f2_val; } BOOST_AUTO_TEST_CASE(test_void) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any x1(&f1); f1_val = 0; x1(); BOOST_CHECK_EQUAL(f1_val, 1); any x2(&f2); f2_val = 0; x2(); BOOST_CHECK_EQUAL(f2_val, 1); typedef ::boost::mpl::vector< common<>, callable > test_concept_int; any x3(&f2); f2_val = 0; int i = x3(); BOOST_CHECK_EQUAL(i, 1); BOOST_CHECK_EQUAL(f2_val, 1); } BOOST_AUTO_TEST_CASE(test_void_const) { typedef ::boost::mpl::vector< common<>, callable > test_concept; const any x1(&f1); f1_val = 0; x1(); BOOST_CHECK_EQUAL(f1_val, 1); const any x2(&f2); f2_val = 0; x2(); BOOST_CHECK_EQUAL(f2_val, 1); typedef ::boost::mpl::vector< common<>, callable > test_concept_int; const any x3(&f2); f2_val = 0; int i = x3(); BOOST_CHECK_EQUAL(i, 1); BOOST_CHECK_EQUAL(f2_val, 1); } int f3_val; void f3(int i) { f3_val += i; } int f4_val; int f4(int i) { return f4_val += i; } BOOST_AUTO_TEST_CASE(test_int) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any x1(&f3); f3_val = 1; x1(3); BOOST_CHECK_EQUAL(f3_val, 4); any x2(&f4); f4_val = 1; x2(2); BOOST_CHECK_EQUAL(f4_val, 3); typedef ::boost::mpl::vector< common<>, callable > test_concept_int; any x3(&f4); f4_val = 1; int i = x3(4); BOOST_CHECK_EQUAL(i, 5); BOOST_CHECK_EQUAL(f4_val, 5); } BOOST_AUTO_TEST_CASE(test_int_const) { typedef ::boost::mpl::vector< common<>, callable > test_concept; const any x1(&f3); f3_val = 1; x1(3); BOOST_CHECK_EQUAL(f3_val, 4); const any x2(&f4); f4_val = 1; x2(2); BOOST_CHECK_EQUAL(f4_val, 3); typedef ::boost::mpl::vector< common<>, callable > test_concept_int; const any x3(&f4); f4_val = 1; int i = x3(4); BOOST_CHECK_EQUAL(i, 5); BOOST_CHECK_EQUAL(f4_val, 5); } BOOST_AUTO_TEST_CASE(test_any) { typedef ::boost::mpl::vector< common<>, common<_a>, callable > test_concept; tuple t1(&f3, 3); any x1(get<0>(t1)); f3_val = 1; x1(get<1>(t1)); BOOST_CHECK_EQUAL(f3_val, 4); tuple t2(&f4, 2); any x2(get<0>(t2)); f4_val = 1; x2(get<1>(t2)); BOOST_CHECK_EQUAL(f4_val, 3); typedef ::boost::mpl::vector< common<>, common<_a>, callable<_a(_a)> > test_concept_int; tuple t3(&f4, 4); any x3(get<0>(t3)); f4_val = 1; int i = any_cast(x3(get<1>(t3))); BOOST_CHECK_EQUAL(i, 5); BOOST_CHECK_EQUAL(f4_val, 5); } BOOST_AUTO_TEST_CASE(test_any_const) { typedef ::boost::mpl::vector< common<>, common<_a>, callable > test_concept; tuple t1(&f3, 3); const any x1(get<0>(t1)); f3_val = 1; x1(get<1>(t1)); BOOST_CHECK_EQUAL(f3_val, 4); tuple t2(&f4, 2); const any x2(get<0>(t2)); f4_val = 1; x2(get<1>(t2)); BOOST_CHECK_EQUAL(f4_val, 3); typedef ::boost::mpl::vector< common<>, common<_a>, callable<_a(_a), const _self> > test_concept_int; tuple t3(&f4, 4); const any x3(get<0>(t3)); f4_val = 1; int i = any_cast(x3(get<1>(t3))); BOOST_CHECK_EQUAL(i, 5); BOOST_CHECK_EQUAL(f4_val, 5); } int overload1; int overload2; int overload3; struct overloaded_function { int operator()() const { return ++overload1; } int operator()(int i) const { return overload2 += i; } int operator()(short i) const { return overload3 += i; } }; BOOST_AUTO_TEST_CASE(test_result_of) { typedef ::boost::mpl::vector< common<>, common<_a>, callable, callable, callable > test_concept; BOOST_MPL_ASSERT((boost::is_same()>::type, void>)); BOOST_MPL_ASSERT((boost::is_same(int)>::type, int>)); BOOST_MPL_ASSERT((boost::is_same(any)>::type, long>)); } BOOST_AUTO_TEST_CASE(test_result_of_const) { typedef ::boost::mpl::vector< common<>, common<_a>, callable, callable, callable > test_concept; BOOST_MPL_ASSERT((boost::is_same()>::type, void>)); BOOST_MPL_ASSERT((boost::is_same(int)>::type, int>)); BOOST_MPL_ASSERT((boost::is_same(any)>::type, long>)); } BOOST_AUTO_TEST_CASE(test_overload) { typedef ::boost::mpl::vector< common<>, common<_a>, callable, callable, callable > test_concept; tuple t(overloaded_function(), static_cast(3)); any f(get<0>(t)); any a(get<1>(t)); overload1 = 0; f(); BOOST_CHECK_EQUAL(overload1, 1); overload2 = 0; f(2); BOOST_CHECK_EQUAL(overload2, 2); overload3 = 0; f(a); BOOST_CHECK_EQUAL(overload3, 3); BOOST_MPL_ASSERT((boost::is_same()>::type, void>)); BOOST_MPL_ASSERT((boost::is_same(int)>::type, void>)); BOOST_MPL_ASSERT((boost::is_same(any)>::type, void>)); } BOOST_AUTO_TEST_CASE(test_overload_return) { typedef ::boost::mpl::vector< common<>, common<_a>, callable, callable, callable > test_concept; tuple t(overloaded_function(), static_cast(3)); any f(get<0>(t)); any a(get<1>(t)); overload1 = 0; BOOST_CHECK_EQUAL(f(), 1); BOOST_CHECK_EQUAL(overload1, 1); overload2 = 0; BOOST_CHECK_EQUAL(f(2), 2); BOOST_CHECK_EQUAL(overload2, 2); overload3 = 0; BOOST_CHECK_EQUAL(f(a), 3); BOOST_CHECK_EQUAL(overload3, 3); //BOOST_MPL_ASSERT((boost::is_same()>::type, int>)); BOOST_MPL_ASSERT((boost::is_same(int)>::type, int>)); BOOST_MPL_ASSERT((boost::is_same(any)>::type, int>)); } BOOST_AUTO_TEST_CASE(test_overload_const) { typedef ::boost::mpl::vector< common<>, common<_a>, callable, callable, callable > test_concept; tuple t(overloaded_function(), static_cast(3)); any f(get<0>(t)); any a(get<1>(t)); overload1 = 0; f(); BOOST_CHECK_EQUAL(overload1, 1); overload2 = 0; f(2); BOOST_CHECK_EQUAL(overload2, 2); overload3 = 0; f(a); BOOST_CHECK_EQUAL(overload3, 3); BOOST_MPL_ASSERT((boost::is_same()>::type, void>)); BOOST_MPL_ASSERT((boost::is_same(int)>::type, void>)); BOOST_MPL_ASSERT((boost::is_same(any)>::type, void>)); } BOOST_AUTO_TEST_CASE(test_overload_return_const) { typedef ::boost::mpl::vector< common<>, common<_a>, callable, callable, callable > test_concept; tuple t(overloaded_function(), static_cast(3)); any f(get<0>(t)); any a(get<1>(t)); overload1 = 0; BOOST_CHECK_EQUAL(f(), 1); BOOST_CHECK_EQUAL(overload1, 1); overload2 = 0; BOOST_CHECK_EQUAL(f(2), 2); BOOST_CHECK_EQUAL(overload2, 2); overload3 = 0; BOOST_CHECK_EQUAL(f(a), 3); BOOST_CHECK_EQUAL(overload3, 3); //BOOST_MPL_ASSERT((boost::is_same()>::type, int>)); BOOST_MPL_ASSERT((boost::is_same(int)>::type, int>)); BOOST_MPL_ASSERT((boost::is_same(any)>::type, int>)); } struct model_ret_ref { model_ret_ref& operator()() { return *this; } }; BOOST_AUTO_TEST_CASE(test_ref_any_result) { typedef ::boost::mpl::vector< common<>, callable<_self&()> > test_concept; any x1 = model_ret_ref(); any x2(x1()); BOOST_CHECK_EQUAL(any_cast(&x1), any_cast(&x2)); } int f_ret_ref_val; int& f_ret_ref() { return f_ret_ref_val; } BOOST_AUTO_TEST_CASE(test_ref_int_result) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any x1 = f_ret_ref; int& result = x1(); BOOST_CHECK_EQUAL(&result, &f_ret_ref_val); } struct model_ret_cref { const model_ret_cref& operator()() { return *this; } }; BOOST_AUTO_TEST_CASE(test_cref_any_result) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any x1 = model_ret_ref(); any x2(x1()); BOOST_CHECK_EQUAL(any_cast(&x1), any_cast(&x2)); } int f_ret_cref_val; const int& f_ret_cref() { return f_ret_cref_val; } BOOST_AUTO_TEST_CASE(test_cref_int_result) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any x1 = f_ret_cref; const int& result = x1(); BOOST_CHECK_EQUAL(&result, &f_ret_cref_val); } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES int f_rv_value = 0; void f_rv(int&& i) { f_rv_value += i; } BOOST_AUTO_TEST_CASE(test_rvalue_int) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any f(&f_rv); f_rv_value = 1; f(2); BOOST_CHECK_EQUAL(f_rv_value, 3); } BOOST_AUTO_TEST_CASE(test_rvalue_any) { typedef ::boost::mpl::vector< common<>, common<_a>, callable > test_concept; tuple t1(&f_rv, 3); any x1(get<0>(t1)); f_rv_value = 1; x1(std::move(get<1>(t1))); BOOST_CHECK_EQUAL(f_rv_value, 4); } BOOST_AUTO_TEST_CASE(test_const_rvalue_int) { typedef ::boost::mpl::vector< common<>, callable > test_concept; const any f(&f_rv); f_rv_value = 1; f(2); BOOST_CHECK_EQUAL(f_rv_value, 3); } BOOST_AUTO_TEST_CASE(test_const_rvalue_any) { typedef ::boost::mpl::vector< common<>, common<_a>, callable > test_concept; tuple t1(&f_rv, 3); const any x1(get<0>(t1)); f_rv_value = 1; x1(std::move(get<1>(t1))); BOOST_CHECK_EQUAL(f_rv_value, 4); } struct model_ret_rref { model_ret_rref&& operator()() { return std::move(*this); } }; BOOST_AUTO_TEST_CASE(test_rvalue_any_result) { typedef ::boost::mpl::vector< common<>, callable<_self&&()> > test_concept; any x1 = model_ret_rref(); any x2(x1()); BOOST_CHECK_EQUAL(any_cast(&x1), any_cast(&x2)); } int f_ret_rv_val; int&& f_ret_rv() { return std::move(f_ret_rv_val); } BOOST_AUTO_TEST_CASE(test_rvalue_int_result) { typedef ::boost::mpl::vector< common<>, callable > test_concept; any x1 = f_ret_rv; int&& result = x1(); BOOST_CHECK_EQUAL(&result, &f_ret_rv_val); } #endif