1 // Boost.Range library
2 //
3 // Copyright Neil Groves 2010. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 //
9 // For more information, see http://www.boost.org/libs/range/
10 //
11 #include <boost/range/algorithm_ext/erase.hpp>
12
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15
16 #include <boost/range/iterator.hpp>
17 #include <algorithm>
18 #include <list>
19 #include <vector>
20
21 namespace
22 {
23 template< class Container >
test_erase_impl()24 void test_erase_impl()
25 {
26 Container source;
27 for (int i = 0; i < 10; ++i)
28 source.push_back(i);
29
30 Container reference(source);
31 Container test(source);
32
33 typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
34
35 iterator_t first_ref = reference.begin();
36 iterator_t last_ref = reference.end();
37
38 boost::iterator_range< iterator_t > test_range(test.begin(), test.end());
39
40 reference.erase(first_ref, last_ref);
41 boost::erase(test, test_range);
42
43 BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
44 test.begin(), test.end() );
45 }
46
test_erase()47 void test_erase()
48 {
49 test_erase_impl<std::vector<int> >();
50 test_erase_impl<std::list<int> >();
51 }
52
53 template< class Container >
test_remove_erase_impl()54 void test_remove_erase_impl()
55 {
56 Container source;
57 for (int i = 0; i < 10; ++i)
58 source.push_back(i);
59
60 Container reference(source);
61 Container test(source);
62
63 boost::remove_erase(test, 5);
64
65 reference.erase( std::find(reference.begin(), reference.end(), 5) );
66
67 BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
68 test.begin(), test.end() );
69 }
70
test_remove_erase()71 void test_remove_erase()
72 {
73 test_remove_erase_impl<std::vector<int> >();
74 test_remove_erase_impl<std::list<int> >();
75 }
76
77 struct is_even
78 {
79 typedef bool result_type;
80 typedef int argument_type;
operator ()__anondb0b9c940111::is_even81 bool operator()(int x) const { return x % 2 == 0; }
82 };
83
84 template< class Container >
test_remove_erase_if_impl()85 void test_remove_erase_if_impl()
86 {
87 Container source;
88 for (int i = 0; i < 10; ++i)
89 source.push_back(i);
90
91 Container reference;
92 typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iterator_t;
93 iterator_t last_source = source.end();
94 is_even pred;
95 for (iterator_t it_source = source.begin(); it_source != last_source; ++it_source)
96 {
97 if (!pred(*it_source))
98 reference.push_back(*it_source);
99 }
100
101 Container test(source);
102 boost::remove_erase_if(test, is_even());
103
104 BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
105 test.begin(), test.end() );
106
107 }
108
test_remove_erase_if()109 void test_remove_erase_if()
110 {
111 test_remove_erase_if_impl<std::vector<int> >();
112 test_remove_erase_if_impl<std::list<int> >();
113 }
114
115 }
116
117 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])118 init_unit_test_suite(int argc, char* argv[])
119 {
120 boost::unit_test::test_suite* test
121 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.erase" );
122
123 test->add( BOOST_TEST_CASE( &test_erase ) );
124 test->add( BOOST_TEST_CASE( &test_remove_erase ) );
125 test->add( BOOST_TEST_CASE( &test_remove_erase_if ) );
126
127 return test;
128 }
129