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