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_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.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 >
test_replace_if_impl(Container & cont,UnaryPredicate pred)30         void test_replace_if_impl(Container& cont, UnaryPredicate pred)
31         {
32             const int what = 2;
33             const int with_what = 5;
34 
35             std::vector<int> reference(cont.begin(), cont.end());
36             std::replace_if(reference.begin(), reference.end(),
37                 boost::bind(pred, _1, what), with_what);
38 
39             std::vector<int> target(cont.begin(), cont.end());
40             boost::replace_if(target, boost::bind(pred, _1, what), with_what);
41 
42             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
43                                            target.begin(), target.end() );
44 
45             std::vector<int> target2(cont.begin(), cont.end());
46             boost::replace_if(boost::make_iterator_range(target2),
47                               boost::bind(pred, _1, what), with_what);
48 
49             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
50                                            target2.begin(), target2.end() );
51         }
52 
53         template< class Container >
test_replace_if_impl(Container & cont)54         void test_replace_if_impl(Container& cont)
55         {
56             test_replace_if_impl(cont, std::equal_to<int>());
57             test_replace_if_impl(cont, std::not_equal_to<int>());
58         }
59 
60         template< class Container >
test_replace_if_impl()61         void test_replace_if_impl()
62         {
63             using namespace boost::assign;
64 
65             Container cont;
66             test_replace_if_impl(cont);
67 
68             cont.clear();
69             cont += 1;
70             test_replace_if_impl(cont);
71 
72             cont.clear();
73             cont += 1,2,3,4,5,6,7,8,9;
74             test_replace_if_impl(cont);
75         }
76 
test_replace_if()77         void test_replace_if()
78         {
79             test_replace_if_impl< std::vector<int> >();
80             test_replace_if_impl< std::list<int> >();
81             test_replace_if_impl< std::deque<int> >();
82         }
83     }
84 }
85 
86 
87 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])88 init_unit_test_suite(int argc, char* argv[])
89 {
90     boost::unit_test::test_suite* test
91         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.replace_if" );
92 
93     test->add( BOOST_TEST_CASE( &boost::test_replace_if ) );
94 
95     return test;
96 }
97