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/transform.hpp> |
12 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/assign.hpp> |
17 | #include "../test_function/multiply_by_x.hpp" |
18 | #include <algorithm> |
19 | #include <list> |
20 | #include <set> |
21 | #include <vector> |
22 | |
23 | namespace boost |
24 | { |
25 | namespace |
26 | { |
27 | template< class Container > |
28 | void test_transform_impl1(Container& cont) |
29 | { |
30 | using namespace boost::range_test_function; |
31 | |
32 | const Container& ccont = cont; |
33 | |
34 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; |
35 | |
36 | std::vector<value_t> target(cont.size()); |
37 | std::vector<value_t> reference(cont.size()); |
38 | typedef BOOST_DEDUCED_TYPENAME std::vector<value_t>::iterator iterator_t; |
39 | |
40 | multiply_by_x<int> fn(2); |
41 | |
42 | iterator_t reference_it |
43 | = std::transform(cont.begin(), cont.end(), reference.begin(), fn); |
44 | |
45 | boost::ignore_unused_variable_warning(reference_it); |
46 | |
47 | iterator_t test_it |
48 | = boost::transform(cont, target.begin(), fn); |
49 | |
50 | BOOST_CHECK( test_it == target.end() ); |
51 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
52 | target.begin(), target.end() ); |
53 | |
54 | BOOST_CHECK( test_it == boost::transform(boost::make_iterator_range(cont), target.begin(), fn) ); |
55 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
56 | target.begin(), target.end() ); |
57 | |
58 | target.clear(); |
59 | target.resize(ccont.size()); |
60 | |
61 | test_it = boost::transform(ccont, target.begin(), fn); |
62 | |
63 | BOOST_CHECK( test_it == target.end() ); |
64 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
65 | target.begin(), target.end() ); |
66 | BOOST_CHECK( test_it == boost::transform(boost::make_iterator_range(ccont), target.begin(), fn) ); |
67 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
68 | target.begin(), target.end() ); |
69 | } |
70 | |
71 | template< class Container > |
72 | void test_transform_impl1() |
73 | { |
74 | using namespace boost::assign; |
75 | |
76 | Container cont; |
77 | |
78 | test_transform_impl1(cont); |
79 | |
80 | cont += 1; |
81 | test_transform_impl1(cont); |
82 | |
83 | cont += 2,3,4,5,6,7; |
84 | test_transform_impl1(cont); |
85 | } |
86 | |
87 | template< class Container1, class Container2 > |
88 | void test_transform_impl2(Container1& cont1, Container2& cont2) |
89 | { |
90 | const Container1& ccont1 = cont1; |
91 | const Container2& ccont2 = cont2; |
92 | |
93 | BOOST_CHECK_EQUAL( cont1.size(), cont2.size() ); |
94 | |
95 | typedef BOOST_DEDUCED_TYPENAME Container1::value_type value_t; |
96 | |
97 | std::vector<value_t> target(cont1.size()); |
98 | std::vector<value_t> reference(cont1.size()); |
99 | typedef BOOST_DEDUCED_TYPENAME std::vector<value_t>::iterator iterator_t; |
100 | |
101 | std::multiplies<int> fn; |
102 | |
103 | iterator_t reference_it |
104 | = std::transform(cont1.begin(), cont1.end(), |
105 | cont2.begin(), reference.begin(), fn); |
106 | |
107 | boost::ignore_unused_variable_warning(reference_it); |
108 | |
109 | iterator_t test_it |
110 | = boost::transform(cont1, cont2, target.begin(), fn); |
111 | |
112 | BOOST_CHECK( test_it == target.end() ); |
113 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
114 | target.begin(), target.end() ); |
115 | |
116 | BOOST_CHECK( test_it == boost::transform(boost::make_iterator_range(cont1), cont2, target.begin(), fn) ); |
117 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
118 | target.begin(), target.end() ); |
119 | |
120 | BOOST_CHECK( test_it == boost::transform(cont1, boost::make_iterator_range(cont2), target.begin(), fn) ); |
121 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
122 | target.begin(), target.end() ); |
123 | |
124 | BOOST_CHECK( test_it == boost::transform(boost::make_iterator_range(cont1), |
125 | boost::make_iterator_range(cont2), |
126 | target.begin(), fn) ); |
127 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
128 | target.begin(), target.end() ); |
129 | |
130 | |
131 | target.clear(); |
132 | target.resize(ccont1.size()); |
133 | |
134 | test_it = boost::transform(ccont1, ccont2, target.begin(), fn); |
135 | |
136 | BOOST_CHECK( test_it == target.end() ); |
137 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
138 | target.begin(), target.end() ); |
139 | } |
140 | |
141 | template< class Container1, class Container2 > |
142 | void test_transform_impl2() |
143 | { |
144 | using namespace boost::assign; |
145 | |
146 | Container1 cont1; |
147 | Container2 cont2; |
148 | |
149 | test_transform_impl2(cont1, cont2); |
150 | |
151 | cont1 += 1; |
152 | cont2 += 2; |
153 | test_transform_impl2(cont1, cont2); |
154 | |
155 | cont1 += 2,3,4,5,6,7; |
156 | cont2 += 4,6,8,10,12,14; |
157 | test_transform_impl2(cont1, cont2); |
158 | } |
159 | |
160 | void test_transform() |
161 | { |
162 | test_transform_impl1< std::vector<int> >(); |
163 | test_transform_impl1< std::list<int> >(); |
164 | test_transform_impl1< std::set<int> >(); |
165 | test_transform_impl1< std::multiset<int> >(); |
166 | |
167 | test_transform_impl2< std::vector<int>, std::list<int> >(); |
168 | test_transform_impl2< std::list<int>, std::vector<int> >(); |
169 | test_transform_impl2< std::set<int>, std::set<int> >(); |
170 | test_transform_impl2< std::multiset<int>, std::list<int> >(); |
171 | } |
172 | } |
173 | } |
174 | |
175 | boost::unit_test::test_suite* |
176 | init_unit_test_suite(int argc, char* argv[]) |
177 | { |
178 | boost::unit_test::test_suite* test |
179 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.transform" ); |
180 | |
181 | test->add( BOOST_TEST_CASE( &boost::test_transform ) ); |
182 | |
183 | return test; |
184 | } |
185 | |