replace.pass.cpp 1.6 KB

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