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

source code of libcxx/test/std/ranges/range.adaptors/range.zip/iterator/compare.pass.cpp