File: swap_ranges.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (115 lines) | stat: -rw-r--r-- 4,146 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Boost.Range library
//
//  Copyright Neil Groves 2009. Use, modification and
//  distribution is subject to the Boost Software License, Version
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
//
// For more information, see http://www.boost.org/libs/range/
//
#include <boost/range/algorithm/swap_ranges.hpp>

#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>

#include <boost/assign.hpp>
#include <algorithm>
#include <functional>
#include <list>
#include <numeric>
#include <deque>
#include <vector>

namespace
{
    template<class Container1, class Container2>
    void test_swap_ranges_impl(const Container1& source1, const Container2& source2)
    {
        Container1 reference1(source1);
        Container2 reference2(source2);
        std::swap_ranges(reference1.begin(), reference1.end(), reference2.begin());

        Container1 test1(source1);
        Container2 test2(source2);
        boost::swap_ranges(test1, test2);

        BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
                                       test1.begin(), test1.end() );

        BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
                                       test2.begin(), test2.end() );
                                       
        test1 = source1;
        test2 = source2;
        boost::swap_ranges(boost::make_iterator_range(test1), test2);
        BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
                                       test1.begin(), test1.end() );
        BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
                                       test2.begin(), test2.end() );
                                       
        test1 = source1;
        test2 = source2;
        boost::swap_ranges(test1, boost::make_iterator_range(test2));
        BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
                                       test1.begin(), test1.end() );
        BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
                                       test2.begin(), test2.end() );
                                       
        test1 = source1;
        test2 = source2;
        boost::swap_ranges(boost::make_iterator_range(test1),
                           boost::make_iterator_range(test2));
        BOOST_CHECK_EQUAL_COLLECTIONS( reference1.begin(), reference1.end(),
                                       test1.begin(), test1.end() );
        BOOST_CHECK_EQUAL_COLLECTIONS( reference2.begin(), reference2.end(),
                                       test2.begin(), test2.end() );
    }

    template<class Container1, class Container2>
    void test_swap_ranges_impl()
    {
        using namespace boost::assign;

        Container1 c1;
        Container2 c2;

        test_swap_ranges_impl(c1, c2);

        c1.clear();
        c1 += 1;
        c2.clear();
        c2 += 2;
        test_swap_ranges_impl(c1, c2);

        c1.clear();
        c1 += 1,2,3,4,5,6,7,8,9,10;
        c2.clear();
        c2 += 10,9,8,7,6,5,4,3,2,1;
        test_swap_ranges_impl(c1, c2);
    }

    inline void test_swap_ranges()
    {
        test_swap_ranges_impl< std::vector<int>, std::vector<int> >();
        test_swap_ranges_impl< std::vector<int>, std::list<int> >();
        test_swap_ranges_impl< std::vector<int>, std::deque<int> >();
        test_swap_ranges_impl< std::list<int>, std::vector<int> >();
        test_swap_ranges_impl< std::list<int>, std::list<int> >();
        test_swap_ranges_impl< std::list<int>, std::deque<int> >();
        test_swap_ranges_impl< std::deque<int>, std::vector<int> >();
        test_swap_ranges_impl< std::deque<int>, std::list<int> >();
        test_swap_ranges_impl< std::deque<int>, std::deque<int> >();
    }
}

boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
    boost::unit_test::test_suite* test
        = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.swap_ranges" );

    test->add( BOOST_TEST_CASE( &test_swap_ranges ) );

    return test;
}