set_intersection.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 Iterator, class Container2>
  25. void check_result(
  26. Container1& reference,
  27. Iterator reference_result,
  28. Container2& test_cont,
  29. Iterator test_result
  30. )
  31. {
  32. BOOST_CHECK_EQUAL(
  33. std::distance<Iterator>(reference.begin(), reference_result),
  34. std::distance<Iterator>(test_cont.begin(), test_result)
  35. );
  36. BOOST_CHECK_EQUAL_COLLECTIONS(
  37. reference.begin(), reference.end(),
  38. test_cont.begin(), test_cont.end()
  39. );
  40. }
  41. template<class Container1, class Container2>
  42. void test(Container1& cont1, Container2& cont2)
  43. {
  44. typedef BOOST_DEDUCED_TYPENAME Container1::value_type value_t;
  45. typedef BOOST_DEDUCED_TYPENAME std::vector<value_t>::iterator iterator_t;
  46. std::vector<value_t> reference(cont1.size() + cont2.size());
  47. std::vector<value_t> test_cont(reference);
  48. iterator_t reference_result
  49. = std::set_intersection(cont1.begin(), cont1.end(),
  50. cont2.begin(), cont2.end(),
  51. reference.begin());
  52. iterator_t test_result
  53. = boost::set_intersection(cont1, cont2, test_cont.begin());
  54. check_result(reference, reference_result,
  55. test_cont, test_result);
  56. test_result = boost::set_intersection(
  57. boost::make_iterator_range(cont1), cont2,
  58. test_cont.begin());
  59. check_result(reference, reference_result,
  60. test_cont, test_result);
  61. test_result = boost::set_intersection(
  62. cont1, boost::make_iterator_range(cont2),
  63. test_cont.begin());
  64. check_result(reference, reference_result,
  65. test_cont, test_result);
  66. test_result = boost::set_intersection(
  67. boost::make_iterator_range(cont1),
  68. boost::make_iterator_range(cont2),
  69. test_cont.begin());
  70. check_result(reference, reference_result,
  71. test_cont, test_result);
  72. }
  73. template<class Container, class BinaryPredicate>
  74. void sort_container(Container& cont, BinaryPredicate pred)
  75. {
  76. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  77. std::vector<value_t> temp(cont.begin(), cont.end());
  78. std::sort(temp.begin(), temp.end(), pred);
  79. cont.assign(temp.begin(), temp.end());
  80. }
  81. template<class Container1,
  82. class Container2,
  83. class BinaryPredicate>
  84. void test_pred(Container1 cont1, Container2 cont2,
  85. BinaryPredicate pred)
  86. {
  87. typedef BOOST_DEDUCED_TYPENAME Container1::value_type value_t;
  88. typedef BOOST_DEDUCED_TYPENAME std::vector<value_t>::iterator iterator_t;
  89. sort_container(cont1, pred);
  90. sort_container(cont2, pred);
  91. std::vector<value_t> reference(cont1.size() + cont2.size());
  92. std::vector<value_t> test_cont(reference);
  93. iterator_t reference_result
  94. = std::set_intersection(cont1.begin(), cont1.end(),
  95. cont2.begin(), cont2.end(),
  96. reference.begin(),
  97. pred);
  98. iterator_t test_result
  99. = boost::set_intersection(cont1, cont2, test_cont.begin(), pred);
  100. check_result(reference, reference_result,
  101. test_cont, test_result);
  102. test_result = boost::set_intersection(
  103. boost::make_iterator_range(cont1), cont2,
  104. test_cont.begin(), pred);
  105. check_result(reference, reference_result,
  106. test_cont, test_result);
  107. test_result = boost::set_intersection(
  108. cont1, boost::make_iterator_range(cont2),
  109. test_cont.begin(), pred);
  110. check_result(reference, reference_result,
  111. test_cont, test_result);
  112. test_result = boost::set_intersection(
  113. boost::make_iterator_range(cont1),
  114. boost::make_iterator_range(cont2),
  115. test_cont.begin(), pred);
  116. check_result(reference, reference_result,
  117. test_cont, test_result);
  118. }
  119. template<class Container1, class Container2>
  120. void test_set_intersection_impl(
  121. Container1& cont1,
  122. Container2& cont2
  123. )
  124. {
  125. test(cont1, cont2);
  126. test_pred(cont1, cont2, std::less<int>());
  127. test_pred(cont1, cont2, std::greater<int>());
  128. }
  129. template<class Container1, class Container2>
  130. void test_set_intersection_impl()
  131. {
  132. using namespace boost::assign;
  133. Container1 cont1;
  134. Container2 cont2;
  135. test_set_intersection_impl(cont1, cont2);
  136. cont1.clear();
  137. cont2.clear();
  138. cont1 += 1;
  139. test_set_intersection_impl(cont1, cont2);
  140. cont1.clear();
  141. cont2.clear();
  142. cont2 += 1;
  143. test_set_intersection_impl(cont1, cont2);
  144. cont1.clear();
  145. cont2.clear();
  146. cont1 += 1,2,3,4,5,6,7,8,9;
  147. cont2 += 2,3,4;
  148. test_set_intersection_impl(cont1, cont2);
  149. cont1.clear();
  150. cont2.clear();
  151. cont1 += 2,3,4;
  152. cont2 += 1,2,3,4,5,6,7,8,9;
  153. test_set_intersection_impl(cont1, cont2);
  154. }
  155. void test_set_intersection()
  156. {
  157. test_set_intersection_impl< std::vector<int>, std::vector<int> >();
  158. test_set_intersection_impl< std::list<int>, std::list<int> >();
  159. test_set_intersection_impl< std::deque<int>, std::deque<int> >();
  160. test_set_intersection_impl< std::vector<int>, std::list<int> >();
  161. test_set_intersection_impl< std::list<int>, std::vector<int> >();
  162. }
  163. }
  164. }
  165. boost::unit_test::test_suite*
  166. init_unit_test_suite(int argc, char* argv[])
  167. {
  168. boost::unit_test::test_suite* test
  169. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.set_intersection" );
  170. test->add( BOOST_TEST_CASE( &boost::test_set_intersection ) );
  171. return test;
  172. }