mismatch.pass.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // requires HasEqualTo<Iter1::value_type, Iter2::value_type>
  12. // constexpr pair<Iter1, Iter2> // constexpr after c++17
  13. // mismatch(Iter1 first1, Iter1 last1, Iter2 first2);
  14. //
  15. // template<InputIterator Iter1, InputIterator Iter2Pred>
  16. // constexpr pair<Iter1, Iter2> // constexpr after c++17
  17. // mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2); // C++14
  18. #include <algorithm>
  19. #include <cassert>
  20. #include "test_macros.h"
  21. #include "test_iterators.h"
  22. #if TEST_STD_VER > 17
  23. TEST_CONSTEXPR bool test_constexpr() {
  24. int ia[] = {1, 3, 6, 7};
  25. int ib[] = {1, 3};
  26. int ic[] = {1, 3, 5, 7};
  27. typedef input_iterator<int*> II;
  28. typedef bidirectional_iterator<int*> BI;
  29. auto p1 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic));
  30. if (p1.first != ia+2 || p1.second != ic+2)
  31. return false;
  32. auto p2 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic), std::end(ic));
  33. if (p2.first != ia+2 || p2.second != ic+2)
  34. return false;
  35. auto p3 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic));
  36. if (p3.first != ib+2 || p3.second != ic+2)
  37. return false;
  38. auto p4 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic), std::end(ic));
  39. if (p4.first != ib+2 || p4.second != ic+2)
  40. return false;
  41. auto p5 = std::mismatch(II(std::begin(ib)), II(std::end(ib)), II(std::begin(ic)));
  42. if (p5.first != II(ib+2) || p5.second != II(ic+2))
  43. return false;
  44. auto p6 = std::mismatch(BI(std::begin(ib)), BI(std::end(ib)), BI(std::begin(ic)), BI(std::end(ic)));
  45. if (p6.first != BI(ib+2) || p6.second != BI(ic+2))
  46. return false;
  47. return true;
  48. }
  49. #endif
  50. int main()
  51. {
  52. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  53. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  54. int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
  55. const unsigned sb = sizeof(ib)/sizeof(ib[0]); ((void)sb); // unused in C++11
  56. typedef input_iterator<const int*> II;
  57. typedef random_access_iterator<const int*> RAI;
  58. assert(std::mismatch(II(ia), II(ia + sa), II(ib))
  59. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  60. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib))
  61. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  62. #if TEST_STD_VER > 11 // We have the four iteration version
  63. assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib+sb))
  64. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  65. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), RAI(ib+sb))
  66. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  67. assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib+2))
  68. == (std::pair<II, II>(II(ia+2), II(ib+2))));
  69. #endif
  70. #if TEST_STD_VER > 17
  71. static_assert(test_constexpr());
  72. #endif
  73. }