adjacent_remove_if.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler
  5. // Copyright Christopher Di Bella
  6. //
  7. // Use, modification and distribution is subject to the
  8. // Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // Project home: https://github.com/ericniebler/range-v3
  13. //
  14. // Copyright 2005 - 2007 Adobe Systems Incorporated
  15. // Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
  16. // or a copy at http://stlab.adobe.com/licenses.html)
  17. //===----------------------------------------------------------------------===//
  18. //
  19. // The LLVM Compiler Infrastructure
  20. //
  21. // This file is dual licensed under the MIT and the University of Illinois Open
  22. // Source Licenses. See LICENSE.TXT for details.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #include <range/v3/algorithm/adjacent_remove_if.hpp>
  26. #include <range/v3/core.hpp>
  27. #include "../simple_test.hpp"
  28. #include "../test_utils.hpp"
  29. #include "../test_iterators.hpp"
  30. template<class Iter, class Sent = Iter>
  31. void
  32. test_iter()
  33. {
  34. int ia[] = {0, 1, 1, 1, 4, 2, 2, 4, 2};
  35. constexpr auto sa = ranges::size(ia);
  36. Iter r = ranges::adjacent_remove_if(Iter(ia), Sent(ia+sa), ranges::equal_to{});
  37. CHECK(base(r) == ia + sa-3);
  38. CHECK(ia[0] == 0);
  39. CHECK(ia[1] == 1);
  40. CHECK(ia[2] == 4);
  41. CHECK(ia[3] == 2);
  42. CHECK(ia[4] == 4);
  43. CHECK(ia[5] == 2);
  44. }
  45. template<class Iter, class Sent = Iter>
  46. void
  47. test_range()
  48. {
  49. int ia[] = {0, 1, 1, 1, 4, 2, 2, 4, 2};
  50. constexpr auto sa = ranges::size(ia);
  51. Iter r = ranges::adjacent_remove_if(
  52. ranges::make_subrange(Iter(ia), Sent(ia+sa)),
  53. ranges::equal_to{});
  54. CHECK(base(r) == ia + sa-3);
  55. CHECK(ia[0] == 0);
  56. CHECK(ia[1] == 1);
  57. CHECK(ia[2] == 4);
  58. CHECK(ia[3] == 2);
  59. CHECK(ia[4] == 4);
  60. CHECK(ia[5] == 2);
  61. }
  62. struct pred
  63. {
  64. bool operator()(const std::unique_ptr<int> &i, const std::unique_ptr<int> &j)
  65. {
  66. return *i == 2 && *j == 3;
  67. }
  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::adjacent_remove_if(Iter(ia), Sent(ia+sa), pred());
  85. CHECK(base(r) == ia + sa-2);
  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. CHECK(*ia[6] == 2);
  93. }
  94. template<class Iter, class Sent = Iter>
  95. void
  96. test_range_rvalue()
  97. {
  98. constexpr unsigned sa = 9;
  99. std::unique_ptr<int> ia[sa];
  100. ia[0].reset(new int(0));
  101. ia[1].reset(new int(1));
  102. ia[2].reset(new int(2));
  103. ia[3].reset(new int(3));
  104. ia[4].reset(new int(4));
  105. ia[5].reset(new int(2));
  106. ia[6].reset(new int(3));
  107. ia[7].reset(new int(4));
  108. ia[8].reset(new int(2));
  109. Iter r = ranges::adjacent_remove_if(ranges::make_subrange(Iter(ia), Sent(ia+sa)), pred());
  110. CHECK(base(r) == ia + sa-2);
  111. CHECK(*ia[0] == 0);
  112. CHECK(*ia[1] == 1);
  113. CHECK(*ia[2] == 3);
  114. CHECK(*ia[3] == 4);
  115. CHECK(*ia[4] == 3);
  116. CHECK(*ia[5] == 4);
  117. CHECK(*ia[6] == 2);
  118. }
  119. template<class Iter, class Sent = Iter>
  120. bool constexpr test_constexpr()
  121. {
  122. int ia[] = {0, 1, 1, 1, 4, 2, 2, 4, 2};
  123. constexpr auto sa = ranges::size(ia);
  124. Iter r = ranges::adjacent_remove_if(ranges::make_subrange(Iter(ia), Sent(ia + sa)),
  125. ranges::equal_to{});
  126. STATIC_CHECK_RETURN(base(r) == ia + sa - 3);
  127. STATIC_CHECK_RETURN(ia[0] == 0);
  128. STATIC_CHECK_RETURN(ia[1] == 1);
  129. STATIC_CHECK_RETURN(ia[2] == 4);
  130. STATIC_CHECK_RETURN(ia[3] == 2);
  131. STATIC_CHECK_RETURN(ia[4] == 4);
  132. STATIC_CHECK_RETURN(ia[5] == 2);
  133. return true;
  134. }
  135. struct S
  136. {
  137. int i;
  138. };
  139. int main()
  140. {
  141. test_iter<ForwardIterator<int*> >();
  142. test_iter<BidirectionalIterator<int*> >();
  143. test_iter<RandomAccessIterator<int*> >();
  144. test_iter<int*>();
  145. test_iter<ForwardIterator<int*>, Sentinel<int*>>();
  146. test_iter<BidirectionalIterator<int*>, Sentinel<int*>>();
  147. test_iter<RandomAccessIterator<int*>, Sentinel<int*>>();
  148. test_range<ForwardIterator<int*> >();
  149. test_range<BidirectionalIterator<int*> >();
  150. test_range<RandomAccessIterator<int*> >();
  151. test_range<int*>();
  152. test_range<ForwardIterator<int*>, Sentinel<int*>>();
  153. test_range<BidirectionalIterator<int*>, Sentinel<int*>>();
  154. test_range<RandomAccessIterator<int*>, Sentinel<int*>>();
  155. test_iter_rvalue<ForwardIterator<std::unique_ptr<int>*> >();
  156. test_iter_rvalue<BidirectionalIterator<std::unique_ptr<int>*> >();
  157. test_iter_rvalue<RandomAccessIterator<std::unique_ptr<int>*> >();
  158. test_iter_rvalue<std::unique_ptr<int>*>();
  159. test_iter_rvalue<ForwardIterator<std::unique_ptr<int>*>, Sentinel<std::unique_ptr<int>*>>();
  160. test_iter_rvalue<BidirectionalIterator<std::unique_ptr<int>*>, Sentinel<std::unique_ptr<int>*>>();
  161. test_iter_rvalue<RandomAccessIterator<std::unique_ptr<int>*>, Sentinel<std::unique_ptr<int>*>>();
  162. test_range_rvalue<ForwardIterator<std::unique_ptr<int>*> >();
  163. test_range_rvalue<BidirectionalIterator<std::unique_ptr<int>*> >();
  164. test_range_rvalue<RandomAccessIterator<std::unique_ptr<int>*> >();
  165. test_range_rvalue<std::unique_ptr<int>*>();
  166. test_range_rvalue<ForwardIterator<std::unique_ptr<int>*>, Sentinel<std::unique_ptr<int>*>>();
  167. test_range_rvalue<BidirectionalIterator<std::unique_ptr<int>*>, Sentinel<std::unique_ptr<int>*>>();
  168. test_range_rvalue<RandomAccessIterator<std::unique_ptr<int>*>, Sentinel<std::unique_ptr<int>*>>();
  169. {
  170. // Check projection
  171. S ia[] = {S{0}, S{1}, S{1}, S{1}, S{4}, S{2}, S{2}, S{4}, S{2}};
  172. constexpr auto sa = ranges::size(ia);
  173. S* r = ranges::adjacent_remove_if(ia, ranges::equal_to{}, &S::i);
  174. CHECK(r == ia + sa-3);
  175. CHECK(ia[0].i == 0);
  176. CHECK(ia[1].i == 1);
  177. CHECK(ia[2].i == 4);
  178. CHECK(ia[3].i == 2);
  179. CHECK(ia[4].i == 4);
  180. CHECK(ia[5].i == 2);
  181. }
  182. {
  183. // Check rvalue range
  184. S ia[] = {S{0}, S{1}, S{1}, S{2}, S{3}, S{5}, S{8}, S{13}, S{21}};
  185. constexpr auto sa = ranges::size(ia);
  186. using namespace std::placeholders;
  187. auto r = ranges::adjacent_remove_if(
  188. ranges::views::all(ia),
  189. [](int x, int y) noexcept { return (x + y) % 2 == 0; },
  190. &S::i);
  191. CHECK(r == ia + sa-3);
  192. CHECK(ia[0].i == 0);
  193. CHECK(ia[1].i == 1);
  194. CHECK(ia[2].i == 2);
  195. CHECK(ia[3].i == 5);
  196. CHECK(ia[4].i == 8);
  197. CHECK(ia[5].i == 21);
  198. }
  199. STATIC_CHECK(test_constexpr<ForwardIterator<int *>>());
  200. STATIC_CHECK(test_constexpr<BidirectionalIterator<int *>>());
  201. STATIC_CHECK(test_constexpr<RandomAccessIterator<int *>>());
  202. STATIC_CHECK(test_constexpr<int *>());
  203. STATIC_CHECK(test_constexpr<ForwardIterator<int *>, Sentinel<int *>>());
  204. STATIC_CHECK(test_constexpr<BidirectionalIterator<int *>, Sentinel<int *>>());
  205. STATIC_CHECK(test_constexpr<RandomAccessIterator<int *>, Sentinel<int *>>());
  206. return ::test_result();
  207. }