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: gcc-10
12 
13 // template<class I2, sentinel_for<I> S2>
14 //   requires sentinel_for<S, I2>
15 // friend bool operator==(
16 //   const common_iterator& x, const common_iterator<I2, S2>& y);
17 // template<class I2, sentinel_for<I> S2>
18 //   requires sentinel_for<S, I2> && equality_comparable_with<I, I2>
19 // friend bool operator==(
20 //   const common_iterator& x, const common_iterator<I2, S2>& y);
21 
22 #include <iterator>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "types.h"
27 
28 void test() {
29   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
30 
31   {
32     auto iter1 = simple_iterator<int*>(buffer);
33     auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
34     auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
35 
36     const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
37     const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
38 
39     assert(commonIter1 != commonSent1);
40     assert(commonIter2 != commonSent2);
41     assert(commonSent1 != commonIter1);
42     assert(commonSent2 != commonIter2);
43 
44     for (auto i = 1; commonIter1 != commonSent1; ++i) {
45       assert(*(commonIter1++) == i);
46     }
47     assert(commonIter1 == commonSent1);
48     assert(commonSent1 == commonIter1);
49   }
50   {
51     auto iter1 = value_iterator<int*>(buffer);
52     auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
53     auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
54 
55     const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
56     const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
57 
58     assert(commonIter1 != commonSent1);
59     assert(commonIter2 != commonSent2);
60     assert(commonSent1 != commonIter1);
61     assert(commonSent2 != commonIter2);
62 
63     for (auto i = 1; commonIter1 != commonSent1; ++i) {
64       assert(*(commonIter1++) == i);
65     }
66     assert(commonIter1 == commonSent1);
67     assert(commonSent1 == commonIter1);
68   }
69   {
70     auto iter1 = simple_iterator<int*>(buffer);
71     auto iter2 = comparable_iterator<int*>(buffer);
72     auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
73     auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
74 
75     const auto commonIter2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(iter2);
76     const auto commonSent2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
77 
78     assert(commonIter1 == commonIter2);
79     assert(commonSent1 != commonIter2);
80     assert(commonSent1 == commonSent2);
81     assert(commonSent2 == commonSent1);
82 
83     assert(commonIter1 != commonSent1);
84     assert(commonIter2 != commonSent2);
85     assert(commonSent1 != commonIter1);
86     assert(commonSent2 != commonIter2);
87 
88     assert(commonIter1 == commonIter2);
89     assert(commonIter2 == commonIter1);
90 
91     for (auto i = 1; commonIter1 != commonSent1; ++i) {
92       assert(*(commonIter1++) == i);
93     }
94     assert(commonIter1 == commonSent1);
95     assert(commonSent1 == commonIter1);
96 
97     // This check may *seem* incorrect (our iterators point to two completely different
98     // elements of buffer). However, this is actually what the Standard wants.
99     // See https://eel.is/c++draft/iterators.common#common.iter.cmp-2.
100     assert(commonIter1 == commonIter2);
101   }
102   {
103     auto iter1 = cpp17_input_iterator<int*>(buffer);
104     auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
105     auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
106 
107     const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
108     const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
109 
110     assert(commonIter1 != commonSent1);
111     assert(commonIter2 != commonSent2);
112     assert(commonSent1 != commonIter1);
113     assert(commonSent2 != commonIter2);
114 
115     for (auto i = 1; commonIter1 != commonSent1; ++i) {
116       assert(*(commonIter1++) == i);
117     }
118     assert(commonIter1 == commonSent1);
119     assert(commonSent1 == commonIter1);
120   }
121   {
122     auto iter1 = forward_iterator<int*>(buffer);
123     auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
124     auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
125 
126     const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
127     const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
128 
129     assert(commonIter1 != commonSent1);
130     assert(commonIter2 != commonSent2);
131     assert(commonSent1 != commonIter1);
132     assert(commonSent2 != commonIter2);
133 
134     for (auto i = 1; commonIter1 != commonSent1; ++i) {
135       assert(*(commonIter1++) == i);
136     }
137     assert(commonIter1 == commonSent1);
138     assert(commonSent1 == commonIter1);
139   }
140   {
141     auto iter1 = random_access_iterator<int*>(buffer);
142     auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
143     auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
144 
145     const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
146     const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
147 
148     assert(commonIter1 != commonSent1);
149     assert(commonIter2 != commonSent2);
150     assert(commonSent1 != commonIter1);
151     assert(commonSent2 != commonIter2);
152 
153     assert(commonSent1 == commonSent2);
154     assert(commonSent2 == commonSent1);
155 
156     for (auto i = 1; commonIter1 != commonSent1; ++i) {
157       assert(*(commonIter1++) == i);
158     }
159     assert(commonIter1 == commonSent1);
160     assert(commonSent1 == commonIter1);
161   }
162 }
163 
164 int main(int, char**) {
165   test();
166 
167   return 0;
168 }
169