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)
9 // For more information, see http://www.boost.org/libs/range/
11 #include <boost/range/algorithm/find.hpp>
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
16 #include <boost/assign.hpp>
17 #include "../test_driver/range_return_test_driver.hpp"
25 namespace boost_range_test_algorithm_find
27 class find_test_policy
30 template<class Container
>
31 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
32 test_iter(Container
& cont
)
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
);
41 template<boost::range_return_value return_type
>
44 template<class Container
, class Policy
>
45 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type
46 operator()(Policy
&, Container
& cont
)
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
);
56 template<class Container
>
57 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
58 reference(Container
& cont
)
60 return std::find(cont
.begin(), cont
.end(), 3);
64 template<class Container
>
65 void test_find_container()
67 using namespace boost::assign
;
69 typedef BOOST_DEDUCED_TYPENAME
boost::remove_const
<Container
>::type container_t
;
71 boost::range_test::range_return_test_driver test_driver
;
74 Container
& cont
= mcont
;
75 test_driver(cont
, find_test_policy());
79 test_driver(cont
, find_test_policy());
82 mcont
+= 1,2,3,4,5,6,7,8,9;
83 test_driver(cont
, find_test_policy());
88 test_find_container
< std::vector
<int> >();
89 test_find_container
< std::list
<int> >();
90 test_find_container
< std::deque
<int> >();
92 test_find_container
< const std::vector
<int> >();
93 test_find_container
< const std::list
<int> >();
94 test_find_container
< const std::deque
<int> >();
97 const std::vector
<int>& cvi
= vi
;
98 std::vector
<int>::const_iterator it
= boost::find(vi
, 0);
99 std::vector
<int>::const_iterator it2
= boost::find(cvi
, 0);
100 BOOST_CHECK( it
== it2
);
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()
113 for (int i
= 0; i
< 10; ++i
)
116 BOOST_CHECK(boost::find
<boost::return_found_end
>(l
, 3));
117 BOOST_CHECK(!boost::find
<boost::return_found_end
>(l
, 10));
121 boost::unit_test::test_suite
*
122 init_unit_test_suite(int argc
, char* argv
[])
124 boost::unit_test::test_suite
* test
125 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.find" );
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
) );