ends_with.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Range v3 library
  2. //
  3. // Copyright Johel Guerrero 2019
  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. #include <initializer_list>
  13. #include <range/v3/algorithm/ends_with.hpp>
  14. #include <range/v3/iterator/operations.hpp>
  15. #include <range/v3/view/subrange.hpp>
  16. #include "../simple_test.hpp"
  17. #include "../test_iterators.hpp"
  18. int comparison_count = 0;
  19. template<typename T>
  20. bool counting_equals(const T &a, const T &b)
  21. {
  22. ++comparison_count;
  23. return a == b;
  24. }
  25. int main()
  26. {
  27. using namespace ranges;
  28. int ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  29. constexpr auto as = distance(ia);
  30. int ib[] = {5, 6, 7, 8, 9};
  31. constexpr auto bs = distance(ib);
  32. CHECK(ends_with(RandomAccessIterator<const int*>(ia),
  33. RandomAccessIterator<const int*>(ia + as),
  34. RandomAccessIterator<const int*>(ib),
  35. RandomAccessIterator<const int*>(ib + bs)));
  36. CHECK(!ends_with(InputIterator<const int*>(ia),
  37. InputIterator<const int*, true>(ia + as),
  38. InputIterator<const int*>(ib),
  39. InputIterator<const int*, true>(ib + bs - 1)));
  40. CHECK(!ends_with(ForwardIterator<const int*>(ia),
  41. Sentinel<const int*>(ia + as),
  42. ForwardIterator<const int*>(ib),
  43. Sentinel<const int*>(ib + bs - 1)));
  44. CHECK(!ends_with(make_subrange(RandomAccessIterator<const int*>(ia),
  45. RandomAccessIterator<const int*>(ia + as)),
  46. make_subrange(RandomAccessIterator<const int*>(ib),
  47. RandomAccessIterator<const int*>(ib + bs - 1))));
  48. CHECK(ends_with(make_subrange(InputIterator<const int*>(ia),
  49. InputIterator<const int*, true>(ia + as)),
  50. make_subrange(InputIterator<const int*>(ib),
  51. InputIterator<const int*, true>(ib + bs))));
  52. CHECK(ends_with(make_subrange(ForwardIterator<const int*>(ia),
  53. Sentinel<const int*>(ia + as)),
  54. make_subrange(ForwardIterator<const int*>(ib),
  55. Sentinel<const int*>(ib + bs))));
  56. comparison_count = 0;
  57. CHECK(!ends_with(RandomAccessIterator<const int*>(ib),
  58. RandomAccessIterator<const int*>(ib + bs),
  59. RandomAccessIterator<const int*>(ia),
  60. RandomAccessIterator<const int*>(ia + as),
  61. counting_equals<int>));
  62. CHECK(comparison_count == 0);
  63. comparison_count = 0;
  64. CHECK(ends_with(InputIterator<const int*>(ia),
  65. InputIterator<const int*, true>(ia + as),
  66. InputIterator<const int*>(ib),
  67. InputIterator<const int*, true>(ib + bs),
  68. counting_equals<int>));
  69. CHECK(comparison_count > 0);
  70. comparison_count = 0;
  71. CHECK(ends_with(ForwardIterator<const int*>(ia),
  72. Sentinel<const int*>(ia + as),
  73. ForwardIterator<const int*>(ib),
  74. Sentinel<const int*>(ib + bs),
  75. counting_equals<int>));
  76. CHECK(comparison_count > 0);
  77. comparison_count = 0;
  78. CHECK(ends_with(make_subrange(RandomAccessIterator<const int*>(ia),
  79. RandomAccessIterator<const int*>(ia + as)),
  80. make_subrange(RandomAccessIterator<const int*>(ib),
  81. RandomAccessIterator<const int*>(ib + bs)),
  82. counting_equals<int>));
  83. CHECK(comparison_count > 0);
  84. comparison_count = 0;
  85. CHECK(!ends_with(make_subrange(InputIterator<const int*>(ib),
  86. InputIterator<const int*, true>(ib + bs - 1)),
  87. make_subrange(InputIterator<const int*>(ib),
  88. InputIterator<const int*, true>(ib + bs)),
  89. counting_equals<int>));
  90. CHECK(comparison_count == 0);
  91. comparison_count = 0;
  92. CHECK(!ends_with(make_subrange(ForwardIterator<const int*>(ia),
  93. Sentinel<const int*>(ia)),
  94. make_subrange(ForwardIterator<const int*>(ib),
  95. Sentinel<const int*>(ib + bs)),
  96. counting_equals<int>));
  97. CHECK(comparison_count == 0);
  98. #if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_14 && RANGES_CONSTEXPR_INVOKE
  99. using IL = std::initializer_list<int>;
  100. static_assert(ends_with(IL{0, 1, 2, 3, 4}, IL{3, 4}), "");
  101. static_assert(!ends_with(IL{0, 1, 2, 3, 4}, IL{2, 3}), "");
  102. static_assert(ends_with(IL{}, IL{}), "");
  103. #endif
  104. return ::test_result();
  105. }