3 // Copyright Neil Groves 2009. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see http://www.boost.org/libs/range/
11 #include <boost/range/algorithm/partial_sort.hpp>
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
16 #include <boost/assign.hpp>
17 #include <boost/bind.hpp>
29 struct partial_sort_policy
31 template<class Container
, class Iterator
>
32 void test_partial_sort(Container
& cont
, Iterator mid
)
34 const Container
old_cont(cont
);
36 boost::partial_sort(cont
, mid
);
38 const std::size_t index
= std::distance(cont
.begin(), mid
);
39 Container
cont2(old_cont
);
40 Iterator
mid2(cont2
.begin());
41 std::advance(mid2
, index
);
42 boost::partial_sort(cont2
, mid2
);
44 BOOST_CHECK_EQUAL_COLLECTIONS( cont
.begin(), cont
.end(),
45 cont2
.begin(), cont2
.end() );
48 template<class Container
, class Iterator
>
49 void reference_partial_sort(Container
& cont
, Iterator mid
)
51 std::partial_sort(cont
.begin(), mid
, cont
.end());
55 template<class BinaryPredicate
>
56 struct partial_sort_pred_policy
58 template<class Container
, class Iterator
>
59 void test_partial_sort(Container
& cont
, Iterator mid
)
61 const Container
old_cont(cont
);
63 boost::partial_sort(cont
, mid
, BinaryPredicate());
65 const std::size_t index
= std::distance(cont
.begin(), mid
);
66 Container
cont2(old_cont
);
67 Iterator
mid2(cont2
.begin());
68 std::advance(mid2
, index
);
69 boost::partial_sort(cont2
, mid2
, BinaryPredicate());
71 BOOST_CHECK_EQUAL_COLLECTIONS( cont
.begin(), cont
.end(),
72 cont2
.begin(), cont2
.end() );
75 template<class Container
, class Iterator
>
76 void reference_partial_sort(Container
& cont
, Iterator mid
)
78 std::partial_sort(cont
.begin(), mid
, cont
.end(), BinaryPredicate());
82 template<class Container
, class TestPolicy
>
83 void test_partial_sort_tp_impl(Container
& cont
, TestPolicy policy
)
85 Container
reference(cont
);
88 typedef BOOST_DEDUCED_TYPENAME range_iterator
<Container
>::type iterator_t
;
90 BOOST_CHECK_EQUAL( reference
.size(), test
.size() );
91 if (reference
.size() != test
.size())
94 iterator_t reference_mid
= reference
.begin();
95 iterator_t test_mid
= test
.begin();
97 bool complete
= false;
100 if (reference_mid
== reference
.end())
103 policy
.test_partial_sort(test
, test_mid
);
104 policy
.reference_partial_sort(reference
, reference_mid
);
106 BOOST_CHECK_EQUAL_COLLECTIONS(
107 reference
.begin(), reference
.end(),
108 test
.begin(), test
.end()
111 if (reference_mid
!= reference
.end())
119 template<class Container
>
120 void test_partial_sort_impl(Container
& cont
)
122 test_partial_sort_tp_impl(cont
, partial_sort_policy());
125 template<class Container
, class BinaryPredicate
>
126 void test_partial_sort_impl(Container
& cont
, BinaryPredicate pred
)
128 test_partial_sort_tp_impl(cont
, partial_sort_pred_policy
<BinaryPredicate
>());
131 template<class Container
>
132 void test_partial_sort_impl()
134 using namespace boost::assign
;
137 test_partial_sort_impl(cont
);
138 test_partial_sort_impl(cont
, std::less
<int>());
139 test_partial_sort_impl(cont
, std::greater
<int>());
143 test_partial_sort_impl(cont
);
144 test_partial_sort_impl(cont
, std::less
<int>());
145 test_partial_sort_impl(cont
, std::greater
<int>());
148 cont
+= 1,2,3,4,5,6,7,8,9;
149 test_partial_sort_impl(cont
);
150 test_partial_sort_impl(cont
, std::less
<int>());
151 test_partial_sort_impl(cont
, std::greater
<int>());
154 void test_partial_sort()
156 test_partial_sort_impl
< std::vector
<int> >();
157 test_partial_sort_impl
< std::deque
<int> >();
162 boost::unit_test::test_suite
*
163 init_unit_test_suite(int argc
, char* argv
[])
165 boost::unit_test::test_suite
* test
166 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.partial_sort" );
168 test
->add( BOOST_TEST_CASE( &boost::test_partial_sort
) );