adjacent_find.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm/adjacent_find.hpp>
  12. #include <boost/range/iterator_range.hpp>
  13. #include <boost/test/test_tools.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. #include <boost/assign.hpp>
  16. #include <algorithm>
  17. #include <list>
  18. #include <set>
  19. #include <vector>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. template< class Container >
  25. void test_adjacent_find_impl()
  26. {
  27. using namespace boost::assign;
  28. typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
  29. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator const_iterator_t;
  30. Container cont;
  31. const Container& cref_cont = cont;
  32. std::equal_to<int> pred;
  33. BOOST_CHECK( boost::adjacent_find(cont) == cont.end() );
  34. BOOST_CHECK( boost::adjacent_find(cref_cont) == cref_cont.end() );
  35. BOOST_CHECK( boost::adjacent_find(boost::make_iterator_range(cont)) == cont.end() );
  36. BOOST_CHECK( boost::adjacent_find(cont, pred) == cont.end() );
  37. BOOST_CHECK( boost::adjacent_find(cref_cont, pred) == cref_cont.end() );
  38. BOOST_CHECK( boost::adjacent_find(boost::make_iterator_range(cont), pred) == cont.end() );
  39. cont += 1;
  40. BOOST_CHECK( boost::adjacent_find(cont) == cont.end() );
  41. BOOST_CHECK( boost::adjacent_find(cref_cont) == cref_cont.end() );
  42. BOOST_CHECK( boost::adjacent_find(boost::make_iterator_range(cont)) == cont.end() );
  43. BOOST_CHECK( boost::adjacent_find(cont, pred) == cont.end() );
  44. BOOST_CHECK( boost::adjacent_find(cref_cont, pred) == cref_cont.end() );
  45. BOOST_CHECK( boost::adjacent_find(boost::make_iterator_range(cont), pred) == cont.end() );
  46. cont += 2,3,4,5,5,5,6,7,8,9;
  47. iterator_t it = boost::adjacent_find(cont);
  48. iterator_t it_pred = boost::adjacent_find(cont, pred);
  49. BOOST_CHECK( it == it_pred );
  50. BOOST_CHECK( it != cont.end() );
  51. BOOST_CHECK( it == std::adjacent_find(cont.begin(), cont.end()) );
  52. if (it != cont.end())
  53. {
  54. BOOST_CHECK( *it == 5 );
  55. }
  56. BOOST_CHECK( it == boost::adjacent_find(boost::make_iterator_range(cont)) );
  57. BOOST_CHECK( it_pred == boost::adjacent_find(boost::make_iterator_range(cont), pred) );
  58. const_iterator_t cit = boost::adjacent_find(cref_cont);
  59. const_iterator_t cit_pred = boost::adjacent_find(cref_cont, pred);
  60. BOOST_CHECK( cit == cit_pred );
  61. BOOST_CHECK( cit != cref_cont.end() );
  62. BOOST_CHECK( cit == std::adjacent_find(cref_cont.begin(), cref_cont.end()) );
  63. if (cit != cref_cont.end())
  64. {
  65. BOOST_CHECK( *cit == 5 );
  66. }
  67. }
  68. void test_adjacent_find()
  69. {
  70. test_adjacent_find_impl< std::vector<int> >();
  71. test_adjacent_find_impl< std::list<int> >();
  72. test_adjacent_find_impl< std::multiset<int> >();
  73. }
  74. }
  75. }
  76. boost::unit_test::test_suite*
  77. init_unit_test_suite(int argc, char* argv[])
  78. {
  79. boost::unit_test::test_suite* test
  80. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.adjacent_find" );
  81. test->add( BOOST_TEST_CASE( &boost::test_adjacent_find ) );
  82. return test;
  83. }