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
9 // <algorithm>
10
11 // template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred, class T>
12 // requires OutputIterator<Iter, Iter::reference>
13 // && OutputIterator<Iter, const T&>
14 // && CopyConstructible<Pred>
15 // constexpr void // constexpr after C++17
16 // replace_if(Iter first, Iter last, Pred pred, const T& new_value);
17
18 #include <algorithm>
19 #include <functional>
20 #include <cassert>
21
22 #include "test_macros.h"
23 #include "test_iterators.h"
24
equalToTwo(int v)25 TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
26
27 #if TEST_STD_VER > 17
test_constexpr()28 TEST_CONSTEXPR bool test_constexpr() {
29 int ia[] = {0, 1, 2, 3, 4};
30 const int expected[] = {0, 1, 5, 3, 4};
31
32 std::replace_if(std::begin(ia), std::end(ia), equalToTwo, 5);
33 return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
34 ;
35 }
36 #endif
37
38
39 template <class Iter>
40 void
test()41 test()
42 {
43 int ia[] = {0, 1, 2, 3, 4};
44 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
45 std::replace_if(Iter(ia), Iter(ia+sa), equalToTwo, 5);
46 assert(ia[0] == 0);
47 assert(ia[1] == 1);
48 assert(ia[2] == 5);
49 assert(ia[3] == 3);
50 assert(ia[4] == 4);
51 }
52
main(int,char **)53 int main(int, char**)
54 {
55 test<forward_iterator<int*> >();
56 test<bidirectional_iterator<int*> >();
57 test<random_access_iterator<int*> >();
58 test<int*>();
59
60 #if TEST_STD_VER > 17
61 static_assert(test_constexpr());
62 #endif
63
64 return 0;
65 }
66