stable_sort.cpp 3.2 KB

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