mismatch_pred.pass.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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<InputIterator Iter1, InputIterator Iter2,
  10. // Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
  11. // requires CopyConstructible<Pred>
  12. // constexpr pair<Iter1, Iter2> // constexpr after c++17
  13. // mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
  14. //
  15. // template<InputIterator Iter1, InputIterator Iter2, Predicate Pred>
  16. // constexpr pair<Iter1, Iter2> // constexpr after c++17
  17. // mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred); // C++14
  18. #include <algorithm>
  19. #include <functional>
  20. #include <cassert>
  21. #include "test_macros.h"
  22. #include "test_iterators.h"
  23. #include "counting_predicates.h"
  24. #if TEST_STD_VER > 17
  25. TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
  26. TEST_CONSTEXPR bool test_constexpr() {
  27. int ia[] = {1, 3, 6, 7};
  28. int ib[] = {1, 3};
  29. int ic[] = {1, 3, 5, 7};
  30. typedef input_iterator<int*> II;
  31. typedef bidirectional_iterator<int*> BI;
  32. auto p1 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic), eq);
  33. if (p1.first != ia+2 || p1.second != ic+2)
  34. return false;
  35. auto p2 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic), std::end(ic), eq);
  36. if (p2.first != ia+2 || p2.second != ic+2)
  37. return false;
  38. auto p3 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic), eq);
  39. if (p3.first != ib+2 || p3.second != ic+2)
  40. return false;
  41. auto p4 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic), std::end(ic), eq);
  42. if (p4.first != ib+2 || p4.second != ic+2)
  43. return false;
  44. auto p5 = std::mismatch(II(std::begin(ib)), II(std::end(ib)), II(std::begin(ic)), eq);
  45. if (p5.first != II(ib+2) || p5.second != II(ic+2))
  46. return false;
  47. auto p6 = std::mismatch(BI(std::begin(ib)), BI(std::end(ib)), BI(std::begin(ic)), BI(std::end(ic)), eq);
  48. if (p6.first != BI(ib+2) || p6.second != BI(ic+2))
  49. return false;
  50. return true;
  51. }
  52. #endif
  53. #if TEST_STD_VER > 11
  54. #define HAS_FOUR_ITERATOR_VERSION
  55. #endif
  56. int main(int, char**)
  57. {
  58. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  59. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  60. int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
  61. const unsigned sb = sizeof(ib)/sizeof(ib[0]); ((void)sb); // unused in C++11
  62. typedef input_iterator<const int*> II;
  63. typedef random_access_iterator<const int*> RAI;
  64. typedef std::equal_to<int> EQ;
  65. assert(std::mismatch(II(ia), II(ia + sa), II(ib), EQ())
  66. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  67. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), EQ())
  68. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  69. binary_counting_predicate<EQ, int> bcp((EQ()));
  70. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), std::ref(bcp))
  71. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  72. assert(bcp.count() > 0 && bcp.count() < sa);
  73. bcp.reset();
  74. #if TEST_STD_VER >= 14
  75. assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), EQ())
  76. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  77. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), RAI(ib + sb), EQ())
  78. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  79. assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), std::ref(bcp))
  80. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  81. assert(bcp.count() > 0 && bcp.count() < std::min(sa, sb));
  82. #endif
  83. assert(std::mismatch(ia, ia + sa, ib, EQ()) ==
  84. (std::pair<int*,int*>(ia+3,ib+3)));
  85. #if TEST_STD_VER >= 14
  86. assert(std::mismatch(ia, ia + sa, ib, ib + sb, EQ()) ==
  87. (std::pair<int*,int*>(ia+3,ib+3)));
  88. assert(std::mismatch(ia, ia + sa, ib, ib + 2, EQ()) ==
  89. (std::pair<int*,int*>(ia+2,ib+2)));
  90. #endif
  91. #if TEST_STD_VER > 17
  92. static_assert(test_constexpr());
  93. #endif
  94. return 0;
  95. }