includes.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #include <boost/range/algorithm/set_algorithm.hpp>
  10. #include <boost/test/test_tools.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/assign.hpp>
  13. #include <boost/bind.hpp>
  14. #include <algorithm>
  15. #include <functional>
  16. #include <list>
  17. #include <numeric>
  18. #include <deque>
  19. #include <vector>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. template<class Container1, class Container2>
  25. void test(Container1& cont1, Container2& cont2)
  26. {
  27. Container1 old_cont1(cont1);
  28. Container2 old_cont2(cont2);
  29. bool reference_result
  30. = std::includes(cont1.begin(), cont1.end(),
  31. cont2.begin(), cont2.end());
  32. bool test_result = boost::includes(cont1, cont2);
  33. BOOST_CHECK( reference_result == test_result );
  34. BOOST_CHECK_EQUAL_COLLECTIONS(
  35. old_cont1.begin(), old_cont1.end(),
  36. cont1.begin(), cont1.end()
  37. );
  38. BOOST_CHECK_EQUAL_COLLECTIONS(
  39. old_cont2.begin(), old_cont2.end(),
  40. cont2.begin(), cont2.end()
  41. );
  42. BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), cont2) );
  43. BOOST_CHECK( test_result == boost::includes(cont1, boost::make_iterator_range(cont2)) );
  44. BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2)) );
  45. }
  46. template<class Container, class BinaryPredicate>
  47. void sort_container(Container& cont, BinaryPredicate pred)
  48. {
  49. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  50. std::vector<value_t> temp(cont.begin(), cont.end());
  51. std::sort(temp.begin(), temp.end(), pred);
  52. cont.assign(temp.begin(), temp.end());
  53. }
  54. template<class Container1,
  55. class Container2,
  56. class BinaryPredicate>
  57. void test_pred(Container1 cont1, Container2 cont2,
  58. BinaryPredicate pred)
  59. {
  60. sort_container(cont1, pred);
  61. sort_container(cont2, pred);
  62. Container1 old_cont1(cont1);
  63. Container2 old_cont2(cont2);
  64. bool reference_result
  65. = std::includes(cont1.begin(), cont1.end(),
  66. cont2.begin(), cont2.end(),
  67. pred);
  68. bool test_result = boost::includes(cont1, cont2, pred);
  69. BOOST_CHECK( reference_result == test_result );
  70. BOOST_CHECK_EQUAL_COLLECTIONS(
  71. old_cont1.begin(), old_cont1.end(),
  72. cont1.begin(), cont1.end()
  73. );
  74. BOOST_CHECK_EQUAL_COLLECTIONS(
  75. old_cont2.begin(), old_cont2.end(),
  76. cont2.begin(), cont2.end()
  77. );
  78. BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), cont2, pred) );
  79. BOOST_CHECK( test_result == boost::includes(cont1, boost::make_iterator_range(cont2), pred) );
  80. BOOST_CHECK( test_result == boost::includes(boost::make_iterator_range(cont1), boost::make_iterator_range(cont2), pred) );
  81. }
  82. template<class Container1, class Container2>
  83. void test_includes_impl(
  84. Container1& cont1,
  85. Container2& cont2
  86. )
  87. {
  88. test(cont1, cont2);
  89. test_pred(cont1, cont2, std::less<int>());
  90. test_pred(cont1, cont2, std::greater<int>());
  91. }
  92. template<class Container1, class Container2>
  93. void test_includes_impl()
  94. {
  95. using namespace boost::assign;
  96. Container1 cont1;
  97. Container2 cont2;
  98. test_includes_impl(cont1, cont2);
  99. cont1.clear();
  100. cont2.clear();
  101. cont1 += 1;
  102. test_includes_impl(cont1, cont2);
  103. cont1.clear();
  104. cont2.clear();
  105. cont2 += 1;
  106. test_includes_impl(cont1, cont2);
  107. cont1.clear();
  108. cont2.clear();
  109. cont1 += 1,2,3,4,5,6,7,8,9;
  110. cont2 += 2,3,4;
  111. test_includes_impl(cont1, cont2);
  112. cont1.clear();
  113. cont2.clear();
  114. cont1 += 2,3,4;
  115. cont2 += 1,2,3,4,5,6,7,8,9;
  116. test_includes_impl(cont1, cont2);
  117. }
  118. void test_includes()
  119. {
  120. test_includes_impl< std::vector<int>, std::vector<int> >();
  121. test_includes_impl< std::list<int>, std::list<int> >();
  122. test_includes_impl< std::deque<int>, std::deque<int> >();
  123. test_includes_impl< std::vector<int>, std::list<int> >();
  124. test_includes_impl< std::list<int>, std::vector<int> >();
  125. }
  126. }
  127. }
  128. boost::unit_test::test_suite*
  129. init_unit_test_suite(int argc, char* argv[])
  130. {
  131. boost::unit_test::test_suite* test
  132. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.includes" );
  133. test->add( BOOST_TEST_CASE( &boost::test_includes ) );
  134. return test;
  135. }