binary_search.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/binary_search.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>
  25. void test(Container& cont)
  26. {
  27. Container reference(cont);
  28. Container test(cont);
  29. bool reference_result
  30. = std::binary_search(reference.begin(), reference.end(), 5);
  31. bool test_result = boost::binary_search(test, 5);
  32. BOOST_CHECK( reference_result == test_result );
  33. BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5) );
  34. BOOST_CHECK_EQUAL_COLLECTIONS(
  35. reference.begin(), reference.end(),
  36. test.begin(), test.end()
  37. );
  38. }
  39. template<class Container, class BinaryPredicate>
  40. void sort_container(Container& cont, BinaryPredicate pred)
  41. {
  42. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  43. std::vector<value_t> temp(cont.begin(), cont.end());
  44. std::sort(temp.begin(), temp.end(), pred);
  45. cont.assign(temp.begin(), temp.end());
  46. }
  47. template<class Container, class BinaryPredicate>
  48. void test_pred(Container& cont, BinaryPredicate pred)
  49. {
  50. Container reference(cont);
  51. Container test(cont);
  52. sort_container(reference, pred);
  53. sort_container(test, pred);
  54. bool reference_result
  55. = std::binary_search(reference.begin(), reference.end(), 5,
  56. pred);
  57. bool test_result = boost::binary_search(test, 5, pred);
  58. BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5, pred) );
  59. BOOST_CHECK( reference_result == test_result );
  60. BOOST_CHECK_EQUAL_COLLECTIONS(
  61. reference.begin(), reference.end(),
  62. test.begin(), test.end()
  63. );
  64. }
  65. template<class Container>
  66. void test_binary_search_impl()
  67. {
  68. using namespace boost::assign;
  69. Container cont;
  70. test(cont);
  71. test_pred(cont, std::less<int>());
  72. test_pred(cont, std::greater<int>());
  73. cont.clear();
  74. cont += 1;
  75. test(cont);
  76. test_pred(cont, std::less<int>());
  77. test_pred(cont, std::greater<int>());
  78. cont.clear();
  79. cont += 1,2,3,4,5,6,7,8,9;
  80. test(cont);
  81. test_pred(cont, std::less<int>());
  82. test_pred(cont, std::greater<int>());
  83. }
  84. void test_binary_search()
  85. {
  86. test_binary_search_impl< std::vector<int> >();
  87. test_binary_search_impl< std::list<int> >();
  88. test_binary_search_impl< std::deque<int> >();
  89. }
  90. }
  91. }
  92. boost::unit_test::test_suite*
  93. init_unit_test_suite(int argc, char* argv[])
  94. {
  95. boost::unit_test::test_suite* test
  96. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.binary_search" );
  97. test->add( BOOST_TEST_CASE( &boost::test_binary_search ) );
  98. return test;
  99. }