iota.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2010. 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_ext/iota.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <vector>
  18. namespace
  19. {
  20. template< class Container >
  21. void test_iota_impl(std::size_t n)
  22. {
  23. Container test;
  24. test.resize( n );
  25. boost::iota( test, n );
  26. Container reference;
  27. reference.resize( n );
  28. std::size_t i = n;
  29. typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator_t;
  30. iterator_t last = reference.end();
  31. for (iterator_t it = reference.begin(); it != last; ++it, ++i)
  32. {
  33. *it = i;
  34. }
  35. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  36. test.begin(), test.end() );
  37. }
  38. template< class Container >
  39. void test_iota_impl()
  40. {
  41. test_iota_impl< Container >(0);
  42. test_iota_impl< Container >(1);
  43. test_iota_impl< Container >(2);
  44. test_iota_impl< Container >(100);
  45. }
  46. void test_iota()
  47. {
  48. test_iota_impl< std::vector<std::size_t> >();
  49. test_iota_impl< std::list<std::size_t> >();
  50. }
  51. }
  52. boost::unit_test::test_suite*
  53. init_unit_test_suite(int argc, char* argv[])
  54. {
  55. boost::unit_test::test_suite* test
  56. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.iota" );
  57. test->add( BOOST_TEST_CASE( &test_iota ) );
  58. return test;
  59. }