File: iterator_range_drop.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (200 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (14)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Boost.Range library
//
//  Copyright Neil Groves 2014. 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/detail/workaround.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <boost/cstdint.hpp>

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

#include <vector>

namespace boost_range_test
{
    namespace
    {

class single_pass_iterator
        : public boost::iterator_facade<
            single_pass_iterator,
            boost::int32_t,
            boost::single_pass_traversal_tag,
            const boost::int32_t&
        >
{
    friend class boost::iterator_core_access;

    typedef std::vector<boost::int32_t>::const_iterator iterator_t;

public:
    single_pass_iterator() { }

    explicit single_pass_iterator(iterator_t it)
        : m_it(it)
    {
    }

private:
    void increment()
    {
        ++m_it;
    }

    bool equal(single_pass_iterator other) const
    {
        return m_it == other.m_it;
    }

    reference dereference() const
    {
        return *m_it;
    }

    iterator_t m_it;
};

class bidirectional_iterator
        : public boost::iterator_facade<
            bidirectional_iterator,
            boost::int32_t,
            boost::bidirectional_traversal_tag,
            const boost::int32_t&
        >
{
    friend class boost::iterator_core_access;

    typedef std::vector<boost::int32_t>::const_iterator iterator_t;

public:
    bidirectional_iterator() { }

    explicit bidirectional_iterator(iterator_t it)
        : m_it(it)
    {
    }

private:
    void increment()
    {
        ++m_it;
    }

    void decrement()
    {
        --m_it;
    }

    bool equal(bidirectional_iterator other) const
    {
        return m_it == other.m_it;
    }

    reference dereference() const
    {
        return *m_it;
    }

    iterator_t m_it;
};

template<typename SinglePassRange>
boost::iterator_range<single_pass_iterator>
single_pass_range(const SinglePassRange& rng)
{
    return boost::iterator_range<single_pass_iterator>(
                single_pass_iterator(boost::begin(rng)),
                single_pass_iterator(boost::end(rng)));
}

template<typename BidirectionalRange>
boost::iterator_range<bidirectional_iterator>
bidirectional_range(const BidirectionalRange& rng)
{
    return boost::iterator_range<bidirectional_iterator>(
                bidirectional_iterator(boost::begin(rng)),
                bidirectional_iterator(boost::end(rng)));
}

void test_drop_front()
{
    std::vector<boost::int32_t> v;
    std::vector<boost::int32_t> ref_output;

    for (boost::int32_t i = 0; i < 10; ++i)
    {
        v.push_back(i);
        ref_output.push_back(i);
    }

    boost::iterator_range<single_pass_iterator> rng = single_pass_range(v);

    BOOST_CHECK_EQUAL_COLLECTIONS(rng.begin(), rng.end(),
                                  ref_output.begin(), ref_output.end());

    rng.drop_front();

    ref_output.erase(ref_output.begin());

    BOOST_CHECK_EQUAL_COLLECTIONS(rng.begin(), rng.end(),
                                  ref_output.begin(), ref_output.end());

    rng.drop_front(5);

    ref_output.erase(ref_output.begin(), ref_output.begin() + 5);

    BOOST_CHECK_EQUAL_COLLECTIONS(rng.begin(), rng.end(),
                                  ref_output.begin(), ref_output.end());
}

void test_drop_back()
{
    std::vector<boost::int32_t> v;
    std::vector<boost::int32_t> ref_output;

    for (boost::int32_t i = 0; i < 10; ++i)
    {
        v.push_back(i);
        ref_output.push_back(i);
    }

    boost::iterator_range<bidirectional_iterator> rng = bidirectional_range(v);

    BOOST_CHECK_EQUAL_COLLECTIONS(rng.begin(), rng.end(),
                                  ref_output.begin(), ref_output.end());

    rng.drop_back();

    ref_output.pop_back();

    BOOST_CHECK_EQUAL_COLLECTIONS(rng.begin(), rng.end(),
                                  ref_output.begin(), ref_output.end());

    rng.drop_back(5);

    ref_output.erase(ref_output.end() - 5, ref_output.end());

    BOOST_CHECK_EQUAL_COLLECTIONS(rng.begin(), rng.end(),
                                  ref_output.begin(), ref_output.end());
}

    } // anonymous namespace
} // namespace boost_range_test

boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    boost::unit_test::test_suite* test =
        BOOST_TEST_SUITE("Boost.Range iterator_range drop functions");

    test->add(BOOST_TEST_CASE(&boost_range_test::test_drop_front));
    test->add(BOOST_TEST_CASE(&boost_range_test::test_drop_back));

    return test;
}