File: function_input_iterator_test.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 (129 lines) | stat: -rw-r--r-- 3,974 bytes parent folder | download | duplicates (4)
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
// Copyright 2010 (c) Dean Michael Berris
// 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 <cstddef>

#include <algorithm>
#include <iterator>
#include <vector>

#include <boost/config.hpp>

#if !defined(BOOST_NO_CXX11_DECLTYPE)
// Force boost::result_of use decltype, even on compilers that don't support N3276.
// This enables this test to also verify if the iterator works with lambdas
// on such compilers with this config macro. Note that without the macro result_of
// (and consequently the iterator) is guaranteed to _not_ work, so this case is not
// worth testing anyway.
#define BOOST_RESULT_OF_USE_DECLTYPE
#endif

#include <boost/core/lightweight_test.hpp>
#include <boost/iterator/function_input_iterator.hpp>

namespace {

struct ones {
    typedef int result_type;
    result_type operator() () {
        return 1;
    }
};

int ones_function () {
    return 1;
}

struct counter {
    typedef int result_type;
    int n;
    explicit counter(int n_) : n(n_) { }
    result_type operator() () {
        return n++;
    }
};

} // namespace

using namespace std;

int main()
{
    // test the iterator with function objects
    ones ones_generator;
    vector<int> values(10);
    generate(values.begin(), values.end(), ones());

    vector<int> generated;
    copy(
        boost::make_function_input_iterator(ones_generator, 0),
        boost::make_function_input_iterator(ones_generator, 10),
        back_inserter(generated)
        );

    BOOST_TEST_ALL_EQ(values.begin(), values.end(), generated.begin(), generated.end());

    // test the iterator with normal functions
    vector<int>().swap(generated);
    copy(
        boost::make_function_input_iterator(&ones_function, 0),
        boost::make_function_input_iterator(&ones_function, 10),
        back_inserter(generated)
        );

    BOOST_TEST_ALL_EQ(values.begin(), values.end(), generated.begin(), generated.end());

    // test the iterator with a reference to a function
    vector<int>().swap(generated);
    copy(
        boost::make_function_input_iterator(ones_function, 0),
        boost::make_function_input_iterator(ones_function, 10),
        back_inserter(generated)
        );

    BOOST_TEST_ALL_EQ(values.begin(), values.end(), generated.begin(), generated.end());

    // test the iterator with a stateful function object
    counter counter_generator(42);
    vector<int>().swap(generated);
    copy(
        boost::make_function_input_iterator(counter_generator, 0),
        boost::make_function_input_iterator(counter_generator, 10),
        back_inserter(generated)
        );

    BOOST_TEST_EQ(generated.size(), 10u);
    BOOST_TEST_EQ(counter_generator.n, 42 + 10);
    for(std::size_t i = 0; i != 10; ++i)
        BOOST_TEST_EQ(generated[i], static_cast<int>(42 + i));

    // Test that incrementing the iterator returns a reference to the iterator type
    {
        typedef boost::iterators::function_input_iterator<counter, int> function_counter_iterator_t;
        function_counter_iterator_t it1(counter_generator, 0);
        function_counter_iterator_t it2(++it1);
        function_counter_iterator_t it3(it2++);
        BOOST_TEST_EQ(*it3, 54);
    }

#if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) \
    && defined(BOOST_RESULT_OF_USE_DECLTYPE)
    // test the iterator with lambda expressions
    int num = 42;
    auto lambda_generator = [&num] { return num++; };
    vector<int>().swap(generated);
    copy(
        boost::make_function_input_iterator(lambda_generator, 0),
        boost::make_function_input_iterator(lambda_generator, 10),
        back_inserter(generated)
        );

    BOOST_TEST_EQ(generated.size(), 10u);
    for(std::size_t i = 0; i != 10; ++i)
        BOOST_TEST_EQ(generated[i], static_cast<int>(42 + i));
#endif // BOOST_NO_CXX11_LAMBDAS

    return boost::report_errors();
}