replace_copy_if.pass.cpp 3.5 KB

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