replace_if.pass.cpp 1.7 KB

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