Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2019 The Chromium Authors |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 2 | // 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 | |
| 10 | namespace base { |
| 11 | |
| 12 | #if defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_START_END) // [r"constexpr variable 'iter' must be initialized by a constant expression"] |
| 13 | |
| 14 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 15 | |
| 16 | void 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 | |
| 23 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 24 | |
| 25 | void 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 | |
| 32 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 33 | |
| 34 | void 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 | |
| 41 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 42 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 43 | |
| 44 | void 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 | |
| 53 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 54 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 55 | |
| 56 | void 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 | |
| 65 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 66 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 67 | |
| 68 | void 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 | |
| 77 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 78 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 79 | |
| 80 | void 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 | |
| 89 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 90 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 91 | |
| 92 | void 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 | |
| 101 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 102 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 103 | |
| 104 | void 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 | |
| 113 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 114 | |
| 115 | constexpr 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 | |
| 122 | void 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 | |
| 128 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 129 | |
| 130 | constexpr 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 | |
| 137 | void 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 | |
| 143 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 144 | |
| 145 | constexpr 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 | |
| 152 | void 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 | |
| 158 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 159 | |
| 160 | constexpr 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 | |
| 167 | void 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 | |
| 173 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 174 | |
| 175 | constexpr 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 | |
| 182 | void 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 | |
| 188 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 189 | |
| 190 | constexpr 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 | |
| 197 | void 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 | |
| 203 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 204 | |
| 205 | void 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 | |
| 213 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 214 | |
| 215 | void 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 | |
| 223 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 224 | |
| 225 | constexpr 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 | |
| 232 | void 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 | |
| 238 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 239 | |
| 240 | constexpr 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 | |
| 247 | void 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 | |
| 253 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 254 | |
| 255 | void 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 | |
| 263 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 264 | |
| 265 | void 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 | |
| 273 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 274 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 275 | |
| 276 | void 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 | |
| 285 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 286 | |
| 287 | void 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 | |
| 295 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 296 | |
| 297 | void 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 | |
| 305 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 306 | |
| 307 | void 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 | |
| 315 | constexpr int kArray[] = {1, 2, 3, 4, 5}; |
| 316 | |
| 317 | void 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 |