search_n.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <vector>
  24. #include <range/v3/core.hpp>
  25. #include <range/v3/algorithm/search_n.hpp>
  26. #include <range/v3/view/counted.hpp>
  27. #include "../simple_test.hpp"
  28. #include "../test_utils.hpp"
  29. #include "../test_iterators.hpp"
  30. template<class Iter, typename Sent = Iter>
  31. void
  32. test_iter_impl()
  33. {
  34. int ia[] = {0, 1, 2, 3, 4, 5};
  35. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  36. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 0, 0).begin() == Iter(ia));
  37. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 0, 0).end() == Iter(ia));
  38. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 1, 0).begin() == Iter(ia+0));
  39. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 1, 0).end() == Iter(ia+1));
  40. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 2, 0).begin() == Iter(ia+sa));
  41. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 2, 0).end() == Iter(ia+sa));
  42. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), sa, 0).begin() == Iter(ia+sa));
  43. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), sa, 0).end() == Iter(ia+sa));
  44. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 0, 3).begin() == Iter(ia));
  45. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 0, 3).end() == Iter(ia));
  46. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 1, 3).begin() == Iter(ia+3));
  47. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 1, 3).end() == Iter(ia+4));
  48. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 2, 3).begin() == Iter(ia+sa));
  49. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 2, 3).end() == Iter(ia+sa));
  50. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), sa, 3).begin() == Iter(ia+sa));
  51. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), sa, 3).end() == Iter(ia+sa));
  52. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 0, 5).begin() == Iter(ia));
  53. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 0, 5).end() == Iter(ia));
  54. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 1, 5).begin() == Iter(ia+5));
  55. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 1, 5).end() == Iter(ia+6));
  56. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 2, 5).begin() == Iter(ia+sa));
  57. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), 2, 5).end() == Iter(ia+sa));
  58. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), sa, 5).begin() == Iter(ia+sa));
  59. CHECK(ranges::search_n(Iter(ia), Sent(ia+sa), sa, 5).end() == Iter(ia+sa));
  60. int ib[] = {0, 0, 1, 1, 2, 2};
  61. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  62. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 0, 0).begin() == Iter(ib));
  63. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 0, 0).end() == Iter(ib));
  64. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 1, 0).begin() == Iter(ib+0));
  65. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 1, 0).end() == Iter(ib+1));
  66. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 2, 0).begin() == Iter(ib+0));
  67. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 2, 0).end() == Iter(ib+2));
  68. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 3, 0).begin() == Iter(ib+sb));
  69. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 3, 0).end() == Iter(ib+sb));
  70. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), sb, 0).begin() == Iter(ib+sb));
  71. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), sb, 0).end() == Iter(ib+sb));
  72. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 0, 1).begin() == Iter(ib));
  73. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 0, 1).end() == Iter(ib));
  74. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 1, 1).begin() == Iter(ib+2));
  75. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 1, 1).end() == Iter(ib+3));
  76. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 2, 1).begin() == Iter(ib+2));
  77. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 2, 1).end() == Iter(ib+4));
  78. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 3, 1).begin() == Iter(ib+sb));
  79. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 3, 1).end() == Iter(ib+sb));
  80. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), sb, 1).begin() == Iter(ib+sb));
  81. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), sb, 1).end() == Iter(ib+sb));
  82. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 0, 2).begin() == Iter(ib));
  83. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 0, 2).end() == Iter(ib));
  84. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 1, 2).begin() == Iter(ib+4));
  85. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 1, 2).end() == Iter(ib+5));
  86. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 2, 2).begin() == Iter(ib+4));
  87. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 2, 2).end() == Iter(ib+6));
  88. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 3, 2).begin() == Iter(ib+sb));
  89. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), 3, 2).end() == Iter(ib+sb));
  90. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), sb, 2).begin() == Iter(ib+sb));
  91. CHECK(ranges::search_n(Iter(ib), Sent(ib+sb), sb, 2).end() == Iter(ib+sb));
  92. int ic[] = {0, 0, 0};
  93. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  94. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 0, 0).begin() == Iter(ic));
  95. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 0, 0).end() == Iter(ic));
  96. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 1, 0).begin() == Iter(ic));
  97. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 1, 0).end() == Iter(ic+1));
  98. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 2, 0).begin() == Iter(ic));
  99. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 2, 0).end() == Iter(ic+2));
  100. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 3, 0).begin() == Iter(ic));
  101. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 3, 0).end() == Iter(ic+3));
  102. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 4, 0).begin() == Iter(ic+sc));
  103. CHECK(ranges::search_n(Iter(ic), Sent(ic+sc), 4, 0).end() == Iter(ic+sc));
  104. }
  105. template<class Iter, class Iter2>
  106. void
  107. test_iter()
  108. {
  109. using Sent = typename sentinel_type<Iter>::type;
  110. test_iter_impl<Iter>();
  111. test_iter_impl<Iter, Sent>();
  112. using SizedSent1 = typename sentinel_type<Iter, true>::type;
  113. test_iter_impl<Iter, SizedSent1>();
  114. }
  115. template<class Iter, typename Sent = Iter>
  116. void
  117. test_range_impl()
  118. {
  119. int ia[] = {0, 1, 2, 3, 4, 5};
  120. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  121. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 0, 0).begin() == Iter(ia));
  122. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 1, 0).begin() == Iter(ia+0));
  123. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 2, 0).begin() == Iter(ia+sa));
  124. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), sa, 0).begin() == Iter(ia+sa));
  125. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 0, 3).begin() == Iter(ia));
  126. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 1, 3).begin() == Iter(ia+3));
  127. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 2, 3).begin() == Iter(ia+sa));
  128. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), sa, 3).begin() == Iter(ia+sa));
  129. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 0, 5).begin() == Iter(ia));
  130. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 1, 5).begin() == Iter(ia+5));
  131. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), 2, 5).begin() == Iter(ia+sa));
  132. CHECK(ranges::search_n(ranges::make_subrange(Iter(ia), Sent(ia+sa)), sa, 5).begin() == Iter(ia+sa));
  133. int ib[] = {0, 0, 1, 1, 2, 2};
  134. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  135. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 0, 0).begin() == Iter(ib));
  136. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 1, 0).begin() == Iter(ib+0));
  137. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 2, 0).begin() == Iter(ib+0));
  138. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 3, 0).begin() == Iter(ib+sb));
  139. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), sb, 0).begin() == Iter(ib+sb));
  140. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 0, 1).begin() == Iter(ib));
  141. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 1, 1).begin() == Iter(ib+2));
  142. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 2, 1).begin() == Iter(ib+2));
  143. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 3, 1).begin() == Iter(ib+sb));
  144. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), sb, 1).begin() == Iter(ib+sb));
  145. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 0, 2).begin() == Iter(ib));
  146. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 1, 2).begin() == Iter(ib+4));
  147. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 2, 2).begin() == Iter(ib+4));
  148. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), 3, 2).begin() == Iter(ib+sb));
  149. CHECK(ranges::search_n(ranges::make_subrange(Iter(ib), Sent(ib+sb)), sb, 2).begin() == Iter(ib+sb));
  150. int ic[] = {0, 0, 0};
  151. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  152. CHECK(ranges::search_n(ranges::make_subrange(Iter(ic), Sent(ic+sc)), 0, 0).begin() == Iter(ic));
  153. CHECK(ranges::search_n(ranges::make_subrange(Iter(ic), Sent(ic+sc)), 1, 0).begin() == Iter(ic));
  154. CHECK(ranges::search_n(ranges::make_subrange(Iter(ic), Sent(ic+sc)), 2, 0).begin() == Iter(ic));
  155. CHECK(ranges::search_n(ranges::make_subrange(Iter(ic), Sent(ic+sc)), 3, 0).begin() == Iter(ic));
  156. CHECK(ranges::search_n(ranges::make_subrange(Iter(ic), Sent(ic+sc)), 4, 0).begin() == Iter(ic+sc));
  157. }
  158. template<class Iter, class Iter2>
  159. void
  160. test_range()
  161. {
  162. using Sent = typename sentinel_type<Iter>::type;
  163. test_range_impl<Iter>();
  164. test_range_impl<Iter, Sent>();
  165. using SizedSent1 = typename sentinel_type<Iter, true>::type;
  166. test_range_impl<Iter, SizedSent1>();
  167. }
  168. template<class Iter, class Iter2>
  169. void
  170. test()
  171. {
  172. test_iter<Iter, Iter2>();
  173. test_range<Iter, Iter2>();
  174. }
  175. struct S
  176. {
  177. int i;
  178. };
  179. constexpr bool test_constexpr()
  180. {
  181. using namespace ranges;
  182. int ia[] = {0, 1, 2, 2, 4, 5};
  183. auto r = search_n(ia, 2, 2, equal_to{});
  184. STATIC_CHECK_RETURN(r.begin() == ia + 2);
  185. return true;
  186. }
  187. int main()
  188. {
  189. test<ForwardIterator<const int*>, ForwardIterator<const int*> >();
  190. test<ForwardIterator<const int*>, BidirectionalIterator<const int*> >();
  191. test<ForwardIterator<const int*>, RandomAccessIterator<const int*> >();
  192. test<BidirectionalIterator<const int*>, ForwardIterator<const int*> >();
  193. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*> >();
  194. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*> >();
  195. test<RandomAccessIterator<const int*>, ForwardIterator<const int*> >();
  196. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*> >();
  197. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*> >();
  198. // Test projections:
  199. {
  200. S const in[] = {{0}, {1}, {2}, {2}, {4}, {5}};
  201. auto sub = ranges::search_n(in, 2, 2, std::equal_to<int>{}, &S::i);
  202. CHECK(sub.begin() == in+2);
  203. CHECK(sub.end() == in+4);
  204. }
  205. // Test counted ranges
  206. {
  207. int in[] = {0,1,2,2,4,5};
  208. auto rng = ranges::views::counted(BidirectionalIterator<int*>(in), 6);
  209. auto sub = ranges::search_n(rng, 2, 2);
  210. CHECK(base(sub.begin().base()) == in+2);
  211. CHECK(base(sub.end().base()) == in+4);
  212. CHECK(sub.begin().count() == 4);
  213. CHECK(sub.end().count() == 2);
  214. auto sub2 = ranges::search_n(rng, 3, 2);
  215. CHECK(base(sub2.begin().base()) == in+6);
  216. CHECK(base(sub2.end().base()) == in+6);
  217. CHECK(sub2.begin().count() == 0);
  218. CHECK(sub2.end().count() == 0);
  219. }
  220. // Test rvalue ranges
  221. {
  222. int ib[] = {0, 0, 1, 1, 2, 2};
  223. CHECK(ranges::search_n(ranges::views::all(ib), 2, 1).begin() == ib+2);
  224. }
  225. #ifndef RANGES_WORKAROUND_MSVC_573728
  226. {
  227. int ib[] = {0, 0, 1, 1, 2, 2};
  228. CHECK(::is_dangling(ranges::search_n(std::move(ib), 2, 1)));
  229. }
  230. #endif // RANGES_WORKAROUND_MSVC_573728
  231. {
  232. std::vector<int> ib{0, 0, 1, 1, 2, 2};
  233. CHECK(::is_dangling(ranges::search_n(std::move(ib), 2, 1)));
  234. }
  235. {
  236. STATIC_CHECK(test_constexpr());
  237. }
  238. return ::test_result();
  239. }