find_end.pass.cpp 3.7 KB

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