find_end_pred.pass.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // template<ForwardIterator Iter1, ForwardIterator Iter2,
  11. // Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
  12. // requires CopyConstructible<Pred>
  13. // constexpr Iter1 // constexpr after C++17
  14. // find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred);
  15. #include <algorithm>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "test_iterators.h"
  19. struct count_equal
  20. {
  21. static unsigned count;
  22. template <class T>
  23. TEST_CONSTEXPR_CXX14 bool operator()(const T& x, const T& y)
  24. {++count; return x == y;}
  25. };
  26. #if TEST_STD_VER > 17
  27. constexpr bool test_constexpr() {
  28. int ia[] = {0, 1, 2};
  29. int ib[] = {4, 5, 6};
  30. int ic[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
  31. typedef forward_iterator<int*> FI;
  32. typedef bidirectional_iterator<int*> BI;
  33. typedef random_access_iterator<int*> RI;
  34. std::equal_to<int> eq{};
  35. return (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ia)), FI(std::end(ia)), eq) == FI(ic+15))
  36. && (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ib)), FI(std::end(ib)), eq) == FI(std::end(ic)))
  37. && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ia)), BI(std::end(ia)), eq) == BI(ic+15))
  38. && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ib)), BI(std::end(ib)), eq) == BI(std::end(ic)))
  39. && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ia)), RI(std::end(ia)), eq) == RI(ic+15))
  40. && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ib)), RI(std::end(ib)), eq) == RI(std::end(ic)))
  41. ;
  42. }
  43. #endif
  44. unsigned count_equal::count = 0;
  45. template <class Iter1, class Iter2>
  46. void
  47. test()
  48. {
  49. int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
  50. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  51. int b[] = {0};
  52. count_equal::count = 0;
  53. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia+sa-1));
  54. assert(count_equal::count <= 1*(sa-1+1));
  55. int c[] = {0, 1};
  56. count_equal::count = 0;
  57. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2), count_equal()) == Iter1(ia+18));
  58. assert(count_equal::count <= 2*(sa-2+1));
  59. int d[] = {0, 1, 2};
  60. count_equal::count = 0;
  61. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3), count_equal()) == Iter1(ia+15));
  62. assert(count_equal::count <= 3*(sa-3+1));
  63. int e[] = {0, 1, 2, 3};
  64. count_equal::count = 0;
  65. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4), count_equal()) == Iter1(ia+11));
  66. assert(count_equal::count <= 4*(sa-4+1));
  67. int f[] = {0, 1, 2, 3, 4};
  68. count_equal::count = 0;
  69. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5), count_equal()) == Iter1(ia+6));
  70. assert(count_equal::count <= 5*(sa-5+1));
  71. int g[] = {0, 1, 2, 3, 4, 5};
  72. count_equal::count = 0;
  73. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6), count_equal()) == Iter1(ia));
  74. assert(count_equal::count <= 6*(sa-6+1));
  75. int h[] = {0, 1, 2, 3, 4, 5, 6};
  76. count_equal::count = 0;
  77. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7), count_equal()) == Iter1(ia+sa));
  78. assert(count_equal::count <= 7*(sa-7+1));
  79. count_equal::count = 0;
  80. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b), count_equal()) == Iter1(ia+sa));
  81. assert(count_equal::count <= 0);
  82. count_equal::count = 0;
  83. assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia));
  84. assert(count_equal::count <= 0);
  85. }
  86. int main()
  87. {
  88. test<forward_iterator<const int*>, forward_iterator<const int*> >();
  89. test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
  90. test<forward_iterator<const int*>, random_access_iterator<const int*> >();
  91. test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
  92. test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
  93. test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
  94. test<random_access_iterator<const int*>, forward_iterator<const int*> >();
  95. test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
  96. test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
  97. #if TEST_STD_VER > 17
  98. static_assert(test_constexpr());
  99. #endif
  100. }