find.cpp 3.0 KB

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