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