• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2014 Peter Dimov
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 
9 #include <boost/generator_iterator.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <algorithm>
12 
13 class X
14 {
15 private:
16 
17     int v;
18 
19 public:
20 
21     typedef int result_type;
22 
X()23     X(): v( 0 )
24     {
25     }
26 
operator ()()27     int operator()()
28     {
29         return ++v;
30     }
31 };
32 
copy_n(InputIterator first,Size n,OutputIterator result)33 template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n( InputIterator first, Size n, OutputIterator result )
34 {
35     while( n-- > 0 )
36     {
37         *result++ = *first++;
38     }
39 
40     return result;
41 }
42 
copy_test()43 void copy_test()
44 {
45     X x;
46     boost::generator_iterator<X> in( &x );
47 
48     int const N = 4;
49     int v[ N ] = { 0 };
50 
51     ::copy_n( in, 4, v );
52 
53     BOOST_TEST_EQ( v[0], 1 );
54     BOOST_TEST_EQ( v[1], 2 );
55     BOOST_TEST_EQ( v[2], 3 );
56     BOOST_TEST_EQ( v[3], 4 );
57 }
58 
main()59 int main()
60 {
61     copy_test();
62     return boost::report_errors();
63 }
64