replace_copy.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //
  12. // Copyright 2005 - 2007 Adobe Systems Incorporated
  13. // Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
  14. // or a copy at http://stlab.adobe.com/licenses.html)
  15. //===----------------------------------------------------------------------===//
  16. //
  17. // The LLVM Compiler Infrastructure
  18. //
  19. // This file is dual licensed under the MIT and the University of Illinois Open
  20. // Source Licenses. See LICENSE.TXT for details.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include <utility>
  24. #include <range/v3/core.hpp>
  25. #include <range/v3/algorithm/replace_copy.hpp>
  26. #include "../simple_test.hpp"
  27. #include "../test_utils.hpp"
  28. #include "../test_iterators.hpp"
  29. template<class InIter, class OutIter, class Sent = InIter>
  30. void test_iter()
  31. {
  32. int ia[] = {0, 1, 2, 3, 4};
  33. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  34. int ib[sa] = {0};
  35. ranges::replace_copy_result<InIter, OutIter> r = ranges::replace_copy(InIter(ia), Sent(ia+sa), OutIter(ib), 2, 5);
  36. CHECK(base(r.in) == ia + sa);
  37. CHECK(base(r.out) == ib + sa);
  38. CHECK(ib[0] == 0);
  39. CHECK(ib[1] == 1);
  40. CHECK(ib[2] == 5);
  41. CHECK(ib[3] == 3);
  42. CHECK(ib[4] == 4);
  43. }
  44. template<class InIter, class OutIter, class Sent = InIter>
  45. void test_rng()
  46. {
  47. int ia[] = {0, 1, 2, 3, 4};
  48. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  49. int ib[sa] = {0};
  50. auto rng = ranges::make_subrange(InIter(ia), Sent(ia+sa));
  51. ranges::replace_copy_result<InIter, OutIter> r = ranges::replace_copy(rng, OutIter(ib), 2, 5);
  52. CHECK(base(r.in) == ia + sa);
  53. CHECK(base(r.out) == ib + sa);
  54. CHECK(ib[0] == 0);
  55. CHECK(ib[1] == 1);
  56. CHECK(ib[2] == 5);
  57. CHECK(ib[3] == 3);
  58. CHECK(ib[4] == 4);
  59. }
  60. template<class InIter, class OutIter>
  61. void test()
  62. {
  63. using Sent = typename sentinel_type<InIter>::type;
  64. test_iter<InIter, OutIter>();
  65. test_iter<InIter, OutIter>();
  66. test_rng<InIter, OutIter, Sent>();
  67. test_rng<InIter, OutIter, Sent>();
  68. }
  69. constexpr bool test_constexpr()
  70. {
  71. using namespace ranges;
  72. int ia[] = {0, 1, 2, 3, 4};
  73. int ib[5] = {0};
  74. constexpr auto sa = ranges::size(ia);
  75. const auto r = ranges::replace_copy(ia, ib, 2, 42);
  76. STATIC_CHECK_RETURN(r.in == ia + sa);
  77. STATIC_CHECK_RETURN(r.out == ib + sa);
  78. STATIC_CHECK_RETURN(ib[0] == 0);
  79. STATIC_CHECK_RETURN(ib[1] == 1);
  80. STATIC_CHECK_RETURN(ib[2] == 42);
  81. STATIC_CHECK_RETURN(ib[3] == 3);
  82. STATIC_CHECK_RETURN(ib[4] == 4);
  83. return true;
  84. }
  85. int main()
  86. {
  87. test<InputIterator<const int*>, OutputIterator<int*> >();
  88. test<InputIterator<const int*>, ForwardIterator<int*> >();
  89. test<InputIterator<const int*>, BidirectionalIterator<int*> >();
  90. test<InputIterator<const int*>, RandomAccessIterator<int*> >();
  91. test<InputIterator<const int*>, int*>();
  92. test<ForwardIterator<const int*>, OutputIterator<int*> >();
  93. test<ForwardIterator<const int*>, ForwardIterator<int*> >();
  94. test<ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  95. test<ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  96. test<ForwardIterator<const int*>, int*>();
  97. test<BidirectionalIterator<const int*>, OutputIterator<int*> >();
  98. test<BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  99. test<BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  100. test<BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  101. test<BidirectionalIterator<const int*>, int*>();
  102. test<RandomAccessIterator<const int*>, OutputIterator<int*> >();
  103. test<RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  104. test<RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  105. test<RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  106. test<RandomAccessIterator<const int*>, int*>();
  107. test<const int*, OutputIterator<int*> >();
  108. test<const int*, ForwardIterator<int*> >();
  109. test<const int*, BidirectionalIterator<int*> >();
  110. test<const int*, RandomAccessIterator<int*> >();
  111. test<const int*, int*>();
  112. // Test projection
  113. {
  114. using P = std::pair<int, std::string>;
  115. P in[] = {{0, "0"}, {1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}};
  116. P out[ranges::size(in)] = {};
  117. ranges::replace_copy_result<P *, P *> r = ranges::replace_copy(in, out, 2, P{5, "5"}, &std::pair<int, std::string>::first);
  118. CHECK(r.in == ranges::end(in));
  119. CHECK(r.out == ranges::end(out));
  120. CHECK(out[0] == P{0, "0"});
  121. CHECK(out[1] == P{1, "1"});
  122. CHECK(out[2] == P{5, "5"});
  123. CHECK(out[3] == P{3, "3"});
  124. CHECK(out[4] == P{4, "4"});
  125. }
  126. {
  127. STATIC_CHECK(test_constexpr());
  128. }
  129. return ::test_result();
  130. }