diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 14b7b2a..96e440b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -50,6 +50,8 @@ test-suite iterator [ run permutation_iterator_test.cpp : : : # on ] [ run function_input_iterator_test.cpp ] + [ run function_output_iterator_test.cpp ] + [ compile-fail function_output_iterator_cf.cpp ] [ run generator_iterator_test.cpp ] diff --git a/test/function_output_iterator_cf.cpp b/test/function_output_iterator_cf.cpp new file mode 100644 index 0000000..a3f1d9c --- /dev/null +++ b/test/function_output_iterator_cf.cpp @@ -0,0 +1,37 @@ +// Copyright 2022 (c) Andrey Semashev +// 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 + +namespace { + +struct sum_func +{ + typedef void result_type; + + explicit sum_func(int& n) : m_n(n) {} + result_type operator() (int x) const + { + m_n += x; + } + +private: + int& m_n; +}; + +} // namespace + +int main() +{ + int n = 0; + boost::iterators::function_output_iterator< sum_func > it1 = + boost::iterators::make_function_output_iterator(sum_func(n)); + boost::iterators::function_output_iterator< sum_func > it2 = + boost::iterators::make_function_output_iterator(sum_func(n)); + + *it1 = *it2; + + return 0; +} diff --git a/test/function_output_iterator_test.cpp b/test/function_output_iterator_test.cpp new file mode 100644 index 0000000..953a443 --- /dev/null +++ b/test/function_output_iterator_test.cpp @@ -0,0 +1,61 @@ +// Copyright 2022 (c) Andrey Semashev +// 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 + +#include +#include + +namespace { + +struct sum_func +{ + typedef void result_type; + + explicit sum_func(int& n) : m_n(n) {} + result_type operator() (int x) const + { + m_n += x; + } + +private: + int& m_n; +}; + +} // namespace + +int main() +{ + { + int n = 0; + boost::iterators::function_output_iterator< sum_func > it = + boost::iterators::make_function_output_iterator(sum_func(n)); + + *it = 1; + ++it; + *it = 2; + ++it; + *it = 3; + + BOOST_TEST_EQ(n, 6); + } + +#if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) + { + int n = 0; + auto it = boost::iterators::make_function_output_iterator([&n](int x) { n -= x; }); + + *it = 1; + ++it; + *it = 2; + ++it; + *it = 3; + + BOOST_TEST_EQ(n, -6); + } +#endif + + return boost::report_errors(); +}