replace.pass.cpp 1.6 KB

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