| 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/algorithm/swap_ranges.hpp> |
| 12 | |
| 13 | #include <boost/test/test_tools.hpp> |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
| 16 | #include <boost/assign.hpp> |
| 17 | #include <algorithm> |
| 18 | #include <functional> |
| 19 | #include <list> |
| 20 | #include <numeric> |
| 21 | #include <deque> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | template<class Container1, class Container2> |
| 27 | void test_swap_ranges_impl(const Container1& source1, const Container2& source2) |
| 28 | { |
| 29 | Container1 reference1(source1); |
| 30 | Container2 reference2(source2); |
| 31 | std::swap_ranges(reference1.begin(), reference1.end(), reference2.begin()); |
| 32 | |
| 33 | Container1 test1(source1); |
| 34 | Container2 test2(source2); |
| 35 | boost::swap_ranges(test1, test2); |
| 36 | |
| 37 | BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(), |
| 38 | test1.begin(), test1.end() ); |
| 39 | |
| 40 | BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(), |
| 41 | test2.begin(), test2.end() ); |
| 42 | |
| 43 | test1 = source1; |
| 44 | test2 = source2; |
| 45 | boost::swap_ranges(boost::make_iterator_range(test1), test2); |
| 46 | BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(), |
| 47 | test1.begin(), test1.end() ); |
| 48 | BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(), |
| 49 | test2.begin(), test2.end() ); |
| 50 | |
| 51 | test1 = source1; |
| 52 | test2 = source2; |
| 53 | boost::swap_ranges(test1, boost::make_iterator_range(test2)); |
| 54 | BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(), |
| 55 | test1.begin(), test1.end() ); |
| 56 | BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(), |
| 57 | test2.begin(), test2.end() ); |
| 58 | |
| 59 | test1 = source1; |
| 60 | test2 = source2; |
| 61 | boost::swap_ranges(boost::make_iterator_range(test1), |
| 62 | boost::make_iterator_range(test2)); |
| 63 | BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(), |
| 64 | test1.begin(), test1.end() ); |
| 65 | BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(), |
| 66 | test2.begin(), test2.end() ); |
| 67 | } |
| 68 | |
| 69 | template<class Container1, class Container2> |
| 70 | void test_swap_ranges_impl() |
| 71 | { |
| 72 | using namespace boost::assign; |
| 73 | |
| 74 | Container1 c1; |
| 75 | Container2 c2; |
| 76 | |
| 77 | test_swap_ranges_impl(c1, c2); |
| 78 | |
| 79 | c1.clear(); |
| 80 | c1 += 1; |
| 81 | c2.clear(); |
| 82 | c2 += 2; |
| 83 | test_swap_ranges_impl(c1, c2); |
| 84 | |
| 85 | c1.clear(); |
| 86 | c1 += 1,2,3,4,5,6,7,8,9,10; |
| 87 | c2.clear(); |
| 88 | c2 += 10,9,8,7,6,5,4,3,2,1; |
| 89 | test_swap_ranges_impl(c1, c2); |
| 90 | } |
| 91 | |
| 92 | inline void test_swap_ranges() |
| 93 | { |
| 94 | test_swap_ranges_impl< std::vector<int>, std::vector<int> >(); |
| 95 | test_swap_ranges_impl< std::vector<int>, std::list<int> >(); |
| 96 | test_swap_ranges_impl< std::vector<int>, std::deque<int> >(); |
| 97 | test_swap_ranges_impl< std::list<int>, std::vector<int> >(); |
| 98 | test_swap_ranges_impl< std::list<int>, std::list<int> >(); |
| 99 | test_swap_ranges_impl< std::list<int>, std::deque<int> >(); |
| 100 | test_swap_ranges_impl< std::deque<int>, std::vector<int> >(); |
| 101 | test_swap_ranges_impl< std::deque<int>, std::list<int> >(); |
| 102 | test_swap_ranges_impl< std::deque<int>, std::deque<int> >(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | boost::unit_test::test_suite* |
| 107 | init_unit_test_suite(int argc, char* argv[]) |
| 108 | { |
| 109 | boost::unit_test::test_suite* test |
| 110 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.swap_ranges" ); |
| 111 | |
| 112 | test->add( BOOST_TEST_CASE( &test_swap_ranges ) ); |
| 113 | |
| 114 | return test; |
| 115 | } |
| 116 | |