mismatch_pred.pass.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // pair<Iter1, Iter2>
  14. // mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
  15. #include <algorithm>
  16. #include <functional>
  17. #include <cassert>
  18. #include "test_iterators.h"
  19. #include "counting_predicates.hpp"
  20. #if _LIBCPP_STD_VER > 11
  21. #define HAS_FOUR_ITERATOR_VERSION
  22. #endif
  23. int main()
  24. {
  25. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  26. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  27. int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
  28. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  29. typedef input_iterator<const int*> II;
  30. typedef random_access_iterator<const int*> RAI;
  31. typedef std::equal_to<int> EQ;
  32. assert(std::mismatch(II(ia), II(ia + sa), II(ib), EQ())
  33. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  34. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), EQ())
  35. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  36. binary_counting_predicate<EQ, int> bcp((EQ()));
  37. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), std::ref(bcp))
  38. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  39. assert(bcp.count() > 0 && bcp.count() < sa);
  40. bcp.reset();
  41. #ifdef HAS_FOUR_ITERATOR_VERSION
  42. assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), EQ())
  43. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  44. assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), RAI(ib + sb), EQ())
  45. == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
  46. assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), std::ref(bcp))
  47. == (std::pair<II, II>(II(ia+3), II(ib+3))));
  48. assert(bcp.count() > 0 && bcp.count() < std::min(sa, sb));
  49. #endif
  50. assert(std::mismatch(ia, ia + sa, ib, EQ()) ==
  51. (std::pair<int*,int*>(ia+3,ib+3)));
  52. #ifdef HAS_FOUR_ITERATOR_VERSION
  53. assert(std::mismatch(ia, ia + sa, ib, ib + sb, EQ()) ==
  54. (std::pair<int*,int*>(ia+3,ib+3)));
  55. assert(std::mismatch(ia, ia + sa, ib, ib + 2, EQ()) ==
  56. (std::pair<int*,int*>(ia+2,ib+2)));
  57. #endif
  58. }