summaryrefslogtreecommitdiff
path: root/libs/utility/generator_iterator_test.cpp
blob: 1c7a1104071c9c53818f2eca5d3ec8f6def97bbb (plain)
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
//
// Copyright 2014 Peter Dimov
//
// Distributed under 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
//

#include <boost/generator_iterator.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <algorithm>

class X
{
private:

    int v;

public:

    typedef int result_type;

    X(): v( 0 )
    {
    }

    int operator()()
    {
        return ++v;
    }
};

template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n( InputIterator first, Size n, OutputIterator result )
{
    while( n-- > 0 )
    {
        *result++ = *first++;
    }

    return result;
}

void copy_test()
{
    X x;
    boost::generator_iterator<X> in( &x );

    int const N = 4;
    int v[ N ] = { 0 };

    ::copy_n( in, 4, v );

    BOOST_TEST_EQ( v[0], 1 );
    BOOST_TEST_EQ( v[1], 2 );
    BOOST_TEST_EQ( v[2], 3 );
    BOOST_TEST_EQ( v[3], 4 );
}

int main()
{
    copy_test();
    return boost::report_errors();
}