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/search.hpp>
12 
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15 
16 #include <boost/assign.hpp>
17 #include <algorithm>
18 #include <list>
19 #include <set>
20 #include <vector>
21 
22 namespace boost
23 {
24     namespace
25     {
26         template< class Container1, class Container2 >
test_search_impl(Container1 & cont1,Container2 & cont2)27         void test_search_impl(Container1& cont1, Container2& cont2)
28         {
29             typedef BOOST_DEDUCED_TYPENAME Container1::const_iterator const_iterator1_t;
30             typedef BOOST_DEDUCED_TYPENAME Container1::iterator iterator1_t;
31 
32             const Container1& ccont1 = cont1;
33             const Container2& ccont2 = cont2;
34 
35             iterator1_t it = boost::search(cont1, cont2);
36             BOOST_CHECK( it == boost::search(boost::make_iterator_range(cont1), cont2) );
37             BOOST_CHECK( it == boost::search(cont1, boost::make_iterator_range(cont2)) );
38             BOOST_CHECK( it == boost::search(boost::make_iterator_range(cont1),
39                                              boost::make_iterator_range(cont2)) );
40             iterator1_t it2 = boost::search(cont1, ccont2);
41             BOOST_CHECK( it2 == boost::search(boost::make_iterator_range(cont1), ccont2) );
42             BOOST_CHECK( it2 == boost::search(cont1, boost::make_iterator_range(ccont2)) );
43             BOOST_CHECK( it2 == boost::search(boost::make_iterator_range(cont1),
44                                               boost::make_iterator_range(ccont2)) );
45             const_iterator1_t cit = boost::search(ccont1, cont2);
46             BOOST_CHECK( cit == boost::search(boost::make_iterator_range(ccont1), cont2) );
47             BOOST_CHECK( cit == boost::search(ccont1, boost::make_iterator_range(cont2)) );
48             BOOST_CHECK( cit == boost::search(boost::make_iterator_range(ccont1),
49                                               boost::make_iterator_range(cont2)) );
50             const_iterator1_t cit2 = boost::search(ccont1, ccont2);
51             BOOST_CHECK( cit2 == boost::search(boost::make_iterator_range(ccont1), ccont2) );
52             BOOST_CHECK( cit2 == boost::search(ccont1, boost::make_iterator_range(ccont2)) );
53             BOOST_CHECK( cit2 == boost::search(boost::make_iterator_range(ccont1),
54                                                boost::make_iterator_range(ccont2)) );
55 
56             BOOST_CHECK( it == std::search(cont1.begin(), cont1.end(), cont2.begin(), cont2.end()) );
57             BOOST_CHECK( it2 == std::search(cont1.begin(), cont1.end(), ccont2.begin(), ccont2.end()) );
58             BOOST_CHECK( cit == std::search(ccont1.begin(), ccont1.end(), cont2.begin(), cont2.end()) );
59             BOOST_CHECK( cit2 == std::search(ccont1.begin(), ccont1.end(), ccont2.begin(), ccont2.end()) );
60         }
61 
62         template< class Container1, class Container2 >
test_search_impl()63         void test_search_impl()
64         {
65             using namespace boost::assign;
66 
67             Container1 cont1;
68             Container2 cont2;
69 
70             test_search_impl(cont1, cont2);
71 
72             cont1 += 1;
73             test_search_impl(cont1, cont2);
74 
75             cont1.clear();
76             cont2 += 1;
77             test_search_impl(cont1, cont2);
78 
79             cont1 += 1;
80             test_search_impl(cont1, cont2);
81 
82             cont1.clear();
83             cont2.clear();
84             cont1 += 1,2,3,4,5,6,7,8,9;
85             cont2 += 10,11,12;
86             test_search_impl(cont1, cont2);
87 
88             cont2.clear();
89             cont2 += 4,5,6;
90             test_search_impl(cont1, cont2);
91         }
92 
test_search()93         void test_search()
94         {
95             test_search_impl< std::list<int>, std::list<int> >();
96             test_search_impl< std::vector<int>, std::vector<int> >();
97             test_search_impl< std::set<int>, std::set<int> >();
98             test_search_impl< std::list<int>, std::vector<int> >();
99             test_search_impl< std::vector<int>, std::list<int> >();
100         }
101     }
102 }
103 
104 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])105 init_unit_test_suite(int argc, char* argv[])
106 {
107     boost::unit_test::test_suite* test
108         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.search" );
109 
110     test->add( BOOST_TEST_CASE( &boost::test_search ) );
111 
112     return test;
113 }
114