]>
Commit | Line | Data |
---|---|---|
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 <boost/bind.hpp> | |
16 | #include <algorithm> | |
17 | #include <functional> | |
18 | #include <list> | |
19 | #include <numeric> | |
20 | #include <deque> | |
21 | #include <vector> | |
22 | ||
23 | namespace boost | |
24 | { | |
25 | namespace | |
26 | { | |
27 | template<class Container> | |
28 | void test(Container& cont) | |
29 | { | |
30 | Container reference(cont); | |
31 | Container test(cont); | |
32 | ||
33 | bool reference_result | |
34 | = std::binary_search(reference.begin(), reference.end(), 5); | |
35 | ||
36 | bool test_result = boost::binary_search(test, 5); | |
37 | ||
38 | BOOST_CHECK( reference_result == test_result ); | |
39 | ||
40 | BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5) ); | |
41 | ||
42 | BOOST_CHECK_EQUAL_COLLECTIONS( | |
43 | reference.begin(), reference.end(), | |
44 | test.begin(), test.end() | |
45 | ); | |
46 | } | |
47 | ||
48 | template<class Container, class BinaryPredicate> | |
49 | void sort_container(Container& cont, BinaryPredicate pred) | |
50 | { | |
51 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; | |
52 | ||
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()); | |
56 | } | |
57 | ||
58 | template<class Container, class BinaryPredicate> | |
59 | void test_pred(Container& cont, BinaryPredicate pred) | |
60 | { | |
61 | Container reference(cont); | |
62 | Container test(cont); | |
63 | ||
64 | sort_container(reference, pred); | |
65 | sort_container(test, pred); | |
66 | ||
67 | bool reference_result | |
68 | = std::binary_search(reference.begin(), reference.end(), 5, | |
69 | pred); | |
70 | ||
71 | bool test_result = boost::binary_search(test, 5, pred); | |
72 | ||
73 | BOOST_CHECK( test_result == boost::binary_search(boost::make_iterator_range(test), 5, pred) ); | |
74 | ||
75 | BOOST_CHECK( reference_result == test_result ); | |
76 | ||
77 | BOOST_CHECK_EQUAL_COLLECTIONS( | |
78 | reference.begin(), reference.end(), | |
79 | test.begin(), test.end() | |
80 | ); | |
81 | } | |
82 | ||
83 | template<class Container> | |
84 | void test_binary_search_impl() | |
85 | { | |
86 | using namespace boost::assign; | |
87 | ||
88 | Container cont; | |
89 | ||
90 | test(cont); | |
91 | test_pred(cont, std::less<int>()); | |
92 | test_pred(cont, std::greater<int>()); | |
93 | ||
94 | cont.clear(); | |
95 | cont += 1; | |
96 | test(cont); | |
97 | test_pred(cont, std::less<int>()); | |
98 | test_pred(cont, std::greater<int>()); | |
99 | ||
100 | cont.clear(); | |
101 | cont += 1,2,3,4,5,6,7,8,9; | |
102 | test(cont); | |
103 | test_pred(cont, std::less<int>()); | |
104 | test_pred(cont, std::greater<int>()); | |
105 | } | |
106 | ||
107 | void test_binary_search() | |
108 | { | |
109 | test_binary_search_impl< std::vector<int> >(); | |
110 | test_binary_search_impl< std::list<int> >(); | |
111 | test_binary_search_impl< std::deque<int> >(); | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | ||
117 | boost::unit_test::test_suite* | |
118 | init_unit_test_suite(int argc, char* argv[]) | |
119 | { | |
120 | boost::unit_test::test_suite* test | |
121 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.binary_search" ); | |
122 | ||
123 | test->add( BOOST_TEST_CASE( &boost::test_binary_search ) ); | |
124 | ||
125 | return test; | |
126 | } |