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

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