1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // friend constexpr iterator operator-(iterator i, difference_type n)
12 // requires advanceable<W>;
13 // friend constexpr difference_type operator-(const iterator& x, const iterator& y)
14 // requires advanceable<W>;
19 #include <type_traits>
21 #include "test_macros.h"
24 // If we're compiling for 32 bit or windows, int and long are the same size, so long long is the correct difference type.
25 #if INTPTR_MAX == INT32_MAX || defined(_WIN32)
26 using IntDiffT
= long long;
28 using IntDiffT
= long;
31 constexpr bool test() {
32 // <iterator> - difference_type
34 // When "_Start" is signed integer like.
36 std::ranges::iota_view
<int> io(0);
37 auto iter1
= std::next(io
.begin(), 10);
38 auto iter2
= std::next(io
.begin(), 10);
39 assert(iter1
== iter2
);
40 assert(iter1
- 5 != iter2
);
41 assert(iter1
- 5 == std::ranges::prev(iter2
, 5));
43 static_assert(!std::is_reference_v
<decltype(iter2
- 5)>);
46 // When "_Start" is not integer like.
48 std::ranges::iota_view
io(SomeInt(0));
49 auto iter1
= std::next(io
.begin(), 10);
50 auto iter2
= std::next(io
.begin(), 10);
51 assert(iter1
== iter2
);
52 assert(iter1
- 5 != iter2
);
53 assert(iter1
- 5 == std::ranges::prev(iter2
, 5));
55 static_assert(!std::is_reference_v
<decltype(iter2
- 5)>);
58 // When "_Start" is unsigned integer like and n is greater than or equal to zero.
60 std::ranges::iota_view
<unsigned> io(0);
61 auto iter1
= std::next(io
.begin(), 10);
62 auto iter2
= std::next(io
.begin(), 10);
63 assert(iter1
== iter2
);
64 assert(iter1
- 5 != iter2
);
65 assert(iter1
- 5 == std::ranges::prev(iter2
, 5));
67 static_assert(!std::is_reference_v
<decltype(iter2
- 5)>);
70 std::ranges::iota_view
<unsigned> io(0);
71 auto iter1
= std::next(io
.begin(), 10);
72 auto iter2
= std::next(io
.begin(), 10);
73 assert(iter1
- 0 == iter2
);
76 // When "_Start" is unsigned integer like and n is less than zero.
78 std::ranges::iota_view
<unsigned> io(0);
79 auto iter1
= std::next(io
.begin(), 10);
80 auto iter2
= std::next(io
.begin(), 10);
81 assert(iter1
- 5 != iter2
);
82 assert(iter1
- 5 == std::ranges::prev(iter2
, 5));
84 static_assert(!std::is_reference_v
<decltype(iter2
- 5)>);
88 // <iterator> - <iterator>
90 // When "_Start" is signed integer like.
92 std::ranges::iota_view
<int> io(0);
93 auto iter1
= std::next(io
.begin(), 10);
94 auto iter2
= std::next(io
.begin(), 5);
95 assert(iter1
- iter2
== 5);
97 LIBCPP_STATIC_ASSERT(std::same_as
<decltype(iter1
- iter2
), IntDiffT
>);
100 std::ranges::iota_view
<int> io(0);
101 auto iter1
= std::next(io
.begin(), 10);
102 auto iter2
= std::next(io
.begin(), 10);
103 assert(iter1
- iter2
== 0);
105 LIBCPP_STATIC_ASSERT(std::same_as
<decltype(iter1
- iter2
), IntDiffT
>);
108 std::ranges::iota_view
<int> io(0);
109 auto iter1
= std::next(io
.begin(), 5);
110 auto iter2
= std::next(io
.begin(), 10);
111 assert(iter1
- iter2
== -5);
113 LIBCPP_STATIC_ASSERT(std::same_as
<decltype(iter1
- iter2
), IntDiffT
>);
116 // When "_Start" is unsigned integer like and y > x.
118 std::ranges::iota_view
<unsigned> io(0);
119 auto iter1
= std::next(io
.begin(), 5);
120 auto iter2
= std::next(io
.begin(), 10);
121 assert(iter1
- iter2
== -5);
123 LIBCPP_STATIC_ASSERT(std::same_as
<decltype(iter1
- iter2
), IntDiffT
>);
126 // When "_Start" is unsigned integer like and x >= y.
128 std::ranges::iota_view
<unsigned> io(0);
129 auto iter1
= std::next(io
.begin(), 10);
130 auto iter2
= std::next(io
.begin(), 5);
131 assert(iter1
- iter2
== 5);
133 LIBCPP_STATIC_ASSERT(std::same_as
<decltype(iter1
- iter2
), IntDiffT
>);
136 std::ranges::iota_view
<unsigned> io(0);
137 auto iter1
= std::next(io
.begin(), 10);
138 auto iter2
= std::next(io
.begin(), 10);
139 assert(iter1
- iter2
== 0);
141 LIBCPP_STATIC_ASSERT(std::same_as
<decltype(iter1
- iter2
), IntDiffT
>);
144 // When "_Start" is not integer like.
146 std::ranges::iota_view
io(SomeInt(0));
147 auto iter1
= std::next(io
.begin(), 10);
148 auto iter2
= std::next(io
.begin(), 5);
149 assert(iter1
- iter2
== 5);
151 static_assert(std::same_as
<decltype(iter1
- iter2
), int>);
154 std::ranges::iota_view
io(SomeInt(0));
155 auto iter1
= std::next(io
.begin(), 10);
156 auto iter2
= std::next(io
.begin(), 10);
157 assert(iter1
- iter2
== 0);
159 static_assert(std::same_as
<decltype(iter1
- iter2
), int>);
162 std::ranges::iota_view
io(SomeInt(0));
163 auto iter1
= std::next(io
.begin(), 5);
164 auto iter2
= std::next(io
.begin(), 10);
165 assert(iter1
- iter2
== -5);
167 static_assert(std::same_as
<decltype(iter1
- iter2
), int>);
174 int main(int, char**) {
176 static_assert(test());