replace_copy.pass.cpp 3.4 KB

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