test_same_type.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2012 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #include <boost/type_erasure/any.hpp>
  11. #include <boost/type_erasure/builtin.hpp>
  12. #include <boost/type_erasure/operators.hpp>
  13. #include <boost/type_erasure/any_cast.hpp>
  14. #include <boost/type_erasure/deduced.hpp>
  15. #include <boost/type_erasure/same_type.hpp>
  16. #include <boost/mpl/vector.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/type_traits/remove_pointer.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #define BOOST_TEST_MAIN
  21. #include <boost/test/unit_test.hpp>
  22. using namespace boost::type_erasure;
  23. BOOST_AUTO_TEST_CASE(test_deduce_dereference)
  24. {
  25. typedef ::boost::mpl::vector<
  26. copy_constructible<>,
  27. typeid_<_a>,
  28. dereferenceable<deduced<boost::remove_pointer<_self> >&>,
  29. same_type<deduced<boost::remove_pointer<_self> >, _a>
  30. > test_concept;
  31. int i;
  32. any<test_concept> x(&i);
  33. any<test_concept, _a&> y(*x);
  34. BOOST_CHECK_EQUAL(&any_cast<int&>(y), &i);
  35. }
  36. BOOST_MPL_ASSERT((
  37. boost::is_same<
  38. deduced<boost::remove_pointer<_self> >::type,
  39. deduced<boost::remove_pointer<_self> > >));
  40. BOOST_MPL_ASSERT((
  41. boost::is_same<deduced<boost::remove_pointer<int*> >::type, int >));
  42. BOOST_AUTO_TEST_CASE(test_duplicate)
  43. {
  44. typedef ::boost::mpl::vector<
  45. copy_constructible<>,
  46. typeid_<_a>,
  47. dereferenceable<deduced<boost::remove_pointer<_self> >&>,
  48. same_type<deduced<boost::remove_pointer<_self> >, _a>,
  49. same_type<deduced<boost::remove_pointer<_self> >, _a>
  50. > test_concept;
  51. int i;
  52. any<test_concept> x(&i);
  53. any<test_concept, _a&> y(*x);
  54. BOOST_CHECK_EQUAL(&any_cast<int&>(y), &i);
  55. }
  56. BOOST_AUTO_TEST_CASE(test_convert)
  57. {
  58. typedef ::boost::mpl::vector<
  59. copy_constructible<>,
  60. typeid_<_a>,
  61. dereferenceable<deduced<boost::remove_pointer<_self> >&>,
  62. same_type<deduced<boost::remove_pointer<_self> >, _a>
  63. > test_concept_src;
  64. typedef ::boost::mpl::vector<
  65. copy_constructible<_b>,
  66. typeid_<_c>,
  67. dereferenceable<deduced<boost::remove_pointer<_b> >&, _b>,
  68. same_type<deduced<boost::remove_pointer<_b> >, _c>
  69. > test_concept_dest;
  70. int i;
  71. any<test_concept_src> x1(&i);
  72. any<test_concept_src, _a&> y1(*x1);
  73. any<test_concept_dest, _b> x2(x1);
  74. any<test_concept_dest, _c&> y2(*x2);
  75. BOOST_CHECK_EQUAL(&any_cast<int&>(y2), &i);
  76. }