remove_copy.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <memory>
  24. #include <utility>
  25. #include <vector>
  26. #include <range/v3/core.hpp>
  27. #include <range/v3/algorithm/remove_copy.hpp>
  28. #include "../simple_test.hpp"
  29. #include "../test_utils.hpp"
  30. #include "../test_iterators.hpp"
  31. template<class InIter, class OutIter, class Sent = InIter>
  32. void
  33. test_iter()
  34. {
  35. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  36. constexpr auto sa = ranges::size(ia);
  37. int ib[sa];
  38. ranges::remove_copy_result<InIter, OutIter> r = ranges::remove_copy(InIter(ia), Sent(ia+sa), OutIter(ib), 2);
  39. CHECK(base(r.in) == ia + sa);
  40. CHECK(base(r.out) == ib + sa-3);
  41. CHECK(ib[0] == 0);
  42. CHECK(ib[1] == 1);
  43. CHECK(ib[2] == 3);
  44. CHECK(ib[3] == 4);
  45. CHECK(ib[4] == 3);
  46. CHECK(ib[5] == 4);
  47. }
  48. template<class InIter, class OutIter, class Sent = InIter>
  49. void
  50. test_range()
  51. {
  52. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  53. constexpr auto sa = ranges::size(ia);
  54. int ib[sa];
  55. ranges::remove_copy_result<InIter, OutIter> r = ranges::remove_copy(::as_lvalue(ranges::make_subrange(InIter(ia), Sent(ia+sa))), OutIter(ib), 2);
  56. CHECK(base(r.in) == ia + sa);
  57. CHECK(base(r.out) == ib + sa-3);
  58. CHECK(ib[0] == 0);
  59. CHECK(ib[1] == 1);
  60. CHECK(ib[2] == 3);
  61. CHECK(ib[3] == 4);
  62. CHECK(ib[4] == 3);
  63. CHECK(ib[5] == 4);
  64. }
  65. template<class InIter, class OutIter, class Sent = InIter>
  66. void
  67. test()
  68. {
  69. test_iter<InIter, OutIter, Sent>();
  70. test_range<InIter, OutIter, Sent>();
  71. }
  72. struct S
  73. {
  74. int i;
  75. };
  76. constexpr bool test_constexpr()
  77. {
  78. using namespace ranges;
  79. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  80. int ib[6] = {0};
  81. constexpr auto sa = ranges::size(ia);
  82. auto r = ranges::remove_copy(ia, ib, 2);
  83. STATIC_CHECK_RETURN(r.in == ia + sa);
  84. STATIC_CHECK_RETURN(r.out == ib + (sa - 3));
  85. STATIC_CHECK_RETURN(ib[0] == 0);
  86. STATIC_CHECK_RETURN(ib[1] == 1);
  87. STATIC_CHECK_RETURN(ib[2] == 3);
  88. STATIC_CHECK_RETURN(ib[3] == 4);
  89. STATIC_CHECK_RETURN(ib[4] == 3);
  90. STATIC_CHECK_RETURN(ib[5] == 4);
  91. return true;
  92. }
  93. int main()
  94. {
  95. test<InputIterator<const int*>, OutputIterator<int*>>();
  96. test<InputIterator<const int*>, ForwardIterator<int*>>();
  97. test<InputIterator<const int*>, BidirectionalIterator<int*>>();
  98. test<InputIterator<const int*>, RandomAccessIterator<int*>>();
  99. test<InputIterator<const int*>, int*>();
  100. test<ForwardIterator<const int*>, OutputIterator<int*>>();
  101. test<ForwardIterator<const int*>, ForwardIterator<int*>>();
  102. test<ForwardIterator<const int*>, BidirectionalIterator<int*>>();
  103. test<ForwardIterator<const int*>, RandomAccessIterator<int*>>();
  104. test<ForwardIterator<const int*>, int*>();
  105. test<BidirectionalIterator<const int*>, OutputIterator<int*>>();
  106. test<BidirectionalIterator<const int*>, ForwardIterator<int*>>();
  107. test<BidirectionalIterator<const int*>, BidirectionalIterator<int*>>();
  108. test<BidirectionalIterator<const int*>, RandomAccessIterator<int*>>();
  109. test<BidirectionalIterator<const int*>, int*>();
  110. test<RandomAccessIterator<const int*>, OutputIterator<int*>>();
  111. test<RandomAccessIterator<const int*>, ForwardIterator<int*>>();
  112. test<RandomAccessIterator<const int*>, BidirectionalIterator<int*>>();
  113. test<RandomAccessIterator<const int*>, RandomAccessIterator<int*>>();
  114. test<RandomAccessIterator<const int*>, int*>();
  115. test<const int*, OutputIterator<int*>>();
  116. test<const int*, ForwardIterator<int*>>();
  117. test<const int*, BidirectionalIterator<int*>>();
  118. test<const int*, RandomAccessIterator<int*>>();
  119. test<const int*, int*>();
  120. test<InputIterator<const int*>, OutputIterator<int*>, Sentinel<const int*>>();
  121. test<InputIterator<const int*>, ForwardIterator<int*>, Sentinel<const int*>>();
  122. test<InputIterator<const int*>, BidirectionalIterator<int*>, Sentinel<const int*>>();
  123. test<InputIterator<const int*>, RandomAccessIterator<int*>, Sentinel<const int*>>();
  124. test<InputIterator<const int*>, int*, Sentinel<const int*>>();
  125. test<ForwardIterator<const int*>, OutputIterator<int*>, Sentinel<const int*>>();
  126. test<ForwardIterator<const int*>, ForwardIterator<int*>, Sentinel<const int*>>();
  127. test<ForwardIterator<const int*>, BidirectionalIterator<int*>, Sentinel<const int*>>();
  128. test<ForwardIterator<const int*>, RandomAccessIterator<int*>, Sentinel<const int*>>();
  129. test<ForwardIterator<const int*>, int*, Sentinel<const int*>>();
  130. test<BidirectionalIterator<const int*>, OutputIterator<int*>, Sentinel<const int*>>();
  131. test<BidirectionalIterator<const int*>, ForwardIterator<int*>, Sentinel<const int*>>();
  132. test<BidirectionalIterator<const int*>, BidirectionalIterator<int*>, Sentinel<const int*>>();
  133. test<BidirectionalIterator<const int*>, RandomAccessIterator<int*>, Sentinel<const int*>>();
  134. test<BidirectionalIterator<const int*>, int*, Sentinel<const int*>>();
  135. test<RandomAccessIterator<const int*>, OutputIterator<int*>, Sentinel<const int*>>();
  136. test<RandomAccessIterator<const int*>, ForwardIterator<int*>, Sentinel<const int*>>();
  137. test<RandomAccessIterator<const int*>, BidirectionalIterator<int*>, Sentinel<const int*>>();
  138. test<RandomAccessIterator<const int*>, RandomAccessIterator<int*>, Sentinel<const int*>>();
  139. test<RandomAccessIterator<const int*>, int*, Sentinel<const int*>>();
  140. // Check projection
  141. {
  142. S ia[] = {S{0}, S{1}, S{2}, S{3}, S{4}, S{2}, S{3}, S{4}, S{2}};
  143. constexpr auto sa = ranges::size(ia);
  144. S ib[sa];
  145. ranges::remove_copy_result<S*, S*> r = ranges::remove_copy(ia, ib, 2, &S::i);
  146. CHECK(r.in == ia + sa);
  147. CHECK(r.out == ib + sa-3);
  148. CHECK(ib[0].i == 0);
  149. CHECK(ib[1].i == 1);
  150. CHECK(ib[2].i == 3);
  151. CHECK(ib[3].i == 4);
  152. CHECK(ib[4].i == 3);
  153. CHECK(ib[5].i == 4);
  154. }
  155. // Check rvalue range
  156. {
  157. S ia[] = {S{0}, S{1}, S{2}, S{3}, S{4}, S{2}, S{3}, S{4}, S{2}};
  158. constexpr auto sa = ranges::size(ia);
  159. S ib[sa] = {};
  160. auto r0 = ranges::remove_copy(std::move(ia), ib, 2, &S::i);
  161. #ifndef RANGES_WORKAROUND_MSVC_573728
  162. static_assert(std::is_same<decltype(r0),
  163. ranges::remove_copy_result<ranges::dangling, S *>>::value, "");
  164. #endif // RANGES_WORKAROUND_MSVC_573728
  165. CHECK(r0.out == ib + sa-3);
  166. CHECK(ib[0].i == 0);
  167. CHECK(ib[1].i == 1);
  168. CHECK(ib[2].i == 3);
  169. CHECK(ib[3].i == 4);
  170. CHECK(ib[4].i == 3);
  171. CHECK(ib[5].i == 4);
  172. std::fill(ranges::begin(ib), ranges::end(ib), S{});
  173. std::vector<S> vec(ranges::begin(ia), ranges::end(ia));
  174. auto r1 = ranges::remove_copy(std::move(vec), ib, 2, &S::i);
  175. static_assert(std::is_same<decltype(r1),
  176. ranges::remove_copy_result<ranges::dangling, S *>>::value, "");
  177. CHECK(r1.out == ib + sa-3);
  178. CHECK(ib[0].i == 0);
  179. CHECK(ib[1].i == 1);
  180. CHECK(ib[2].i == 3);
  181. CHECK(ib[3].i == 4);
  182. CHECK(ib[4].i == 3);
  183. CHECK(ib[5].i == 4);
  184. }
  185. {
  186. STATIC_CHECK(test_constexpr());
  187. }
  188. return ::test_result();
  189. }