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/adaptor/replaced_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/range/algorithm_ext.hpp>
18 
19 #include <algorithm>
20 #include <list>
21 #include <set>
22 #include <vector>
23 
24 namespace boost
25 {
26     namespace
27     {
28         struct if_value_is_one
29         {
operator ()boost::__anonad3a3b870111::if_value_is_one30             bool operator()(int x) const { return x == 1; }
31         };
32 
33         template< class Container >
replaced_if_test_impl(Container & c)34         void replaced_if_test_impl( Container& c )
35         {
36             using namespace boost::adaptors;
37 
38             if_value_is_one pred;
39 
40             const int replacement_value = 0;
41 
42             std::vector< int > test_result1;
43             boost::push_back(test_result1, c | replaced_if(pred, replacement_value));
44 
45             std::vector< int > test_result2;
46             boost::push_back(test_result2, boost::adaptors::replace_if(c, pred, replacement_value));
47 
48             std::vector< int > reference( c.begin(), c.end() );
49             std::replace_if(reference.begin(), reference.end(), pred, replacement_value);
50 
51             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
52                                            test_result1.begin(), test_result1.end() );
53 
54             BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
55                                            test_result2.begin(), test_result2.end() );
56         }
57 
58         template< class Container >
replaced_if_test_impl()59         void replaced_if_test_impl()
60         {
61             using namespace boost::assign;
62 
63             Container c;
64 
65             // Test empty
66             replaced_if_test_impl(c);
67 
68             // Test one
69             c += 1;
70             replaced_if_test_impl(c);
71 
72             // Test many
73             c += 1,1,1,2,2,2,3,3,3,3,3,4,5,6,6,6,7,8,9;
74             replaced_if_test_impl(c);
75         }
76 
replaced_if_test()77         void replaced_if_test()
78         {
79             replaced_if_test_impl< std::vector< int > >();
80             replaced_if_test_impl< std::list< int > >();
81             replaced_if_test_impl< std::set< int > >();
82             replaced_if_test_impl< std::multiset< int > >();
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.adaptor.replaced_if" );
92 
93     test->add( BOOST_TEST_CASE( &boost::replaced_if_test ) );
94 
95     return test;
96 }
97