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.hpp> |
12 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/assign.hpp> |
17 | #include <algorithm> |
18 | #include <functional> |
19 | #include <list> |
20 | #include <numeric> |
21 | #include <deque> |
22 | #include <vector> |
23 | |
24 | namespace |
25 | { |
26 | template<typename Iterator, typename Value> |
27 | void test_append(Iterator target, Value value) |
28 | { |
29 | *target++ = value; |
30 | } |
31 | |
32 | template< class Container, class Value > |
33 | void test_replace_copy_impl( const Container& c, Value to_replace ) |
34 | { |
35 | const Value replace_with = to_replace * 2; |
36 | |
37 | typedef typename boost::range_value<Container>::type value_type; |
38 | std::vector<value_type> reference; |
39 | |
40 | typedef BOOST_DEDUCED_TYPENAME std::vector<value_type>::iterator |
41 | iterator_t BOOST_RANGE_UNUSED; |
42 | |
43 | test_append( |
44 | std::replace_copy(c.begin(), c.end(), |
45 | std::back_inserter(reference), to_replace, replace_with), |
46 | to_replace); |
47 | |
48 | std::vector<value_type> test; |
49 | test_append( |
50 | boost::replace_copy(c, std::back_inserter(test), to_replace, replace_with), |
51 | to_replace); |
52 | |
53 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
54 | test.begin(), test.end() ); |
55 | |
56 | std::vector<value_type> test2; |
57 | test_append( |
58 | boost::replace_copy(boost::make_iterator_range(c), |
59 | std::back_inserter(test2), to_replace, |
60 | replace_with), |
61 | to_replace); |
62 | |
63 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
64 | test2.begin(), test2.end() ); |
65 | } |
66 | |
67 | template< class Container > |
68 | void test_replace_copy_impl() |
69 | { |
70 | using namespace boost::assign; |
71 | |
72 | Container cont; |
73 | test_replace_copy_impl(cont, 0); |
74 | |
75 | cont.clear(); |
76 | cont += 1; |
77 | test_replace_copy_impl(cont, 0); |
78 | test_replace_copy_impl(cont, 1); |
79 | |
80 | cont.clear(); |
81 | cont += 1,1,1,1,1; |
82 | test_replace_copy_impl(cont, 0); |
83 | test_replace_copy_impl(cont, 1); |
84 | |
85 | cont.clear(); |
86 | cont += 1,2,3,4,5,6,7,8,9; |
87 | test_replace_copy_impl(cont, 1); |
88 | test_replace_copy_impl(cont, 9); |
89 | test_replace_copy_impl(cont, 4); |
90 | } |
91 | |
92 | void test_replace_copy() |
93 | { |
94 | test_replace_copy_impl< std::vector<int> >(); |
95 | test_replace_copy_impl< std::list<int> >(); |
96 | test_replace_copy_impl< std::deque<int> >(); |
97 | } |
98 | } |
99 | |
100 | boost::unit_test::test_suite* |
101 | init_unit_test_suite(int argc, char* argv[]) |
102 | { |
103 | boost::unit_test::test_suite* test |
104 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.replace_copy" ); |
105 | |
106 | test->add( BOOST_TEST_CASE( &test_replace_copy ) ); |
107 | |
108 | return test; |
109 | } |
110 | |
111 | |