remove_copy_if.cpp 7.8 KB

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