copy_backward.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <cstring>
  12. #include <algorithm>
  13. #include <utility>
  14. #include <vector>
  15. #include <range/v3/core.hpp>
  16. #include <range/v3/algorithm/copy_backward.hpp>
  17. #include "../array.hpp"
  18. #include "../simple_test.hpp"
  19. #include "../test_iterators.hpp"
  20. #include "../test_utils.hpp"
  21. constexpr bool test_constexpr()
  22. {
  23. using IL = std::initializer_list<int>;
  24. constexpr test::array<int, 4> input{{0, 1, 2, 3}};
  25. test::array<int, 4> a1{{0, 0, 0, 0}};
  26. auto res = ranges::copy_backward(input, ranges::end(a1));
  27. STATIC_CHECK_RETURN(res.in == ranges::end(input));
  28. STATIC_CHECK_RETURN(res.out == ranges::begin(a1));
  29. STATIC_CHECK_RETURN(ranges::equal(a1, IL{0, 1, 2, 3}));
  30. return true;
  31. }
  32. int main()
  33. {
  34. using ranges::begin;
  35. using ranges::end;
  36. using ranges::size;
  37. std::pair<int, int> const a[] = {{0, 0}, {0, 1}, {1, 2}, {1, 3}, {3, 4}, {3, 5}};
  38. static_assert(size(a) == 6, "");
  39. std::pair<int, int> out[size(a)] = {};
  40. {
  41. auto res = ranges::copy_backward(begin(a), end(a), end(out));
  42. CHECK(res.in == end(a));
  43. CHECK(res.out == begin(out));
  44. CHECK(std::equal(a, a + size(a), out));
  45. }
  46. {
  47. std::fill_n(out, size(out), std::make_pair(0, 0));
  48. auto res = ranges::copy_backward(a, end(out));
  49. CHECK(res.in == end(a));
  50. CHECK(res.out == begin(out));
  51. CHECK(std::equal(a, a + size(a), out));
  52. }
  53. #ifndef RANGES_WORKAROUND_MSVC_573728
  54. {
  55. std::fill_n(out, size(out), std::make_pair(0, 0));
  56. auto res = ranges::copy_backward(std::move(a), end(out));
  57. CHECK(::is_dangling(res.in));
  58. CHECK(res.out == begin(out));
  59. CHECK(std::equal(a, a + size(a), out));
  60. }
  61. #endif
  62. {
  63. std::fill_n(out, size(out), std::make_pair(0, 0));
  64. std::vector<std::pair<int, int>> vec(begin(a), end(a));
  65. auto res = ranges::copy_backward(std::move(vec), end(out));
  66. CHECK(::is_dangling(res.in));
  67. CHECK(res.out == begin(out));
  68. CHECK(std::equal(a, a + size(a), out));
  69. }
  70. {
  71. std::fill_n(out, size(out), std::make_pair(0, 0));
  72. auto res = ranges::copy_backward(ranges::views::all(a), end(out));
  73. CHECK(res.in == end(a));
  74. CHECK(res.out == begin(out));
  75. CHECK(std::equal(a, a + size(a), out));
  76. }
  77. {
  78. STATIC_CHECK(test_constexpr());
  79. }
  80. return test_result();
  81. }