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// In the description of the algorithms,
11// given an iterator a whose difference type is D,
12// and an expression n of integer-like type other than cv D,
13// the semantics of a + n and a - n are, respectively,
14// those of a + D(n) and a - D(n).
15
16#include <algorithm>
17#include <cstddef>
18#include <functional>
19#include <iterator>
20
21#include "test_macros.h"
22
23// This iterator rejects expressions like (a + n) and (a - n)
24// whenever n is of any type other than difference_type.
25//
26template<class It, class DifferenceType>
27class PickyIterator {
28 It it_;
29public:
30 using iterator_category = std::random_access_iterator_tag;
31 using value_type = typename std::iterator_traits<It>::value_type;
32 using difference_type = DifferenceType;
33 using pointer = It;
34 using reference = typename std::iterator_traits<It>::reference;
35
36 TEST_CONSTEXPR_CXX14 PickyIterator() = default;
37 TEST_CONSTEXPR_CXX14 explicit PickyIterator(It it) : it_(it) {}
38 TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
39 TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
40 TEST_CONSTEXPR_CXX14 reference operator[](difference_type n) const {return it_[n];}
41
42 friend TEST_CONSTEXPR_CXX14 bool operator==(PickyIterator a, PickyIterator b) { return a.it_ == b.it_; }
43 friend TEST_CONSTEXPR_CXX14 bool operator!=(PickyIterator a, PickyIterator b) { return a.it_ != b.it_; }
44 friend TEST_CONSTEXPR_CXX14 bool operator<(PickyIterator a, PickyIterator b) { return a.it_ < b.it_; }
45 friend TEST_CONSTEXPR_CXX14 bool operator>(PickyIterator a, PickyIterator b) { return a.it_ > b.it_; }
46 friend TEST_CONSTEXPR_CXX14 bool operator<=(PickyIterator a, PickyIterator b) { return a.it_ <= b.it_; }
47 friend TEST_CONSTEXPR_CXX14 bool operator>=(PickyIterator a, PickyIterator b) { return a.it_ >= b.it_; }
48
49 TEST_CONSTEXPR_CXX14 PickyIterator& operator++() {++it_; return *this;}
50 TEST_CONSTEXPR_CXX14 PickyIterator operator++(int) {auto tmp = *this; ++(*this); return tmp;}
51 TEST_CONSTEXPR_CXX14 PickyIterator& operator--() {--it_; return *this;}
52 TEST_CONSTEXPR_CXX14 PickyIterator operator--(int) {auto tmp = *this; --(*this); return tmp;}
53
54 TEST_CONSTEXPR_CXX14 PickyIterator& operator+=(difference_type n) {it_ += n; return *this;}
55 TEST_CONSTEXPR_CXX14 PickyIterator& operator-=(difference_type n) {it_ -= n; return *this;}
56 friend TEST_CONSTEXPR_CXX14 PickyIterator operator+(PickyIterator it, difference_type n) {it += n; return it;}
57 friend TEST_CONSTEXPR_CXX14 PickyIterator operator+(difference_type n, PickyIterator it) {it += n; return it;}
58 friend TEST_CONSTEXPR_CXX14 PickyIterator operator-(PickyIterator it, difference_type n) {it -= n; return it;}
59 friend TEST_CONSTEXPR_CXX14 difference_type operator-(PickyIterator it, PickyIterator jt) {return it.it_ - jt.it_;}
60
61 template<class X> void operator+=(X) = delete;
62 template<class X> void operator-=(X) = delete;
63 template<class X> friend void operator+(PickyIterator, X) = delete;
64 template<class X> friend void operator+(X, PickyIterator) = delete;
65 template<class X> friend void operator-(PickyIterator, X) = delete;
66};
67
68struct UnaryVoid { TEST_CONSTEXPR_CXX14 void operator()(void*) const {} };
69struct UnaryTrue { TEST_CONSTEXPR bool operator()(void*) const { return true; } };
70struct NullaryValue { TEST_CONSTEXPR std::nullptr_t operator()() const { return nullptr; } };
71struct UnaryTransform { TEST_CONSTEXPR std::nullptr_t operator()(void*) const { return nullptr; } };
72struct BinaryTransform { TEST_CONSTEXPR std::nullptr_t operator()(void*, void*) const { return nullptr; } };
73
74TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
75{
76 void *a[10] = {};
77 void *b[10] = {};
78 auto first = PickyIterator<void**, long>(a);
79 auto mid = PickyIterator<void**, long>(a+5);
80 auto last = PickyIterator<void**, long>(a+10);
81 auto first2 = PickyIterator<void**, long long>(b);
82 auto mid2 = PickyIterator<void**, long long>(b+5);
83 auto last2 = PickyIterator<void**, long long>(b+10);
84 void *value = nullptr;
85 int count = 1;
86
87 (void)std::adjacent_find(first, last);
88 (void)std::adjacent_find(first, last, std::equal_to<void*>());
89#if TEST_STD_VER >= 11
90 (void)std::all_of(first, last, UnaryTrue());
91 (void)std::any_of(first, last, UnaryTrue());
92#endif
93 (void)std::binary_search(first, last, value);
94 (void)std::binary_search(first, last, value, std::less<void*>());
95#if TEST_STD_VER > 17
96 (void)std::clamp(value, value, value);
97 (void)std::clamp(value, value, value, std::less<void*>());
98#endif
99 (void)std::copy(first, last, first2);
100 (void)std::copy_backward(first, last, last2);
101 (void)std::copy_n(first, count, first2);
102 (void)std::count(first, last, value);
103 (void)std::count_if(first, last, UnaryTrue());
104 (void)std::distance(first, last);
105 (void)std::equal(first, last, first2);
106 (void)std::equal(first, last, first2, std::equal_to<void*>());
107#if TEST_STD_VER > 11
108 (void)std::equal(first, last, first2, last2);
109 (void)std::equal(first, last, first2, last2, std::equal_to<void*>());
110#endif
111 (void)std::equal_range(first, last, value);
112 (void)std::equal_range(first, last, value, std::less<void*>());
113 (void)std::fill(first, last, value);
114 (void)std::fill_n(first, count, value);
115 (void)std::find(first, last, value);
116 (void)std::find_end(first, last, first2, mid2);
117 (void)std::find_end(first, last, first2, mid2, std::equal_to<void*>());
118 (void)std::find_if(first, last, UnaryTrue());
119 (void)std::find_if_not(first, last, UnaryTrue());
120 (void)std::for_each(first, last, UnaryVoid());
121#if TEST_STD_VER > 14
122 (void)std::for_each_n(first, count, UnaryVoid());
123#endif
124 (void)std::generate(first, last, NullaryValue());
125 (void)std::generate_n(first, count, NullaryValue());
126 (void)std::includes(first, last, first2, last2);
127 (void)std::includes(first, last, first2, last2, std::less<void*>());
128 (void)std::is_heap(first, last);
129 (void)std::is_heap(first, last, std::less<void*>());
130 (void)std::is_heap_until(first, last);
131 (void)std::is_heap_until(first, last, std::less<void*>());
132 (void)std::is_partitioned(first, last, UnaryTrue());
133 (void)std::is_permutation(first, last, first2);
134 (void)std::is_permutation(first, last, first2, std::equal_to<void*>());
135#if TEST_STD_VER > 11
136 (void)std::is_permutation(first, last, first2, last2);
137 (void)std::is_permutation(first, last, first2, last2, std::equal_to<void*>());
138#endif
139 (void)std::is_sorted(first, last);
140 (void)std::is_sorted(first, last, std::less<void*>());
141 (void)std::is_sorted_until(first, last);
142 (void)std::is_sorted_until(first, last, std::less<void*>());
143 if (!TEST_IS_CONSTANT_EVALUATED) (void)std::inplace_merge(first, mid, last);
144 if (!TEST_IS_CONSTANT_EVALUATED) (void)std::inplace_merge(first, mid, last, std::less<void*>());
145 (void)std::iter_swap(first, mid);
146 (void)std::lexicographical_compare(first, last, first2, last2);
147 (void)std::lexicographical_compare(first, last, first2, last2, std::less<void*>());
148#if TEST_STD_VER > 17
149 // `lexicographical_compare_three_way` static_asserts that the difference type is an integer, as
150 // required by https://eel.is/c++draft/iterator.iterators#2.2
151 //(void)std::lexicographical_compare_three_way(first, last, first2, last2);
152 //(void)std::lexicographical_compare_three_way(first, last, first2, last2, std::compare_three_way());
153#endif
154 (void)std::lower_bound(first, last, value);
155 (void)std::lower_bound(first, last, value, std::less<void*>());
156 (void)std::make_heap(first, last);
157 (void)std::make_heap(first, last, std::less<void*>());
158 (void)std::max(a: value, b: value);
159 (void)std::max(a: value, b: value, comp: std::less<void*>());
160#if TEST_STD_VER >= 11
161 (void)std::max({ value, value });
162 (void)std::max({ value, value }, std::less<void*>());
163#endif
164 (void)std::max_element(first, last);
165 (void)std::max_element(first, last, std::less<void*>());
166 (void)std::merge(first, mid, mid, last, first2);
167 (void)std::merge(first, mid, mid, last, first2, std::less<void*>());
168 (void)std::min(a: value, b: value);
169 (void)std::min(a: value, b: value, comp: std::less<void*>());
170#if TEST_STD_VER >= 11
171 (void)std::min({ value, value });
172 (void)std::min({ value, value }, std::less<void*>());
173#endif
174 (void)std::min_element(first, last);
175 (void)std::min_element(first, last, std::less<void*>());
176 (void)std::minmax(a: value, b: value);
177 (void)std::minmax(a: value, b: value, comp: std::less<void*>());
178#if TEST_STD_VER >= 11
179 (void)std::minmax({ value, value });
180 (void)std::minmax({ value, value }, std::less<void*>());
181#endif
182 (void)std::minmax_element(first, last);
183 (void)std::minmax_element(first, last, std::less<void*>());
184 (void)std::mismatch(first, last, first2);
185 (void)std::mismatch(first, last, first2, std::equal_to<void*>());
186#if TEST_STD_VER > 11
187 (void)std::mismatch(first, last, first2, last2);
188 (void)std::mismatch(first, last, first2, last2, std::equal_to<void*>());
189#endif
190 (void)std::move(first, last, first2);
191 (void)std::move_backward(first, last, last2);
192 (void)std::next_permutation(first, last);
193 (void)std::next_permutation(first, last, std::less<void*>());
194 (void)std::none_of(first, last, UnaryTrue());
195 (void)std::nth_element(first, mid, last);
196 (void)std::nth_element(first, mid, last, std::less<void*>());
197 (void)std::partial_sort(first, mid, last);
198 (void)std::partial_sort(first, mid, last, std::less<void*>());
199 (void)std::partial_sort_copy(first, last, first2, mid2);
200 (void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
201 (void)std::partition(first, last, UnaryTrue());
202 (void)std::partition_copy(first, last, first2, last2, UnaryTrue());
203 (void)std::partition_point(first, last, UnaryTrue());
204 (void)std::pop_heap(first, last);
205 (void)std::pop_heap(first, last, std::less<void*>());
206 (void)std::prev_permutation(first, last);
207 (void)std::prev_permutation(first, last, std::less<void*>());
208 (void)std::push_heap(first, last);
209 (void)std::push_heap(first, last, std::less<void*>());
210 (void)std::remove(first, last, value);
211 (void)std::remove_copy(first, last, first2, value);
212 (void)std::remove_copy_if(first, last, first2, UnaryTrue());
213 (void)std::remove_if(first, last, UnaryTrue());
214 (void)std::replace(first, last, value, value);
215 (void)std::replace_copy(first, last, first2, value, value);
216 (void)std::replace_copy_if(first, last, first2, UnaryTrue(), value);
217 (void)std::replace_if(first, last, UnaryTrue(), value);
218 (void)std::reverse(first, last);
219 (void)std::reverse_copy(first, last, first2);
220 (void)std::rotate(first, mid, last);
221 (void)std::rotate_copy(first, mid, last, first2);
222 (void)std::search(first, last, first2, mid2);
223 (void)std::search(first, last, first2, mid2, std::equal_to<void*>());
224 (void)std::search_n(first, last, count, value);
225 (void)std::search_n(first, last, count, value, std::equal_to<void*>());
226 (void)std::set_difference(first, mid, mid, last, first2);
227 (void)std::set_difference(first, mid, mid, last, first2, std::less<void*>());
228 (void)std::set_intersection(first, mid, mid, last, first2);
229 (void)std::set_intersection(first, mid, mid, last, first2, std::less<void*>());
230 (void)std::set_symmetric_difference(first, mid, mid, last, first2);
231 (void)std::set_symmetric_difference(first, mid, mid, last, first2, std::less<void*>());
232 (void)std::set_union(first, mid, mid, last, first2);
233 (void)std::set_union(first, mid, mid, last, first2, std::less<void*>());
234#if TEST_STD_VER > 17
235 (void)std::shift_left(first, last, count);
236 (void)std::shift_right(first, last, count);
237#endif
238 (void)std::sort(first, last);
239 (void)std::sort(first, last, std::less<void*>());
240 (void)std::sort_heap(first, last);
241 (void)std::sort_heap(first, last, std::less<void*>());
242 if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_partition(first, last, UnaryTrue());
243 if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_sort(first, last);
244 if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_sort(first, last, std::less<void*>());
245 (void)std::swap_ranges(first, last, first2);
246 (void)std::transform(first, last, first2, UnaryTransform());
247 (void)std::transform(first, mid, mid, first2, BinaryTransform());
248 (void)std::unique(first, last);
249 (void)std::unique(first, last, std::equal_to<void*>());
250 (void)std::unique_copy(first, last, first2);
251 (void)std::unique_copy(first, last, first2, std::equal_to<void*>());
252 (void)std::upper_bound(first, last, value);
253 (void)std::upper_bound(first, last, value, std::less<void*>());
254
255 return true;
256}
257
258void test()
259{
260 all_the_algorithms();
261#if TEST_STD_VER > 17
262 static_assert(all_the_algorithms());
263#endif
264}
265

source code of libcxx/test/std/algorithms/robust_re_difference_type.compile.pass.cpp