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 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/range/iterator.hpp> |
17 | #include <algorithm> |
18 | #include <list> |
19 | #include <vector> |
20 | |
21 | namespace |
22 | { |
23 | template< class Container > |
24 | void test_is_sorted_impl() |
25 | { |
26 | Container ascending; |
27 | Container descending; |
28 | |
29 | // Empty ranges are regarded as sorted against any predicate. |
30 | BOOST_CHECK( boost::is_sorted(ascending) ); |
31 | BOOST_CHECK( boost::is_sorted(ascending, std::less<std::size_t>()) ); |
32 | BOOST_CHECK( boost::is_sorted(ascending, std::greater<std::size_t>()) ); |
33 | |
34 | for (std::size_t i = 0; i < 10; ++i) |
35 | { |
36 | ascending.push_back(i); |
37 | descending.push_back(9 - i); |
38 | } |
39 | |
40 | BOOST_CHECK( boost::is_sorted(ascending) ); |
41 | BOOST_CHECK( !boost::is_sorted(descending) ); |
42 | BOOST_CHECK( !boost::is_sorted(ascending, std::greater<std::size_t>()) ); |
43 | BOOST_CHECK( boost::is_sorted(descending, std::greater<std::size_t>()) ); |
44 | } |
45 | |
46 | void test_is_sorted() |
47 | { |
48 | test_is_sorted_impl< std::vector<std::size_t> >(); |
49 | test_is_sorted_impl< std::list<std::size_t> >(); |
50 | } |
51 | } |
52 | |
53 | boost::unit_test::test_suite* |
54 | init_unit_test_suite(int argc, char* argv[]) |
55 | { |
56 | boost::unit_test::test_suite* test |
57 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.is_sorted"); |
58 | |
59 | test->add( BOOST_TEST_CASE( &test_is_sorted ) ); |
60 | |
61 | return test; |
62 | } |
63 |