find_end.pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // requires HasEqualTo<Iter1::value_type, Iter2::value_type>
  12. // constexpr Iter1 // constexpr after C++17
  13. // find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
  14. #include <algorithm>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_iterators.h"
  18. #if TEST_STD_VER > 17
  19. TEST_CONSTEXPR bool test_constexpr() {
  20. int ia[] = {0, 1, 2};
  21. int ib[] = {4, 5, 6};
  22. int ic[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
  23. typedef forward_iterator<int*> FI;
  24. typedef bidirectional_iterator<int*> BI;
  25. typedef random_access_iterator<int*> RI;
  26. return (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ia)), FI(std::end(ia))) == FI(ic+15))
  27. && (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ib)), FI(std::end(ib))) == FI(std::end(ic)))
  28. && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ia)), BI(std::end(ia))) == BI(ic+15))
  29. && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ib)), BI(std::end(ib))) == BI(std::end(ic)))
  30. && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ia)), RI(std::end(ia))) == RI(ic+15))
  31. && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ib)), RI(std::end(ib))) == RI(std::end(ic)))
  32. ;
  33. }
  34. #endif
  35. template <class Iter1, class Iter2>
  36. void
  37. test()
  38. {
  39. int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
  40. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  41. int b[] = {0};
  42. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1)) == Iter1(ia+sa-1));
  43. int c[] = {0, 1};
  44. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2)) == Iter1(ia+18));
  45. int d[] = {0, 1, 2};
  46. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3)) == Iter1(ia+15));
  47. int e[] = {0, 1, 2, 3};
  48. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4)) == Iter1(ia+11));
  49. int f[] = {0, 1, 2, 3, 4};
  50. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5)) == Iter1(ia+6));
  51. int g[] = {0, 1, 2, 3, 4, 5};
  52. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6)) == Iter1(ia));
  53. int h[] = {0, 1, 2, 3, 4, 5, 6};
  54. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7)) == Iter1(ia+sa));
  55. assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b)) == Iter1(ia+sa));
  56. assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1)) == Iter1(ia));
  57. }
  58. int main()
  59. {
  60. test<forward_iterator<const int*>, forward_iterator<const int*> >();
  61. test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
  62. test<forward_iterator<const int*>, random_access_iterator<const int*> >();
  63. test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
  64. test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
  65. test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
  66. test<random_access_iterator<const int*>, forward_iterator<const int*> >();
  67. test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
  68. test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
  69. #if TEST_STD_VER > 17
  70. static_assert(test_constexpr());
  71. #endif
  72. }