]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/range/test/algorithm_test/binary_search.cpp
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)
7 // For more information, see http://www.boost.org/libs/range/
9 #include <boost/range/algorithm/binary_search.hpp>
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
14 #include <boost/assign.hpp>
15 #include <boost/bind.hpp>
27 template<class Container
>
28 void test(Container
& cont
)
30 Container
reference(cont
);
34 = std::binary_search(reference
.begin(), reference
.end(), 5);
36 bool test_result
= boost::binary_search(test
, 5);
38 BOOST_CHECK( reference_result
== test_result
);
40 BOOST_CHECK( test_result
== boost::binary_search(boost::make_iterator_range(test
), 5) );
42 BOOST_CHECK_EQUAL_COLLECTIONS(
43 reference
.begin(), reference
.end(),
44 test
.begin(), test
.end()
48 template<class Container
, class BinaryPredicate
>
49 void sort_container(Container
& cont
, BinaryPredicate pred
)
51 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
53 std::vector
<value_t
> temp(cont
.begin(), cont
.end());
54 std::sort(temp
.begin(), temp
.end(), pred
);
55 cont
.assign(temp
.begin(), temp
.end());
58 template<class Container
, class BinaryPredicate
>
59 void test_pred(Container
& cont
, BinaryPredicate pred
)
61 Container
reference(cont
);
64 sort_container(reference
, pred
);
65 sort_container(test
, pred
);
68 = std::binary_search(reference
.begin(), reference
.end(), 5,
71 bool test_result
= boost::binary_search(test
, 5, pred
);
73 BOOST_CHECK( test_result
== boost::binary_search(boost::make_iterator_range(test
), 5, pred
) );
75 BOOST_CHECK( reference_result
== test_result
);
77 BOOST_CHECK_EQUAL_COLLECTIONS(
78 reference
.begin(), reference
.end(),
79 test
.begin(), test
.end()
83 template<class Container
>
84 void test_binary_search_impl()
86 using namespace boost::assign
;
91 test_pred(cont
, std::less
<int>());
92 test_pred(cont
, std::greater
<int>());
97 test_pred(cont
, std::less
<int>());
98 test_pred(cont
, std::greater
<int>());
101 cont
+= 1,2,3,4,5,6,7,8,9;
103 test_pred(cont
, std::less
<int>());
104 test_pred(cont
, std::greater
<int>());
107 void test_binary_search()
109 test_binary_search_impl
< std::vector
<int> >();
110 test_binary_search_impl
< std::list
<int> >();
111 test_binary_search_impl
< std::deque
<int> >();
117 boost::unit_test::test_suite
*
118 init_unit_test_suite(int argc
, char* argv
[])
120 boost::unit_test::test_suite
* test
121 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.binary_search" );
123 test
->add( BOOST_TEST_CASE( &boost::test_binary_search
) );