]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright 2010 (c) Dean Michael Berris |
2 | // Distributed under the Boost Software License Version 1.0. | |
3 | // (See accompanying file LICENSE_1_0.txt or copy at | |
4 | // http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #include <cassert> | |
7 | #include <cstddef> | |
8 | ||
9 | #include <algorithm> | |
10 | #include <iostream> | |
11 | #include <iterator> | |
12 | #include <vector> | |
13 | ||
14 | #include <boost/iterator/function_input_iterator.hpp> | |
15 | ||
16 | namespace { | |
17 | ||
18 | struct ones { | |
19 | typedef int result_type; | |
20 | result_type operator() () { | |
21 | return 1; | |
22 | } | |
23 | }; | |
24 | ||
25 | int ones_function () { | |
26 | return 1; | |
27 | } | |
28 | ||
29 | struct counter { | |
30 | typedef int result_type; | |
31 | int n; | |
32 | explicit counter(int n_) : n(n_) { } | |
33 | result_type operator() () { | |
34 | return n++; | |
35 | } | |
36 | }; | |
37 | ||
38 | } // namespace | |
39 | ||
40 | using namespace std; | |
41 | ||
42 | int main(int argc, char * argv[]) | |
43 | { | |
44 | // test the iterator with function objects | |
45 | ones ones_generator; | |
46 | vector<int> values(10); | |
47 | generate(values.begin(), values.end(), ones()); | |
48 | ||
49 | vector<int> generated; | |
50 | copy( | |
51 | boost::make_function_input_iterator(ones_generator, 0), | |
52 | boost::make_function_input_iterator(ones_generator, 10), | |
53 | back_inserter(generated) | |
54 | ); | |
55 | ||
56 | assert(values.size() == generated.size()); | |
57 | assert(equal(values.begin(), values.end(), generated.begin())); | |
58 | cout << "function iterator test with function objects successful." << endl; | |
59 | ||
60 | // test the iterator with normal functions | |
61 | vector<int>().swap(generated); | |
62 | copy( | |
63 | boost::make_function_input_iterator(&ones_function, 0), | |
64 | boost::make_function_input_iterator(&ones_function, 10), | |
65 | back_inserter(generated) | |
66 | ); | |
67 | ||
68 | assert(values.size() == generated.size()); | |
69 | assert(equal(values.begin(), values.end(), generated.begin())); | |
70 | cout << "function iterator test with pointer to function successful." << endl; | |
71 | ||
72 | // test the iterator with a reference to a function | |
73 | vector<int>().swap(generated); | |
74 | copy( | |
75 | boost::make_function_input_iterator(ones_function, 0), | |
76 | boost::make_function_input_iterator(ones_function, 10), | |
77 | back_inserter(generated) | |
78 | ); | |
79 | ||
80 | assert(values.size() == generated.size()); | |
81 | assert(equal(values.begin(), values.end(), generated.begin())); | |
82 | cout << "function iterator test with reference to function successful." << endl; | |
83 | ||
84 | // test the iterator with a stateful function object | |
85 | counter counter_generator(42); | |
86 | vector<int>().swap(generated); | |
87 | copy( | |
88 | boost::make_function_input_iterator(counter_generator, 0), | |
89 | boost::make_function_input_iterator(counter_generator, 10), | |
90 | back_inserter(generated) | |
91 | ); | |
92 | ||
93 | assert(generated.size() == 10); | |
94 | assert(counter_generator.n == 42 + 10); | |
95 | for(std::size_t i = 0; i != 10; ++i) | |
96 | assert(generated[i] == 42 + i); | |
97 | cout << "function iterator test with stateful function object successful." << endl; | |
98 | ||
99 | return 0; | |
100 | } | |
101 |