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)
14 #include <boost/config.hpp>
15 #include <boost/iterator/function_input_iterator.hpp>
20 typedef int result_type;
21 result_type operator() () {
26 int ones_function () {
31 typedef int result_type;
33 explicit counter(int n_) : n(n_) { }
34 result_type operator() () {
43 int main(int argc, char * argv[])
45 // test the iterator with function objects
47 vector<int> values(10);
48 generate(values.begin(), values.end(), ones());
50 vector<int> generated;
52 boost::make_function_input_iterator(ones_generator, 0),
53 boost::make_function_input_iterator(ones_generator, 10),
54 back_inserter(generated)
57 assert(values.size() == generated.size());
58 assert(equal(values.begin(), values.end(), generated.begin()));
59 cout << "function iterator test with function objects successful." << endl;
61 // test the iterator with normal functions
62 vector<int>().swap(generated);
64 boost::make_function_input_iterator(&ones_function, 0),
65 boost::make_function_input_iterator(&ones_function, 10),
66 back_inserter(generated)
69 assert(values.size() == generated.size());
70 assert(equal(values.begin(), values.end(), generated.begin()));
71 cout << "function iterator test with pointer to function successful." << endl;
73 // test the iterator with a reference to a function
74 vector<int>().swap(generated);
76 boost::make_function_input_iterator(ones_function, 0),
77 boost::make_function_input_iterator(ones_function, 10),
78 back_inserter(generated)
81 assert(values.size() == generated.size());
82 assert(equal(values.begin(), values.end(), generated.begin()));
83 cout << "function iterator test with reference to function successful." << endl;
85 // test the iterator with a stateful function object
86 counter counter_generator(42);
87 vector<int>().swap(generated);
89 boost::make_function_input_iterator(counter_generator, 0),
90 boost::make_function_input_iterator(counter_generator, 10),
91 back_inserter(generated)
94 assert(generated.size() == 10);
95 assert(counter_generator.n == 42 + 10);
96 for(std::size_t i = 0; i != 10; ++i)
97 assert(generated[i] == 42 + i);
98 cout << "function iterator test with stateful function object successful." << endl;
100 #if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
101 // test the iterator with lambda expressions
103 auto lambda_generator = [&num] { return num++; };
104 vector<int>().swap(generated);
106 boost::make_function_input_iterator(lambda_generator, 0),
107 boost::make_function_input_iterator(lambda_generator, 10),
108 back_inserter(generated)
111 assert(generated.size() == 10);
112 for(std::size_t i = 0; i != 10; ++i)
113 assert(generated[i] == 42 + i);
114 cout << "function iterator test with lambda expressions successful." << endl;
115 #endif // BOOST_NO_CXX11_LAMBDAS