test_equal.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/type_erasure/tuple.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. BOOST_AUTO_TEST_CASE(test_basic)
  20. {
  21. typedef boost::mpl::vector<copy_constructible<>, equality_comparable<> > test_concept;
  22. any<test_concept> x(1);
  23. any<test_concept> y(2);
  24. BOOST_CHECK(!(x == y));
  25. BOOST_CHECK((x == x));
  26. BOOST_CHECK((x != y));
  27. BOOST_CHECK(!(x != x));
  28. }
  29. BOOST_AUTO_TEST_CASE(test_mixed_unequal)
  30. {
  31. typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, equality_comparable<_a, _b> > test_concept;
  32. tuple<test_concept, _a, _b> t(1, 2.0);
  33. any<test_concept, _a> x(get<0>(t));
  34. any<test_concept, _b> y(get<1>(t));
  35. BOOST_CHECK(!(x == y));
  36. BOOST_CHECK((x != y));
  37. }
  38. BOOST_AUTO_TEST_CASE(test_mixed_equal)
  39. {
  40. typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, equality_comparable<_a, _b> > test_concept;
  41. tuple<test_concept, _a, _b> t(1, 1);
  42. any<test_concept, _a> x(get<0>(t));
  43. any<test_concept, _b> y(get<1>(t));
  44. BOOST_CHECK((x == y));
  45. BOOST_CHECK(!(x != y));
  46. }
  47. BOOST_AUTO_TEST_CASE(test_fixed_lhs_unequal)
  48. {
  49. typedef boost::mpl::vector<copy_constructible<>, equality_comparable<int, _self> > test_concept;
  50. int x(1);
  51. any<test_concept> y(2.0);
  52. BOOST_CHECK(!(x == y));
  53. BOOST_CHECK((x != y));
  54. }
  55. BOOST_AUTO_TEST_CASE(test_fixed_lhs_equal)
  56. {
  57. typedef boost::mpl::vector<copy_constructible<>, equality_comparable<int, _self> > test_concept;
  58. int x(1);
  59. any<test_concept> y(1);
  60. BOOST_CHECK((x == y));
  61. BOOST_CHECK(!(x != y));
  62. }
  63. BOOST_AUTO_TEST_CASE(test_fixed_rhs_unequal)
  64. {
  65. typedef boost::mpl::vector<copy_constructible<>, equality_comparable<_self, int> > test_concept;
  66. any<test_concept> x(2.0);
  67. int y(1);
  68. BOOST_CHECK(!(x == y));
  69. BOOST_CHECK((x != y));
  70. }
  71. BOOST_AUTO_TEST_CASE(test_fixed_rhs_equal)
  72. {
  73. typedef boost::mpl::vector<copy_constructible<>, equality_comparable<_self, int> > test_concept;
  74. any<test_concept> x(1);
  75. int y(1);
  76. BOOST_CHECK((x == y));
  77. BOOST_CHECK(!(x != y));
  78. }
  79. BOOST_AUTO_TEST_CASE(test_relaxed)
  80. {
  81. typedef boost::mpl::vector<copy_constructible<>, equality_comparable<>, relaxed> test_concept;
  82. any<test_concept> x(1);
  83. any<test_concept> y(2);
  84. any<test_concept> z(std::string("test"));
  85. BOOST_CHECK(!(x == y));
  86. BOOST_CHECK((x == x));
  87. BOOST_CHECK((x != y));
  88. BOOST_CHECK(!(x != x));
  89. BOOST_CHECK(!(x == z));
  90. BOOST_CHECK((x != z));
  91. }
  92. BOOST_AUTO_TEST_CASE(test_overload)
  93. {
  94. typedef boost::mpl::vector<
  95. copy_constructible<_a>,
  96. copy_constructible<_b>,
  97. equality_comparable<_a>,
  98. equality_comparable<_a, int>,
  99. equality_comparable<int, _a>,
  100. equality_comparable<_b>,
  101. equality_comparable<_b, int>,
  102. equality_comparable<int, _b>,
  103. equality_comparable<_a, _b>
  104. > test_concept;
  105. tuple<test_concept, _a, _b> t(1, 2.0);
  106. any<test_concept, _a> x(get<0>(t));
  107. any<test_concept, _b> y(get<1>(t));
  108. BOOST_CHECK(x == x);
  109. BOOST_CHECK(!(x != x));
  110. BOOST_CHECK(x == 1);
  111. BOOST_CHECK(x != 2);
  112. BOOST_CHECK(1 == x);
  113. BOOST_CHECK(2 != x);
  114. BOOST_CHECK(y == y);
  115. BOOST_CHECK(!(y != y));
  116. BOOST_CHECK(y == 2);
  117. BOOST_CHECK(y != 3);
  118. BOOST_CHECK(2 == y);
  119. BOOST_CHECK(3 != y);
  120. BOOST_CHECK(!(x == y));
  121. BOOST_CHECK(x != y);
  122. }