test_binding.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/static_binding.hpp>
  11. #include <boost/type_erasure/binding.hpp>
  12. #include <boost/type_erasure/placeholder.hpp>
  13. #include <boost/type_erasure/builtin.hpp>
  14. #include <boost/mpl/vector.hpp>
  15. #include <boost/mpl/map.hpp>
  16. #define BOOST_TEST_MAIN
  17. #include <boost/test/unit_test.hpp>
  18. using namespace boost::type_erasure;
  19. BOOST_AUTO_TEST_CASE(test_empty_binding)
  20. {
  21. boost::mpl::map<> m1;
  22. binding<boost::mpl::vector<> > b1(m1);
  23. binding<boost::mpl::vector<> > b2(make_binding<boost::mpl::map<> >());
  24. BOOST_CHECK(b1 == b2);
  25. BOOST_CHECK(!(b1 != b2));
  26. boost::mpl::map<boost::mpl::pair<_a, int> > m2;
  27. binding<boost::mpl::vector<> > b3(m2);
  28. BOOST_CHECK(b3 == b1);
  29. BOOST_CHECK(!(b3 != b1));
  30. binding<boost::mpl::vector<> > b4(make_binding<boost::mpl::map<boost::mpl::pair<_a, int> > >());
  31. BOOST_CHECK(b4 == b1);
  32. BOOST_CHECK(!(b4 != b1));
  33. binding<boost::mpl::vector<> > b5(b1, m1);
  34. BOOST_CHECK(b5 == b1);
  35. BOOST_CHECK(!(b5 != b1));
  36. binding<boost::mpl::vector<> > b6(b1, make_binding<boost::mpl::map<> >());
  37. BOOST_CHECK(b6 == b1);
  38. BOOST_CHECK(!(b6 != b1));
  39. boost::mpl::map<boost::mpl::pair<_a, _b> > m3;
  40. binding<boost::mpl::vector<> > b7(b1, m3);
  41. BOOST_CHECK(b7 == b1);
  42. BOOST_CHECK(!(b7 != b1));
  43. binding<boost::mpl::vector<> > b8(b1, make_binding<boost::mpl::map<boost::mpl::pair<_a, _b> > >());
  44. BOOST_CHECK(b8 == b1);
  45. BOOST_CHECK(!(b8 != b1));
  46. }
  47. BOOST_AUTO_TEST_CASE(test_binding_one)
  48. {
  49. boost::mpl::map<boost::mpl::pair<_a, int> > m1;
  50. binding<typeid_<_a> > b1(m1);
  51. BOOST_CHECK(b1.find<typeid_<_a> >()() == typeid(int));
  52. binding<typeid_<_a> > b2(make_binding<boost::mpl::map<boost::mpl::pair<_a, int> > >());
  53. BOOST_CHECK(b2.find<typeid_<_a> >()() == typeid(int));
  54. BOOST_CHECK(b1 == b2);
  55. BOOST_CHECK(!(b1 != b2));
  56. boost::mpl::map<boost::mpl::pair<_a, _a> > m2;
  57. binding<typeid_<_a> > b3(b1, m2);
  58. BOOST_CHECK(b3.find<typeid_<_a> >()() == typeid(int));
  59. BOOST_CHECK(b3 == b1);
  60. BOOST_CHECK(!(b3 != b1));
  61. binding<typeid_<_a> > b4(b1, make_binding<boost::mpl::map<boost::mpl::pair<_a, _a> > >());
  62. BOOST_CHECK(b4.find<typeid_<_a> >()() == typeid(int));
  63. BOOST_CHECK(b4 == b1);
  64. BOOST_CHECK(!(b4 != b1));
  65. boost::mpl::map<boost::mpl::pair<_b, _a> > m3;
  66. binding<typeid_<_b> > b5(b1, m3);
  67. BOOST_CHECK(b5.find<typeid_<_b> >()() == typeid(int));
  68. binding<typeid_<_b> > b6(b1, make_binding<boost::mpl::map<boost::mpl::pair<_b, _a> > >());
  69. BOOST_CHECK(b6.find<typeid_<_b> >()() == typeid(int));
  70. }
  71. BOOST_AUTO_TEST_CASE(test_binding_two)
  72. {
  73. boost::mpl::map<boost::mpl::pair<_a, int>, boost::mpl::pair<_b, char> > m1;
  74. binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b1(m1);
  75. BOOST_CHECK(b1.find<typeid_<_a> >()() == typeid(int));
  76. BOOST_CHECK(b1.find<typeid_<_b> >()() == typeid(char));
  77. binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b2(
  78. make_binding<boost::mpl::map<boost::mpl::pair<_a, int>, boost::mpl::pair<_b, char> > >());
  79. BOOST_CHECK(b2.find<typeid_<_a> >()() == typeid(int));
  80. BOOST_CHECK(b2.find<typeid_<_b> >()() == typeid(char));
  81. BOOST_CHECK(b1 == b2);
  82. BOOST_CHECK(!(b1 != b2));
  83. // select the first
  84. boost::mpl::map<boost::mpl::pair<_a, _a> > m2;
  85. binding<typeid_<_a> > b3(b1, m2);
  86. BOOST_CHECK(b3.find<typeid_<_a> >()() == typeid(int));
  87. binding<typeid_<_a> > b4(b1, make_binding<boost::mpl::map<boost::mpl::pair<_a, _a> > >());
  88. BOOST_CHECK(b4.find<typeid_<_a> >()() == typeid(int));
  89. // select the second
  90. boost::mpl::map<boost::mpl::pair<_b, _b> > m3;
  91. binding<typeid_<_b> > b5(b1, m3);
  92. BOOST_CHECK(b5.find<typeid_<_b> >()() == typeid(char));
  93. binding<typeid_<_b> > b6(b1, make_binding<boost::mpl::map<boost::mpl::pair<_b, _b> > >());
  94. BOOST_CHECK(b6.find<typeid_<_b> >()() == typeid(char));
  95. // rename both
  96. boost::mpl::map<boost::mpl::pair<_c, _a>, boost::mpl::pair<_d, _b> > m4;
  97. binding<boost::mpl::vector<typeid_<_c>, typeid_<_d> > > b7(b1, m4);
  98. BOOST_CHECK(b7.find<typeid_<_c> >()() == typeid(int));
  99. BOOST_CHECK(b7.find<typeid_<_d> >()() == typeid(char));
  100. binding<boost::mpl::vector<typeid_<_c>, typeid_<_d> > > b8(b1,
  101. make_binding<boost::mpl::map<boost::mpl::pair<_c, _a>, boost::mpl::pair<_d, _b> > >());
  102. BOOST_CHECK(b8.find<typeid_<_c> >()() == typeid(int));
  103. BOOST_CHECK(b8.find<typeid_<_d> >()() == typeid(char));
  104. // switch the placeholders
  105. boost::mpl::map<boost::mpl::pair<_a, _b>, boost::mpl::pair<_b, _a> > m5;
  106. binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b9(b1, m5);
  107. BOOST_CHECK(b9.find<typeid_<_b> >()() == typeid(int));
  108. BOOST_CHECK(b9.find<typeid_<_a> >()() == typeid(char));
  109. binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b10(b1,
  110. make_binding<boost::mpl::map<boost::mpl::pair<_a, _b>, boost::mpl::pair<_b, _a> > >());
  111. BOOST_CHECK(b10.find<typeid_<_b> >()() == typeid(int));
  112. BOOST_CHECK(b10.find<typeid_<_a> >()() == typeid(char));
  113. }