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/upper_bound.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>
16 #include "../test_driver/range_return_test_driver.hpp"
24 namespace boost_range_test_algorithm_upper_bound
26 class upper_bound_policy
29 template< class Container
>
30 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
31 test_iter(Container
& cont
)
33 typedef BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type iter_t
;
34 iter_t result
= boost::upper_bound(cont
, 5);
35 BOOST_CHECK( result
== boost::upper_bound(boost::make_iterator_range(cont
), 5) );
39 template<boost::range_return_value result_type
>
42 template<class Container
, class Policy
>
43 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,result_type
>::type
44 operator()(Policy
&, Container
& cont
)
46 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,result_type
>::type result_t
;
47 result_t result
= boost::upper_bound
<result_type
>(cont
, 5);
48 BOOST_CHECK( result
== boost::upper_bound
<result_type
>(boost::make_iterator_range(cont
), 5) );
53 template< class Container
>
54 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
55 reference(Container
& cont
)
57 return std::upper_bound(cont
.begin(), cont
.end(), 5);
61 template< class BinaryPredicate
>
62 struct upper_bound_pred_policy
64 template< class Container
>
65 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
66 test_iter(Container
& cont
)
68 typedef BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type iter_t
;
69 iter_t result
= boost::upper_bound(cont
, 5, BinaryPredicate());
70 BOOST_CHECK( result
== boost::upper_bound(boost::make_iterator_range(cont
), 5, BinaryPredicate()) );
74 template< boost::range_return_value result_type
>
77 template< class Container
, class Policy
>
78 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,result_type
>::type
79 operator()(Policy
& policy
, Container
& cont
)
81 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,result_type
>::type result_t
;
83 result_t result
= boost::upper_bound
<result_type
>(cont
, 5, policy
.pred());
85 BOOST_CHECK( result
== boost::upper_bound
<result_type
>(
86 boost::make_iterator_range(cont
), 5, policy
.pred()) );
92 template<class Container
>
93 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
94 reference(Container
& cont
)
96 return std::upper_bound(
97 cont
.begin(), cont
.end(), 5, BinaryPredicate());
100 BinaryPredicate
& pred() { return m_pred
; }
103 BinaryPredicate m_pred
;
106 template<class Container
,
108 class BinaryPredicate
>
109 void test_upper_bound_impl(TestPolicy policy
, BinaryPredicate pred
)
111 using namespace boost::assign
;
113 typedef BOOST_DEDUCED_TYPENAME
boost::remove_const
<Container
>::type container_t
;
114 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
116 boost::range_test::range_return_test_driver test_driver
;
119 Container
& cont
= mcont
;
121 test_driver(cont
, policy
);
126 std::vector
<value_t
> temp(mcont
.begin(), mcont
.end());
127 std::sort(temp
.begin(), temp
.end(), pred
);
128 mcont
.assign(temp
.begin(), temp
.end());
130 test_driver(cont
, policy
);
133 mcont
+= 1,2,3,4,5,6,7,8,9;
135 temp
.assign(mcont
.begin(), mcont
.end());
136 std::sort(temp
.begin(), temp
.end(), pred
);
137 mcont
.assign(temp
.begin(), temp
.end());
139 test_driver(cont
, policy
);
142 template<class Container
>
143 void test_upper_bound_impl()
145 test_upper_bound_impl
<Container
>(
146 upper_bound_policy(),
150 test_upper_bound_impl
<Container
>(
151 upper_bound_pred_policy
<std::less
<int> >(),
155 test_upper_bound_impl
<Container
>(
156 upper_bound_pred_policy
<std::greater
<int> >(),
161 void test_upper_bound()
163 test_upper_bound_impl
< std::vector
<int> >();
164 test_upper_bound_impl
< std::list
<int> >();
165 test_upper_bound_impl
< std::deque
<int> >();
167 test_upper_bound_impl
< const std::vector
<int> >();
168 test_upper_bound_impl
< const std::list
<int> >();
169 test_upper_bound_impl
< const std::deque
<int> >();
173 boost::unit_test::test_suite
*
174 init_unit_test_suite(int argc
, char* argv
[])
176 boost::unit_test::test_suite
* test
177 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.upper_bound" );
179 test
->add( BOOST_TEST_CASE( &boost_range_test_algorithm_upper_bound::test_upper_bound
) );