mismatch_pred.pass.cpp 4.1 KB

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