lower_bound.cpp 5.8 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/lower_bound.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 <boost/range/algorithm/lower_bound.hpp>
  15. #include "../test_driver/range_return_test_driver.hpp"
  16. #include <algorithm>
  17. #include <functional>
  18. #include <list>
  19. #include <numeric>
  20. #include <deque>
  21. #include <vector>
  22. namespace boost_range_test_algorithm_lower_bound
  23. {
  24. class lower_bound_policy
  25. {
  26. public:
  27. template< class Container >
  28. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  29. test_iter(Container& cont)
  30. {
  31. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t;
  32. iter_t result = boost::lower_bound(cont, 5);
  33. BOOST_CHECK( result == boost::lower_bound(boost::make_iterator_range(cont), 5) );
  34. return result;
  35. }
  36. template<boost::range_return_value return_type>
  37. struct test_range
  38. {
  39. template<class Container, class Policy>
  40. BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
  41. operator()(Policy&, Container& cont)
  42. {
  43. typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
  44. result_t result = boost::lower_bound<return_type>(cont, 5);
  45. BOOST_CHECK( result == boost::lower_bound<return_type>(boost::make_iterator_range(cont), 5) );
  46. return result;
  47. }
  48. };
  49. template< class Container >
  50. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  51. reference(Container& cont)
  52. {
  53. return std::lower_bound(cont.begin(), cont.end(), 5);
  54. }
  55. };
  56. template< class BinaryPredicate >
  57. struct lower_bound_pred_policy
  58. {
  59. template< class Container >
  60. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  61. test_iter(Container& cont)
  62. {
  63. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t;
  64. iter_t result = boost::lower_bound(cont, 5, m_pred);
  65. BOOST_CHECK( result == boost::lower_bound(
  66. boost::make_iterator_range(cont), 5, m_pred) );
  67. return result;
  68. }
  69. template< boost::range_return_value return_type >
  70. struct test_range
  71. {
  72. template<class Container, class Policy>
  73. BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
  74. operator()(Policy& policy, Container& cont)
  75. {
  76. typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
  77. result_t result = boost::lower_bound<return_type>(cont, 5, policy.pred());
  78. BOOST_CHECK( result == boost::lower_bound<return_type>(
  79. boost::make_iterator_range(cont), 5, policy.pred()) );
  80. return result;
  81. }
  82. };
  83. template<class Container>
  84. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  85. reference(Container& cont)
  86. {
  87. return std::lower_bound(
  88. cont.begin(), cont.end(), 5, m_pred);
  89. }
  90. BinaryPredicate& pred() { return m_pred; }
  91. private:
  92. BinaryPredicate m_pred;
  93. };
  94. template<class Container,
  95. class TestPolicy,
  96. class BinaryPredicate>
  97. void test_lower_bound_impl(TestPolicy policy, BinaryPredicate pred)
  98. {
  99. using namespace boost::assign;
  100. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container>::type container_t;
  101. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  102. boost::range_test::range_return_test_driver test_driver;
  103. container_t mcont;
  104. Container& cont = mcont;
  105. test_driver(cont, policy);
  106. mcont.clear();
  107. mcont += 1;
  108. std::vector<value_t> temp(mcont.begin(), mcont.end());
  109. std::sort(temp.begin(), temp.end(), pred);
  110. mcont.assign(temp.begin(), temp.end());
  111. test_driver(cont, policy);
  112. mcont.clear();
  113. mcont += 1,2,3,4,5,6,7,8,9;
  114. temp.assign(mcont.begin(), mcont.end());
  115. std::sort(temp.begin(), temp.end(), pred);
  116. mcont.assign(temp.begin(), temp.end());
  117. test_driver(cont, policy);
  118. }
  119. template<class Container>
  120. void test_lower_bound_impl()
  121. {
  122. test_lower_bound_impl<Container>(
  123. lower_bound_policy(),
  124. std::less<int>()
  125. );
  126. test_lower_bound_impl<Container>(
  127. lower_bound_pred_policy<std::less<int> >(),
  128. std::less<int>()
  129. );
  130. test_lower_bound_impl<Container>(
  131. lower_bound_pred_policy<std::greater<int> >(),
  132. std::greater<int>()
  133. );
  134. }
  135. void test_lower_bound()
  136. {
  137. test_lower_bound_impl< std::vector<int> >();
  138. test_lower_bound_impl< std::list<int> >();
  139. test_lower_bound_impl< std::deque<int> >();
  140. test_lower_bound_impl< const std::vector<int> >();
  141. test_lower_bound_impl< const std::list<int> >();
  142. test_lower_bound_impl< const std::deque<int> >();
  143. }
  144. }
  145. boost::unit_test::test_suite*
  146. init_unit_test_suite(int argc, char* argv[])
  147. {
  148. boost::unit_test::test_suite* test
  149. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.lower_bound" );
  150. test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_lower_bound::test_lower_bound ) );
  151. return test;
  152. }