| 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 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 |
|
| 11 |
// template<class I, sentinel_for<I> S> |
| 12 |
// requires (!sized_sentinel_for<S, I>) |
| 13 |
// constexpr iter_difference_t<I> ranges::distance(I first, S last); |
| 14 |
// |
| 15 |
// template<class I, sized_sentinel_for<decay_t<I>> S> |
| 16 |
// constexpr iter_difference_t<I> ranges::distance(I&& first, S last); // TODO: update when LWG3664 is resolved |
| 17 |
|
| 18 |
#include <iterator> |
| 19 |
#include <cassert> |
| 20 |
|
| 21 |
#include "test_iterators.h" |
| 22 |
#include "test_macros.h" |
| 23 |
|
| 24 |
template<class It, class Sent> |
| 25 |
constexpr void test_unsized() { |
| 26 |
static_assert(std::sentinel_for<Sent, It> && !std::sized_sentinel_for<Sent, It>); |
| 27 |
int a[3] = {1,2,3}; |
| 28 |
{ |
| 29 |
It first = It(a); |
| 30 |
auto last = Sent(It(a)); |
| 31 |
assert(std::ranges::distance(first, last) == 0); |
| 32 |
assert(std::ranges::distance(It(a), last) == 0); |
| 33 |
assert(std::ranges::distance(first, Sent(It(a))) == 0); |
| 34 |
assert(std::ranges::distance(It(a), Sent(It(a))) == 0); |
| 35 |
ASSERT_SAME_TYPE(decltype(std::ranges::distance(It(a), Sent(It(a)))), std::iter_difference_t<It>); |
| 36 |
} |
| 37 |
{ |
| 38 |
It first = It(a); |
| 39 |
auto last = Sent(It(a + 3)); |
| 40 |
assert(std::ranges::distance(first, last) == 3); |
| 41 |
|
| 42 |
// Test all const/ref-qualifications of both operands. |
| 43 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<Sent&>(last)) == 3); |
| 44 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<Sent&&>(last)) == 3); |
| 45 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<const Sent&>(last)) == 3); |
| 46 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<const Sent&&>(last)) == 3); |
| 47 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<Sent&>(last)) == 3); |
| 48 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<Sent&&>(last)) == 3); |
| 49 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<const Sent&>(last)) == 3); |
| 50 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<const Sent&&>(last)) == 3); |
| 51 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<Sent&>(last)) == 3); |
| 52 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<Sent&&>(last)) == 3); |
| 53 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<const Sent&>(last)) == 3); |
| 54 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<const Sent&&>(last)) == 3); |
| 55 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<Sent&>(last)) == 3); |
| 56 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<Sent&&>(last)) == 3); |
| 57 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<const Sent&>(last)) == 3); |
| 58 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<const Sent&&>(last)) == 3); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
template<class It, class Sent> |
| 63 |
constexpr void test_sized() { |
| 64 |
static_assert(std::sized_sentinel_for<Sent, It>); |
| 65 |
int a[] = {1,2,3}; |
| 66 |
{ |
| 67 |
It first = It(a + 3); |
| 68 |
auto last = Sent(It(a)); |
| 69 |
assert(std::ranges::distance(first, last) == -3); |
| 70 |
|
| 71 |
// Test all const/ref-qualifications of both operands. |
| 72 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<Sent&>(last)) == -3); |
| 73 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<Sent&&>(last)) == -3); |
| 74 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<const Sent&>(last)) == -3); |
| 75 |
assert(std::ranges::distance(static_cast<It&>(first), static_cast<const Sent&&>(last)) == -3); |
| 76 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<Sent&>(last)) == -3); |
| 77 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<Sent&&>(last)) == -3); |
| 78 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<const Sent&>(last)) == -3); |
| 79 |
assert(std::ranges::distance(static_cast<It&&>(first), static_cast<const Sent&&>(last)) == -3); |
| 80 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<Sent&>(last)) == -3); |
| 81 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<Sent&&>(last)) == -3); |
| 82 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<const Sent&>(last)) == -3); |
| 83 |
assert(std::ranges::distance(static_cast<const It&>(first), static_cast<const Sent&&>(last)) == -3); |
| 84 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<Sent&>(last)) == -3); |
| 85 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<Sent&&>(last)) == -3); |
| 86 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<const Sent&>(last)) == -3); |
| 87 |
assert(std::ranges::distance(static_cast<const It&&>(first), static_cast<const Sent&&>(last)) == -3); |
| 88 |
} |
| 89 |
{ |
| 90 |
It first = It(a); |
| 91 |
auto last = Sent(It(a)); |
| 92 |
assert(std::ranges::distance(first, last) == 0); |
| 93 |
assert(std::ranges::distance(It(a), last) == 0); |
| 94 |
assert(std::ranges::distance(first, Sent(It(a))) == 0); |
| 95 |
assert(std::ranges::distance(It(a), Sent(It(a))) == 0); |
| 96 |
ASSERT_SAME_TYPE(decltype(std::ranges::distance(It(a), Sent(It(a)))), std::iter_difference_t<It>); |
| 97 |
} |
| 98 |
{ |
| 99 |
It first = It(a); |
| 100 |
auto last = Sent(It(a + 3)); |
| 101 |
assert(std::ranges::distance(first, last) == 3); |
| 102 |
assert(std::ranges::distance(It(a), last) == 3); |
| 103 |
assert(std::ranges::distance(first, Sent(It(a + 3))) == 3); |
| 104 |
assert(std::ranges::distance(It(a), Sent(It(a + 3))) == 3); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
struct StrideCounter { |
| 109 |
int *it_; |
| 110 |
int *inc_; |
| 111 |
using value_type = int; |
| 112 |
using difference_type = int; |
| 113 |
explicit StrideCounter(); |
| 114 |
constexpr explicit StrideCounter(int *it, int *inc) : it_(it), inc_(inc) {} |
| 115 |
constexpr auto& operator++() { ++it_; *inc_ += 1; return *this; } |
| 116 |
StrideCounter operator++(int); |
| 117 |
int& operator*() const; |
| 118 |
bool operator==(StrideCounter) const; |
| 119 |
}; |
| 120 |
static_assert(std::forward_iterator<StrideCounter>); |
| 121 |
static_assert(!std::sized_sentinel_for<StrideCounter, StrideCounter>); |
| 122 |
|
| 123 |
struct SizedStrideCounter { |
| 124 |
int *it_; |
| 125 |
int *minus_; |
| 126 |
using value_type = int; |
| 127 |
explicit SizedStrideCounter(); |
| 128 |
constexpr explicit SizedStrideCounter(int *it, int *minus) : it_(it), minus_(minus) {} |
| 129 |
SizedStrideCounter& operator++(); |
| 130 |
SizedStrideCounter operator++(int); |
| 131 |
int& operator*() const; |
| 132 |
bool operator==(SizedStrideCounter) const; |
| 133 |
constexpr int operator-(SizedStrideCounter rhs) const { |
| 134 |
*minus_ += 1; |
| 135 |
return static_cast<int>(it_ - rhs.it_); |
| 136 |
} |
| 137 |
}; |
| 138 |
static_assert(std::forward_iterator<SizedStrideCounter>); |
| 139 |
static_assert(std::sized_sentinel_for<SizedStrideCounter, SizedStrideCounter>); |
| 140 |
|
| 141 |
constexpr void test_stride_counting() { |
| 142 |
{ |
| 143 |
int a[] = {1, 2, 3}; |
| 144 |
int inc = 0; |
| 145 |
StrideCounter first(a, &inc); |
| 146 |
StrideCounter last(a+3, nullptr); |
| 147 |
std::same_as<int> auto result = std::ranges::distance(first, last); |
| 148 |
assert(result == 3); |
| 149 |
assert(inc == 3); |
| 150 |
} |
| 151 |
{ |
| 152 |
int a[] = {1, 2, 3}; |
| 153 |
int minus = 0; |
| 154 |
SizedStrideCounter first(a, &minus); |
| 155 |
SizedStrideCounter last(a+3, nullptr); |
| 156 |
std::same_as<int> auto result = std::ranges::distance(first, last); |
| 157 |
assert(result == 3); |
| 158 |
assert(minus == 1); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
constexpr bool test() { |
| 163 |
{ |
| 164 |
int a[] = {1, 2, 3}; |
| 165 |
assert(std::ranges::distance(a, a + 3) == 3); |
| 166 |
assert(std::ranges::distance(a, a) == 0); |
| 167 |
assert(std::ranges::distance(a + 3, a) == -3); |
| 168 |
} |
| 169 |
|
| 170 |
test_unsized<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>(); |
| 171 |
test_unsized<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>(); |
| 172 |
test_unsized<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>(); |
| 173 |
test_unsized<bidirectional_iterator<int*>, sentinel_wrapper<bidirectional_iterator<int*>>>(); |
| 174 |
test_unsized<random_access_iterator<int*>, sentinel_wrapper<random_access_iterator<int*>>>(); |
| 175 |
test_unsized<contiguous_iterator<int*>, sentinel_wrapper<contiguous_iterator<int*>>>(); |
| 176 |
test_unsized<int*, sentinel_wrapper<int*>>(); |
| 177 |
test_unsized<const int*, sentinel_wrapper<const int*>>(); |
| 178 |
test_unsized<forward_iterator<int*>, forward_iterator<int*>>(); |
| 179 |
test_unsized<bidirectional_iterator<int*>, bidirectional_iterator<int*>>(); |
| 180 |
|
| 181 |
test_sized<cpp17_input_iterator<int*>, sized_sentinel<cpp17_input_iterator<int*>>>(); |
| 182 |
test_sized<cpp20_input_iterator<int*>, sized_sentinel<cpp20_input_iterator<int*>>>(); |
| 183 |
test_sized<cpp17_output_iterator<int*>, sized_sentinel<cpp17_output_iterator<int*>>>(); |
| 184 |
test_sized<forward_iterator<int*>, sized_sentinel<forward_iterator<int*>>>(); |
| 185 |
test_sized<bidirectional_iterator<int*>, sized_sentinel<bidirectional_iterator<int*>>>(); |
| 186 |
test_sized<random_access_iterator<int*>, sized_sentinel<random_access_iterator<int*>>>(); |
| 187 |
test_sized<contiguous_iterator<int*>, sized_sentinel<contiguous_iterator<int*>>>(); |
| 188 |
test_sized<int*, sized_sentinel<int*>>(); |
| 189 |
test_sized<const int*, sized_sentinel<const int*>>(); |
| 190 |
test_sized<int*, int*>(); |
| 191 |
test_sized<int*, const int*>(); |
| 192 |
test_sized<random_access_iterator<int*>, random_access_iterator<int*>>(); |
| 193 |
test_sized<contiguous_iterator<int*>, contiguous_iterator<int*>>(); |
| 194 |
|
| 195 |
{ |
| 196 |
using It = cpp20_input_iterator<int*>; // non-copyable, thus not a sentinel for itself |
| 197 |
static_assert(!std::is_copy_constructible_v<It>); |
| 198 |
static_assert(!std::sentinel_for<It, It>); |
| 199 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&>); |
| 200 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&&>); |
| 201 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&&, It&>); |
| 202 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&&, It&&>); |
| 203 |
} |
| 204 |
{ |
| 205 |
using It = cpp20_input_iterator<int*>; // non-copyable |
| 206 |
using Sent = sentinel_wrapper<It>; // not a sized sentinel |
| 207 |
static_assert(std::sentinel_for<Sent, It> && !std::sized_sentinel_for<Sent, It>); |
| 208 |
int a[] = {1,2,3}; |
| 209 |
Sent last = Sent(It(a + 3)); |
| 210 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, Sent&>); |
| 211 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, Sent&&>); |
| 212 |
assert(std::ranges::distance(It(a), last) == 3); |
| 213 |
assert(std::ranges::distance(It(a), Sent(It(a + 3))) == 3); |
| 214 |
} |
| 215 |
{ |
| 216 |
using It = cpp17_input_iterator<int*>; // not a sentinel for itself |
| 217 |
static_assert(!std::sentinel_for<It, It>); |
| 218 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&>); |
| 219 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&, It&&>); |
| 220 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&&, It&>); |
| 221 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), It&&, It&&>); |
| 222 |
} |
| 223 |
|
| 224 |
// Calling it on a non-iterator or non-sentinel isn't allowed. |
| 225 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), int, int>); |
| 226 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), int*, int>); |
| 227 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), int, int*>); |
| 228 |
static_assert(!std::is_invocable_v<decltype(std::ranges::distance), int*, char*>); |
| 229 |
|
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
int main(int, char**) { |
| 234 |
test(); |
| 235 |
static_assert(test()); |
| 236 |
|
| 237 |
return 0; |
| 238 |
} |