adjacent_find_pred.pass.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 Iter, EquivalenceRelation<auto, Iter::value_type> Pred>
  10. // requires CopyConstructible<Pred>
  11. // constexpr Iter // constexpr after C++17
  12. // adjacent_find(Iter first, Iter last, Pred pred);
  13. #include <algorithm>
  14. #include <functional>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_iterators.h"
  18. #if TEST_STD_VER > 17
  19. TEST_CONSTEXPR bool eq (int a, int b) { return a == b; }
  20. TEST_CONSTEXPR bool test_constexpr() {
  21. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  22. int ib[] = {0, 1, 2, 7, 0, 1, 2, 3};
  23. return (std::adjacent_find(std::begin(ia), std::end(ia), eq) == ia+2)
  24. && (std::adjacent_find(std::begin(ib), std::end(ib), eq) == std::end(ib))
  25. ;
  26. }
  27. #endif
  28. int main(int, char**)
  29. {
  30. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  31. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  32. assert(std::adjacent_find(forward_iterator<const int*>(ia),
  33. forward_iterator<const int*>(ia + sa),
  34. std::equal_to<int>()) ==
  35. forward_iterator<const int*>(ia+2));
  36. assert(std::adjacent_find(forward_iterator<const int*>(ia),
  37. forward_iterator<const int*>(ia),
  38. std::equal_to<int>()) ==
  39. forward_iterator<const int*>(ia));
  40. assert(std::adjacent_find(forward_iterator<const int*>(ia+3),
  41. forward_iterator<const int*>(ia + sa),
  42. std::equal_to<int>()) ==
  43. forward_iterator<const int*>(ia+sa));
  44. #if TEST_STD_VER > 17
  45. static_assert(test_constexpr());
  46. #endif
  47. return 0;
  48. }