1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// ranges::advance(it, n, sent)
#include <iterator>
#include <cassert>
#include <climits>
#include <concepts>
#include <cstddef>
#include "test_iterators.h"
#include "../types.h"
template <bool Count, typename It>
constexpr void
check_forward(int* first, int* last, std::iter_difference_t<It> n, int* expected, int expected_equals_count = -1) {
using Difference = std::iter_difference_t<It>;
Difference const M = (expected - first); // expected travel distance
// `expected_equals_count` is only relevant when `Count` is true.
assert(Count || expected_equals_count == -1);
{
It it(first);
auto sent = sentinel_wrapper(It(last));
std::same_as<Difference> auto result = std::ranges::advance(it, n, sent);
assert(result == n - M);
assert(base(it) == expected);
}
// Count operations
if constexpr (Count) {
auto it = stride_counting_iterator(It(first));
auto sent = sentinel_wrapper(stride_counting_iterator(It(last)));
(void)std::ranges::advance(it, n, sent);
// We don't have a sized sentinel, so we have to increment one-by-one
// regardless of the iterator category.
assert(it.stride_count() == M);
assert(it.stride_displacement() == M);
assert(it.equals_count() == expected_equals_count);
}
}
template <typename It>
constexpr void check_forward_sized_sentinel(int* first, int* last, std::iter_difference_t<It> n, int* expected) {
using Difference = std::iter_difference_t<It>;
Difference const size = (last - first);
Difference const M = (expected - first); // expected travel distance
{
It it(first);
auto sent = distance_apriori_sentinel(size);
std::same_as<Difference> auto result = std::ranges::advance(it, n, sent);
assert(result == n - M);
assert(base(it) == expected);
}
// Count operations
{
auto it = stride_counting_iterator(It(first));
auto sent = distance_apriori_sentinel(size);
(void)std::ranges::advance(it, n, sent);
if constexpr (std::random_access_iterator<It>) {
assert(it.stride_count() <= 1);
assert(it.stride_displacement() <= 1);
} else {
assert(it.stride_count() == M);
assert(it.stride_displacement() == M);
}
}
}
struct Expected {
int stride_count;
int stride_displacement;
int equals_count;
};
template <bool Count, typename It>
constexpr void
check_backward(int* first, int* last, std::iter_difference_t<It> n, int* expected, Expected expected_counts) {
// Check preconditions for `advance` when called with negative `n`
// (see [range.iter.op.advance]). In addition, allow `n == 0`.
assert(n <= 0);
static_assert(std::bidirectional_iterator<It>);
using Difference = std::iter_difference_t<It>;
Difference const M = (expected - last); // expected travel distance (which is negative)
{
It it(last);
It sent(first);
std::same_as<Difference> auto result = std::ranges::advance(it, n, sent);
assert(result == n - M);
assert(base(it) == expected);
}
// Count operations
{
auto it = stride_counting_iterator(It(last));
auto sent = stride_counting_iterator(It(first));
static_assert(std::bidirectional_iterator<stride_counting_iterator<It>>);
static_assert(Count == !std::sized_sentinel_for<It, It>);
(void)std::ranges::advance(it, n, sent);
assert(it.stride_count() == expected_counts.stride_count);
assert(it.stride_displacement() == expected_counts.stride_displacement);
assert(it.equals_count() == expected_counts.equals_count);
}
}
struct iota_iterator {
using difference_type = int;
using value_type = int;
constexpr int operator*() const { return x; }
constexpr iota_iterator& operator++() { ++x; return *this; }
constexpr iota_iterator operator++(int) { ++x; return iota_iterator{x - 1}; }
constexpr bool operator==(const iota_iterator&) const = default;
constexpr int operator-(const iota_iterator& that) const { return x - that.x; }
constexpr iota_iterator& operator--() { --x; return *this; }
constexpr iota_iterator operator--(int) { --x; return iota_iterator{x + 1}; }
int x;
};
static_assert(std::bidirectional_iterator<iota_iterator>);
static_assert(std::sized_sentinel_for<iota_iterator, iota_iterator>);
constexpr bool test() {
int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// Basic functionality test: advance forward, bound has the same type
{
int *p;
p = range+5; assert(std::ranges::advance(p, 0, range+7) == 0); assert(p == range+5);
p = range+5; assert(std::ranges::advance(p, 1, range+7) == 0); assert(p == range+6);
p = range+5; assert(std::ranges::advance(p, 2, range+7) == 0); assert(p == range+7);
p = range+5; assert(std::ranges::advance(p, 3, range+7) == 1); assert(p == range+7);
}
// Basic functionality test: advance forward, bound is not the same type and not assignable
{
int *p;
using ConstPtr = const int*;
p = range+5; assert(std::ranges::advance(p, 0, ConstPtr(range+7)) == 0); assert(p == range+5);
p = range+5; assert(std::ranges::advance(p, 1, ConstPtr(range+7)) == 0); assert(p == range+6);
p = range+5; assert(std::ranges::advance(p, 2, ConstPtr(range+7)) == 0); assert(p == range+7);
p = range+5; assert(std::ranges::advance(p, 3, ConstPtr(range+7)) == 1); assert(p == range+7);
}
// Basic functionality test: advance forward, bound has different type but assignable
{
const int *pc;
pc = range+5; assert(std::ranges::advance(pc, 0, range+7) == 0); assert(pc == range+5);
pc = range+5; assert(std::ranges::advance(pc, 1, range+7) == 0); assert(pc == range+6);
pc = range+5; assert(std::ranges::advance(pc, 2, range+7) == 0); assert(pc == range+7);
pc = range+5; assert(std::ranges::advance(pc, 3, range+7) == 1); assert(pc == range+7);
}
// Basic functionality test: advance backward, bound has the same type
// Note that we don't test advancing backward with a bound of a different type because that's UB
{
int *p;
p = range+5; assert(std::ranges::advance(p, 0, range+3) == 0); assert(p == range+5);
p = range+5; assert(std::ranges::advance(p, -1, range+3) == 0); assert(p == range+4);
p = range+5; assert(std::ranges::advance(p, -2, range+3) == 0); assert(p == range+3);
p = range+5; assert(std::ranges::advance(p, -3, range+3) == -1); assert(p == range+3);
}
// Basic functionality test: advance backward with an array as a sentinel
{
int* p;
p = range+5; assert(std::ranges::advance(p, 0, range) == 0); assert(p == range+5);
p = range+5; assert(std::ranges::advance(p, -1, range) == 0); assert(p == range+4);
p = range+5; assert(std::ranges::advance(p, -5, range) == 0); assert(p == range);
p = range+5; assert(std::ranges::advance(p, -6, range) == -1); assert(p == range);
}
// Exhaustive checks for n and range sizes
for (int size = 0; size != 10; ++size) {
for (int n = 0; n != 20; ++n) {
{
int* expected = n > size ? range + size : range + n;
int equals_count = n > size ? size + 1 : n;
// clang-format off
check_forward<false, cpp17_input_iterator<int*>>( range, range+size, n, expected);
check_forward<false, cpp20_input_iterator<int*>>( range, range+size, n, expected);
check_forward<true, forward_iterator<int*>>( range, range+size, n, expected, equals_count);
check_forward<true, bidirectional_iterator<int*>>(range, range+size, n, expected, equals_count);
check_forward<true, random_access_iterator<int*>>(range, range+size, n, expected, equals_count);
check_forward<true, contiguous_iterator<int*>>( range, range+size, n, expected, equals_count);
check_forward<true, int*>( range, range+size, n, expected, equals_count);
// clang-format on
check_forward_sized_sentinel<cpp17_input_iterator<int*>>( range, range+size, n, expected);
check_forward_sized_sentinel<cpp20_input_iterator<int*>>( range, range+size, n, expected);
check_forward_sized_sentinel<forward_iterator<int*>>( range, range+size, n, expected);
check_forward_sized_sentinel<bidirectional_iterator<int*>>(range, range+size, n, expected);
check_forward_sized_sentinel<random_access_iterator<int*>>(range, range+size, n, expected);
check_forward_sized_sentinel<contiguous_iterator<int*>>( range, range+size, n, expected);
check_forward_sized_sentinel<int*>( range, range+size, n, expected);
}
// Input and forward iterators are not tested as the backwards case does
// not apply for them.
{
int* expected = n > size ? range : range + size - n;
{
Expected expected_counts = {
.stride_count = static_cast<int>(range + size - expected),
.stride_displacement = -expected_counts.stride_count,
.equals_count = n > size ? size + 1 : n,
};
check_backward<true, bidirectional_iterator<int*>>(range, range + size, -n, expected, expected_counts);
}
{
Expected expected_counts = {
// If `n >= size`, the algorithm can just do `it = std::move(sent);`
// instead of doing iterator arithmetic.
.stride_count = (n >= size) ? 0 : 1,
.stride_displacement = (n >= size) ? 0 : 1,
.equals_count = 0,
};
check_backward<false, random_access_iterator<int*>>(range, range + size, -n, expected, expected_counts);
check_backward<false, contiguous_iterator<int*>>(range, range + size, -n, expected, expected_counts);
check_backward<false, int*>(range, range + size, -n, expected, expected_counts);
}
}
}
}
// Regression-test that INT_MIN doesn't cause any undefined behavior
{
auto i = iota_iterator{+1};
assert(std::ranges::advance(i, INT_MIN, iota_iterator{-2}) == INT_MIN+3);
assert(i == iota_iterator{-2});
i = iota_iterator{+1};
assert(std::ranges::advance(i, -2, iota_iterator{INT_MIN+1}) == 0);
assert(i == iota_iterator{-1});
i = iota_iterator{+1};
assert(std::ranges::advance(i, INT_MIN, iota_iterator{INT_MIN+1}) == 0);
assert(i == iota_iterator{INT_MIN+1});
}
return true;
}
int main(int, char**) {
assert(test());
static_assert(test());
return 0;
}
|