sort.cpp 3.1 KB

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