find_if.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_if.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 "../test_function/greater_than_x.hpp"
  17. #include "../test_function/false_predicate.hpp"
  18. #include <algorithm>
  19. #include <functional>
  20. #include <deque>
  21. #include <list>
  22. #include <vector>
  23. namespace boost_range_test_algorithm_find_if
  24. {
  25. template<class UnaryPredicate>
  26. class find_if_test_policy
  27. {
  28. public:
  29. explicit find_if_test_policy(UnaryPredicate pred)
  30. : m_pred(pred) {}
  31. template<class Container>
  32. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  33. test_iter(Container& cont)
  34. {
  35. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t;
  36. iter_t result = boost::find_if(cont, m_pred);
  37. BOOST_CHECK( result == boost::find_if(boost::make_iterator_range(cont), m_pred) );
  38. return result;
  39. }
  40. template<boost::range_return_value return_type>
  41. struct test_range
  42. {
  43. template<class Container>
  44. BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
  45. operator()(find_if_test_policy& policy, Container& cont)
  46. {
  47. typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
  48. result_t result = boost::find_if<return_type>(cont, policy.pred());
  49. BOOST_CHECK( result == boost::find_if<return_type>(boost::make_iterator_range(cont), policy.pred()) );
  50. return result;
  51. }
  52. };
  53. template<class Container>
  54. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  55. reference(Container& cont)
  56. {
  57. return std::find_if(cont.begin(), cont.end(), m_pred);
  58. }
  59. UnaryPredicate& pred() { return m_pred; }
  60. private:
  61. UnaryPredicate m_pred;
  62. };
  63. template<class UnaryPredicate>
  64. find_if_test_policy<UnaryPredicate>
  65. make_policy(UnaryPredicate pred)
  66. {
  67. return find_if_test_policy<UnaryPredicate>(pred);
  68. }
  69. template<class Container>
  70. void test_find_if_container()
  71. {
  72. using namespace boost::assign;
  73. using namespace boost::range_test_function;
  74. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container>::type container_t;
  75. boost::range_test::range_return_test_driver test_driver;
  76. container_t mcont;
  77. Container& cont = mcont;
  78. test_driver(cont, make_policy(greater_than_x<int>(5)));
  79. test_driver(cont, make_policy(false_predicate()));
  80. mcont.clear();
  81. mcont += 1;
  82. test_driver(cont, make_policy(greater_than_x<int>(5)));
  83. test_driver(cont, make_policy(false_predicate()));
  84. mcont.clear();
  85. mcont += 1,2,3,4,5,6,7,8,9;
  86. test_driver(cont, make_policy(greater_than_x<int>(5)));
  87. test_driver(cont, make_policy(false_predicate()));
  88. }
  89. void test_find_if()
  90. {
  91. test_find_if_container< std::vector<int> >();
  92. test_find_if_container< std::list<int> >();
  93. test_find_if_container< std::deque<int> >();
  94. test_find_if_container< const std::vector<int> >();
  95. test_find_if_container< const std::list<int> >();
  96. test_find_if_container< const std::deque<int> >();
  97. }
  98. }
  99. boost::unit_test::test_suite*
  100. init_unit_test_suite(int argc, char* argv[])
  101. {
  102. boost::unit_test::test_suite* test
  103. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.find_if" );
  104. test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_find_if::test_find_if ) );
  105. return test;
  106. }