upper_bound.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <utility>
  16. #include <vector>
  17. #include <range/v3/core.hpp>
  18. #include <range/v3/algorithm/upper_bound.hpp>
  19. #include "../simple_test.hpp"
  20. #include "../test_iterators.hpp"
  21. struct my_int
  22. {
  23. int value;
  24. };
  25. bool compare(my_int lhs, my_int rhs)
  26. {
  27. return lhs.value < rhs.value;
  28. }
  29. void not_totally_ordered()
  30. {
  31. // This better compile!
  32. std::vector<my_int> vec;
  33. ranges::upper_bound(vec, my_int{10}, compare);
  34. }
  35. int main()
  36. {
  37. using ranges::begin;
  38. using ranges::end;
  39. using ranges::size;
  40. using ranges::less;
  41. using P = std::pair<int, int>;
  42. constexpr P a[] = {{0, 0}, {0, 1}, {1, 2}, {1, 3}, {3, 4}, {3, 5}};
  43. constexpr P const c[] = {{0, 0}, {0, 1}, {1, 2}, {1, 3}, {3, 4}, {3, 5}};
  44. CHECK(ranges::aux::upper_bound_n(begin(a), size(a), a[0]) == &a[1]);
  45. CHECK(ranges::aux::upper_bound_n(begin(a), size(a), a[1], less()) == &a[2]);
  46. CHECK(ranges::aux::upper_bound_n(begin(a), size(a), 1, less(), &std::pair<int, int>::first) == &a[4]);
  47. CHECK(ranges::upper_bound(begin(a), end(a), a[0]) == &a[1]);
  48. CHECK(ranges::upper_bound(begin(a), end(a), a[1], less()) == &a[2]);
  49. CHECK(ranges::upper_bound(begin(a), end(a), 1, less(), &std::pair<int, int>::first) == &a[4]);
  50. CHECK(ranges::upper_bound(a, a[2]) == &a[3]);
  51. CHECK(ranges::upper_bound(c, c[3]) == &c[4]);
  52. CHECK(ranges::upper_bound(a, a[4], less()) == &a[5]);
  53. CHECK(ranges::upper_bound(c, c[5], less()) == &c[6]);
  54. CHECK(ranges::upper_bound(a, 1, less(), &std::pair<int, int>::first) == &a[4]);
  55. CHECK(ranges::upper_bound(c, 1, less(), &std::pair<int, int>::first) == &c[4]);
  56. std::vector<P> vec_a(ranges::begin(a), ranges::end(a));
  57. std::vector<P> const vec_c(ranges::begin(c), ranges::end(c));
  58. CHECK(ranges::upper_bound(ranges::views::all(a), a[2]) == &a[3]);
  59. CHECK(ranges::upper_bound(ranges::views::all(c), c[3]) == &c[4]);
  60. #ifndef RANGES_WORKAROUND_MSVC_573728
  61. CHECK(::is_dangling(ranges::upper_bound(std::move(a), a[2])));
  62. CHECK(::is_dangling(ranges::upper_bound(std::move(c), c[3])));
  63. #endif // RANGES_WORKAROUND_MSVC_573728
  64. CHECK(::is_dangling(ranges::upper_bound(std::move(vec_a), vec_a[2])));
  65. CHECK(::is_dangling(ranges::upper_bound(std::move(vec_c), vec_c[3])));
  66. CHECK(ranges::upper_bound(ranges::views::all(a), a[4], less()) == &a[5]);
  67. CHECK(ranges::upper_bound(ranges::views::all(c), c[5], less()) == &c[6]);
  68. #ifndef RANGES_WORKAROUND_MSVC_573728
  69. CHECK(::is_dangling(ranges::upper_bound(std::move(a), a[4], less())));
  70. CHECK(::is_dangling(ranges::upper_bound(std::move(c), c[5], less())));
  71. #endif // RANGES_WORKAROUND_MSVC_573728
  72. CHECK(::is_dangling(ranges::upper_bound(std::move(vec_a), vec_a[4], less())));
  73. CHECK(::is_dangling(ranges::upper_bound(std::move(vec_c), vec_c[5], less())));
  74. CHECK(ranges::upper_bound(ranges::views::all(a), 1, less(), &std::pair<int, int>::first) == &a[4]);
  75. CHECK(ranges::upper_bound(ranges::views::all(c), 1, less(), &std::pair<int, int>::first) == &c[4]);
  76. #ifndef RANGES_WORKAROUND_MSVC_573728
  77. CHECK(::is_dangling(ranges::upper_bound(std::move(a), 1, less(), &std::pair<int, int>::first)));
  78. CHECK(::is_dangling(ranges::upper_bound(std::move(c), 1, less(), &std::pair<int, int>::first)));
  79. #endif // RANGES_WORKAROUND_MSVC_573728
  80. CHECK(::is_dangling(ranges::upper_bound(std::move(vec_a), 1, less(), &std::pair<int, int>::first)));
  81. CHECK(::is_dangling(ranges::upper_bound(std::move(vec_c), 1, less(), &std::pair<int, int>::first)));
  82. {
  83. using namespace ranges;
  84. STATIC_CHECK(aux::upper_bound_n(begin(a), size(a), a[0]) == &a[1]);
  85. STATIC_CHECK(aux::upper_bound_n(begin(a), size(a), a[1], less()) == &a[2]);
  86. STATIC_CHECK(upper_bound(begin(a), end(a), a[0]) == &a[1]);
  87. STATIC_CHECK(upper_bound(begin(a), end(a), a[1], less()) == &a[2]);
  88. STATIC_CHECK(upper_bound(a, a[2]) == &a[3]);
  89. STATIC_CHECK(upper_bound(a, a[3], less()) == &a[4]);
  90. STATIC_CHECK(upper_bound(a, std::make_pair(1, 3), less()) == &a[4]);
  91. #if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_17
  92. // requires constexpr std::addressof
  93. STATIC_CHECK(upper_bound(views::all(a), std::make_pair(1, 3), less()) == &a[4]);
  94. #endif
  95. }
  96. return test_result();
  97. }