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/stable_sort.hpp>
10 
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/assign.hpp>
15 #include <algorithm>
16 #include <functional>
17 #include <list>
18 #include <numeric>
19 #include <deque>
20 #include <vector>
21 
22 namespace boost
23 {
24     namespace
25     {
26         template<class Container>
test_stable_sort_impl(Container & cont)27         void test_stable_sort_impl(Container& cont)
28         {
29             Container reference(cont);
30             Container test(cont);
31 
32             boost::stable_sort(test);
33             std::stable_sort(reference.begin(), reference.end());
34 
35             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
36                                            test.begin(), test.end() );
37 
38             test = cont;
39             boost::stable_sort(boost::make_iterator_range(test));
40             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
41                                            test.begin(), test.end() );
42         }
43 
44         template<class Container, class BinaryPredicate>
test_stable_sort_impl(Container & cont,BinaryPredicate pred)45         void test_stable_sort_impl(Container& cont, BinaryPredicate pred)
46         {
47             Container reference(cont);
48             Container test(cont);
49 
50             boost::stable_sort(test, pred);
51             std::stable_sort(reference.begin(), reference.end(), pred);
52 
53             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
54                                            test.begin(), test.end() );
55 
56             test = cont;
57             boost::stable_sort(boost::make_iterator_range(test), pred);
58             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
59                                            test.begin(), test.end() );
60         }
61 
62         template<class Container>
test_stable_sort_impl()63         void test_stable_sort_impl()
64         {
65             using namespace boost::assign;
66 
67             Container cont;
68             test_stable_sort_impl(cont);
69             test_stable_sort_impl(cont, std::less<int>());
70             test_stable_sort_impl(cont, std::greater<int>());
71 
72             cont.clear();
73             cont += 1;
74             test_stable_sort_impl(cont);
75             test_stable_sort_impl(cont, std::less<int>());
76             test_stable_sort_impl(cont, std::greater<int>());
77 
78             cont.clear();
79             cont += 1,2,3,4,5,6,7,8,9;
80             test_stable_sort_impl(cont);
81             test_stable_sort_impl(cont, std::less<int>());
82             test_stable_sort_impl(cont, std::greater<int>());
83         }
84 
test_stable_sort()85         void test_stable_sort()
86         {
87             test_stable_sort_impl< std::vector<int> >();
88             test_stable_sort_impl< std::deque<int> >();
89         }
90     }
91 }
92 
93 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])94 init_unit_test_suite(int argc, char* argv[])
95 {
96     boost::unit_test::test_suite* test
97         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.stable_sort" );
98 
99     test->add( BOOST_TEST_CASE( &boost::test_stable_sort ) );
100 
101     return test;
102 }
103