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 // common_iterator& operator++();
12 // decltype(auto) operator++(int);
17 #include "test_macros.h"
23 // Reference: http://eel.is/c++draft/iterators.common#common.iter.nav-5
24 // Case 2: can-reference
26 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
27 auto iter1
= simple_iterator
<int*>(buffer
);
28 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
29 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
31 assert(*(commonIter1
++) == 1);
32 assert(*commonIter1
== 2);
33 assert(*(++commonIter1
) == 3);
34 assert(*commonIter1
== 3);
36 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
37 assert(*(commonIter1
++) == i
);
39 assert(commonIter1
== commonSent1
);
42 // Case 2: can-reference
44 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
45 auto iter1
= value_iterator
<int*>(buffer
);
46 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
47 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
49 assert(*(commonIter1
++) == 1);
50 assert(*commonIter1
== 2);
51 assert(*(++commonIter1
) == 3);
52 assert(*commonIter1
== 3);
54 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
55 assert(*(commonIter1
++) == i
);
57 assert(commonIter1
== commonSent1
);
60 // Case 3: postfix-proxy
62 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
63 auto iter1
= void_plus_plus_iterator
<int*>(buffer
);
64 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
65 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
67 assert(*(commonIter1
++) == 1);
68 assert(*commonIter1
== 2);
69 assert(*(++commonIter1
) == 3);
70 assert(*commonIter1
== 3);
72 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
73 assert(*(commonIter1
++) == i
);
75 assert(commonIter1
== commonSent1
);
78 // Case 2: where this is not referencable or move constructible
80 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
81 auto iter1
= value_type_not_move_constructible_iterator
<int*>(buffer
);
82 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
83 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
86 ASSERT_SAME_TYPE(decltype(commonIter1
++), void);
87 assert(*commonIter1
== 2);
88 assert(*(++commonIter1
) == 3);
89 assert(*commonIter1
== 3);
91 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
92 assert(*commonIter1
== i
);
95 assert(commonIter1
== commonSent1
);
98 // Case 2: can-reference
100 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
101 auto iter1
= cpp17_input_iterator
<int*>(buffer
);
102 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
103 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
105 assert(*(commonIter1
++) == 1);
106 assert(*commonIter1
== 2);
107 assert(*(++commonIter1
) == 3);
108 assert(*commonIter1
== 3);
110 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
111 assert(*(commonIter1
++) == i
);
113 assert(commonIter1
== commonSent1
);
116 // Case 1: forward_iterator
118 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
119 auto iter1
= forward_iterator
<int*>(buffer
);
120 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
121 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
123 assert(*(commonIter1
++) == 1);
124 assert(*commonIter1
== 2);
125 assert(*(++commonIter1
) == 3);
126 assert(*commonIter1
== 3);
128 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
129 assert(*(commonIter1
++) == i
);
131 assert(commonIter1
== commonSent1
);
134 // Case 1: forward_iterator
136 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
137 auto iter1
= random_access_iterator
<int*>(buffer
);
138 auto commonIter1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(iter1
);
139 auto commonSent1
= std::common_iterator
<decltype(iter1
), sentinel_type
<int*>>(sentinel_type
<int*>{buffer
+ 8});
141 assert(*(commonIter1
++) == 1);
142 assert(*commonIter1
== 2);
143 assert(*(++commonIter1
) == 3);
144 assert(*commonIter1
== 3);
146 for (auto i
= 3; commonIter1
!= commonSent1
; ++i
) {
147 assert(*(commonIter1
++) == i
);
149 assert(commonIter1
== commonSent1
);
152 // Increment a common_iterator<cpp17_output_iterator>: iter_value_t is not always valid for
153 // output iterators (it isn't for our test cpp17_output_iterator). This is worth testing
154 // because it gets tricky when we define operator++(int).
156 int buffer
[] = {0, 1, 2, 3, 4};
157 using Common
= std::common_iterator
<cpp17_output_iterator
<int*>, sentinel_type
<int*>>;
158 auto iter
= Common(cpp17_output_iterator
<int*>(buffer
));
159 auto sent
= Common(sentinel_type
<int*>{buffer
+ 5});
162 assert(buffer
[0] == 90);
165 assert(buffer
[1] == 91);
168 assert(buffer
[2] == 92);
172 assert(iter
!= sent
);
174 assert(iter
== sent
);
178 int main(int, char**) {