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 #include <algorithm>
12 #include <cstddef>
13 #include <functional>
14 #include <iterator>
15
16 #include "test_macros.h"
17
18 struct Incomplete;
19 template<class T> struct Holder { T t; };
20
21 template<class>
22 struct ConvertibleToIntegral {
operator intConvertibleToIntegral23 TEST_CONSTEXPR operator int() const { return 1; }
24 };
25
26 struct Tester {
27 using Element = Holder<Incomplete>*;
28 Element data[10] = {};
29 };
30
operator ()UnaryVoid31 struct UnaryVoid { TEST_CONSTEXPR_CXX14 void operator()(void*) const {} };
operator ()UnaryTrue32 struct UnaryTrue { TEST_CONSTEXPR bool operator()(void*) const { return true; } };
operator ()NullaryValue33 struct NullaryValue { TEST_CONSTEXPR std::nullptr_t operator()() const { return nullptr; } };
operator ()UnaryTransform34 struct UnaryTransform { TEST_CONSTEXPR std::nullptr_t operator()(void*) const { return nullptr; } };
operator ()BinaryTransform35 struct BinaryTransform { TEST_CONSTEXPR std::nullptr_t operator()(void*, void*) const { return nullptr; } };
36
all_the_algorithms()37 TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
38 {
39 Tester t;
40 Tester u;
41 Holder<Incomplete> **first = t.data;
42 Holder<Incomplete> **mid = t.data+5;
43 Holder<Incomplete> **last = t.data+10;
44 Holder<Incomplete> **first2 = u.data;
45 Holder<Incomplete> **mid2 = u.data+5;
46 Holder<Incomplete> **last2 = u.data+10;
47 Tester::Element value = nullptr;
48 ConvertibleToIntegral<Tester::Element> count;
49
50 (void)std::adjacent_find(first, last);
51 (void)std::adjacent_find(first, last, std::equal_to<void*>());
52 #if TEST_STD_VER >= 11
53 (void)std::all_of(first, last, UnaryTrue());
54 (void)std::any_of(first, last, UnaryTrue());
55 #endif
56 (void)std::binary_search(first, last, value);
57 (void)std::binary_search(first, last, value, std::less<void*>());
58 #if TEST_STD_VER > 17
59 (void)std::clamp(value, value, value);
60 (void)std::clamp(value, value, value, std::less<void*>());
61 #endif
62 (void)std::copy(first, last, first2);
63 (void)std::copy_backward(first, last, last2);
64 (void)std::copy_n(first, count, first2);
65 (void)std::count(first, last, value);
66 (void)std::count_if(first, last, UnaryTrue());
67 (void)std::distance(first, last);
68 (void)std::equal(first, last, first2);
69 (void)std::equal(first, last, first2, std::equal_to<void*>());
70 #if TEST_STD_VER > 11
71 (void)std::equal(first, last, first2, last2);
72 (void)std::equal(first, last, first2, last2, std::equal_to<void*>());
73 #endif
74 (void)std::equal_range(first, last, value);
75 (void)std::equal_range(first, last, value, std::less<void*>());
76 (void)std::fill(first, last, value);
77 (void)std::fill_n(first, count, value);
78 (void)std::find(first, last, value);
79 (void)std::find_end(first, last, first2, mid2);
80 (void)std::find_end(first, last, first2, mid2, std::equal_to<void*>());
81 (void)std::find_if(first, last, UnaryTrue());
82 (void)std::find_if_not(first, last, UnaryTrue());
83 (void)std::for_each(first, last, UnaryVoid());
84 #if TEST_STD_VER > 14
85 (void)std::for_each_n(first, count, UnaryVoid());
86 #endif
87 (void)std::generate(first, last, NullaryValue());
88 (void)std::generate_n(first, count, NullaryValue());
89 (void)std::includes(first, last, first2, last2);
90 (void)std::includes(first, last, first2, last2, std::less<void*>());
91 (void)std::is_heap(first, last);
92 (void)std::is_heap(first, last, std::less<void*>());
93 (void)std::is_heap_until(first, last);
94 (void)std::is_heap_until(first, last, std::less<void*>());
95 (void)std::is_partitioned(first, last, UnaryTrue());
96 (void)std::is_permutation(first, last, first2);
97 (void)std::is_permutation(first, last, first2, std::equal_to<void*>());
98 #if TEST_STD_VER > 11
99 (void)std::is_permutation(first, last, first2, last2);
100 (void)std::is_permutation(first, last, first2, last2, std::equal_to<void*>());
101 #endif
102 (void)std::is_sorted(first, last);
103 (void)std::is_sorted(first, last, std::less<void*>());
104 (void)std::is_sorted_until(first, last);
105 (void)std::is_sorted_until(first, last, std::less<void*>());
106 // RELIES ON ADL SWAP (void)std::inplace_merge(first, mid, last);
107 // RELIES ON ADL SWAP (void)std::inplace_merge(first, mid, last, std::less<void*>());
108 // RELIES ON ADL SWAP (void)std::iter_swap(first, mid);
109 (void)std::lexicographical_compare(first, last, first2, last2);
110 (void)std::lexicographical_compare(first, last, first2, last2, std::less<void*>());
111 #if TEST_STD_VER > 17
112 (void)std::lexicographical_compare_three_way(first, last, first2, last2);
113 (void)std::lexicographical_compare_three_way(first, last, first2, last2, std::compare_three_way());
114 #endif
115 (void)std::lower_bound(first, last, value);
116 (void)std::lower_bound(first, last, value, std::less<void*>());
117 (void)std::make_heap(first, last);
118 (void)std::make_heap(first, last, std::less<void*>());
119 (void)std::max(value, value);
120 (void)std::max(value, value, std::less<void*>());
121 #if TEST_STD_VER >= 11
122 (void)std::max({ value, value });
123 (void)std::max({ value, value }, std::less<void*>());
124 #endif
125 (void)std::max_element(first, last);
126 (void)std::max_element(first, last, std::less<void*>());
127 (void)std::merge(first, mid, mid, last, first2);
128 (void)std::merge(first, mid, mid, last, first2, std::less<void*>());
129 (void)std::min(value, value);
130 (void)std::min(value, value, std::less<void*>());
131 #if TEST_STD_VER >= 11
132 (void)std::min({ value, value });
133 (void)std::min({ value, value }, std::less<void*>());
134 #endif
135 (void)std::min_element(first, last);
136 (void)std::min_element(first, last, std::less<void*>());
137 (void)std::minmax(value, value);
138 (void)std::minmax(value, value, std::less<void*>());
139 #if TEST_STD_VER >= 11
140 (void)std::minmax({ value, value });
141 (void)std::minmax({ value, value }, std::less<void*>());
142 #endif
143 (void)std::minmax_element(first, last);
144 (void)std::minmax_element(first, last, std::less<void*>());
145 (void)std::mismatch(first, last, first2);
146 (void)std::mismatch(first, last, first2, std::equal_to<void*>());
147 #if TEST_STD_VER > 11
148 (void)std::mismatch(first, last, first2, last2);
149 (void)std::mismatch(first, last, first2, last2, std::equal_to<void*>());
150 #endif
151 (void)std::move(first, last, first2);
152 (void)std::move_backward(first, last, last2);
153 // RELIES ON ADL SWAP (void)std::next_permutation(first, last);
154 // RELIES ON ADL SWAP (void)std::next_permutation(first, last, std::less<void*>());
155 #if TEST_STD_VER >= 11
156 (void)std::none_of(first, last, UnaryTrue());
157 #endif
158 // RELIES ON ADL SWAP (void)std::nth_element(first, mid, last);
159 // RELIES ON ADL SWAP (void)std::nth_element(first, mid, last, std::less<void*>());
160 // RELIES ON ADL SWAP (void)std::partial_sort(first, mid, last);
161 // RELIES ON ADL SWAP (void)std::partial_sort(first, mid, last, std::less<void*>());
162 (void)std::partial_sort_copy(first, last, first2, mid2);
163 (void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
164 // RELIES ON ADL SWAP (void)std::partition(first, last, UnaryTrue());
165 (void)std::partition_copy(first, last, first2, last2, UnaryTrue());
166 (void)std::partition_point(first, last, UnaryTrue());
167 (void)std::pop_heap(first, last);
168 (void)std::pop_heap(first, last, std::less<void*>());
169 // RELIES ON ADL SWAP (void)std::prev_permutation(first, last);
170 // RELIES ON ADL SWAP (void)std::prev_permutation(first, last, std::less<void*>());
171 (void)std::push_heap(first, last);
172 (void)std::push_heap(first, last, std::less<void*>());
173 (void)std::remove(first, last, value);
174 (void)std::remove_copy(first, last, first2, value);
175 (void)std::remove_copy_if(first, last, first2, UnaryTrue());
176 (void)std::remove_if(first, last, UnaryTrue());
177 (void)std::replace(first, last, value, value);
178 (void)std::replace_copy(first, last, first2, value, value);
179 (void)std::replace_copy_if(first, last, first2, UnaryTrue(), value);
180 (void)std::replace_if(first, last, UnaryTrue(), value);
181 // RELIES ON ADL SWAP (void)std::reverse(first, last);
182 (void)std::reverse_copy(first, last, first2);
183 // RELIES ON ADL SWAP (void)std::rotate(first, mid, last);
184 (void)std::rotate_copy(first, mid, last, first2);
185 (void)std::search(first, last, first2, mid2);
186 (void)std::search(first, last, first2, mid2, std::equal_to<void*>());
187 (void)std::search_n(first, last, count, value);
188 (void)std::search_n(first, last, count, value, std::equal_to<void*>());
189 (void)std::set_difference(first, mid, mid, last, first2);
190 (void)std::set_difference(first, mid, mid, last, first2, std::less<void*>());
191 (void)std::set_intersection(first, mid, mid, last, first2);
192 (void)std::set_intersection(first, mid, mid, last, first2, std::less<void*>());
193 (void)std::set_symmetric_difference(first, mid, mid, last, first2);
194 (void)std::set_symmetric_difference(first, mid, mid, last, first2, std::less<void*>());
195 (void)std::set_union(first, mid, mid, last, first2);
196 (void)std::set_union(first, mid, mid, last, first2, std::less<void*>());
197 #if TEST_STD_VER > 17
198 (void)std::shift_left(first, last, count);
199 (void)std::shift_right(first, last, count);
200 #endif
201 // RELIES ON ADL SWAP (void)std::sort(first, last);
202 // RELIES ON ADL SWAP (void)std::sort(first, last, std::less<void*>());
203 (void)std::sort_heap(first, last);
204 (void)std::sort_heap(first, last, std::less<void*>());
205 // RELIES ON ADL SWAP (void)std::stable_partition(first, last, UnaryTrue());
206 // RELIES ON ADL SWAP (void)std::stable_sort(first, last);
207 // RELIES ON ADL SWAP (void)std::stable_sort(first, last, std::less<void*>());
208 // RELIES ON ADL SWAP (void)std::swap_ranges(first, last, first2);
209 (void)std::transform(first, last, first2, UnaryTransform());
210 (void)std::transform(first, mid, mid, first2, BinaryTransform());
211 (void)std::unique(first, last);
212 (void)std::unique(first, last, std::equal_to<void*>());
213 (void)std::unique_copy(first, last, first2);
214 (void)std::unique_copy(first, last, first2, std::equal_to<void*>());
215 (void)std::upper_bound(first, last, value);
216 (void)std::upper_bound(first, last, value, std::less<void*>());
217
218 return true;
219 }
220
test()221 void test()
222 {
223 all_the_algorithms();
224 #if TEST_STD_VER > 17
225 static_assert(all_the_algorithms());
226 #endif
227 }
228