equal.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm/equal.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <set>
  18. #include <vector>
  19. namespace boost
  20. {
  21. namespace
  22. {
  23. template< class Container1, class Container2 >
  24. void test_equal_impl()
  25. {
  26. using namespace boost::assign;
  27. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container1>::type container1_t;
  28. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container2>::type container2_t;
  29. container1_t mcont1;
  30. container2_t mcont2;
  31. Container1& cont1 = mcont1;
  32. Container2& cont2 = mcont2;
  33. BOOST_CHECK( boost::equal(cont1, cont2) );
  34. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2) );
  35. BOOST_CHECK( boost::equal(cont1, boost::make_iterator_range(cont2)) );
  36. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) );
  37. BOOST_CHECK( boost::equal(cont1, cont2, std::equal_to<int>()) );
  38. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2, std::equal_to<int>()) );
  39. BOOST_CHECK( boost::equal(cont1, boost::make_iterator_range(cont2), std::equal_to<int>()) );
  40. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::equal_to<int>()) );
  41. BOOST_CHECK( boost::equal(cont1, cont2, std::not_equal_to<int>()) );
  42. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2, std::not_equal_to<int>()) );
  43. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  44. mcont1 += 1;
  45. BOOST_CHECK( !boost::equal(cont1, cont2) );
  46. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2) );
  47. BOOST_CHECK( !boost::equal(cont1, boost::make_iterator_range(cont2)) );
  48. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) );
  49. BOOST_CHECK( !boost::equal(cont1, cont2, std::equal_to<int>()) );
  50. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2, std::equal_to<int>()) );
  51. BOOST_CHECK( !boost::equal(cont1, boost::make_iterator_range(cont2), std::equal_to<int>()) );
  52. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::equal_to<int>()) );
  53. BOOST_CHECK( !boost::equal(cont1, cont2, std::not_equal_to<int>()) );
  54. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2, std::not_equal_to<int>()) );
  55. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  56. mcont1.clear();
  57. mcont2 += 1;
  58. BOOST_CHECK( !boost::equal(cont1, cont2) );
  59. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2) );
  60. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) );
  61. BOOST_CHECK( !boost::equal(cont1, cont2, std::equal_to<int>()) );
  62. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2, std::equal_to<int>()) );
  63. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::equal_to<int>()) );
  64. BOOST_CHECK( !boost::equal(cont1, cont2, std::not_equal_to<int>()) );
  65. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2, std::not_equal_to<int>()) );
  66. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  67. mcont1 += 1;
  68. BOOST_CHECK( boost::equal(cont1, cont2) );
  69. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2) );
  70. BOOST_CHECK( boost::equal(cont1, boost::make_iterator_range(cont2)) );
  71. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) );
  72. BOOST_CHECK( boost::equal(cont1, cont2, std::equal_to<int>()) );
  73. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2, std::equal_to<int>()) );
  74. BOOST_CHECK( boost::equal(cont1, boost::make_iterator_range(cont2), std::equal_to<int>()) );
  75. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::equal_to<int>()) );
  76. BOOST_CHECK( !boost::equal(cont1, cont2, std::not_equal_to<int>()) );
  77. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2, std::not_equal_to<int>()) );
  78. BOOST_CHECK( !boost::equal(cont1, boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  79. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  80. mcont1 += 2,3,4,5,6,7,8,9;
  81. mcont2 += 2,3,4,5,6,7,8,9;
  82. BOOST_CHECK( boost::equal(cont1, cont2) );
  83. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2) );
  84. BOOST_CHECK( boost::equal(cont1, boost::make_iterator_range(cont2)) );
  85. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) );
  86. BOOST_CHECK( boost::equal(cont1, cont2, std::equal_to<int>()) );
  87. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), cont2, std::equal_to<int>()) );
  88. BOOST_CHECK( boost::equal(cont1, boost::make_iterator_range(cont2), std::equal_to<int>()) );
  89. BOOST_CHECK( boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::equal_to<int>()) );
  90. BOOST_CHECK( !boost::equal(cont1, cont2, std::not_equal_to<int>()) );
  91. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), cont2, std::not_equal_to<int>()) );
  92. BOOST_CHECK( !boost::equal(cont1, boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  93. BOOST_CHECK( !boost::equal(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), std::not_equal_to<int>()) );
  94. }
  95. template< class Container1, class Container2 >
  96. void test_driver()
  97. {
  98. typedef Container1 container1_t;
  99. typedef Container2 container2_t;
  100. typedef BOOST_DEDUCED_TYPENAME boost::add_const<Container1>::type const_container1_t;
  101. typedef BOOST_DEDUCED_TYPENAME boost::add_const<Container2>::type const_container2_t;
  102. test_equal_impl< const_container1_t, const_container2_t >();
  103. test_equal_impl< const_container1_t, container2_t >();
  104. test_equal_impl< container1_t, const_container2_t >();
  105. test_equal_impl< container1_t, container2_t >();
  106. }
  107. void test_equal()
  108. {
  109. test_driver< std::list<int>, std::list<int> >();
  110. test_driver< std::vector<int>, std::vector<int> >();
  111. test_driver< std::set<int>, std::set<int> >();
  112. test_driver< std::multiset<int>, std::multiset<int> >();
  113. test_driver< std::list<int>, std::vector<int> >();
  114. test_driver< std::vector<int>, std::list<int> >();
  115. }
  116. }
  117. }
  118. boost::unit_test::test_suite*
  119. init_unit_test_suite(int argc, char* argv[])
  120. {
  121. boost::unit_test::test_suite* test
  122. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.equal" );
  123. test->add( BOOST_TEST_CASE( &boost::test_equal ) );
  124. return test;
  125. }