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