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