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