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 > |
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 | |
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 > |
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 | |
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; |
81 | bool operator()(int x) const { return x % 2 == 0; } |
82 | }; |
83 | |
84 | template< class Container > |
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 | |
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 | template< class Container > |
116 | void test_unique_erase_impl() |
117 | { |
118 | Container source; |
119 | source.push_back(1); |
120 | source.push_back(1); |
121 | source.push_back(1); |
122 | source.push_back(2); |
123 | source.push_back(3); |
124 | source.push_back(3); |
125 | |
126 | Container reference; |
127 | reference.push_back(1); |
128 | reference.push_back(2); |
129 | reference.push_back(3); |
130 | |
131 | boost::unique_erase(source); |
132 | |
133 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
134 | source.begin(), source.end() ); |
135 | } |
136 | |
137 | void test_unique_erase() |
138 | { |
139 | test_unique_erase_impl<std::vector<int> >(); |
140 | test_unique_erase_impl<std::list<int> >(); |
141 | } |
142 | |
143 | struct distance_smaller_2 |
144 | { |
145 | typedef bool result_type; |
146 | typedef int argument_type; |
147 | bool operator()(int x, int y) const { return std::abs(x: x - y) < 2; } |
148 | }; |
149 | |
150 | template< class Container > |
151 | void test_unique_erase_pred_impl() |
152 | { |
153 | Container source; |
154 | for (int i = 0; i < 10; ++i) |
155 | source.push_back(i); |
156 | |
157 | Container reference; |
158 | for (int i = 0; i < 10; i += 2) |
159 | reference.push_back(i); |
160 | |
161 | |
162 | boost::unique_erase(source, distance_smaller_2()); |
163 | |
164 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
165 | source.begin(), source.end() ); |
166 | } |
167 | |
168 | void test_unique_erase_pred() |
169 | { |
170 | test_unique_erase_pred_impl<std::vector<int> >(); |
171 | test_unique_erase_pred_impl<std::list<int> >(); |
172 | } |
173 | } |
174 | |
175 | boost::unit_test::test_suite* |
176 | init_unit_test_suite(int argc, char* argv[]) |
177 | { |
178 | boost::unit_test::test_suite* test |
179 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.erase" ); |
180 | |
181 | test->add( BOOST_TEST_CASE( &test_erase ) ); |
182 | test->add( BOOST_TEST_CASE( &test_remove_erase ) ); |
183 | test->add( BOOST_TEST_CASE( &test_remove_erase_if ) ); |
184 | test->add( BOOST_TEST_CASE( &test_unique_erase ) ); |
185 | test->add( BOOST_TEST_CASE( &test_unique_erase_pred ) ); |
186 | |
187 | return test; |
188 | } |
189 | |