lower_bound.cpp 3.7 KB

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