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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
10 | |
11 | // <algorithm> |
12 | // |
13 | // Range algorithms should work with proxy iterators. For example, the implementations should use `iter_swap` (which is |
14 | // a customization point) rather than plain `swap` (which might not work with certain valid iterators). |
15 | |
16 | #include <algorithm> |
17 | |
18 | #include <array> |
19 | #include <concepts> |
20 | #include <initializer_list> |
21 | #include <iterator> |
22 | #include <memory> |
23 | #include <random> |
24 | #include <ranges> |
25 | |
26 | #include "MoveOnly.h" |
27 | #include "test_iterators.h" |
28 | #include "test_macros.h" |
29 | |
30 | // (in, ...) |
31 | template <class Func, std::ranges::range Input, class ...Args> |
32 | constexpr void test(Func&& func, Input& in, Args&& ...args) { |
33 | (void)func(in.begin(), in.end(), std::forward<Args>(args)...); |
34 | (void)func(in, std::forward<Args>(args)...); |
35 | } |
36 | |
37 | // (in1, in2, ...) |
38 | template <class Func, std::ranges::range Range1, std::ranges::range Range2, class ...Args> |
39 | constexpr void test(Func&& func, Range1& r1, Range2& r2, Args&& ...args) { |
40 | (void)func(r1.begin(), r1.end(), r2.begin(), r2.end(), std::forward<Args>(args)...); |
41 | (void)func(r1, r2, std::forward<Args>(args)...); |
42 | } |
43 | |
44 | // (in, mid, ...) |
45 | template <class Func, std::ranges::range Input, class ...Args> |
46 | constexpr void test_mid(Func&& func, Input& in, std::ranges::iterator_t<Input> mid, Args&& ...args) { |
47 | (void)func(in.begin(), mid, in.end(), std::forward<Args>(args)...); |
48 | (void)func(in, mid, std::forward<Args>(args)...); |
49 | } |
50 | |
51 | std::mt19937 rand_gen() { return std::mt19937(); } |
52 | |
53 | template <class T> |
54 | constexpr void run_tests() { |
55 | std::array input = {T{1}, T{2}, T{3}}; |
56 | ProxyRange in{input}; |
57 | std::array input2 = {T{4}, T{5}, T{6}}; |
58 | ProxyRange in2{input2}; |
59 | |
60 | auto mid = in.begin() + 1; |
61 | |
62 | std::array output = {T{4}, T{5}, T{6}, T{7}, T{8}, T{9}}; |
63 | ProxyIterator out{output.begin()}; |
64 | ProxyIterator out2{output.begin() + 1}; |
65 | ProxyIterator out_end{output.end()}; |
66 | |
67 | T num{2}; |
68 | Proxy<T&> x{num}; |
69 | int count = 1; |
70 | |
71 | auto unary_pred = [](const Proxy<T&>&) { return true; }; |
72 | auto binary_func = [](const Proxy<T>&, const Proxy<T>&) -> Proxy<T> { return Proxy<T>(T()); }; |
73 | auto gen = [] { return Proxy<T>(T{42}); }; |
74 | |
75 | test(std::ranges::any_of, in, unary_pred); |
76 | test(std::ranges::all_of, in, unary_pred); |
77 | #if TEST_STD_VER >= 23 |
78 | test(std::ranges::contains, in, x); |
79 | test(std::ranges::ends_with, in, in2); |
80 | #endif |
81 | test(std::ranges::none_of, in, unary_pred); |
82 | test(std::ranges::find, in, x); |
83 | test(std::ranges::find_if, in, unary_pred); |
84 | test(std::ranges::find_if_not, in, unary_pred); |
85 | test(std::ranges::find_first_of, in, in2); |
86 | test(std::ranges::adjacent_find, in); |
87 | test(std::ranges::mismatch, in, in2); |
88 | test(std::ranges::equal, in, in2); |
89 | test(std::ranges::lexicographical_compare, in, in2); |
90 | test(std::ranges::partition_point, in, unary_pred); |
91 | test(std::ranges::lower_bound, in, x); |
92 | test(std::ranges::upper_bound, in, x); |
93 | test(std::ranges::equal_range, in, x); |
94 | test(std::ranges::binary_search, in, x); |
95 | |
96 | test(std::ranges::min_element, in); |
97 | test(std::ranges::max_element, in); |
98 | test(std::ranges::minmax_element, in); |
99 | test(std::ranges::count, in, x); |
100 | test(std::ranges::count_if, in, unary_pred); |
101 | test(std::ranges::search, in, in2); |
102 | test(std::ranges::search_n, in, count, x); |
103 | test(std::ranges::find_end, in, in2); |
104 | test(std::ranges::is_partitioned, in, unary_pred); |
105 | test(std::ranges::is_sorted, in); |
106 | test(std::ranges::is_sorted_until, in); |
107 | test(std::ranges::includes, in, in2); |
108 | test(std::ranges::is_heap, in); |
109 | test(std::ranges::is_heap_until, in); |
110 | test(std::ranges::is_permutation, in, in2); |
111 | test(std::ranges::for_each, in, std::identity{}); |
112 | std::ranges::for_each_n(in.begin(), count, std::identity{}); |
113 | if constexpr (std::copyable<T>) { |
114 | test(std::ranges::copy, in, out); |
115 | std::ranges::copy_n(in.begin(), count, out); |
116 | test(std::ranges::copy_if, in, out, unary_pred); |
117 | test(std::ranges::copy_backward, in, out_end); |
118 | } |
119 | test(std::ranges::move, in, out); |
120 | test(std::ranges::move_backward, in, out_end); |
121 | if constexpr (std::copyable<T>) { |
122 | test(std::ranges::fill, in, x); |
123 | std::ranges::fill_n(in.begin(), count, x); |
124 | test(std::ranges::transform, in, out, std::identity{}); |
125 | test(std::ranges::transform, in, in2, out, binary_func); |
126 | } |
127 | test(std::ranges::generate, in, gen); |
128 | std::ranges::generate_n(in.begin(), count, gen); |
129 | if constexpr (std::copyable<T>) { |
130 | test(std::ranges::remove_copy, in, out, x); |
131 | test(std::ranges::remove_copy_if, in, out, unary_pred); |
132 | test(std::ranges::replace, in, x, x); |
133 | test(std::ranges::replace_if, in, unary_pred, x); |
134 | test(std::ranges::replace_copy, in, out, x, x); |
135 | test(std::ranges::replace_copy_if, in, out, unary_pred, x); |
136 | } |
137 | #if TEST_STD_VER > 20 |
138 | test(std::ranges::starts_with, in, in2); |
139 | #endif |
140 | test(std::ranges::swap_ranges, in, in2); |
141 | if constexpr (std::copyable<T>) { |
142 | test(std::ranges::reverse_copy, in, out); |
143 | test_mid(std::ranges::rotate_copy, in, mid, out); |
144 | test(std::ranges::unique_copy, in, out); |
145 | test(std::ranges::partition_copy, in, out, out2, unary_pred); |
146 | test(std::ranges::partial_sort_copy, in, in2); |
147 | test(std::ranges::merge, in, in2, out); |
148 | test(std::ranges::set_difference, in, in2, out); |
149 | test(std::ranges::set_intersection, in, in2, out); |
150 | test(std::ranges::set_symmetric_difference, in, in2, out); |
151 | test(std::ranges::set_union, in, in2, out); |
152 | } |
153 | test(std::ranges::remove, in, x); |
154 | test(std::ranges::remove_if, in, unary_pred); |
155 | test(std::ranges::reverse, in); |
156 | test_mid(std::ranges::rotate, in, mid); |
157 | if (!std::is_constant_evaluated()) // `shuffle` isn't `constexpr`. |
158 | test(std::ranges::shuffle, in, rand_gen()); |
159 | if (!std::is_constant_evaluated()) { |
160 | if constexpr (std::copyable<T>) |
161 | test(std::ranges::sample, in, out, count, rand_gen()); |
162 | } |
163 | test(std::ranges::unique, in); |
164 | test(std::ranges::partition, in, unary_pred); |
165 | if (!std::is_constant_evaluated()) |
166 | test(std::ranges::stable_partition, in, unary_pred); |
167 | test(std::ranges::sort, in); |
168 | if (!std::is_constant_evaluated()) |
169 | test(std::ranges::stable_sort, in); |
170 | test_mid(std::ranges::partial_sort, in, mid); |
171 | test_mid(std::ranges::nth_element, in, mid); |
172 | if (!std::is_constant_evaluated()) |
173 | test_mid(std::ranges::inplace_merge, in, mid); |
174 | test(std::ranges::make_heap, in); |
175 | test(std::ranges::push_heap, in); |
176 | test(std::ranges::pop_heap, in); |
177 | test(std::ranges::sort_heap, in); |
178 | test(std::ranges::prev_permutation, in); |
179 | test(std::ranges::next_permutation, in); |
180 | |
181 | // The algorithms that work on uninitialized memory have constraints that prevent proxy iterators from being used with |
182 | // them. |
183 | } |
184 | |
185 | constexpr bool test_all() { |
186 | run_tests<int>(); |
187 | run_tests<MoveOnly>(); |
188 | |
189 | return true; |
190 | } |
191 | |
192 | int main(int, char**) { |
193 | test_all(); |
194 | static_assert(test_all()); |
195 | |
196 | return 0; |
197 | } |
198 | |