| 1 | // Boost.Range library |
| 2 | // |
| 3 | // Copyright Neil Groves 2009. 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/adaptor/sliced.hpp> |
| 12 | |
| 13 | #include <boost/test/test_tools.hpp> |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
| 16 | #include <boost/assign.hpp> |
| 17 | #include <boost/range/algorithm_ext.hpp> |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <deque> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace boost |
| 24 | { |
| 25 | namespace |
| 26 | { |
| 27 | template< class Container > |
| 28 | void sliced_test_impl( Container& c ) |
| 29 | { |
| 30 | using namespace boost::adaptors; |
| 31 | |
| 32 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; |
| 33 | |
| 34 | std::vector< value_t > test_result1; |
| 35 | boost::push_back(test_result1, c | sliced(0u,c.size())); |
| 36 | |
| 37 | BOOST_CHECK_EQUAL_COLLECTIONS( test_result1.begin(), test_result1.end(), |
| 38 | c.begin(), c.end() ); |
| 39 | |
| 40 | std::vector< value_t > test_alt_result1; |
| 41 | boost::push_back(test_alt_result1, adaptors::slice(c, 0u, c.size())); |
| 42 | BOOST_CHECK_EQUAL_COLLECTIONS( test_alt_result1.begin(), test_alt_result1.end(), |
| 43 | c.begin(), c.end() ); |
| 44 | |
| 45 | BOOST_CHECK( boost::empty(c | sliced(0u, 0u)) ); |
| 46 | |
| 47 | const std::size_t half_count = c.size() / 2u; |
| 48 | if (half_count > 0u) |
| 49 | { |
| 50 | std::vector< value_t > test_result2; |
| 51 | boost::push_back(test_result2, c | sliced(0u, half_count)); |
| 52 | |
| 53 | BOOST_CHECK_EQUAL_COLLECTIONS( test_result2.begin(), test_result2.end(), |
| 54 | c.begin(), c.begin() + half_count ); |
| 55 | |
| 56 | std::vector< value_t > test_alt_result2; |
| 57 | boost::push_back(test_alt_result2, adaptors::slice(c, 0u, half_count)); |
| 58 | BOOST_CHECK_EQUAL_COLLECTIONS( test_alt_result2.begin(), test_alt_result2.end(), |
| 59 | c.begin(), c.begin() + half_count ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | template< class Container > |
| 64 | void sliced_test_impl() |
| 65 | { |
| 66 | using namespace boost::assign; |
| 67 | |
| 68 | Container c; |
| 69 | |
| 70 | // Test empty |
| 71 | sliced_test_impl(c); |
| 72 | |
| 73 | // Test one element |
| 74 | c += 1; |
| 75 | sliced_test_impl(c); |
| 76 | |
| 77 | // Test many elements |
| 78 | c += 1,1,1,2,2,3,4,5,6,6,6,7,8,9; |
| 79 | sliced_test_impl(c); |
| 80 | } |
| 81 | |
| 82 | void sliced_test() |
| 83 | { |
| 84 | sliced_test_impl< std::vector< int > >(); |
| 85 | sliced_test_impl< std::deque< int > >(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | boost::unit_test::test_suite* |
| 91 | init_unit_test_suite(int argc, char* argv[]) |
| 92 | { |
| 93 | boost::unit_test::test_suite* test |
| 94 | = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.sliced" ); |
| 95 | |
| 96 | test->add( BOOST_TEST_CASE( &boost::sliced_test ) ); |
| 97 | |
| 98 | return test; |
| 99 | } |
| 100 | |