generator_iterator_test.cpp 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include <boost/generator_iterator.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <algorithm>
  11. class X
  12. {
  13. private:
  14. int v;
  15. public:
  16. typedef int result_type;
  17. X(): v( 0 )
  18. {
  19. }
  20. int operator()()
  21. {
  22. return ++v;
  23. }
  24. };
  25. template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n( InputIterator first, Size n, OutputIterator result )
  26. {
  27. while( n-- > 0 )
  28. {
  29. *result++ = *first++;
  30. }
  31. return result;
  32. }
  33. void copy_test()
  34. {
  35. X x;
  36. boost::generator_iterator<X> in( &x );
  37. int const N = 4;
  38. int v[ N ] = { 0 };
  39. ::copy_n( in, 4, v );
  40. BOOST_TEST_EQ( v[0], 1 );
  41. BOOST_TEST_EQ( v[1], 2 );
  42. BOOST_TEST_EQ( v[2], 3 );
  43. BOOST_TEST_EQ( v[3], 4 );
  44. }
  45. int main()
  46. {
  47. copy_test();
  48. return boost::report_errors();
  49. }