Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.replace / ranges.replace_if.pass.cpp
blob67e5e87af35be0018ee593ae15be9097f7a54388
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity,
14 // indirect_unary_predicate<projected<I, Proj>> Pred>
15 // requires indirectly_writable<I, const T&>
16 // constexpr I ranges::replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = {});
17 // template<input_range R, class T, class Proj = identity,
18 // indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
19 // requires indirectly_writable<iterator_t<R>, const T&>
20 // constexpr borrowed_iterator_t<R>
21 // ranges::replace_if(R&& r, Pred pred, const T& new_value, Proj proj = {});
23 #include <algorithm>
24 #include <array>
25 #include <cassert>
26 #include <ranges>
28 #include "almost_satisfies_types.h"
29 #include "boolean_testable.h"
30 #include "test_iterators.h"
32 struct FalsePredicate {
33 bool operator()(auto&&) { return false; }
36 template <class Iter, class Sent = sentinel_wrapper<Iter>>
37 concept HasReplaceIt = requires(Iter iter, Sent sent) { std::ranges::replace_if(iter, sent, FalsePredicate{}, 0); };
39 static_assert(HasReplaceIt<int*>);
40 static_assert(!HasReplaceIt<InputIteratorNotDerivedFrom>);
41 static_assert(!HasReplaceIt<InputIteratorNotIndirectlyReadable>);
42 static_assert(!HasReplaceIt<InputIteratorNotInputOrOutputIterator>);
43 static_assert(!HasReplaceIt<int*, SentinelForNotSemiregular>);
44 static_assert(!HasReplaceIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
45 static_assert(!HasReplaceIt<int**>); // not indirectly_writable
46 static_assert(!HasReplaceIt<IndirectBinaryPredicateNotIndirectlyReadable>);
48 template <class Range>
49 concept HasReplaceR = requires(Range range) { std::ranges::replace_if(range, FalsePredicate{}, 0); };
51 static_assert(HasReplaceR<UncheckedRange<int*>>);
52 static_assert(!HasReplaceR<InputRangeNotDerivedFrom>);
53 static_assert(!HasReplaceR<InputRangeNotIndirectlyReadable>);
54 static_assert(!HasReplaceR<InputRangeNotInputOrOutputIterator>);
55 static_assert(!HasReplaceR<InputRangeNotSentinelSemiregular>);
56 static_assert(!HasReplaceR<InputRangeNotSentinelEqualityComparableWith>);
57 static_assert(!HasReplaceR<UncheckedRange<int**>>); // not indirectly_writable
58 static_assert(!HasReplaceR<InputRangeIndirectBinaryPredicateNotIndirectlyReadable>);
60 template <class Iter, class Sent, int N, class Pred>
61 constexpr void test(std::array<int, N> a_, Pred pred, int val, std::array<int, N> expected) {
63 auto a = a_;
64 std::same_as<Iter> auto ret = std::ranges::replace_if(Iter(a.data()), Sent(Iter(a.data() + N)),
65 pred,
66 val);
67 assert(base(ret) == a.data() + N);
68 assert(a == expected);
71 auto a = a_;
72 auto range = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data() + N)));
73 std::same_as<Iter> auto ret = std::ranges::replace_if(range, pred, val);
74 assert(base(ret) == a.data() + N);
75 assert(a == expected);
79 template <class Iter, class Sent = Iter>
80 constexpr void test_iterators() {
81 // simple test
82 test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i < 3; }, 23, {23, 23, 3, 4});
83 // no match
84 test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i < 0; }, 23, {1, 2, 3, 4});
85 // all match
86 test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i > 0; }, 23, {23, 23, 23, 23});
87 // empty range
88 test<Iter, Sent, 0>({}, [](int i) { return i > 0; }, 23, {});
89 // single element range
90 test<Iter, Sent, 1>({1}, [](int i) { return i > 0; }, 2, {2});
93 constexpr bool test() {
94 test_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
95 test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
96 test_iterators<forward_iterator<int*>>();
97 test_iterators<bidirectional_iterator<int*>>();
98 test_iterators<random_access_iterator<int*>>();
99 test_iterators<contiguous_iterator<int*>>();
100 test_iterators<int*>();
102 { // check that the projection is used
103 struct S {
104 constexpr S(int i_) : i(i_) {}
105 int i;
108 S a[] = {1, 2, 3, 4};
109 std::ranges::replace_if(a, a + 4, [](int i) { return i == 3; }, S{0}, &S::i);
112 S a[] = {1, 2, 3, 4};
113 std::ranges::replace_if(a, [](int i) { return i == 3; }, S{0}, &S::i);
117 { // check that std::invoke is used
118 struct S {
119 constexpr S(int i_) : i(i_) {}
120 constexpr bool check() const { return false; }
121 constexpr const S& identity() const { return *this; }
122 int i;
125 S a[] = {1, 2, 3, 4};
126 auto ret = std::ranges::replace_if(std::begin(a), std::end(a), &S::check, S{2}, &S::identity);
127 assert(ret == std::end(a));
130 S a[] = {1, 2, 3, 4};
131 auto ret = std::ranges::replace_if(a, &S::check, S{2}, &S::identity);
132 assert(ret == std::end(a));
136 { // check that the implicit conversion to bool works
138 int a[] = {1, 2, 2, 4};
139 auto ret = std::ranges::replace_if(std::begin(a), std::end(a), [](int) { return BooleanTestable{false}; }, 2);
140 assert(ret == std::end(a));
143 int a[] = {1, 2, 2, 4};
144 auto ret = std::ranges::replace_if(a, [](int) { return BooleanTestable{false}; }, 2);
145 assert(ret == std::end(a));
149 { // check that std::ranges::dangling is returned
150 [[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
151 std::ranges::replace_if(std::array {1, 2, 3, 4}, [](int) { return false; }, 1);
154 { // check that the complexity requirements are met
156 int predicateCount = 0;
157 auto pred = [&](int) { ++predicateCount; return false; };
158 auto projectionCount = 0;
159 auto proj = [&](int i) { ++projectionCount; return i; };
160 int a[] = {1, 2, 3, 4, 5};
161 auto ret = std::ranges::replace_if(a, a + 5, pred, 1, proj);
162 assert(ret == a + 5);
163 assert(predicateCount == 5);
164 assert(projectionCount == 5);
167 int predicateCount = 0;
168 auto pred = [&](int) { ++predicateCount; return false; };
169 auto projectionCount = 0;
170 auto proj = [&](int i) { ++projectionCount; return i; };
171 int a[] = {1, 2, 3, 4, 5};
172 auto ret = std::ranges::replace_if(a, pred, 1, proj);
173 assert(ret == a + 5);
174 assert(predicateCount == 5);
175 assert(projectionCount == 5);
179 return true;
182 int main(int, char**) {
183 test();
184 static_assert(test());
186 return 0;