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