• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright Neil Groves 2009. Use, modification and
2 //  distribution is subject to the Boost Software License, Version
3 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 //  http://www.boost.org/LICENSE_1_0.txt)
5 //
6 //
7 // For more information, see http://www.boost.org/libs/range/
8 //
9 #include <boost/range/algorithm/binary_search.hpp>
10 
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/assign.hpp>
15 #include <algorithm>
16 #include <functional>
17 #include <list>
18 #include <numeric>
19 #include <deque>
20 #include <vector>
21 
22 namespace boost
23 {
24     namespace
25     {
26         template<class Container>
test(Container & cont)27         void test(Container& cont)
28         {
29             Container reference(cont);
30             Container test(cont);
31 
32             bool reference_result
33                 = std::binary_search(reference.begin(), reference.end(), 5);
34 
35             bool test_result = boost::binary_search(test, 5);
36 
37             BOOST_CHECK( reference_result == test_result );
38 
39             BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5) );
40 
41             BOOST_CHECK_EQUAL_COLLECTIONS(
42                 reference.begin(), reference.end(),
43                 test.begin(), test.end()
44                 );
45         }
46 
47         template<class Container, class BinaryPredicate>
sort_container(Container & cont,BinaryPredicate pred)48         void sort_container(Container& cont, BinaryPredicate pred)
49         {
50             typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
51 
52             std::vector<value_t> temp(cont.begin(), cont.end());
53             std::sort(temp.begin(), temp.end(), pred);
54             cont.assign(temp.begin(), temp.end());
55         }
56 
57         template<class Container, class BinaryPredicate>
test_pred(Container & cont,BinaryPredicate pred)58         void test_pred(Container& cont, BinaryPredicate pred)
59         {
60             Container reference(cont);
61             Container test(cont);
62 
63             sort_container(reference, pred);
64             sort_container(test, pred);
65 
66             bool reference_result
67                 = std::binary_search(reference.begin(), reference.end(), 5,
68                                         pred);
69 
70             bool test_result = boost::binary_search(test, 5, pred);
71 
72             BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5, pred) );
73 
74             BOOST_CHECK( reference_result == test_result );
75 
76             BOOST_CHECK_EQUAL_COLLECTIONS(
77                 reference.begin(), reference.end(),
78                 test.begin(), test.end()
79                 );
80         }
81 
82         template<class Container>
test_binary_search_impl()83         void test_binary_search_impl()
84         {
85             using namespace boost::assign;
86 
87             Container cont;
88 
89             test(cont);
90             test_pred(cont, std::less<int>());
91             test_pred(cont, std::greater<int>());
92 
93             cont.clear();
94             cont += 1;
95             test(cont);
96             test_pred(cont, std::less<int>());
97             test_pred(cont, std::greater<int>());
98 
99             cont.clear();
100             cont += 1,2,3,4,5,6,7,8,9;
101             test(cont);
102             test_pred(cont, std::less<int>());
103             test_pred(cont, std::greater<int>());
104         }
105 
test_binary_search()106         void test_binary_search()
107         {
108             test_binary_search_impl< std::vector<int> >();
109             test_binary_search_impl< std::list<int> >();
110             test_binary_search_impl< std::deque<int> >();
111         }
112     }
113 }
114 
115 
116 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])117 init_unit_test_suite(int argc, char* argv[])
118 {
119     boost::unit_test::test_suite* test
120         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.binary_search" );
121 
122     test->add( BOOST_TEST_CASE( &boost::test_binary_search ) );
123 
124     return test;
125 }
126