remove_if.cpp 7.3 KB

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