copy_backward.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Credits:
  12. // awulkiew highlighted that this test was not successfully testing the
  13. // algorithm.
  14. //
  15. #include <boost/range/algorithm/copy_backward.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <boost/test/unit_test.hpp>
  18. #include <boost/assign.hpp>
  19. #include <boost/range/iterator.hpp>
  20. #include <algorithm>
  21. #include <list>
  22. #include <vector>
  23. namespace boost_range_test
  24. {
  25. namespace
  26. {
  27. template<typename Container>
  28. void test_copy_backward_impl(std::size_t n)
  29. {
  30. Container source;
  31. typedef typename Container::value_type value_t;
  32. for (std::size_t i = 0; i < n; ++i)
  33. source.push_back(static_cast<value_t>(i));
  34. std::vector<value_t> target(n);
  35. typedef typename boost::range_iterator<
  36. std::vector<value_t>
  37. >::type iterator_t;
  38. iterator_t it = boost::copy_backward(source, target.end());
  39. BOOST_CHECK(it == target.begin());
  40. BOOST_CHECK_EQUAL_COLLECTIONS(target.begin(), target.end(),
  41. source.begin(), source.end());
  42. BOOST_CHECK(it == boost::copy_backward(
  43. boost::make_iterator_range(source), target.end()));
  44. BOOST_CHECK_EQUAL_COLLECTIONS(target.begin(), target.end(),
  45. source.begin(), source.end());
  46. }
  47. template<typename Container>
  48. void test_copy_backward_impl()
  49. {
  50. test_copy_backward_impl<Container>(0u);
  51. test_copy_backward_impl<Container>(1u);
  52. test_copy_backward_impl<Container>(100u);
  53. }
  54. void test_copy_backward()
  55. {
  56. test_copy_backward_impl<std::vector<int> >();
  57. test_copy_backward_impl<std::list<int> >();
  58. }
  59. } // anonymous namespace
  60. } // namespace boost_range_test
  61. boost::unit_test::test_suite*
  62. init_unit_test_suite(int, char*[])
  63. {
  64. boost::unit_test::test_suite* test
  65. = BOOST_TEST_SUITE("RangeTestSuite.algorithm.copy_backward");
  66. test->add(BOOST_TEST_CASE(&boost_range_test::test_copy_backward));
  67. return test;
  68. }