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