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, c++20
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11
12 // friend constexpr bool operator==(const iterator& x, const iterator& y)
13 // requires (equality_comparable<iterator_t<maybe-const<Const, Views>>> && ...);
14 // friend constexpr bool operator<(const iterator& x, const iterator& y)
15 // requires all-random-access<Const, Views...>;
16 // friend constexpr bool operator>(const iterator& x, const iterator& y)
17 // requires all-random-access<Const, Views...>;
18 // friend constexpr bool operator<=(const iterator& x, const iterator& y)
19 // requires all-random-access<Const, Views...>;
20 // friend constexpr bool operator>=(const iterator& x, const iterator& y)
21 // requires all-random-access<Const, Views...>;
22 // friend constexpr auto operator<=>(const iterator& x, const iterator& y)
23 // requires all-random-access<Const, Views...> &&
24 // (three_way_comparable<iterator_t<maybe-const<Const, Views>>> && ...);
25
26 #include <ranges>
27 #include <compare>
28
29 #include "test_iterators.h"
30 #include "../types.h"
31
32 // This is for testing that zip iterator never calls underlying iterator's >, >=, <=, !=.
33 // The spec indicates that zip iterator's >= is negating zip iterator's < instead of calling underlying iterator's >=.
34 // Declare all the operations >, >=, <= etc to make it satisfy random_access_iterator concept,
35 // but not define them. If the zip iterator's >,>=, <=, etc isn't implemented in the way defined by the standard
36 // but instead calling underlying iterator's >,>=,<=, we will get a linker error for the runtime tests and
37 // non-constant expression for the compile time tests.
38 struct LessThanIterator {
39 int* it_ = nullptr;
40 LessThanIterator() = default;
LessThanIteratorLessThanIterator41 constexpr LessThanIterator(int* it) : it_(it) {}
42
43 using iterator_category = std::random_access_iterator_tag;
44 using value_type = int;
45 using difference_type = intptr_t;
46
operator *LessThanIterator47 constexpr int& operator*() const { return *it_; }
operator []LessThanIterator48 constexpr int& operator[](difference_type n) const { return it_[n]; }
operator ++LessThanIterator49 constexpr LessThanIterator& operator++() {
50 ++it_;
51 return *this;
52 }
operator --LessThanIterator53 constexpr LessThanIterator& operator--() {
54 --it_;
55 return *this;
56 }
operator ++LessThanIterator57 constexpr LessThanIterator operator++(int) { return LessThanIterator(it_++); }
operator --LessThanIterator58 constexpr LessThanIterator operator--(int) { return LessThanIterator(it_--); }
59
operator +=LessThanIterator60 constexpr LessThanIterator& operator+=(difference_type n) {
61 it_ += n;
62 return *this;
63 }
operator -=LessThanIterator64 constexpr LessThanIterator& operator-=(difference_type n) {
65 it_ -= n;
66 return *this;
67 }
68
operator +(LessThanIterator x,difference_type n)69 constexpr friend LessThanIterator operator+(LessThanIterator x, difference_type n) {
70 x += n;
71 return x;
72 }
operator +(difference_type n,LessThanIterator x)73 constexpr friend LessThanIterator operator+(difference_type n, LessThanIterator x) {
74 x += n;
75 return x;
76 }
operator -(LessThanIterator x,difference_type n)77 constexpr friend LessThanIterator operator-(LessThanIterator x, difference_type n) {
78 x -= n;
79 return x;
80 }
operator -(LessThanIterator x,LessThanIterator y)81 constexpr friend difference_type operator-(LessThanIterator x, LessThanIterator y) { return x.it_ - y.it_; }
82
83 constexpr friend bool operator==(LessThanIterator const&, LessThanIterator const&) = default;
84 friend bool operator!=(LessThanIterator const&, LessThanIterator const&);
85
operator <(LessThanIterator const & x,LessThanIterator const & y)86 constexpr friend bool operator<(LessThanIterator const& x, LessThanIterator const& y) { return x.it_ < y.it_; }
87 friend bool operator<=(LessThanIterator const&, LessThanIterator const&);
88 friend bool operator>(LessThanIterator const&, LessThanIterator const&);
89 friend bool operator>=(LessThanIterator const&, LessThanIterator const&);
90 };
91 static_assert(std::random_access_iterator<LessThanIterator>);
92
93 struct SmallerThanRange : IntBufferView {
94 using IntBufferView::IntBufferView;
beginSmallerThanRange95 constexpr LessThanIterator begin() const { return {buffer_}; }
endSmallerThanRange96 constexpr LessThanIterator end() const { return {buffer_ + size_}; }
97 };
98 static_assert(std::ranges::random_access_range<SmallerThanRange>);
99
100 struct ForwardCommonView : IntBufferView {
101 using IntBufferView::IntBufferView;
102 using iterator = forward_iterator<int*>;
103
beginForwardCommonView104 constexpr iterator begin() const { return iterator(buffer_); }
endForwardCommonView105 constexpr iterator end() const { return iterator(buffer_ + size_); }
106 };
107
compareOperatorTest(auto && iter1,auto && iter2)108 constexpr void compareOperatorTest(auto&& iter1, auto&& iter2) {
109 assert(!(iter1 < iter1));
110 assert(iter1 < iter2);
111 assert(!(iter2 < iter1));
112 assert(iter1 <= iter1);
113 assert(iter1 <= iter2);
114 assert(!(iter2 <= iter1));
115 assert(!(iter1 > iter1));
116 assert(!(iter1 > iter2));
117 assert(iter2 > iter1);
118 assert(iter1 >= iter1);
119 assert(!(iter1 >= iter2));
120 assert(iter2 >= iter1);
121 assert(iter1 == iter1);
122 assert(!(iter1 == iter2));
123 assert(iter2 == iter2);
124 assert(!(iter1 != iter1));
125 assert(iter1 != iter2);
126 assert(!(iter2 != iter2));
127 }
128
inequalityOperatorsDoNotExistTest(auto && iter1,auto && iter2)129 constexpr void inequalityOperatorsDoNotExistTest(auto&& iter1, auto&& iter2) {
130 using Iter1 = decltype(iter1);
131 using Iter2 = decltype(iter2);
132 static_assert(!std::is_invocable_v<std::less<>, Iter1, Iter2>);
133 static_assert(!std::is_invocable_v<std::less_equal<>, Iter1, Iter2>);
134 static_assert(!std::is_invocable_v<std::greater<>, Iter1, Iter2>);
135 static_assert(!std::is_invocable_v<std::greater_equal<>, Iter1, Iter2>);
136 }
137
test()138 constexpr bool test() {
139 {
140 // Test a new-school iterator with operator<=>; the iterator should also have operator<=>.
141 using It = three_way_contiguous_iterator<int*>;
142 using SubRange = std::ranges::subrange<It>;
143 static_assert(std::three_way_comparable<It>);
144 using R = std::ranges::zip_view<SubRange, SubRange>;
145 static_assert(std::three_way_comparable<std::ranges::iterator_t<R>>);
146
147 int a[] = {1, 2, 3, 4};
148 int b[] = {5, 6, 7, 8, 9};
149 auto r = std::views::zip(SubRange(It(a), It(a + 4)), SubRange(It(b), It(b + 5)));
150 auto iter1 = r.begin();
151 auto iter2 = iter1 + 1;
152
153 compareOperatorTest(iter1, iter2);
154
155 assert((iter1 <=> iter2) == std::strong_ordering::less);
156 assert((iter1 <=> iter1) == std::strong_ordering::equal);
157 assert((iter2 <=> iter1) == std::strong_ordering::greater);
158 }
159
160 {
161 // Test an old-school iterator with no operator<=>; the transform iterator shouldn't have
162 // operator<=> either.
163 using It = random_access_iterator<int*>;
164 using Subrange = std::ranges::subrange<It>;
165 static_assert(!std::three_way_comparable<It>);
166 using R = std::ranges::zip_view<Subrange, Subrange>;
167 static_assert(!std::three_way_comparable<std::ranges::iterator_t<R>>);
168
169 int a[] = {1, 2, 3, 4};
170 int b[] = {5, 6, 7, 8, 9};
171 auto r = std::views::zip(Subrange(It(a), It(a + 4)), Subrange(It(b), It(b + 5)));
172 auto iter1 = r.begin();
173 auto iter2 = iter1 + 1;
174
175 compareOperatorTest(iter1, iter2);
176 }
177
178 {
179 // non random_access_range
180 int buffer1[1] = {1};
181 int buffer2[2] = {1, 2};
182
183 std::ranges::zip_view v{InputCommonView(buffer1), InputCommonView(buffer2)};
184 using View = decltype(v);
185 static_assert(!std::ranges::forward_range<View>);
186 static_assert(std::ranges::input_range<View>);
187 static_assert(std::ranges::common_range<View>);
188
189 auto it1 = v.begin();
190 auto it2 = v.end();
191 assert(it1 != it2);
192
193 ++it1;
194 assert(it1 == it2);
195
196 inequalityOperatorsDoNotExistTest(it1, it2);
197 }
198
199 {
200 // in this case sentinel is computed by getting each of the underlying sentinel, so only one
201 // underlying iterator is comparing equal
202 int buffer1[1] = {1};
203 int buffer2[2] = {1, 2};
204 std::ranges::zip_view v{ForwardCommonView(buffer1), ForwardCommonView(buffer2)};
205 using View = decltype(v);
206 static_assert(std::ranges::common_range<View>);
207 static_assert(!std::ranges::bidirectional_range<View>);
208
209 auto it1 = v.begin();
210 auto it2 = v.end();
211 assert(it1 != it2);
212
213 ++it1;
214 // it1: <buffer1 + 1, buffer2 + 1>
215 // it2: <buffer1 + 1, buffer2 + 2>
216 assert(it1 == it2);
217
218 inequalityOperatorsDoNotExistTest(it1, it2);
219 }
220
221 {
222 // only < and == are needed
223 int a[] = {1, 2, 3, 4};
224 int b[] = {5, 6, 7, 8, 9};
225 auto r = std::views::zip(SmallerThanRange(a), SmallerThanRange(b));
226 auto iter1 = r.begin();
227 auto iter2 = iter1 + 1;
228
229 compareOperatorTest(iter1, iter2);
230 }
231
232 {
233 // underlying iterator does not support ==
234 using IterNoEqualView = BasicView<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>;
235 int buffer[] = {1};
236 std::ranges::zip_view r(IterNoEqualView{buffer});
237 auto it = r.begin();
238 using Iter = decltype(it);
239 static_assert(!std::invocable<std::equal_to<>, Iter, Iter>);
240 inequalityOperatorsDoNotExistTest(it, it);
241 }
242 return true;
243 }
244
main(int,char **)245 int main(int, char**) {
246 test();
247 static_assert(test());
248
249 return 0;
250 }
251