remove.cpp 6.3 KB

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