]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iterator/test/function_input_iterator_test.cpp
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)
12 #include <boost/config.hpp>
14 #if !defined(BOOST_NO_CXX11_DECLTYPE)
15 // Force boost::result_of use decltype, even on compilers that don't support N3276.
16 // This enables this test to also verify if the iterator works with lambdas
17 // on such compilers with this config macro. Note that without the macro result_of
18 // (and consequently the iterator) is guaranteed to _not_ work, so this case is not
19 // worth testing anyway.
20 #define BOOST_RESULT_OF_USE_DECLTYPE
23 #include <boost/core/lightweight_test.hpp>
24 #include <boost/iterator/function_input_iterator.hpp>
29 typedef int result_type
;
30 result_type
operator() () {
35 int ones_function () {
40 typedef int result_type
;
42 explicit counter(int n_
) : n(n_
) { }
43 result_type
operator() () {
54 // test the iterator with function objects
56 vector
<int> values(10);
57 generate(values
.begin(), values
.end(), ones());
59 vector
<int> generated
;
61 boost::make_function_input_iterator(ones_generator
, 0),
62 boost::make_function_input_iterator(ones_generator
, 10),
63 back_inserter(generated
)
66 BOOST_TEST_ALL_EQ(values
.begin(), values
.end(), generated
.begin(), generated
.end());
68 // test the iterator with normal functions
69 vector
<int>().swap(generated
);
71 boost::make_function_input_iterator(&ones_function
, 0),
72 boost::make_function_input_iterator(&ones_function
, 10),
73 back_inserter(generated
)
76 BOOST_TEST_ALL_EQ(values
.begin(), values
.end(), generated
.begin(), generated
.end());
78 // test the iterator with a reference to a function
79 vector
<int>().swap(generated
);
81 boost::make_function_input_iterator(ones_function
, 0),
82 boost::make_function_input_iterator(ones_function
, 10),
83 back_inserter(generated
)
86 BOOST_TEST_ALL_EQ(values
.begin(), values
.end(), generated
.begin(), generated
.end());
88 // test the iterator with a stateful function object
89 counter
counter_generator(42);
90 vector
<int>().swap(generated
);
92 boost::make_function_input_iterator(counter_generator
, 0),
93 boost::make_function_input_iterator(counter_generator
, 10),
94 back_inserter(generated
)
97 BOOST_TEST_EQ(generated
.size(), 10u);
98 BOOST_TEST_EQ(counter_generator
.n
, 42 + 10);
99 for(std::size_t i
= 0; i
!= 10; ++i
)
100 BOOST_TEST_EQ(generated
[i
], static_cast<int>(42 + i
));
102 #if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) \
103 && defined(BOOST_RESULT_OF_USE_DECLTYPE)
104 // test the iterator with lambda expressions
106 auto lambda_generator
= [&num
] { return num
++; };
107 vector
<int>().swap(generated
);
109 boost::make_function_input_iterator(lambda_generator
, 0),
110 boost::make_function_input_iterator(lambda_generator
, 10),
111 back_inserter(generated
)
114 BOOST_TEST_EQ(generated
.size(), 10u);
115 for(std::size_t i
= 0; i
!= 10; ++i
)
116 BOOST_TEST_EQ(generated
[i
], static_cast<int>(42 + i
));
117 #endif // BOOST_NO_CXX11_LAMBDAS
119 return boost::report_errors();