[go: nahoru, domu]

blob: 99d97f51efb42c4559b4b6d5bb9f3a3b6c43db5c [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2019 The Chromium Authors
Jan Wilken Dörrie6c2032812019-11-13 03:32:192// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
8#include "base/containers/checked_iterators.h"
9
10namespace base {
11
12#if defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_START_END) // [r"constexpr variable 'iter' must be initialized by a constant expression"]
13
14constexpr int kArray[] = {1, 2, 3, 4, 5};
15
16void WontCompile() {
17 // start can't be larger than end
18 constexpr CheckedContiguousIterator<const int> iter(kArray + 1, kArray);
19}
20
21#elif defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_START_CURRENT) // [r"constexpr variable 'iter' must be initialized by a constant expression"]
22
23constexpr int kArray[] = {1, 2, 3, 4, 5};
24
25void WontCompile() {
26 // current can't be larger than start
27 constexpr CheckedContiguousIterator<const int> iter(kArray + 1, kArray, kArray + 5);
28}
29
30#elif defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_CURRENT_END) // [r"constexpr variable 'iter' must be initialized by a constant expression"]
31
32constexpr int kArray[] = {1, 2, 3, 4, 5};
33
34void WontCompile() {
35 // current can't be larger than end
36 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 2, kArray + 1);
37}
38
39#elif defined(NCTEST_CHECKED_ITERATORS_EQ_DIFFERENT_ITER) // [r"constexpr variable 'equal' must be initialized by a constant expression"]
40
41constexpr int kArray1[] = {1, 2, 3, 4, 5};
42constexpr int kArray2[] = {1, 2, 3, 4, 5};
43
44void WontCompile() {
45 // Can't compare iterators into different containers
46 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
47 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
48 constexpr bool equal = iter1 == iter2;
49}
50
51#elif defined(NCTEST_CHECKED_ITERATORS_NE_DIFFERENT_ITER) // [r"constexpr variable 'not_equal' must be initialized by a constant expression"]
52
53constexpr int kArray1[] = {1, 2, 3, 4, 5};
54constexpr int kArray2[] = {1, 2, 3, 4, 5};
55
56void WontCompile() {
57 // Can't compare iterators into different containers
58 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
59 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
60 constexpr bool not_equal = iter1 != iter2;
61}
62
63#elif defined(NCTEST_CHECKED_ITERATORS_LT_DIFFERENT_ITER) // [r"constexpr variable 'less_than' must be initialized by a constant expression"]
64
65constexpr int kArray1[] = {1, 2, 3, 4, 5};
66constexpr int kArray2[] = {1, 2, 3, 4, 5};
67
68void WontCompile() {
69 // Can't compare iterators into different containers
70 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
71 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
72 constexpr bool less_than = iter1 < iter2;
73}
74
75#elif defined(NCTEST_CHECKED_ITERATORS_LE_DIFFERENT_ITER) // [r"constexpr variable 'less_equal' must be initialized by a constant expression"]
76
77constexpr int kArray1[] = {1, 2, 3, 4, 5};
78constexpr int kArray2[] = {1, 2, 3, 4, 5};
79
80void WontCompile() {
81 // Can't compare iterators into different containers
82 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
83 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
84 constexpr bool less_equal = iter1 <= iter2;
85}
86
87#elif defined(NCTEST_CHECKED_ITERATORS_GT_DIFFERENT_ITER) // [r"constexpr variable 'greater_than' must be initialized by a constant expression"]
88
89constexpr int kArray1[] = {1, 2, 3, 4, 5};
90constexpr int kArray2[] = {1, 2, 3, 4, 5};
91
92void WontCompile() {
93 // Can't compare iterators into different containers
94 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
95 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
96 constexpr bool greater_than = iter1 > iter2;
97}
98
99#elif defined(NCTEST_CHECKED_ITERATORS_GE_DIFFERENT_ITER) // [r"constexpr variable 'greater_equal' must be initialized by a constant expression"]
100
101constexpr int kArray1[] = {1, 2, 3, 4, 5};
102constexpr int kArray2[] = {1, 2, 3, 4, 5};
103
104void WontCompile() {
105 // Can't compare iterators into different containers
106 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
107 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
108 constexpr bool greater_equal = iter1 >= iter2;
109}
110
111#elif defined(NCTEST_CHECKED_ITERATORS_PRE_INCR_END) // [r"constexpr variable 'pre_incr' must be initialized by a constant expression"]
112
113constexpr int kArray[] = {1, 2, 3, 4, 5};
114
115constexpr int PreIncr() {
116 // Can't pre-increment the end iterator.
117 CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
118 ++end_iter;
119 return 0;
120}
121
122void WontCompile() {
123 constexpr int pre_incr = PreIncr();
124}
125
126#elif defined(NCTEST_CHECKED_ITERATORS_POST_INCR_END) // [r"constexpr variable 'post_incr' must be initialized by a constant expression"]
127
128constexpr int kArray[] = {1, 2, 3, 4, 5};
129
130constexpr int PostIncr() {
131 // Can't post-increment the end iterator.
132 CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
133 end_iter++;
134 return 0;
135}
136
137void WontCompile() {
138 constexpr int post_incr = PostIncr();
139}
140
141#elif defined(NCTEST_CHECKED_ITERATORS_PRE_DECR_BEGIN) // [r"constexpr variable 'pre_decr' must be initialized by a constant expression"]
142
143constexpr int kArray[] = {1, 2, 3, 4, 5};
144
145constexpr int PreDecr() {
146 // Can't pre-decrement the begin iterator.
147 CheckedContiguousIterator<const int> begin_iter(kArray, kArray + 5);
148 --begin_iter;
149 return 0;
150}
151
152void WontCompile() {
153 constexpr int pre_decr = PreDecr();
154}
155
156#elif defined(NCTEST_CHECKED_ITERATORS_POST_DECR_BEGIN) // [r"constexpr variable 'post_decr' must be initialized by a constant expression"]
157
158constexpr int kArray[] = {1, 2, 3, 4, 5};
159
160constexpr int PostDecr() {
161 // Can't post-decrement the begin iterator.
162 CheckedContiguousIterator<const int> begin_iter(kArray, kArray + 5);
163 begin_iter--;
164 return 0;
165}
166
167void WontCompile() {
168 constexpr int post_decr = PostDecr();
169}
170
171#elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END) // [r"constexpr variable 'incr_past_end' must be initialized by a constant expression"]
172
173constexpr int kArray[] = {1, 2, 3, 4, 5};
174
175constexpr int IncrPastEnd() {
176 // Can't increment iterator past the end.
177 CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
178 iter += 6;
179 return 0;
180}
181
182void WontCompile() {
183 constexpr int incr_past_end = IncrPastEnd();
184}
185
186#elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN) // [r"constexpr variable 'decr_past_begin' must be initialized by a constant expression"]
187
188constexpr int kArray[] = {1, 2, 3, 4, 5};
189
190constexpr int DecrPastBegin() {
191 // Can't decrement iterator past the begin.
192 CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
193 iter += -1;
194 return 0;
195}
196
197void WontCompile() {
198 constexpr int decr_past_begin = DecrPastBegin();
199}
200
201#elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END_2) // [r"constexpr variable 'iter_past_end' must be initialized by a constant expression"]
202
203constexpr int kArray[] = {1, 2, 3, 4, 5};
204
205void WontCompile() {
206 // Can't increment iterator past the end.
207 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
208 constexpr auto iter_past_end = iter + 6;
209}
210
211#elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN_2) // [r"constexpr variable 'iter_past_begin' must be initialized by a constant expression"]
212
213constexpr int kArray[] = {1, 2, 3, 4, 5};
214
215void WontCompile() {
216 // Can't decrement iterator past the begin.
217 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
218 constexpr auto iter_past_begin = iter + (-1);
219}
220
221#elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN_3) // [r"constexpr variable 'decr_past_begin' must be initialized by a constant expression"]
222
223constexpr int kArray[] = {1, 2, 3, 4, 5};
224
225constexpr int DecrPastBegin() {
226 // Can't decrement iterator past the begin.
227 CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
228 iter -= 1;
229 return 0;
230}
231
232void WontCompile() {
233 constexpr int decr_past_begin = DecrPastBegin();
234}
235
236#elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END_3) // [r"constexpr variable 'incr_past_end' must be initialized by a constant expression"]
237
238constexpr int kArray[] = {1, 2, 3, 4, 5};
239
240constexpr int IncrPastEnd() {
241 // Can't increment iterator past the end.
242 CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
243 iter -= (-6);
244 return 0;
245}
246
247void WontCompile() {
248 constexpr int incr_past_end = IncrPastEnd();
249}
250
251#elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN_4) // [r"constexpr variable 'iter_past_begin' must be initialized by a constant expression"]
252
253constexpr int kArray[] = {1, 2, 3, 4, 5};
254
255void WontCompile() {
256 // Can't decrement iterator past the begin.
257 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
258 constexpr auto iter_past_begin = iter - 1;
259}
260
261#elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END_4) // [r"constexpr variable 'iter_past_end' must be initialized by a constant expression"]
262
263constexpr int kArray[] = {1, 2, 3, 4, 5};
264
265void WontCompile() {
266 // Can't increment iterator past the end.
267 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
268 constexpr auto iter_past_end = iter - (-6);
269}
270
271#elif defined(NCTEST_CHECKED_ITERATORS_DIFFERENCE_DIFFERENT_ITER) // [r"constexpr variable 'difference' must be initialized by a constant expression"]
272
273constexpr int kArray1[] = {1, 2, 3, 4, 5};
274constexpr int kArray2[] = {1, 2, 3, 4, 5};
275
276void WontCompile() {
277 // Can't compare iterators into different containers
278 constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
279 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
280 constexpr auto difference = iter1 - iter2;
281}
282
283#elif defined(NCTEST_CHECKED_ITERATORS_STAR_END) // [r"constexpr variable 'ref' must be initialized by a constant expression"]
284
285constexpr int kArray[] = {1, 2, 3, 4, 5};
286
287void WontCompile() {
288 // Can't dereference the end iterator by star.
289 constexpr CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
290 constexpr auto& ref = *end_iter;
291}
292
293#elif defined(NCTEST_CHECKED_ITERATORS_ARROW_END) // [r"constexpr variable 'ptr' must be initialized by a constant expression"]
294
295constexpr int kArray[] = {1, 2, 3, 4, 5};
296
297void WontCompile() {
298 // Can't dereference the end iterator by arrow.
299 constexpr CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
300 constexpr auto* ptr = end_iter.operator->();
301}
302
303#elif defined(NCTEST_CHECKED_ITERATORS_NEGATIVE_OPERATOR_AT) // [r"constexpr variable 'ref' must be initialized by a constant expression"]
304
305constexpr int kArray[] = {1, 2, 3, 4, 5};
306
307void WontCompile() {
308 // Can't use a negative index in operator[].
309 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
310 constexpr auto& ref = iter[-1];
311}
312
313#elif defined(NCTEST_CHECKED_ITERATORS_OPERATOR_AT_END) // [r"constexpr variable 'ref' must be initialized by a constant expression"]
314
315constexpr int kArray[] = {1, 2, 3, 4, 5};
316
317void WontCompile() {
318 // Can't use a operator[] to deref the end.
319 constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
320 constexpr auto& ref = iter[5];
321}
322
323#endif
324
325} // namespace base