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