test_add_assign.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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<>, add_assignable<> > test_concept;
  28. any<test_concept> x(1);
  29. any<test_concept> y(2);
  30. any<test_concept>& z(x += y);
  31. BOOST_CHECK_EQUAL(any_cast<int>(x), 3);
  32. BOOST_CHECK_EQUAL(&x, &z);
  33. }
  34. BOOST_AUTO_TEST_CASE(test_int1)
  35. {
  36. typedef ::boost::mpl::vector<common<>, add_assignable<_self, int> > test_concept;
  37. any<test_concept> x(1);
  38. any<test_concept>& z(x += 2);
  39. BOOST_CHECK_EQUAL(any_cast<int>(x), 3);
  40. BOOST_CHECK_EQUAL(&x, &z);
  41. }
  42. BOOST_AUTO_TEST_CASE(test_int2)
  43. {
  44. typedef ::boost::mpl::vector<common<>, add_assignable<int, _self> > test_concept;
  45. int x = 1;
  46. any<test_concept> y(2);
  47. int& z(x += y);
  48. BOOST_CHECK_EQUAL(x, 3);
  49. BOOST_CHECK_EQUAL(&x, &z);
  50. }
  51. BOOST_AUTO_TEST_CASE(test_mixed)
  52. {
  53. typedef ::boost::mpl::vector<common<_a>, common<_b>, add_assignable<_a, _b> > test_concept;
  54. tuple<test_concept, _a, _b> t(1.0, 2);
  55. any<test_concept, _a> x(get<0>(t));
  56. any<test_concept, _b> y(get<1>(t));
  57. any<test_concept, _a>& z(x += y);
  58. BOOST_CHECK_EQUAL(any_cast<double>(x), 3.0);
  59. BOOST_CHECK_EQUAL(&x, &z);
  60. }
  61. BOOST_AUTO_TEST_CASE(test_overload)
  62. {
  63. typedef ::boost::mpl::vector<
  64. common<_a>,
  65. common<_b>,
  66. add_assignable<_a>,
  67. add_assignable<_a, int>,
  68. add_assignable<double, _a>,
  69. add_assignable<_b>,
  70. add_assignable<_b, int>,
  71. add_assignable<double, _b>,
  72. add_assignable<_a, _b>
  73. > test_concept;
  74. tuple<test_concept, _a, _b> t(1.0, 2);
  75. {
  76. any<test_concept, _a> x(get<0>(t));
  77. any<test_concept, _a> y(get<0>(t));
  78. any<test_concept, _a>& z(x += y);
  79. BOOST_CHECK_EQUAL(any_cast<double>(x), 2.0);
  80. BOOST_CHECK_EQUAL(&x, &z);
  81. }
  82. {
  83. any<test_concept, _a> x(get<0>(t));
  84. int y = 5;
  85. any<test_concept, _a>& z(x += y);
  86. BOOST_CHECK_EQUAL(any_cast<double>(x), 6.0);
  87. BOOST_CHECK_EQUAL(&x, &z);
  88. }
  89. {
  90. double x = 11;
  91. any<test_concept, _a> y(get<0>(t));
  92. double& z(x += y);
  93. BOOST_CHECK_EQUAL(x, 12);
  94. BOOST_CHECK_EQUAL(&x, &z);
  95. }
  96. {
  97. any<test_concept, _b> x(get<1>(t));
  98. any<test_concept, _b> y(get<1>(t));
  99. any<test_concept, _b>& z(x += y);
  100. BOOST_CHECK_EQUAL(any_cast<int>(x), 4);
  101. BOOST_CHECK_EQUAL(&x, &z);
  102. }
  103. {
  104. any<test_concept, _b> x(get<1>(t));
  105. int y = 5;
  106. any<test_concept, _b>& z(x += y);
  107. BOOST_CHECK_EQUAL(any_cast<int>(x), 7);
  108. BOOST_CHECK_EQUAL(&x, &z);
  109. }
  110. {
  111. double x = 11;
  112. any<test_concept, _b> y(get<1>(t));
  113. double& z(x += y);
  114. BOOST_CHECK_EQUAL(x, 13);
  115. BOOST_CHECK_EQUAL(&x, &z);
  116. }
  117. {
  118. any<test_concept, _a> x(get<0>(t));
  119. any<test_concept, _b> y(get<1>(t));
  120. any<test_concept, _a>& z(x += y);
  121. BOOST_CHECK_EQUAL(any_cast<double>(x), 3.0);
  122. BOOST_CHECK_EQUAL(&x, &z);
  123. }
  124. }