test_reference.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/tuple.hpp>
  12. #include <boost/type_erasure/builtin.hpp>
  13. #include <boost/type_erasure/operators.hpp>
  14. #include <boost/type_erasure/any_cast.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #define BOOST_TEST_MAIN
  17. #include <boost/test/unit_test.hpp>
  18. using namespace boost::type_erasure;
  19. class no_destroy
  20. {
  21. protected:
  22. ~no_destroy() {}
  23. };
  24. class with_destroy : public no_destroy
  25. {
  26. public:
  27. ~with_destroy() {}
  28. };
  29. template<class T = _self>
  30. struct common : ::boost::mpl::vector<
  31. destructible<T>,
  32. copy_constructible<T>,
  33. typeid_<T>
  34. > {};
  35. BOOST_AUTO_TEST_CASE(test_basic)
  36. {
  37. typedef ::boost::mpl::vector<typeid_<> > test_concept;
  38. with_destroy val;
  39. any<test_concept, _self&> x(static_cast<no_destroy&>(val));
  40. no_destroy& ref = any_cast<no_destroy&>(x);
  41. BOOST_CHECK_EQUAL(&ref, &val);
  42. }
  43. BOOST_AUTO_TEST_CASE(test_increment)
  44. {
  45. typedef ::boost::mpl::vector<incrementable<> > test_concept;
  46. int i = 0;
  47. any<test_concept, _self&> x(i);
  48. ++x;
  49. BOOST_CHECK_EQUAL(i, 1);
  50. }
  51. BOOST_AUTO_TEST_CASE(test_add)
  52. {
  53. typedef ::boost::mpl::vector<common<>, addable<> > test_concept;
  54. int i = 1;
  55. int j = 2;
  56. any<test_concept, _self&> x(i);
  57. any<test_concept, _self&> y(j);
  58. any<test_concept, _self> z(x + y);
  59. int k = any_cast<int>(z);
  60. BOOST_CHECK_EQUAL(k, 3);
  61. }
  62. BOOST_AUTO_TEST_CASE(test_mixed_add)
  63. {
  64. typedef ::boost::mpl::vector<common<>, addable<> > test_concept;
  65. int i = 1;
  66. int j = 2;
  67. any<test_concept, _self&> x(i);
  68. any<test_concept, _self> y(j);
  69. any<test_concept, _self> z(x + y);
  70. int k = any_cast<int>(z);
  71. BOOST_CHECK_EQUAL(k, 3);
  72. }