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 // UNSUPPORTED: libcpp-no-concepts
11 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
12 
13 // friend constexpr iterator operator-(iterator i, difference_type n)
14 //   requires advanceable<W>;
15 // friend constexpr difference_type operator-(const iterator& x, const iterator& y)
16 //   requires advanceable<W>;
17 
18 #include <cassert>
19 #include <cstdint>
20 #include <ranges>
21 
22 #include "test_macros.h"
23 #include "../types.h"
24 
25 // If we're compiling for 32 bit or windows, int and long are the same size, so long long is the correct difference type.
26 #if INTPTR_MAX == INT32_MAX || defined(_WIN32)
27 using IntDiffT = long long;
28 #else
29 using IntDiffT = long;
30 #endif
31 
32 constexpr bool test() {
33   // <iterator> - difference_type
34   {
35     // When "_Start" is signed integer like.
36     {
37       std::ranges::iota_view<int> io(0);
38       auto iter1 = std::next(io.begin(), 10);
39       auto iter2 = std::next(io.begin(), 10);
40       assert(iter1 == iter2);
41       assert(iter1 - 5 != iter2);
42       assert(iter1 - 5 == std::ranges::prev(iter2, 5));
43 
44       static_assert(!std::is_reference_v<decltype(iter2 - 5)>);
45     }
46 
47     // When "_Start" is not integer like.
48     {
49       std::ranges::iota_view io(SomeInt(0));
50       auto iter1 = std::next(io.begin(), 10);
51       auto iter2 = std::next(io.begin(), 10);
52       assert(iter1 == iter2);
53       assert(iter1 - 5 != iter2);
54       assert(iter1 - 5 == std::ranges::prev(iter2, 5));
55 
56       static_assert(!std::is_reference_v<decltype(iter2 - 5)>);
57     }
58 
59     // When "_Start" is unsigned integer like and n is greater than or equal to zero.
60     {
61       std::ranges::iota_view<unsigned> io(0);
62       auto iter1 = std::next(io.begin(), 10);
63       auto iter2 = std::next(io.begin(), 10);
64       assert(iter1 == iter2);
65       assert(iter1 - 5 != iter2);
66       assert(iter1 - 5 == std::ranges::prev(iter2, 5));
67 
68       static_assert(!std::is_reference_v<decltype(iter2 - 5)>);
69     }
70     {
71       std::ranges::iota_view<unsigned> io(0);
72       auto iter1 = std::next(io.begin(), 10);
73       auto iter2 = std::next(io.begin(), 10);
74       assert(iter1 - 0 == iter2);
75     }
76 
77     // When "_Start" is unsigned integer like and n is less than zero.
78     {
79       std::ranges::iota_view<unsigned> io(0);
80       auto iter1 = std::next(io.begin(), 10);
81       auto iter2 = std::next(io.begin(), 10);
82       assert(iter1 - 5 != iter2);
83       assert(iter1 - 5 == std::ranges::prev(iter2, 5));
84 
85       static_assert(!std::is_reference_v<decltype(iter2 - 5)>);
86     }
87   }
88 
89   // <iterator> - <iterator>
90   {
91     // When "_Start" is signed integer like.
92     {
93       std::ranges::iota_view<int> io(0);
94       auto iter1 = std::next(io.begin(), 10);
95       auto iter2 = std::next(io.begin(), 5);
96       assert(iter1 - iter2 == 5);
97 
98       LIBCPP_STATIC_ASSERT(std::same_as<decltype(iter1 - iter2), IntDiffT>);
99     }
100     {
101       std::ranges::iota_view<int> io(0);
102       auto iter1 = std::next(io.begin(), 10);
103       auto iter2 = std::next(io.begin(), 10);
104       assert(iter1 - iter2 == 0);
105 
106       LIBCPP_STATIC_ASSERT(std::same_as<decltype(iter1 - iter2), IntDiffT>);
107     }
108     {
109       std::ranges::iota_view<int> io(0);
110       auto iter1 = std::next(io.begin(), 5);
111       auto iter2 = std::next(io.begin(), 10);
112       assert(iter1 - iter2 == -5);
113 
114       LIBCPP_STATIC_ASSERT(std::same_as<decltype(iter1 - iter2), IntDiffT>);
115     }
116 
117     // When "_Start" is unsigned integer like and y > x.
118     {
119       std::ranges::iota_view<unsigned> io(0);
120       auto iter1 = std::next(io.begin(), 5);
121       auto iter2 = std::next(io.begin(), 10);
122       assert(iter1 - iter2 == -5);
123 
124       LIBCPP_STATIC_ASSERT(std::same_as<decltype(iter1 - iter2), IntDiffT>);
125     }
126 
127     // When "_Start" is unsigned integer like and x >= y.
128     {
129       std::ranges::iota_view<unsigned> io(0);
130       auto iter1 = std::next(io.begin(), 10);
131       auto iter2 = std::next(io.begin(), 5);
132       assert(iter1 - iter2 == 5);
133 
134       LIBCPP_STATIC_ASSERT(std::same_as<decltype(iter1 - iter2), IntDiffT>);
135     }
136     {
137       std::ranges::iota_view<unsigned> io(0);
138       auto iter1 = std::next(io.begin(), 10);
139       auto iter2 = std::next(io.begin(), 10);
140       assert(iter1 - iter2 == 0);
141 
142       LIBCPP_STATIC_ASSERT(std::same_as<decltype(iter1 - iter2), IntDiffT>);
143     }
144 
145     // When "_Start" is not integer like.
146     {
147       std::ranges::iota_view io(SomeInt(0));
148       auto iter1 = std::next(io.begin(), 10);
149       auto iter2 = std::next(io.begin(), 5);
150       assert(iter1 - iter2 == 5);
151 
152       static_assert(std::same_as<decltype(iter1 - iter2), int>);
153     }
154     {
155       std::ranges::iota_view io(SomeInt(0));
156       auto iter1 = std::next(io.begin(), 10);
157       auto iter2 = std::next(io.begin(), 10);
158       assert(iter1 - iter2 == 0);
159 
160       static_assert(std::same_as<decltype(iter1 - iter2), int>);
161     }
162     {
163       std::ranges::iota_view io(SomeInt(0));
164       auto iter1 = std::next(io.begin(), 5);
165       auto iter2 = std::next(io.begin(), 10);
166       assert(iter1 - iter2 == -5);
167 
168       static_assert(std::same_as<decltype(iter1 - iter2), int>);
169     }
170   }
171 
172   return true;
173 }
174 
175 int main(int, char**) {
176   test();
177   static_assert(test());
178 
179   return 0;
180 }
181