1 | // Boost.Range library |
2 | // |
3 | // Copyright Neil Groves 2009. 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/remove_if.hpp> |
12 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/assign.hpp> |
17 | #include <boost/bind/bind.hpp> |
18 | #include <algorithm> |
19 | #include <functional> |
20 | #include <list> |
21 | #include <numeric> |
22 | #include <deque> |
23 | #include <vector> |
24 | |
25 | namespace boost |
26 | { |
27 | namespace |
28 | { |
29 | template< class Container, class UnaryPredicate > |
30 | void test_remove_if_impl( const Container& c, UnaryPredicate pred ) |
31 | { |
32 | Container reference(c); |
33 | |
34 | typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t; |
35 | |
36 | iterator_t reference_it |
37 | = std::remove_if(reference.begin(), reference.end(), pred); |
38 | |
39 | Container test(c); |
40 | iterator_t test_it = boost::remove_if(test, pred); |
41 | |
42 | BOOST_CHECK_EQUAL( std::distance(test.begin(), test_it), |
43 | std::distance(reference.begin(), reference_it) ); |
44 | |
45 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
46 | test.begin(), test.end() ); |
47 | |
48 | Container test2(c); |
49 | iterator_t test_it2 = boost::remove_if( |
50 | boost::make_iterator_range(test2), pred); |
51 | |
52 | BOOST_CHECK_EQUAL( std::distance(test2.begin(), test_it2), |
53 | std::distance(reference.begin(), reference_it) ); |
54 | |
55 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
56 | test2.begin(), test2.end() ); |
57 | } |
58 | |
59 | template< class Container > |
60 | void test_remove_if_( const Container& c, int to_remove ) |
61 | { |
62 | using namespace boost::placeholders; |
63 | |
64 | test_remove_if_impl(c, boost::bind(f: std::equal_to<int>(), a: _1, a: to_remove)); |
65 | test_remove_if_impl(c, boost::bind(f: std::not_equal_to<int>(), a: _1, a: to_remove)); |
66 | } |
67 | |
68 | template< class Container > |
69 | void test_remove_if_impl() |
70 | { |
71 | using namespace boost::assign; |
72 | |
73 | Container cont; |
74 | test_remove_if_(cont, 0); |
75 | |
76 | cont.clear(); |
77 | cont += 1; |
78 | test_remove_if_(cont, 0); |
79 | test_remove_if_(cont, 1); |
80 | |
81 | cont.clear(); |
82 | cont += 1,1,1,1,1; |
83 | test_remove_if_(cont, 0); |
84 | test_remove_if_(cont, 1); |
85 | |
86 | cont.clear(); |
87 | cont += 1,2,3,4,5,6,7,8,9; |
88 | test_remove_if_(cont, 1); |
89 | test_remove_if_(cont, 9); |
90 | test_remove_if_(cont, 4); |
91 | } |
92 | |
93 | inline void test_remove_if() |
94 | { |
95 | test_remove_if_impl< std::vector<int> >(); |
96 | test_remove_if_impl< std::list<int> >(); |
97 | test_remove_if_impl< std::deque<int> >(); |
98 | } |
99 | } |
100 | } |
101 | |
102 | boost::unit_test::test_suite* |
103 | init_unit_test_suite(int argc, char* argv[]) |
104 | { |
105 | boost::unit_test::test_suite* test |
106 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.remove_if" ); |
107 | |
108 | test->add( BOOST_TEST_CASE( &boost::test_remove_if ) ); |
109 | |
110 | return test; |
111 | } |
112 | |