is_sorted.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2010. 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)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm_ext/is_sorted.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <vector>
  18. namespace
  19. {
  20. template< class Container >
  21. void test_is_sorted_impl()
  22. {
  23. Container ascending;
  24. Container descending;
  25. // Empty ranges are regarded as sorted against any predicate.
  26. BOOST_CHECK( boost::is_sorted(ascending) );
  27. BOOST_CHECK( boost::is_sorted(ascending, std::less<std::size_t>()) );
  28. BOOST_CHECK( boost::is_sorted(ascending, std::greater<std::size_t>()) );
  29. for (std::size_t i = 0; i < 10; ++i)
  30. {
  31. ascending.push_back(i);
  32. descending.push_back(9 - i);
  33. }
  34. BOOST_CHECK( boost::is_sorted(ascending) );
  35. BOOST_CHECK( !boost::is_sorted(descending) );
  36. BOOST_CHECK( !boost::is_sorted(ascending, std::greater<std::size_t>()) );
  37. BOOST_CHECK( boost::is_sorted(descending, std::greater<std::size_t>()) );
  38. }
  39. void test_is_sorted()
  40. {
  41. test_is_sorted_impl< std::vector<std::size_t> >();
  42. test_is_sorted_impl< std::list<std::size_t> >();
  43. }
  44. }
  45. boost::unit_test::test_suite*
  46. init_unit_test_suite(int argc, char* argv[])
  47. {
  48. boost::unit_test::test_suite* test
  49. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.is_sorted" );
  50. test->add( BOOST_TEST_CASE( &test_is_sorted ) );
  51. return test;
  52. }