copy.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <vector>
  12. #include <sstream>
  13. #include <cstring>
  14. #include <utility>
  15. #include <algorithm>
  16. #include <range/v3/core.hpp>
  17. #include <range/v3/algorithm/copy.hpp>
  18. #include <range/v3/algorithm/equal.hpp>
  19. #include <range/v3/view/delimit.hpp>
  20. #include <range/v3/iterator/stream_iterators.hpp>
  21. #include "../array.hpp"
  22. #include "../simple_test.hpp"
  23. #include "../test_iterators.hpp"
  24. #if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_14 && RANGES_CONSTEXPR_INVOKE
  25. constexpr /*c++14*/
  26. bool test_constexpr_copy()
  27. {
  28. int a[4] = {0, 0, 0, 0};
  29. int const b[4] = {1, 2, 3, 4};
  30. ranges::copy(b, a);
  31. return ranges::equal(b, a);
  32. }
  33. static_assert(test_constexpr_copy(), "");
  34. #endif
  35. constexpr bool test_constexpr()
  36. {
  37. using IL = std::initializer_list<int>;
  38. constexpr test::array<int, 4> input{{0, 1, 2, 3}};
  39. test::array<int, 4> tmp{{0, 0, 0, 0}};
  40. auto res = ranges::copy(input, ranges::begin(tmp));
  41. STATIC_CHECK_RETURN(res.in == ranges::end(input));
  42. STATIC_CHECK_RETURN(res.out == ranges::end(tmp));
  43. STATIC_CHECK_RETURN(ranges::equal(tmp, IL{0, 1, 2, 3}));
  44. return true;
  45. }
  46. int main()
  47. {
  48. using ranges::begin;
  49. using ranges::end;
  50. using ranges::size;
  51. std::pair<int, int> const a[] = {{0, 0}, {0, 1}, {1, 2}, {1, 3}, {3, 4}, {3, 5}};
  52. static_assert(size(a) == 6, "");
  53. std::pair<int, int> out[size(a)] = {};
  54. auto res = ranges::copy(begin(a), end(a), out);
  55. CHECK(res.in == end(a));
  56. CHECK(res.out == out + size(out));
  57. CHECK(std::equal(a, a + size(a), out));
  58. std::fill_n(out, size(out), std::make_pair(0, 0));
  59. CHECK(!std::equal(a, a + size(a), out));
  60. res = ranges::copy(a, out);
  61. CHECK(res.in == a + size(a));
  62. CHECK(res.out == out + size(out));
  63. CHECK(std::equal(a, a + size(a), out));
  64. std::fill_n(out, size(out), std::make_pair(0, 0));
  65. using ranges::views::delimit;
  66. {
  67. char const *sz = "hello world";
  68. char buf[50];
  69. auto str = delimit(sz, '\0');
  70. auto res3 = ranges::copy(str, buf);
  71. *res3.out = '\0';
  72. CHECK(res3.in == std::next(begin(str), static_cast<std::ptrdiff_t>(std::strlen(sz))));
  73. CHECK(res3.out == buf + std::strlen(sz));
  74. CHECK(std::strcmp(sz, buf) == 0);
  75. }
  76. {
  77. char const *sz = "hello world";
  78. char buf[50];
  79. auto str = delimit(sz, '\0');
  80. auto res3 = ranges::copy(std::move(str), buf);
  81. *res3.out = '\0';
  82. CHECK(!::is_dangling(res3.in));
  83. CHECK(res3.out == buf + std::strlen(sz));
  84. CHECK(std::strcmp(sz, buf) == 0);
  85. }
  86. {
  87. using namespace ranges;
  88. std::ostringstream sout;
  89. std::vector<int> copy_vec{1,1,1,1,1};
  90. copy(copy_vec, ostream_iterator<>(sout, " "));
  91. CHECK(sout.str() == "1 1 1 1 1 ");
  92. }
  93. {
  94. STATIC_CHECK(test_constexpr());
  95. }
  96. return test_result();
  97. }