binary_search.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <range/v3/core.hpp>
  17. #include <range/v3/algorithm/binary_search.hpp>
  18. #include "../simple_test.hpp"
  19. int main()
  20. {
  21. using ranges::begin;
  22. using ranges::end;
  23. using ranges::size;
  24. using ranges::less;
  25. constexpr std::pair<int, int> a[] = {{0, 0}, {0, 1}, {1, 2}, {1, 3}, {3, 4}, {3, 5}};
  26. constexpr const std::pair<int, int> c[] = {
  27. {0, 0}, {0, 1}, {1, 2}, {1, 3}, {3, 4}, {3, 5}};
  28. CHECK(ranges::binary_search(begin(a), end(a), a[0]));
  29. CHECK(ranges::binary_search(begin(a), end(a), a[1], less()));
  30. CHECK(ranges::binary_search(begin(a), end(a), 1, less(), &std::pair<int, int>::first));
  31. CHECK(ranges::binary_search(a, a[2]));
  32. CHECK(ranges::binary_search(c, c[3]));
  33. CHECK(ranges::binary_search(a, a[4], less()));
  34. CHECK(ranges::binary_search(c, c[5], less()));
  35. CHECK(ranges::binary_search(a, 1, less(), &std::pair<int, int>::first));
  36. CHECK(ranges::binary_search(c, 1, less(), &std::pair<int, int>::first));
  37. CHECK(ranges::binary_search(a, 0, less(), &std::pair<int, int>::first));
  38. CHECK(ranges::binary_search(c, 0, less(), &std::pair<int, int>::first));
  39. CHECK(!ranges::binary_search(a, -1, less(), &std::pair<int, int>::first));
  40. CHECK(!ranges::binary_search(c, -1, less(), &std::pair<int, int>::first));
  41. CHECK(!ranges::binary_search(a, 4, less(), &std::pair<int, int>::first));
  42. CHECK(!ranges::binary_search(c, 4, less(), &std::pair<int, int>::first));
  43. STATIC_CHECK(ranges::binary_search(begin(a), end(a), a[0]));
  44. STATIC_CHECK(ranges::binary_search(begin(a), end(a), a[1], less()));
  45. STATIC_CHECK(ranges::binary_search(a, a[2]));
  46. STATIC_CHECK(ranges::binary_search(a, a[4], less()));
  47. STATIC_CHECK(!ranges::binary_search(a, std::make_pair(-1, -1), less()));
  48. return test_result();
  49. }