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