test_subscript.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 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/mpl/vector.hpp>
  15. #define BOOST_TEST_MAIN
  16. #include <boost/test/unit_test.hpp>
  17. using namespace boost::type_erasure;
  18. template<class T = _self>
  19. struct common : ::boost::mpl::vector<
  20. copy_constructible<T>,
  21. typeid_<T>
  22. > {};
  23. BOOST_AUTO_TEST_CASE(test_basic)
  24. {
  25. typedef ::boost::mpl::vector<common<>, subscriptable<int&> > test_concept;
  26. int i[5];
  27. any<test_concept> x(&i[0]);
  28. BOOST_CHECK_EQUAL(&x[0], &i[0]);
  29. }
  30. BOOST_AUTO_TEST_CASE(test_basic_const)
  31. {
  32. typedef ::boost::mpl::vector<common<>, subscriptable<int&, const _self> > test_concept;
  33. int i[5];
  34. const any<test_concept> x(&i[0]);
  35. BOOST_CHECK_EQUAL(&x[0], &i[0]);
  36. }
  37. BOOST_AUTO_TEST_CASE(test_any_result)
  38. {
  39. typedef ::boost::mpl::vector<common<>, common<_a>, subscriptable<_a&, const _self> > test_concept;
  40. typedef ::boost::mpl::map<
  41. ::boost::mpl::pair<_self, int*>,
  42. ::boost::mpl::pair<_a, int>
  43. > types;
  44. int i[5];
  45. any<test_concept> x(&i[0], make_binding<types>());
  46. any<test_concept, _a&> y(x[0]);
  47. BOOST_CHECK_EQUAL(any_cast<int*>(&y), &i[0]);
  48. }
  49. BOOST_AUTO_TEST_CASE(test_any_result_const)
  50. {
  51. typedef ::boost::mpl::vector<common<>, common<_a>, subscriptable<const _a&, const _self> > test_concept;
  52. typedef ::boost::mpl::map<
  53. ::boost::mpl::pair<_self, const int*>,
  54. ::boost::mpl::pair<_a, int>
  55. > types;
  56. const int i[5] = { 0, 0, 0, 0, 0 };
  57. any<test_concept> x(&i[0], make_binding<types>());
  58. any<test_concept, const _a&> y(x[0]);
  59. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i[0]);
  60. }