test_add.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. template<class T = _self>
  20. struct common : ::boost::mpl::vector<
  21. destructible<T>,
  22. copy_constructible<T>,
  23. typeid_<T>
  24. > {};
  25. BOOST_AUTO_TEST_CASE(test_same)
  26. {
  27. typedef ::boost::mpl::vector<common<>, addable<> > test_concept;
  28. any<test_concept> x(1);
  29. any<test_concept> y(2);
  30. any<test_concept> z(x + y);
  31. int i = any_cast<int>(z);
  32. BOOST_CHECK_EQUAL(i, 3);
  33. }
  34. BOOST_AUTO_TEST_CASE(test_int1)
  35. {
  36. typedef ::boost::mpl::vector<common<>, addable<_self, int> > test_concept;
  37. any<test_concept> x(1);
  38. any<test_concept> z(x + 2);
  39. int i = any_cast<int>(z);
  40. BOOST_CHECK_EQUAL(i, 3);
  41. }
  42. BOOST_AUTO_TEST_CASE(test_int2)
  43. {
  44. typedef ::boost::mpl::vector<common<>, addable<int, _self, _self> > test_concept;
  45. any<test_concept> x(1);
  46. any<test_concept> z(2 + x);
  47. int i = any_cast<int>(z);
  48. BOOST_CHECK_EQUAL(i, 3);
  49. }
  50. BOOST_AUTO_TEST_CASE(test_mixed)
  51. {
  52. typedef ::boost::mpl::vector<common<_a>, common<_b>, addable<_a, _b> > test_concept;
  53. tuple<test_concept, _a, _b> x(1.0, 2);
  54. any<test_concept, _a> z(get<0>(x) + get<1>(x));
  55. double d = any_cast<double>(z);
  56. BOOST_CHECK_EQUAL(d, 3);
  57. }
  58. BOOST_AUTO_TEST_CASE(test_overload)
  59. {
  60. typedef ::boost::mpl::vector<
  61. common<_a>,
  62. common<_b>,
  63. addable<_a>,
  64. addable<_a, int>,
  65. addable<int, _a, _a>,
  66. addable<_b>,
  67. addable<_b, int>,
  68. addable<int, _b, _b>,
  69. addable<_a, _b>
  70. > test_concept;
  71. tuple<test_concept, _a, _b> t(1.0, 2);
  72. any<test_concept, _a> x(get<0>(t));
  73. any<test_concept, _b> y(get<1>(t));
  74. {
  75. any<test_concept, _a> z(x + x);
  76. BOOST_CHECK_EQUAL(any_cast<double>(z), 2.0);
  77. }
  78. {
  79. any<test_concept, _a> z(x + 3);
  80. BOOST_CHECK_EQUAL(any_cast<double>(z), 4.0);
  81. }
  82. {
  83. any<test_concept, _a> z(3 + x);
  84. BOOST_CHECK_EQUAL(any_cast<double>(z), 4.0);
  85. }
  86. {
  87. any<test_concept, _b> z(y + y);
  88. BOOST_CHECK_EQUAL(any_cast<int>(z), 4);
  89. }
  90. {
  91. any<test_concept, _b> z(y + 3);
  92. BOOST_CHECK_EQUAL(any_cast<int>(z), 5);
  93. }
  94. {
  95. any<test_concept, _b> z(3 + y);
  96. BOOST_CHECK_EQUAL(any_cast<int>(z), 5);
  97. }
  98. {
  99. any<test_concept, _a> z(x + y);
  100. BOOST_CHECK_EQUAL(any_cast<double>(z), 3);
  101. }
  102. }