find_if.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <utility>
  20. #include <vector>
  21. #include <range/v3/core.hpp>
  22. #include <range/v3/algorithm/find_if.hpp>
  23. #include "../simple_test.hpp"
  24. #include "../test_iterators.hpp"
  25. struct S
  26. {
  27. int i_;
  28. };
  29. constexpr bool is_three(int i)
  30. {
  31. return i == 3;
  32. }
  33. template<class Rng>
  34. constexpr bool contains_three(Rng r)
  35. {
  36. auto it = ranges::find_if(r, is_three);
  37. return it != ranges::end(r);
  38. }
  39. int main()
  40. {
  41. using namespace ranges;
  42. int ia[] = {0, 1, 2, 3, 4, 5};
  43. constexpr auto s = size(ia);
  44. {
  45. InputIterator<const int*> r = find_if(InputIterator<const int*>(ia),
  46. InputIterator<const int*>(ia + s),
  47. [](int i){return i == 3;});
  48. CHECK(*r == 3);
  49. r = find_if(InputIterator<const int*>(ia),
  50. InputIterator<const int*>(ia+s),
  51. [](int i){return i == 10;});
  52. CHECK(r == InputIterator<const int*>(ia+s));
  53. r = find_if(InputIterator<const int*>(ia),
  54. Sentinel<const int*>(ia+s),
  55. [](int i){return i == 3;});
  56. CHECK(*r == 3);
  57. r = find_if(InputIterator<const int*>(ia),
  58. Sentinel<const int*>(ia+s),
  59. [](int i){return i == 10;});
  60. CHECK(r == InputIterator<const int*>(ia+s));
  61. }
  62. {
  63. int *pi = find_if(ia, [](int i){return i == 3;});
  64. CHECK(*pi == 3);
  65. pi = find_if(ia, [](int i){return i == 10;});
  66. CHECK(pi == ia+s);
  67. }
  68. #ifndef RANGES_WORKAROUND_MSVC_573728
  69. {
  70. auto pj0 = find_if(std::move(ia), [](int i){return i == 3;});
  71. CHECK(::is_dangling(pj0));
  72. auto pj1 = find_if(std::move(ia), [](int i){return i == 10;});
  73. CHECK(::is_dangling(pj1));
  74. }
  75. #endif // RANGES_WORKAROUND_MSVC_573728
  76. {
  77. std::vector<int> const vec(begin(ia), end(ia));
  78. auto pj0 = find_if(std::move(vec), [](int i){return i == 3;});
  79. CHECK(::is_dangling(pj0));
  80. auto pj1 = find_if(std::move(vec), [](int i){return i == 10;});
  81. CHECK(::is_dangling(pj1));
  82. }
  83. {
  84. auto* ignore = find_if(ranges::views::all(ia), [](int i){return i == 10;});
  85. (void)ignore;
  86. }
  87. {
  88. S sa[] = {{0}, {1}, {2}, {3}, {4}, {5}};
  89. S *ps = find_if(sa, [](int i){return i == 3;}, &S::i_);
  90. CHECK(ps->i_ == 3);
  91. ps = find_if(sa, [](int i){return i == 10;}, &S::i_);
  92. CHECK(ps == end(sa));
  93. }
  94. {
  95. using IL = std::initializer_list<int>;
  96. STATIC_CHECK(contains_three(IL{0, 1, 2, 3}));
  97. STATIC_CHECK(!contains_three(IL{0, 1, 2}));
  98. }
  99. return ::test_result();
  100. }