[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.replace / replace_copy_if.pass.cpp
blob26cbd705f49b8cfc7568959d20a58d2d85cddaef
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
11 // template<InputIterator InIter, typename OutIter,
12 // Predicate<auto, InIter::value_type> Pred, class T>
13 // requires OutputIterator<OutIter, InIter::reference>
14 // && OutputIterator<OutIter, const T&>
15 // && CopyConstructible<Pred>
16 // constexpr OutIter // constexpr after C++17
17 // replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value);
19 #include <algorithm>
20 #include <functional>
21 #include <cassert>
23 #include "test_macros.h"
24 #include "test_iterators.h"
26 TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
29 #if TEST_STD_VER > 17
30 TEST_CONSTEXPR bool test_constexpr() {
31 int ia[] = {0, 1, 2, 3, 4};
32 int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
33 const int expected[] = {0, 1, 5, 3, 4};
35 auto it = std::replace_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo, 5);
37 return it == (std::begin(ib) + std::size(ia))
38 && *it == 0 // don't overwrite the last value in the output array
39 && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
42 #endif
44 template <class InIter, class OutIter>
45 void
46 test()
48 int ia[] = {0, 1, 2, 3, 4};
49 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
50 int ib[sa] = {0};
51 OutIter r = std::replace_copy_if(InIter(ia), InIter(ia+sa),
52 OutIter(ib), equalToTwo, 5);
53 assert(base(r) == ib + sa);
54 assert(ib[0] == 0);
55 assert(ib[1] == 1);
56 assert(ib[2] == 5);
57 assert(ib[3] == 3);
58 assert(ib[4] == 4);
61 int main(int, char**)
63 test<input_iterator<const int*>, output_iterator<int*> >();
64 test<input_iterator<const int*>, forward_iterator<int*> >();
65 test<input_iterator<const int*>, bidirectional_iterator<int*> >();
66 test<input_iterator<const int*>, random_access_iterator<int*> >();
67 test<input_iterator<const int*>, int*>();
69 test<forward_iterator<const int*>, output_iterator<int*> >();
70 test<forward_iterator<const int*>, forward_iterator<int*> >();
71 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
72 test<forward_iterator<const int*>, random_access_iterator<int*> >();
73 test<forward_iterator<const int*>, int*>();
75 test<bidirectional_iterator<const int*>, output_iterator<int*> >();
76 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
77 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
78 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
79 test<bidirectional_iterator<const int*>, int*>();
81 test<random_access_iterator<const int*>, output_iterator<int*> >();
82 test<random_access_iterator<const int*>, forward_iterator<int*> >();
83 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
84 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
85 test<random_access_iterator<const int*>, int*>();
87 test<const int*, output_iterator<int*> >();
88 test<const int*, forward_iterator<int*> >();
89 test<const int*, bidirectional_iterator<int*> >();
90 test<const int*, random_access_iterator<int*> >();
91 test<const int*, int*>();
93 #if TEST_STD_VER > 17
94 static_assert(test_constexpr());
95 #endif
97 return 0;