123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- // Boost.Range library
- //
- // Copyright Neil Groves 2010. Use, modification and
- // distribution is subject to 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)
- //
- //
- // For more information, see http://www.boost.org/libs/range/
- //
- #include <boost/range/algorithm_ext/erase.hpp>
- #include <boost/test/test_tools.hpp>
- #include <boost/test/unit_test.hpp>
- #include <boost/range/iterator.hpp>
- #include <algorithm>
- #include <list>
- #include <vector>
- namespace
- {
- template< class Container >
- void test_erase_impl()
- {
- Container source;
- for (int i = 0; i < 10; ++i)
- source.push_back(i);
- Container reference(source);
- Container test(source);
- typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
- iterator_t first_ref = reference.begin();
- iterator_t last_ref = reference.end();
- boost::iterator_range< iterator_t > test_range(test.begin(), test.end());
- reference.erase(first_ref, last_ref);
- boost::erase(test, test_range);
- BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
- test.begin(), test.end() );
- }
- void test_erase()
- {
- test_erase_impl<std::vector<int> >();
- test_erase_impl<std::list<int> >();
- }
- template< class Container >
- void test_remove_erase_impl()
- {
- Container source;
- for (int i = 0; i < 10; ++i)
- source.push_back(i);
- Container reference(source);
- Container test(source);
- boost::remove_erase(test, 5);
- reference.erase( std::find(reference.begin(), reference.end(), 5) );
- BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
- test.begin(), test.end() );
- }
- void test_remove_erase()
- {
- test_remove_erase_impl<std::vector<int> >();
- test_remove_erase_impl<std::list<int> >();
- }
- struct is_even
- {
- typedef bool result_type;
- typedef int argument_type;
- bool operator()(int x) const { return x % 2 == 0; }
- };
- template< class Container >
- void test_remove_erase_if_impl()
- {
- Container source;
- for (int i = 0; i < 10; ++i)
- source.push_back(i);
- Container reference;
- typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iterator_t;
- iterator_t last_source = source.end();
- is_even pred;
- for (iterator_t it_source = source.begin(); it_source != last_source; ++it_source)
- {
- if (!pred(*it_source))
- reference.push_back(*it_source);
- }
- Container test(source);
- boost::remove_erase_if(test, is_even());
- BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
- test.begin(), test.end() );
- }
- void test_remove_erase_if()
- {
- test_remove_erase_if_impl<std::vector<int> >();
- test_remove_erase_if_impl<std::list<int> >();
- }
- }
- boost::unit_test::test_suite*
- init_unit_test_suite(int argc, char* argv[])
- {
- boost::unit_test::test_suite* test
- = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.erase" );
- test->add( BOOST_TEST_CASE( &test_erase ) );
- test->add( BOOST_TEST_CASE( &test_remove_erase ) );
- test->add( BOOST_TEST_CASE( &test_remove_erase_if ) );
- return test;
- }
|