replace_copy.pass.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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<InputIterator InIter, typename OutIter, class T>
  10. // requires OutputIterator<OutIter, InIter::reference>
  11. // && OutputIterator<OutIter, const T&>
  12. // && HasEqualTo<InIter::value_type, T>
  13. // constexpr OutIter // constexpr after C++17
  14. // replace_copy(InIter first, InIter last, OutIter result, const T& old_value,
  15. // 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. int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
  24. const int expected[] = {0, 1, 5, 3, 4};
  25. auto it = std::replace_copy(std::begin(ia), std::end(ia), std::begin(ib), 2, 5);
  26. return it == (std::begin(ib) + std::size(ia))
  27. && *it == 0 // don't overwrite the last value in the output array
  28. && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
  29. ;
  30. }
  31. #endif
  32. template <class InIter, class OutIter>
  33. void
  34. test()
  35. {
  36. int ia[] = {0, 1, 2, 3, 4};
  37. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  38. int ib[sa] = {0};
  39. OutIter r = std::replace_copy(InIter(ia), InIter(ia+sa), OutIter(ib), 2, 5);
  40. assert(base(r) == ib + sa);
  41. assert(ib[0] == 0);
  42. assert(ib[1] == 1);
  43. assert(ib[2] == 5);
  44. assert(ib[3] == 3);
  45. assert(ib[4] == 4);
  46. }
  47. int main(int, char**)
  48. {
  49. test<input_iterator<const int*>, output_iterator<int*> >();
  50. test<input_iterator<const int*>, forward_iterator<int*> >();
  51. test<input_iterator<const int*>, bidirectional_iterator<int*> >();
  52. test<input_iterator<const int*>, random_access_iterator<int*> >();
  53. test<input_iterator<const int*>, int*>();
  54. test<forward_iterator<const int*>, output_iterator<int*> >();
  55. test<forward_iterator<const int*>, forward_iterator<int*> >();
  56. test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
  57. test<forward_iterator<const int*>, random_access_iterator<int*> >();
  58. test<forward_iterator<const int*>, int*>();
  59. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  60. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  61. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  62. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  63. test<bidirectional_iterator<const int*>, int*>();
  64. test<random_access_iterator<const int*>, output_iterator<int*> >();
  65. test<random_access_iterator<const int*>, forward_iterator<int*> >();
  66. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  67. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  68. test<random_access_iterator<const int*>, int*>();
  69. test<const int*, output_iterator<int*> >();
  70. test<const int*, forward_iterator<int*> >();
  71. test<const int*, bidirectional_iterator<int*> >();
  72. test<const int*, random_access_iterator<int*> >();
  73. test<const int*, int*>();
  74. #if TEST_STD_VER > 17
  75. static_assert(test_constexpr());
  76. #endif
  77. return 0;
  78. }