mirror of
https://github.com/boostorg/iterator.git
synced 2025-05-09 23:23:54 +00:00
Added tests for function_output_iterator.
This commit is contained in:
parent
1a80896934
commit
129245a985
@ -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 ]
|
||||
|
||||
|
37
test/function_output_iterator_cf.cpp
Normal file
37
test/function_output_iterator_cf.cpp
Normal 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;
|
||||
}
|
61
test/function_output_iterator_test.cpp
Normal file
61
test/function_output_iterator_test.cpp
Normal 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();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user