Added tests for function_output_iterator.

This commit is contained in:
Andrey Semashev 2022-07-01 00:14:14 +03:00
parent 1a80896934
commit 129245a985
3 changed files with 100 additions and 0 deletions

View File

@ -50,6 +50,8 @@ test-suite iterator
[ run permutation_iterator_test.cpp : : : # <stlport-iostream>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 ]

View File

@ -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 <boost/iterator/function_output_iterator.hpp>
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;
}

View File

@ -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 <boost/config.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/iterator/function_output_iterator.hpp>
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();
}