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