replace_copy_if.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_if.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_if_result<InIter, OutIter> r = ranges::replace_copy_if(InIter(ia), Sent(ia+sa), OutIter(ib),
  36. [](int i){return 2==i;}, 5);
  37. CHECK(base(r.in) == ia + sa);
  38. CHECK(base(r.out) == ib + sa);
  39. CHECK(ib[0] == 0);
  40. CHECK(ib[1] == 1);
  41. CHECK(ib[2] == 5);
  42. CHECK(ib[3] == 3);
  43. CHECK(ib[4] == 4);
  44. }
  45. template<class InIter, class OutIter, class Sent = InIter>
  46. void test_rng()
  47. {
  48. int ia[] = {0, 1, 2, 3, 4};
  49. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  50. int ib[sa] = {0};
  51. auto rng = ranges::make_subrange(InIter(ia), Sent(ia+sa));
  52. ranges::replace_copy_if_result<InIter, OutIter> r = ranges::replace_copy_if(rng, OutIter(ib),
  53. [](int i){return 2==i;}, 5);
  54. CHECK(base(r.in) == ia + sa);
  55. CHECK(base(r.out) == ib + sa);
  56. CHECK(ib[0] == 0);
  57. CHECK(ib[1] == 1);
  58. CHECK(ib[2] == 5);
  59. CHECK(ib[3] == 3);
  60. CHECK(ib[4] == 4);
  61. }
  62. template<class InIter, class OutIter>
  63. void test()
  64. {
  65. using Sent = typename sentinel_type<InIter>::type;
  66. test_iter<InIter, OutIter>();
  67. test_iter<InIter, OutIter>();
  68. test_rng<InIter, OutIter, Sent>();
  69. test_rng<InIter, OutIter, Sent>();
  70. }
  71. constexpr bool equals_two(int i)
  72. {
  73. return i == 2;
  74. }
  75. constexpr bool test_constexpr()
  76. {
  77. using namespace ranges;
  78. int ia[] = {0, 1, 2, 3, 4};
  79. int ib[5] = {0};
  80. constexpr auto sa = ranges::size(ia);
  81. const auto r = ranges::replace_copy_if(ia, ib, equals_two, 42);
  82. STATIC_CHECK_RETURN(r.in == ia + sa);
  83. STATIC_CHECK_RETURN(r.out == ib + sa);
  84. STATIC_CHECK_RETURN(ib[0] == 0);
  85. STATIC_CHECK_RETURN(ib[1] == 1);
  86. STATIC_CHECK_RETURN(ib[2] == 42);
  87. STATIC_CHECK_RETURN(ib[3] == 3);
  88. STATIC_CHECK_RETURN(ib[4] == 4);
  89. return true;
  90. }
  91. int main()
  92. {
  93. test<InputIterator<const int*>, OutputIterator<int*> >();
  94. test<InputIterator<const int*>, ForwardIterator<int*> >();
  95. test<InputIterator<const int*>, BidirectionalIterator<int*> >();
  96. test<InputIterator<const int*>, RandomAccessIterator<int*> >();
  97. test<InputIterator<const int*>, int*>();
  98. test<ForwardIterator<const int*>, OutputIterator<int*> >();
  99. test<ForwardIterator<const int*>, ForwardIterator<int*> >();
  100. test<ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  101. test<ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  102. test<ForwardIterator<const int*>, int*>();
  103. test<BidirectionalIterator<const int*>, OutputIterator<int*> >();
  104. test<BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  105. test<BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  106. test<BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  107. test<BidirectionalIterator<const int*>, int*>();
  108. test<RandomAccessIterator<const int*>, OutputIterator<int*> >();
  109. test<RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  110. test<RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  111. test<RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  112. test<RandomAccessIterator<const int*>, int*>();
  113. test<const int*, OutputIterator<int*> >();
  114. test<const int*, ForwardIterator<int*> >();
  115. test<const int*, BidirectionalIterator<int*> >();
  116. test<const int*, RandomAccessIterator<int*> >();
  117. test<const int*, int*>();
  118. // Test projection
  119. {
  120. using P = std::pair<int, std::string>;
  121. P in[] = {{0, "0"}, {1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}};
  122. P out[ranges::size(in)] = {};
  123. ranges::replace_copy_if_result<P *, P *> r = ranges::replace_copy_if(in, out,
  124. [](int i){return 2==i;}, P{5, "5"}, &std::pair<int, std::string>::first);
  125. CHECK(r.in == ranges::end(in));
  126. CHECK(r.out == ranges::end(out));
  127. CHECK(out[0] == P{0, "0"});
  128. CHECK(out[1] == P{1, "1"});
  129. CHECK(out[2] == P{5, "5"});
  130. CHECK(out[3] == P{3, "3"});
  131. CHECK(out[4] == P{4, "4"});
  132. }
  133. {
  134. STATIC_CHECK(test_constexpr());
  135. }
  136. return ::test_result();
  137. }