find_first_of.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. //
  13. // The LLVM Compiler Infrastructure
  14. //
  15. // This file is dual licensed under the MIT and the University of Illinois Open
  16. // Source Licenses. See LICENSE.TXT for details.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include <range/v3/core.hpp>
  20. #include <range/v3/algorithm/find_first_of.hpp>
  21. #include "../simple_test.hpp"
  22. #include "../test_iterators.hpp"
  23. #include "../test_utils.hpp"
  24. namespace rng = ranges;
  25. void test_iter()
  26. {
  27. using namespace ranges;
  28. int ia[] = {0, 1, 2, 3, 0, 1, 2, 3};
  29. static constexpr auto sa = size(ia);
  30. int ib[] = {1, 3, 5, 7};
  31. static constexpr auto sb = size(ib);
  32. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  33. Sentinel<const int*>(ia + sa),
  34. ForwardIterator<const int*>(ib),
  35. Sentinel<const int*>(ib + sb)) ==
  36. InputIterator<const int*>(ia+1));
  37. int ic[] = {7};
  38. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  39. Sentinel<const int*>(ia + sa),
  40. ForwardIterator<const int*>(ic),
  41. Sentinel<const int*>(ic + 1)) ==
  42. InputIterator<const int*>(ia+sa));
  43. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  44. Sentinel<const int*>(ia + sa),
  45. ForwardIterator<const int*>(ic),
  46. Sentinel<const int*>(ic)) ==
  47. InputIterator<const int*>(ia+sa));
  48. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  49. Sentinel<const int*>(ia),
  50. ForwardIterator<const int*>(ic),
  51. Sentinel<const int*>(ic+1)) ==
  52. InputIterator<const int*>(ia));
  53. }
  54. void test_iter_pred()
  55. {
  56. using namespace ranges;
  57. int ia[] = {0, 1, 2, 3, 0, 1, 2, 3};
  58. static constexpr auto sa = size(ia);
  59. int ib[] = {1, 3, 5, 7};
  60. static constexpr auto sb = size(ib);
  61. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  62. Sentinel<const int*>(ia + sa),
  63. ForwardIterator<const int*>(ib),
  64. Sentinel<const int*>(ib + sb),
  65. std::equal_to<int>()) ==
  66. InputIterator<const int*>(ia+1));
  67. int ic[] = {7};
  68. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  69. Sentinel<const int*>(ia + sa),
  70. ForwardIterator<const int*>(ic),
  71. Sentinel<const int*>(ic + 1),
  72. std::equal_to<int>()) ==
  73. InputIterator<const int*>(ia+sa));
  74. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  75. Sentinel<const int*>(ia + sa),
  76. ForwardIterator<const int*>(ic),
  77. Sentinel<const int*>(ic),
  78. std::equal_to<int>()) ==
  79. InputIterator<const int*>(ia+sa));
  80. CHECK(rng::find_first_of(InputIterator<const int*>(ia),
  81. Sentinel<const int*>(ia),
  82. ForwardIterator<const int*>(ic),
  83. Sentinel<const int*>(ic+1),
  84. std::equal_to<int>()) ==
  85. InputIterator<const int*>(ia));
  86. }
  87. void test_rng()
  88. {
  89. using namespace ranges;
  90. int ia[] = {0, 1, 2, 3, 0, 1, 2, 3};
  91. static constexpr auto sa = size(ia);
  92. int ib[] = {1, 3, 5, 7};
  93. static constexpr auto sb = size(ib);
  94. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  95. InputIterator<const int*>(ia + sa)),
  96. make_subrange(ForwardIterator<const int*>(ib),
  97. ForwardIterator<const int*>(ib + sb))) ==
  98. InputIterator<const int*>(ia+1));
  99. CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator<const int*>(ia),
  100. InputIterator<const int*>(ia + sa)),
  101. make_subrange(ForwardIterator<const int*>(ib),
  102. ForwardIterator<const int*>(ib + sb)))));
  103. int ic[] = {7};
  104. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  105. InputIterator<const int*>(ia + sa)),
  106. make_subrange(ForwardIterator<const int*>(ic),
  107. ForwardIterator<const int*>(ic + 1))) ==
  108. InputIterator<const int*>(ia+sa));
  109. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  110. InputIterator<const int*>(ia + sa)),
  111. make_subrange(ForwardIterator<const int*>(ic),
  112. ForwardIterator<const int*>(ic))) ==
  113. InputIterator<const int*>(ia+sa));
  114. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  115. InputIterator<const int*>(ia)),
  116. make_subrange(ForwardIterator<const int*>(ic),
  117. ForwardIterator<const int*>(ic+1))) ==
  118. InputIterator<const int*>(ia));
  119. CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator<const int*>(ia),
  120. InputIterator<const int*>(ia + sa)),
  121. make_subrange(ForwardIterator<const int*>(ic),
  122. ForwardIterator<const int*>(ic + 1)))));
  123. CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator<const int*>(ia),
  124. InputIterator<const int*>(ia + sa)),
  125. make_subrange(ForwardIterator<const int*>(ic),
  126. ForwardIterator<const int*>(ic)))));
  127. CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator<const int*>(ia),
  128. InputIterator<const int*>(ia)),
  129. make_subrange(ForwardIterator<const int*>(ic),
  130. ForwardIterator<const int*>(ic+1)))));
  131. }
  132. void test_rng_pred()
  133. {
  134. using namespace ranges;
  135. int ia[] = {0, 1, 2, 3, 0, 1, 2, 3};
  136. static constexpr auto sa = size(ia);
  137. int ib[] = {1, 3, 5, 7};
  138. static constexpr auto sb = size(ib);
  139. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  140. InputIterator<const int*>(ia + sa)),
  141. make_subrange(ForwardIterator<const int*>(ib),
  142. ForwardIterator<const int*>(ib + sb)),
  143. std::equal_to<int>()) ==
  144. InputIterator<const int*>(ia+1));
  145. int ic[] = {7};
  146. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  147. InputIterator<const int*>(ia + sa)),
  148. make_subrange(ForwardIterator<const int*>(ic),
  149. ForwardIterator<const int*>(ic + 1)),
  150. std::equal_to<int>()) ==
  151. InputIterator<const int*>(ia+sa));
  152. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  153. InputIterator<const int*>(ia + sa)),
  154. make_subrange(ForwardIterator<const int*>(ic),
  155. ForwardIterator<const int*>(ic)),
  156. std::equal_to<int>()) ==
  157. InputIterator<const int*>(ia+sa));
  158. CHECK(rng::find_first_of(make_subrange(InputIterator<const int*>(ia),
  159. InputIterator<const int*>(ia)),
  160. make_subrange(ForwardIterator<const int*>(ic),
  161. ForwardIterator<const int*>(ic+1)),
  162. std::equal_to<int>()) ==
  163. InputIterator<const int*>(ia));
  164. }
  165. struct S
  166. {
  167. int i;
  168. };
  169. void test_rng_pred_proj()
  170. {
  171. using namespace ranges;
  172. S ia[] = {S{0}, S{1}, S{2}, S{3}, S{0}, S{1}, S{2}, S{3}};
  173. static constexpr auto sa = size(ia);
  174. S ib[] = {S{1}, S{3}, S{5}, S{7}};
  175. static constexpr auto sb = size(ib);
  176. CHECK(rng::find_first_of(make_subrange(InputIterator<const S*>(ia),
  177. InputIterator<const S*>(ia + sa)),
  178. make_subrange(ForwardIterator<const S*>(ib),
  179. ForwardIterator<const S*>(ib + sb)),
  180. std::equal_to<int>(), &S::i, &S::i) ==
  181. InputIterator<const S*>(ia+1));
  182. S ic[] = {S{7}};
  183. CHECK(rng::find_first_of(make_subrange(InputIterator<const S*>(ia),
  184. InputIterator<const S*>(ia + sa)),
  185. make_subrange(ForwardIterator<const S*>(ic),
  186. ForwardIterator<const S*>(ic + 1)),
  187. std::equal_to<int>(), &S::i, &S::i) ==
  188. InputIterator<const S*>(ia+sa));
  189. CHECK(rng::find_first_of(make_subrange(InputIterator<const S*>(ia),
  190. InputIterator<const S*>(ia + sa)),
  191. make_subrange(ForwardIterator<const S*>(ic),
  192. ForwardIterator<const S*>(ic)),
  193. std::equal_to<int>(), &S::i, &S::i) ==
  194. InputIterator<const S*>(ia+sa));
  195. CHECK(rng::find_first_of(make_subrange(InputIterator<const S*>(ia),
  196. InputIterator<const S*>(ia)),
  197. make_subrange(ForwardIterator<const S*>(ic),
  198. ForwardIterator<const S*>(ic+1)),
  199. std::equal_to<int>(), &S::i, &S::i) ==
  200. InputIterator<const S*>(ia));
  201. }
  202. void test_constexpr()
  203. {
  204. using namespace ranges;
  205. constexpr int ia[] = {0, 1, 2, 3, 0, 1, 2, 3};
  206. constexpr auto sa = size(ia);
  207. constexpr int ib[] = {1, 3, 5, 7};
  208. constexpr auto sb = size(ib);
  209. STATIC_CHECK(
  210. rng::find_first_of(as_lvalue(make_subrange(InputIterator<const int *>(ia),
  211. InputIterator<const int *>(ia + sa))),
  212. make_subrange(ForwardIterator<const int *>(ib),
  213. ForwardIterator<const int *>(ib + sb)),
  214. equal_to{}) == InputIterator<const int *>(ia + 1));
  215. constexpr int ic[] = {7};
  216. STATIC_CHECK(
  217. rng::find_first_of(as_lvalue(make_subrange(InputIterator<const int *>(ia),
  218. InputIterator<const int *>(ia + sa))),
  219. make_subrange(ForwardIterator<const int *>(ic),
  220. ForwardIterator<const int *>(ic + 1)),
  221. equal_to{}) == InputIterator<const int *>(ia + sa));
  222. STATIC_CHECK(
  223. rng::find_first_of(as_lvalue(make_subrange(InputIterator<const int *>(ia),
  224. InputIterator<const int *>(ia + sa))),
  225. make_subrange(ForwardIterator<const int *>(ic),
  226. ForwardIterator<const int *>(ic)),
  227. equal_to{}) == InputIterator<const int *>(ia + sa));
  228. STATIC_CHECK(
  229. rng::find_first_of(as_lvalue(make_subrange(InputIterator<const int *>(ia),
  230. InputIterator<const int *>(ia))),
  231. make_subrange(ForwardIterator<const int *>(ic),
  232. ForwardIterator<const int *>(ic + 1)),
  233. equal_to{}) == InputIterator<const int *>(ia));
  234. }
  235. int main()
  236. {
  237. ::test_iter();
  238. ::test_iter_pred();
  239. ::test_rng();
  240. ::test_rng_pred();
  241. ::test_rng_pred_proj();
  242. return ::test_result();
  243. }