replace_if.pass.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, Predicate<auto, Iter::value_type> Pred, class T>
  10. // requires OutputIterator<Iter, Iter::reference>
  11. // && OutputIterator<Iter, const T&>
  12. // && CopyConstructible<Pred>
  13. // constexpr void // constexpr after C++17
  14. // replace_if(Iter first, Iter last, Pred pred, const T& new_value);
  15. #include <algorithm>
  16. #include <functional>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. #include "test_iterators.h"
  20. TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
  21. #if TEST_STD_VER > 17
  22. TEST_CONSTEXPR bool test_constexpr() {
  23. int ia[] = {0, 1, 2, 3, 4};
  24. const int expected[] = {0, 1, 5, 3, 4};
  25. std::replace_if(std::begin(ia), std::end(ia), equalToTwo, 5);
  26. return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
  27. ;
  28. }
  29. #endif
  30. template <class Iter>
  31. void
  32. test()
  33. {
  34. int ia[] = {0, 1, 2, 3, 4};
  35. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  36. std::replace_if(Iter(ia), Iter(ia+sa), equalToTwo, 5);
  37. assert(ia[0] == 0);
  38. assert(ia[1] == 1);
  39. assert(ia[2] == 5);
  40. assert(ia[3] == 3);
  41. assert(ia[4] == 4);
  42. }
  43. int main(int, char**)
  44. {
  45. test<forward_iterator<int*> >();
  46. test<bidirectional_iterator<int*> >();
  47. test<random_access_iterator<int*> >();
  48. test<int*>();
  49. #if TEST_STD_VER > 17
  50. static_assert(test_constexpr());
  51. #endif
  52. return 0;
  53. }