search.cpp 12 KB

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