counting_range.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // Disable a warning from <xutility> since this noise might
  12. // stop us detecting a problem in our code.
  13. #include <boost/range/counting_range.hpp>
  14. #include <boost/range/adaptor/indirected.hpp>
  15. #include <boost/test/test_tools.hpp>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/assign.hpp>
  18. #include <algorithm>
  19. #include <deque>
  20. #include <string>
  21. #include <vector>
  22. #include <boost/range/algorithm_ext.hpp>
  23. namespace boost
  24. {
  25. namespace
  26. {
  27. template<class Container>
  28. void counting_range_test_impl(int first, int last)
  29. {
  30. Container reference;
  31. for (int i = first; i < last; ++i)
  32. reference.push_back(i);
  33. Container test;
  34. push_back( test, counting_range(first, last) );
  35. BOOST_CHECK_EQUAL_COLLECTIONS(
  36. reference.begin(), reference.end(),
  37. test.begin(), test.end());
  38. }
  39. template<class Container>
  40. void counting_range_test_impl()
  41. {
  42. counting_range_test_impl<Container>(0, 0);
  43. counting_range_test_impl<Container>(-1, -1);
  44. counting_range_test_impl<Container>(-1, 0);
  45. counting_range_test_impl<Container>(0, 1);
  46. counting_range_test_impl<Container>(-100, 100);
  47. counting_range_test_impl<Container>(50, 55);
  48. }
  49. void counting_range_test_range()
  50. {
  51. std::vector<int> v;
  52. for (int i = 0; i < 10; ++i)
  53. v.push_back(i);
  54. std::vector<std::vector<int>::iterator> x;
  55. push_back(x, counting_range(v));
  56. std::vector<int> t;
  57. push_back(t, x | boost::adaptors::indirected);
  58. BOOST_CHECK_EQUAL_COLLECTIONS(t.begin(), t.end(),
  59. v.begin(), v.end());
  60. }
  61. }
  62. void counting_range_test()
  63. {
  64. counting_range_test_impl<std::vector<int> >();
  65. counting_range_test_impl<std::list<int> >();
  66. counting_range_test_impl<std::deque<int> >();
  67. }
  68. }
  69. boost::unit_test::test_suite*
  70. init_unit_test_suite(int argc, char* argv[])
  71. {
  72. boost::unit_test::test_suite* test
  73. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.counting_range" );
  74. test->add( BOOST_TEST_CASE( &boost::counting_range_test ) );
  75. return test;
  76. }