• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Jeremy Siek 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 //  Revision History
7 //  22 Nov 2002 Thomas Witt
8 //       Added interoperability check.
9 //  28 Oct 2002   Jeremy Siek
10 //       Updated for new iterator adaptors.
11 //  08 Mar 2001   Jeremy Siek
12 //       Moved test of transform iterator into its own file. It to
13 //       to be in iterator_adaptor_test.cpp.
14 
15 #include <boost/assert.hpp>
16 #include <boost/config.hpp>
17 #include <algorithm>
18 #include <boost/iterator/transform_iterator.hpp>
19 #include <boost/iterator/iterator_concepts.hpp>
20 #include <boost/iterator/new_iterator_tests.hpp>
21 #include <boost/pending/iterator_tests.hpp>
22 #include <boost/concept_check.hpp>
23 
24 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
25 namespace boost { namespace detail
26 {
27   template<> struct function_object_result<int (*)(int)>
28   {
29       typedef int type;
30   };
31 }}
32 #endif
33 
34 struct mult_functor {
35   // Functors used with transform_iterator must be
36   // DefaultConstructible, as the transform_iterator must be
37   // DefaultConstructible to satisfy the requirements for
38   // TrivialIterator.
mult_functormult_functor39   mult_functor() { }
mult_functormult_functor40   mult_functor(int aa) : a(aa) { }
operator ()mult_functor41   int operator()(int b) const { return a * b; }
42   int a;
43 };
44 
45 struct adaptable_mult_functor
46  : mult_functor
47 {
48   typedef int result_type;
49   typedef int argument_type;
50   // Functors used with transform_iterator must be
51   // DefaultConstructible, as the transform_iterator must be
52   // DefaultConstructible to satisfy the requirements for
53   // TrivialIterator.
adaptable_mult_functoradaptable_mult_functor54   adaptable_mult_functor() { }
adaptable_mult_functoradaptable_mult_functor55   adaptable_mult_functor(int aa) : mult_functor(aa) { }
56 };
57 
58 
59 struct const_select_first
60 {
61   typedef int const& result_type;
62 
operator ()const_select_first63   int const& operator()(std::pair<int, int>const& p) const
64   {
65     return p.first;
66   }
67 };
68 
69 struct select_first
70   : const_select_first // derivation to allow conversions
71 {
72   typedef int& result_type;
73 
operator ()select_first74   int& operator()(std::pair<int, int>& p) const
75   {
76     return p.first;
77   }
78 };
79 
80 struct select_second
81 {
82   typedef int& result_type;
83 
operator ()select_second84   int& operator()(std::pair<int, int>& p) const
85   {
86     return p.second;
87   }
88 };
89 
90 struct value_select_first
91 {
92   typedef int result_type;
93 
operator ()value_select_first94   int operator()(std::pair<int, int>const& p) const
95   {
96     return p.first;
97   }
98 };
99 
mult_2(int arg)100 int mult_2(int arg)
101 {
102   return arg*2;
103 }
104 
105 struct polymorphic_mult_functor
106 {
107     //Implement result_of protocol
108     template <class FArgs> struct result;
109     template <class F, class T> struct result<const F(T       )> {typedef T type;};
110     template <class F, class T> struct result<const F(T&      )> {typedef T type;};
111     template <class F, class T> struct result<const F(const T&)> {typedef T type;};
112     template <class F, class T> struct result<F(T       )> {typedef void type;};
113     template <class F, class T> struct result<F(T&      )> {typedef void type;};
114     template <class F, class T> struct result<F(const T&)> {typedef void type;};
115 
116     template <class T>
operator ()polymorphic_mult_functor117     T operator()(const T& _arg) const {return _arg*2;}
118     template <class T>
operator ()polymorphic_mult_functor119     void operator()(const T& _arg) { BOOST_ASSERT(0); }
120 };
121 
122 int
main()123 main()
124 {
125   const int N = 10;
126 
127   // Concept checks
128   {
129     typedef boost::transform_iterator<adaptable_mult_functor, int*>       iter_t;
130     typedef boost::transform_iterator<adaptable_mult_functor, int const*> c_iter_t;
131 
132     boost::function_requires< boost_concepts::InteroperableIteratorConcept<iter_t, c_iter_t> >();
133   }
134 
135   // Test transform_iterator
136   {
137     int x[N], y[N];
138     for (int k = 0; k < N; ++k)
139       x[k] = k;
140     std::copy(x, x + N, y);
141 
142     for (int k2 = 0; k2 < N; ++k2)
143       x[k2] = x[k2] * 2;
144 
145     typedef boost::transform_iterator<adaptable_mult_functor, int*> iter_t;
146     iter_t i(y, adaptable_mult_functor(2));
147     boost::input_iterator_test(i, x[0], x[1]);
148     boost::input_iterator_test(iter_t(&y[0], adaptable_mult_functor(2)), x[0], x[1]);
149 
150     boost::random_access_readable_iterator_test(i, N, x);
151   }
152 
153   // Test transform_iterator non adaptable functor
154   {
155     int x[N], y[N];
156     for (int k = 0; k < N; ++k)
157       x[k] = k;
158     std::copy(x, x + N, y);
159 
160     for (int k2 = 0; k2 < N; ++k2)
161       x[k2] = x[k2] * 2;
162 
163     typedef boost::transform_iterator<mult_functor, int*, int> iter_t;
164     iter_t i(y, mult_functor(2));
165     boost::input_iterator_test(i, x[0], x[1]);
166     boost::input_iterator_test(iter_t(&y[0], mult_functor(2)), x[0], x[1]);
167 
168     boost::random_access_readable_iterator_test(i, N, x);
169   }
170 
171   // Test transform_iterator default argument handling
172   {
173     {
174       typedef boost::transform_iterator<adaptable_mult_functor, int*, float> iter_t;
175       BOOST_STATIC_ASSERT((boost::is_same<iter_t::reference,  float>::value));
176       BOOST_STATIC_ASSERT((boost::is_same<iter_t::value_type, float>::value));
177     }
178 
179     {
180       typedef boost::transform_iterator<adaptable_mult_functor, int*, boost::use_default, float> iter_t;
181       BOOST_STATIC_ASSERT((boost::is_same<iter_t::reference,  int>::value));
182       BOOST_STATIC_ASSERT((boost::is_same<iter_t::value_type, float>::value));
183     }
184 
185     {
186       typedef boost::transform_iterator<adaptable_mult_functor, int*, float, double> iter_t;
187       BOOST_STATIC_ASSERT((boost::is_same<iter_t::reference,  float>::value));
188       BOOST_STATIC_ASSERT((boost::is_same<iter_t::value_type, double>::value));
189     }
190   }
191 
192   // Test transform_iterator with function pointers
193   {
194     int x[N], y[N];
195     for (int k = 0; k < N; ++k)
196       x[k] = k;
197     std::copy(x, x + N, y);
198 
199     for (int k2 = 0; k2 < N; ++k2)
200       x[k2] = x[k2] * 2;
201 
202     boost::input_iterator_test(
203         boost::make_transform_iterator(y, mult_2), x[0], x[1]);
204 
205     boost::input_iterator_test(
206         boost::make_transform_iterator(&y[0], mult_2), x[0], x[1]);
207 
208     boost::random_access_readable_iterator_test(
209         boost::make_transform_iterator(y, mult_2), N, x);
210 
211   }
212 
213   // Test transform_iterator as projection iterator
214   {
215     typedef std::pair<int, int> pair_t;
216 
217     int    x[N];
218     int    y[N];
219     pair_t values[N];
220 
221     for(int i = 0; i < N; ++i) {
222 
223       x[i]             = i;
224       y[i]             = N - (i + 1);
225 
226     }
227 
228     std::copy(
229         x
230       , x + N
231       , boost::make_transform_iterator((pair_t*)values, select_first())
232     );
233 
234     std::copy(
235         y
236       , y + N
237       , boost::make_transform_iterator((pair_t*)values, select_second())
238     );
239 
240     boost::random_access_readable_iterator_test(
241         boost::make_transform_iterator((pair_t*)values, value_select_first())
242       , N
243       , x
244     );
245 
246     boost::random_access_readable_iterator_test(
247         boost::make_transform_iterator((pair_t*)values, const_select_first())
248       , N, x
249     );
250 
251     boost::constant_lvalue_iterator_test(
252         boost::make_transform_iterator((pair_t*)values, const_select_first()), x[0]);
253 
254     boost::non_const_lvalue_iterator_test(
255         boost::make_transform_iterator((pair_t*)values, select_first()), x[0], 17);
256 
257     boost::const_nonconst_iterator_test(
258         ++boost::make_transform_iterator((pair_t*)values, select_first())
259       , boost::make_transform_iterator((pair_t*)values, const_select_first())
260     );
261   }
262 
263   // Test transform_iterator with polymorphic object function
264   {
265     int x[N], y[N];
266     for (int k = 0; k < N; ++k)
267       x[k] = k;
268     std::copy(x, x + N, y);
269 
270     for (int k2 = 0; k2 < N; ++k2)
271       x[k2] = x[k2] * 2;
272 
273     boost::input_iterator_test(
274         boost::make_transform_iterator(y, polymorphic_mult_functor()), x[0], x[1]);
275 
276     boost::input_iterator_test(
277         boost::make_transform_iterator(&y[0], polymorphic_mult_functor()), x[0], x[1]);
278 
279     boost::random_access_readable_iterator_test(
280         boost::make_transform_iterator(y, polymorphic_mult_functor()), N, x);
281   }
282 
283   return boost::report_errors();
284 }
285