find.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/find.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.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_find
  23. {
  24. class find_test_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::find(cont, 3);
  33. iter_t result2 = boost::find(boost::make_iterator_range(cont), 3);
  34. BOOST_CHECK( result == result2 );
  35. return result;
  36. }
  37. template<boost::range_return_value return_type>
  38. struct test_range
  39. {
  40. template<class Container, class Policy>
  41. BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
  42. operator()(Policy&, Container& cont)
  43. {
  44. typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
  45. result_t result = boost::find<return_type>(cont, 3);
  46. result_t result2 = boost::find<return_type>(boost::make_iterator_range(cont), 3);
  47. BOOST_CHECK( result == result2 );
  48. return result;
  49. }
  50. };
  51. template<class Container>
  52. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  53. reference(Container& cont)
  54. {
  55. return std::find(cont.begin(), cont.end(), 3);
  56. }
  57. };
  58. template<class Container>
  59. void test_find_container()
  60. {
  61. using namespace boost::assign;
  62. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container>::type container_t;
  63. boost::range_test::range_return_test_driver test_driver;
  64. container_t mcont;
  65. Container& cont = mcont;
  66. test_driver(cont, find_test_policy());
  67. mcont.clear();
  68. mcont += 1;
  69. test_driver(cont, find_test_policy());
  70. mcont.clear();
  71. mcont += 1,2,3,4,5,6,7,8,9;
  72. test_driver(cont, find_test_policy());
  73. }
  74. void test_find()
  75. {
  76. test_find_container< std::vector<int> >();
  77. test_find_container< std::list<int> >();
  78. test_find_container< std::deque<int> >();
  79. test_find_container< const std::vector<int> >();
  80. test_find_container< const std::list<int> >();
  81. test_find_container< const std::deque<int> >();
  82. std::vector<int> vi;
  83. const std::vector<int>& cvi = vi;
  84. std::vector<int>::const_iterator it = boost::find(vi, 0);
  85. std::vector<int>::const_iterator it2 = boost::find(cvi, 0);
  86. BOOST_CHECK( it == it2 );
  87. }
  88. // The find algorithm can be used like a "contains" algorithm
  89. // since the returned iterator_range is convertible to bool.
  90. // Therefore if the return value is an empty range it will
  91. // convert to the equivalent to "false" whereas a range that
  92. // is not empty will convert to "true". Therefore one can
  93. // use the syntax boost::find<boost::return_found_end>(rng, x)
  94. // as a contains function.
  95. void test_find_as_contains()
  96. {
  97. std::list<int> l;
  98. for (int i = 0; i < 10; ++i)
  99. l.push_back(i);
  100. BOOST_CHECK(boost::find<boost::return_found_end>(l, 3));
  101. BOOST_CHECK(!boost::find<boost::return_found_end>(l, 10));
  102. }
  103. }
  104. boost::unit_test::test_suite*
  105. init_unit_test_suite(int argc, char* argv[])
  106. {
  107. boost::unit_test::test_suite* test
  108. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.find" );
  109. test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_find::test_find ) );
  110. test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_find::test_find_as_contains ) );
  111. return test;
  112. }