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_copy_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 |
26 | { |
27 | template< class Iterator, class Value > |
28 | void test_append(Iterator target, Value value) |
29 | { |
30 | *target++ = value; |
31 | } |
32 | |
33 | template< class Container, class UnaryPredicate > |
34 | void test_remove_copy_if_impl( const Container& c, UnaryPredicate pred ) |
35 | { |
36 | typedef BOOST_DEDUCED_TYPENAME boost::range_value<const Container>::type value_type; |
37 | std::vector<value_type> reference; |
38 | |
39 | test_append( |
40 | std::remove_copy_if(c.begin(), c.end(), std::back_inserter(reference), pred), |
41 | value_type() |
42 | ); |
43 | |
44 | std::vector<value_type> test; |
45 | test_append( |
46 | boost::remove_copy_if(c, std::back_inserter(test), pred), |
47 | value_type() |
48 | ); |
49 | |
50 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
51 | test.begin(), test.end() ); |
52 | |
53 | std::vector<value_type> test2; |
54 | test_append( |
55 | boost::remove_copy_if(boost::make_iterator_range(c), |
56 | std::back_inserter(test2), pred), |
57 | value_type() |
58 | ); |
59 | |
60 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
61 | test2.begin(), test2.end() ); |
62 | } |
63 | |
64 | template< class Container > |
65 | void test_remove_copy_if_( const Container& c, int to_remove ) |
66 | { |
67 | using namespace boost::placeholders; |
68 | |
69 | test_remove_copy_if_impl(c, boost::bind(f: std::equal_to<int>(), a: _1, a: to_remove)); |
70 | test_remove_copy_if_impl(c, boost::bind(f: std::not_equal_to<int>(), a: _1, a: to_remove)); |
71 | } |
72 | |
73 | template< class Container > |
74 | void test_remove_copy_if_impl() |
75 | { |
76 | using namespace boost::assign; |
77 | |
78 | Container cont; |
79 | test_remove_copy_if_(cont, 0); |
80 | |
81 | cont.clear(); |
82 | cont += 1; |
83 | test_remove_copy_if_(cont, 0); |
84 | test_remove_copy_if_(cont, 1); |
85 | |
86 | cont.clear(); |
87 | cont += 1,1,1,1,1; |
88 | test_remove_copy_if_(cont, 0); |
89 | test_remove_copy_if_(cont, 1); |
90 | |
91 | cont.clear(); |
92 | cont += 1,2,3,4,5,6,7,8,9; |
93 | test_remove_copy_if_(cont, 1); |
94 | test_remove_copy_if_(cont, 9); |
95 | test_remove_copy_if_(cont, 4); |
96 | } |
97 | |
98 | inline void test_remove_copy_if() |
99 | { |
100 | test_remove_copy_if_impl< std::vector<int> >(); |
101 | test_remove_copy_if_impl< std::list<int> >(); |
102 | test_remove_copy_if_impl< std::deque<int> >(); |
103 | } |
104 | } |
105 | |
106 | boost::unit_test::test_suite* |
107 | init_unit_test_suite(int argc, char* argv[]) |
108 | { |
109 | boost::unit_test::test_suite* test |
110 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.remove_copy_if" ); |
111 | |
112 | test->add( BOOST_TEST_CASE( &test_remove_copy_if ) ); |
113 | |
114 | return test; |
115 | } |
116 | |