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/upper_bound.hpp> |
10 | |
11 | #include <boost/test/test_tools.hpp> |
12 | #include <boost/test/unit_test.hpp> |
13 | |
14 | #include <boost/assign.hpp> |
15 | #include "../test_driver/range_return_test_driver.hpp" |
16 | #include <algorithm> |
17 | #include <functional> |
18 | #include <list> |
19 | #include <numeric> |
20 | #include <deque> |
21 | #include <vector> |
22 | |
23 | namespace boost_range_test_algorithm_upper_bound |
24 | { |
25 | class upper_bound_policy |
26 | { |
27 | public: |
28 | template< class Container > |
29 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
30 | test_iter(Container& cont) |
31 | { |
32 | typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t; |
33 | iter_t result = boost::upper_bound(cont, 5); |
34 | BOOST_CHECK( result == boost::upper_bound(boost::make_iterator_range(cont), 5) ); |
35 | return result; |
36 | } |
37 | |
38 | template<boost::range_return_value result_type> |
39 | struct test_range |
40 | { |
41 | template<class Container, class Policy> |
42 | BOOST_DEDUCED_TYPENAME boost::range_return<Container,result_type>::type |
43 | operator()(Policy&, Container& cont) |
44 | { |
45 | typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,result_type>::type result_t; |
46 | result_t result = boost::upper_bound<result_type>(cont, 5); |
47 | BOOST_CHECK( result == boost::upper_bound<result_type>(boost::make_iterator_range(cont), 5) ); |
48 | return result; |
49 | } |
50 | }; |
51 | |
52 | template< class Container > |
53 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
54 | reference(Container& cont) |
55 | { |
56 | return std::upper_bound(cont.begin(), cont.end(), 5); |
57 | } |
58 | }; |
59 | |
60 | template< class BinaryPredicate > |
61 | struct upper_bound_pred_policy |
62 | { |
63 | template< class Container > |
64 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
65 | test_iter(Container& cont) |
66 | { |
67 | typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t; |
68 | iter_t result = boost::upper_bound(cont, 5, BinaryPredicate()); |
69 | BOOST_CHECK( result == boost::upper_bound(boost::make_iterator_range(cont), 5, BinaryPredicate()) ); |
70 | return result; |
71 | } |
72 | |
73 | template< boost::range_return_value result_type> |
74 | struct test_range |
75 | { |
76 | template< class Container, class Policy > |
77 | BOOST_DEDUCED_TYPENAME boost::range_return<Container,result_type>::type |
78 | operator()(Policy& policy, Container& cont) |
79 | { |
80 | typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,result_type>::type result_t; |
81 | |
82 | result_t result = boost::upper_bound<result_type>(cont, 5, policy.pred()); |
83 | |
84 | BOOST_CHECK( result == boost::upper_bound<result_type>( |
85 | boost::make_iterator_range(cont), 5, policy.pred()) ); |
86 | |
87 | return result; |
88 | } |
89 | }; |
90 | |
91 | template<class Container> |
92 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type |
93 | reference(Container& cont) |
94 | { |
95 | return std::upper_bound( |
96 | cont.begin(), cont.end(), 5, BinaryPredicate()); |
97 | } |
98 | |
99 | BinaryPredicate& pred() { return m_pred; } |
100 | |
101 | private: |
102 | BinaryPredicate m_pred; |
103 | }; |
104 | |
105 | template<class Container, |
106 | class TestPolicy, |
107 | class BinaryPredicate> |
108 | void test_upper_bound_impl(TestPolicy policy, BinaryPredicate pred) |
109 | { |
110 | using namespace boost::assign; |
111 | |
112 | typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container>::type container_t; |
113 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; |
114 | |
115 | boost::range_test::range_return_test_driver test_driver; |
116 | |
117 | container_t mcont; |
118 | Container& cont = mcont; |
119 | |
120 | test_driver(cont, policy); |
121 | |
122 | mcont.clear(); |
123 | mcont += 1; |
124 | |
125 | std::vector<value_t> temp(mcont.begin(), mcont.end()); |
126 | std::sort(temp.begin(), temp.end(), pred); |
127 | mcont.assign(temp.begin(), temp.end()); |
128 | |
129 | test_driver(cont, policy); |
130 | |
131 | mcont.clear(); |
132 | mcont += 1,2,3,4,5,6,7,8,9; |
133 | |
134 | temp.assign(mcont.begin(), mcont.end()); |
135 | std::sort(temp.begin(), temp.end(), pred); |
136 | mcont.assign(temp.begin(), temp.end()); |
137 | |
138 | test_driver(cont, policy); |
139 | } |
140 | |
141 | template<class Container> |
142 | void test_upper_bound_impl() |
143 | { |
144 | test_upper_bound_impl<Container>( |
145 | upper_bound_policy(), |
146 | std::less<int>() |
147 | ); |
148 | |
149 | test_upper_bound_impl<Container>( |
150 | upper_bound_pred_policy<std::less<int> >(), |
151 | std::less<int>() |
152 | ); |
153 | |
154 | test_upper_bound_impl<Container>( |
155 | upper_bound_pred_policy<std::greater<int> >(), |
156 | std::greater<int>() |
157 | ); |
158 | } |
159 | |
160 | void test_upper_bound() |
161 | { |
162 | test_upper_bound_impl< std::vector<int> >(); |
163 | test_upper_bound_impl< std::list<int> >(); |
164 | test_upper_bound_impl< std::deque<int> >(); |
165 | |
166 | test_upper_bound_impl< const std::vector<int> >(); |
167 | test_upper_bound_impl< const std::list<int> >(); |
168 | test_upper_bound_impl< const std::deque<int> >(); |
169 | } |
170 | } |
171 | |
172 | boost::unit_test::test_suite* |
173 | init_unit_test_suite(int argc, char* argv[]) |
174 | { |
175 | boost::unit_test::test_suite* test |
176 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.upper_bound" ); |
177 | |
178 | test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_upper_bound::test_upper_bound ) ); |
179 | |
180 | return test; |
181 | } |
182 | |