equal_range.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/equal_range.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 Container, class Pair>
  25. void check_result(
  26. const Container& reference,
  27. Pair reference_pair,
  28. const Container& test,
  29. Pair test_pair
  30. )
  31. {
  32. typedef BOOST_DEDUCED_TYPENAME range_iterator<const Container>::type
  33. const_iterator_t;
  34. BOOST_CHECK_EQUAL_COLLECTIONS(
  35. reference.begin(), reference.end(),
  36. test.begin(), test.end()
  37. );
  38. BOOST_CHECK_EQUAL(
  39. std::distance<const_iterator_t>(reference.begin(), reference_pair.first),
  40. std::distance<const_iterator_t>(test.begin(), test_pair.first)
  41. );
  42. BOOST_CHECK_EQUAL(
  43. std::distance<const_iterator_t>(reference.begin(), reference_pair.second),
  44. std::distance<const_iterator_t>(test.begin(), test_pair.second)
  45. );
  46. BOOST_CHECK_EQUAL_COLLECTIONS(
  47. reference_pair.first, reference_pair.second,
  48. test_pair.first, test_pair.second
  49. );
  50. }
  51. template<class Container>
  52. void test(Container& cont)
  53. {
  54. Container reference(cont);
  55. Container test(cont);
  56. typedef BOOST_DEDUCED_TYPENAME range_iterator<Container>::type iterator_t;
  57. typedef std::pair<iterator_t, iterator_t> pair_t;
  58. pair_t reference_result
  59. = std::equal_range(reference.begin(), reference.end(), 5);
  60. pair_t test_result = boost::equal_range(test, 5);
  61. check_result(reference, reference_result, test, test_result);
  62. pair_t test_result2 = boost::equal_range(boost::make_iterator_range(test), 5);
  63. check_result(reference, reference_result, test, test_result2);
  64. }
  65. template<class Container, class BinaryPredicate>
  66. void sort_container(Container& cont, BinaryPredicate pred)
  67. {
  68. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  69. std::vector<value_t> temp(cont.begin(), cont.end());
  70. std::sort(temp.begin(), temp.end(), pred);
  71. cont.assign(temp.begin(), temp.end());
  72. }
  73. template<class Container, class BinaryPredicate>
  74. void test_pred(Container& cont, BinaryPredicate pred)
  75. {
  76. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container>::type container_t;
  77. container_t reference_temp(cont);
  78. container_t test_temp(cont);
  79. sort_container(reference_temp, pred);
  80. sort_container(test_temp, pred);
  81. Container reference(reference_temp);
  82. Container test(test_temp);
  83. typedef BOOST_DEDUCED_TYPENAME range_iterator<Container>::type iterator_t;
  84. typedef std::pair<iterator_t, iterator_t> pair_t;
  85. pair_t reference_result
  86. = std::equal_range(reference.begin(), reference.end(), 5,
  87. BinaryPredicate());
  88. pair_t test_result = boost::equal_range(test, 5, BinaryPredicate());
  89. check_result(reference, reference_result, test, test_result);
  90. pair_t test_result2 = boost::equal_range(boost::make_iterator_range(test), 5, BinaryPredicate());
  91. check_result(reference, reference_result, test, test_result2);
  92. }
  93. template<class Container>
  94. void test_driver(const Container& cont)
  95. {
  96. Container mutable_cont(cont);
  97. test(mutable_cont);
  98. test(cont);
  99. }
  100. template<class Container, class BinaryPredicate>
  101. void test_pred_driver(const Container& cont, BinaryPredicate pred)
  102. {
  103. Container mutable_cont(cont);
  104. test_pred(mutable_cont, pred);
  105. test_pred(cont, pred);
  106. }
  107. template<class Container>
  108. void test_equal_range_impl()
  109. {
  110. using namespace boost::assign;
  111. Container cont;
  112. test_driver(cont);
  113. test_pred_driver(cont, std::less<int>());
  114. test_pred_driver(cont, std::greater<int>());
  115. cont.clear();
  116. cont += 1;
  117. test_driver(cont);
  118. test_pred_driver(cont, std::less<int>());
  119. test_pred_driver(cont, std::greater<int>());
  120. cont.clear();
  121. cont += 1,2,3,4,5,6,7,8,9;
  122. test_driver(cont);
  123. test_pred_driver(cont, std::less<int>());
  124. test_pred_driver(cont, std::greater<int>());
  125. }
  126. void test_equal_range()
  127. {
  128. test_equal_range_impl< std::vector<int> >();
  129. test_equal_range_impl< std::list<int> >();
  130. test_equal_range_impl< std::deque<int> >();
  131. }
  132. }
  133. }
  134. boost::unit_test::test_suite*
  135. init_unit_test_suite(int argc, char* argv[])
  136. {
  137. boost::unit_test::test_suite* test
  138. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.equal_range" );
  139. test->add( BOOST_TEST_CASE( &boost::test_equal_range ) );
  140. return test;
  141. }