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     // TODO: lexicographical_compare_three_way
112     (void)std::lower_bound(first, last, value);
113     (void)std::lower_bound(first, last, value, std::less<void*>());
114     (void)std::make_heap(first, last);
115     (void)std::make_heap(first, last, std::less<void*>());
116     (void)std::max(value, value);
117     (void)std::max(value, value, std::less<void*>());
118 #if TEST_STD_VER >= 11
119     (void)std::max({ value, value });
120     (void)std::max({ value, value }, std::less<void*>());
121 #endif
122     (void)std::max_element(first, last);
123     (void)std::max_element(first, last, std::less<void*>());
124     (void)std::merge(first, mid, mid, last, first2);
125     (void)std::merge(first, mid, mid, last, first2, std::less<void*>());
126     (void)std::min(value, value);
127     (void)std::min(value, value, std::less<void*>());
128 #if TEST_STD_VER >= 11
129     (void)std::min({ value, value });
130     (void)std::min({ value, value }, std::less<void*>());
131 #endif
132     (void)std::min_element(first, last);
133     (void)std::min_element(first, last, std::less<void*>());
134     (void)std::minmax(value, value);
135     (void)std::minmax(value, value, std::less<void*>());
136 #if TEST_STD_VER >= 11
137     (void)std::minmax({ value, value });
138     (void)std::minmax({ value, value }, std::less<void*>());
139 #endif
140     (void)std::minmax_element(first, last);
141     (void)std::minmax_element(first, last, std::less<void*>());
142     (void)std::mismatch(first, last, first2);
143     (void)std::mismatch(first, last, first2, std::equal_to<void*>());
144 #if TEST_STD_VER > 11
145     (void)std::mismatch(first, last, first2, last2);
146     (void)std::mismatch(first, last, first2, last2, std::equal_to<void*>());
147 #endif
148     (void)std::move(first, last, first2);
149     (void)std::move_backward(first, last, last2);
150     // RELIES ON ADL SWAP (void)std::next_permutation(first, last);
151     // RELIES ON ADL SWAP (void)std::next_permutation(first, last, std::less<void*>());
152 #if TEST_STD_VER >= 11
153     (void)std::none_of(first, last, UnaryTrue());
154 #endif
155     // RELIES ON ADL SWAP (void)std::nth_element(first, mid, last);
156     // RELIES ON ADL SWAP (void)std::nth_element(first, mid, last, std::less<void*>());
157     // RELIES ON ADL SWAP (void)std::partial_sort(first, mid, last);
158     // RELIES ON ADL SWAP (void)std::partial_sort(first, mid, last, std::less<void*>());
159     (void)std::partial_sort_copy(first, last, first2, mid2);
160     (void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
161     // RELIES ON ADL SWAP (void)std::partition(first, last, UnaryTrue());
162     (void)std::partition_copy(first, last, first2, last2, UnaryTrue());
163     (void)std::partition_point(first, last, UnaryTrue());
164     (void)std::pop_heap(first, last);
165     (void)std::pop_heap(first, last, std::less<void*>());
166     // RELIES ON ADL SWAP (void)std::prev_permutation(first, last);
167     // RELIES ON ADL SWAP (void)std::prev_permutation(first, last, std::less<void*>());
168     (void)std::push_heap(first, last);
169     (void)std::push_heap(first, last, std::less<void*>());
170     (void)std::remove(first, last, value);
171     (void)std::remove_copy(first, last, first2, value);
172     (void)std::remove_copy_if(first, last, first2, UnaryTrue());
173     (void)std::remove_if(first, last, UnaryTrue());
174     (void)std::replace(first, last, value, value);
175     (void)std::replace_copy(first, last, first2, value, value);
176     (void)std::replace_copy_if(first, last, first2, UnaryTrue(), value);
177     (void)std::replace_if(first, last, UnaryTrue(), value);
178     // RELIES ON ADL SWAP (void)std::reverse(first, last);
179     (void)std::reverse_copy(first, last, first2);
180     // RELIES ON ADL SWAP (void)std::rotate(first, mid, last);
181     (void)std::rotate_copy(first, mid, last, first2);
182     (void)std::search(first, last, first2, mid2);
183     (void)std::search(first, last, first2, mid2, std::equal_to<void*>());
184     (void)std::search_n(first, last, count, value);
185     (void)std::search_n(first, last, count, value, std::equal_to<void*>());
186     (void)std::set_difference(first, mid, mid, last, first2);
187     (void)std::set_difference(first, mid, mid, last, first2, std::less<void*>());
188     (void)std::set_intersection(first, mid, mid, last, first2);
189     (void)std::set_intersection(first, mid, mid, last, first2, std::less<void*>());
190     (void)std::set_symmetric_difference(first, mid, mid, last, first2);
191     (void)std::set_symmetric_difference(first, mid, mid, last, first2, std::less<void*>());
192     (void)std::set_union(first, mid, mid, last, first2);
193     (void)std::set_union(first, mid, mid, last, first2, std::less<void*>());
194 #if TEST_STD_VER > 17
195     (void)std::shift_left(first, last, count);
196     (void)std::shift_right(first, last, count);
197 #endif
198     // RELIES ON ADL SWAP (void)std::sort(first, last);
199     // RELIES ON ADL SWAP (void)std::sort(first, last, std::less<void*>());
200     (void)std::sort_heap(first, last);
201     (void)std::sort_heap(first, last, std::less<void*>());
202     // RELIES ON ADL SWAP (void)std::stable_partition(first, last, UnaryTrue());
203     // RELIES ON ADL SWAP (void)std::stable_sort(first, last);
204     // RELIES ON ADL SWAP (void)std::stable_sort(first, last, std::less<void*>());
205     // RELIES ON ADL SWAP (void)std::swap_ranges(first, last, first2);
206     (void)std::transform(first, last, first2, UnaryTransform());
207     (void)std::transform(first, mid, mid, first2, BinaryTransform());
208     (void)std::unique(first, last);
209     (void)std::unique(first, last, std::equal_to<void*>());
210     (void)std::unique_copy(first, last, first2);
211     (void)std::unique_copy(first, last, first2, std::equal_to<void*>());
212     (void)std::upper_bound(first, last, value);
213     (void)std::upper_bound(first, last, value, std::less<void*>());
214 
215     return true;
216 }
217 
test()218 void test()
219 {
220     all_the_algorithms();
221 #if TEST_STD_VER > 17
222     static_assert(all_the_algorithms());
223 #endif
224 }
225