replace_copy_if.pass.cpp 3.5 KB

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