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 | |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 12 | constexpr int kArray1[] = {1, 2, 3, 4, 5}; |
| 13 | constexpr int kArray2[] = {1, 2, 3, 4, 5}; |
| 14 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 15 | constexpr auto GetBeginIter() { |
| 16 | return CheckedContiguousIterator<const int>(kArray1, kArray1 + 5); |
| 17 | } |
| 18 | |
| 19 | constexpr auto GetEndIter() { |
| 20 | return CheckedContiguousIterator<const int>(kArray1, kArray1 + 5, |
| 21 | kArray1 + 5); |
| 22 | } |
| 23 | |
| 24 | void ConstructorOrdering() { |
| 25 | // Start can't be larger than end. |
| 26 | constexpr CheckedContiguousIterator<const int> iter1(kArray1 + 1, kArray1); // expected-error {{constexpr variable 'iter1' must be initialized by a constant expression}} |
| 27 | |
| 28 | // Current can't be larger than start. |
| 29 | constexpr CheckedContiguousIterator<const int> iter2(kArray1 + 1, kArray1, // expected-error {{constexpr variable 'iter2' must be initialized by a constant expression}} |
| 30 | kArray1 + 5); |
| 31 | |
| 32 | // Current can't be larger than end. |
| 33 | constexpr CheckedContiguousIterator<const int> iter3(kArray1, kArray1 + 2, // expected-error {{constexpr variable 'iter3' must be initialized by a constant expression}} |
| 34 | kArray1 + 1); |
| 35 | } |
| 36 | |
| 37 | void CompareItersFromDifferentContainers() { |
| 38 | // Can't compare iterators into different containers. |
| 39 | constexpr auto iter1 = GetBeginIter(); |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 40 | constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5); |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 41 | constexpr bool equal = iter1 == iter2; // expected-error {{constexpr variable 'equal' must be initialized by a constant expression}} |
| 42 | constexpr bool not_equal = iter1 != iter2; // expected-error {{constexpr variable 'not_equal' must be initialized by a constant expression}} |
| 43 | constexpr bool less_than = iter1 < iter2; // expected-error {{constexpr variable 'less_than' must be initialized by a constant expression}} |
| 44 | constexpr bool less_equal = iter1 <= iter2; // expected-error {{constexpr variable 'less_equal' must be initialized by a constant expression}} |
| 45 | constexpr bool greater_than = iter1 > iter2; // expected-error {{constexpr variable 'greater_than' must be initialized by a constant expression}} |
| 46 | constexpr bool greater_equal = iter1 >= iter2; // expected-error {{constexpr variable 'greater_equal' must be initialized by a constant expression}} |
| 47 | constexpr auto difference = iter1 - iter2; // expected-error {{constexpr variable 'difference' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 48 | } |
| 49 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 50 | void DecrementBegin() { |
| 51 | constexpr auto past_begin1 = --GetBeginIter(); // expected-error {{constexpr variable 'past_begin1' must be initialized by a constant expression}} |
| 52 | constexpr auto past_begin2 = GetBeginIter()--; // expected-error {{constexpr variable 'past_begin2' must be initialized by a constant expression}} |
| 53 | constexpr auto past_begin3 = (GetBeginIter() += -1); // expected-error {{constexpr variable 'past_begin3' must be initialized by a constant expression}} |
| 54 | constexpr auto past_begin4 = GetBeginIter() + -1; // expected-error {{constexpr variable 'past_begin4' must be initialized by a constant expression}} |
| 55 | constexpr auto past_begin5 = (GetBeginIter() -= 1); // expected-error {{constexpr variable 'past_begin5' must be initialized by a constant expression}} |
| 56 | constexpr auto past_begin6 = GetBeginIter() - 1; // expected-error {{constexpr variable 'past_begin6' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 57 | } |
| 58 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 59 | void IncrementBeginPastEnd() { |
| 60 | constexpr auto past_end1 = (GetBeginIter() += 6); // expected-error {{constexpr variable 'past_end1' must be initialized by a constant expression}} |
| 61 | constexpr auto past_end2 = GetBeginIter() + 6; // expected-error {{constexpr variable 'past_end2' must be initialized by a constant expression}} |
| 62 | constexpr auto past_end3 = (GetBeginIter() -= -6); // expected-error {{constexpr variable 'past_end3' must be initialized by a constant expression}} |
| 63 | constexpr auto past_end4 = GetBeginIter() - -6; // expected-error {{constexpr variable 'past_end4' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 64 | } |
| 65 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 66 | void IncrementEnd() { |
| 67 | constexpr auto past_end1 = ++GetEndIter(); // expected-error {{constexpr variable 'past_end1' must be initialized by a constant expression}} |
| 68 | constexpr auto past_end2 = GetEndIter()++; // expected-error {{constexpr variable 'past_end2' must be initialized by a constant expression}} |
| 69 | constexpr auto past_end3 = (GetEndIter() += 1); // expected-error {{constexpr variable 'past_end3' must be initialized by a constant expression}} |
| 70 | constexpr auto past_end4 = GetEndIter() + 1; // expected-error {{constexpr variable 'past_end4' must be initialized by a constant expression}} |
| 71 | constexpr auto past_end5 = (GetEndIter() -= -1); // expected-error {{constexpr variable 'past_end5' must be initialized by a constant expression}} |
| 72 | constexpr auto past_end6 = GetEndIter() - -1; // expected-error {{constexpr variable 'past_end6' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 73 | } |
| 74 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 75 | void DecrementEndPastBegin() { |
| 76 | constexpr auto past_begin1 = (GetEndIter() += -6); // expected-error {{constexpr variable 'past_begin1' must be initialized by a constant expression}} |
| 77 | constexpr auto past_begin2 = GetEndIter() + -6; // expected-error {{constexpr variable 'past_begin2' must be initialized by a constant expression}} |
| 78 | constexpr auto past_begin3 = (GetEndIter() -= 6); // expected-error {{constexpr variable 'past_begin3' must be initialized by a constant expression}} |
| 79 | constexpr auto past_begin4 = GetEndIter() - 6; // expected-error {{constexpr variable 'past_begin4' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 80 | } |
| 81 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 82 | void DerefBegin() { |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 83 | // Can't use a negative index in operator[]. |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 84 | constexpr auto& ref1 = GetBeginIter()[-1]; // expected-error {{constexpr variable 'ref1' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 85 | |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 86 | // Can't use a operator[] to deref the end. |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 87 | constexpr auto& ref2 = GetBeginIter()[5]; // expected-error {{constexpr variable 'ref2' must be initialized by a constant expression}} |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 88 | } |
| 89 | |
Peter Kasting | 48cd9b5 | 2023-11-07 05:52:46 | [diff] [blame] | 90 | void DerefEnd() { |
| 91 | // Can't dereference the end iterator. |
| 92 | constexpr auto& ref1 = *GetEndIter(); // expected-error {{constexpr variable 'ref1' must be initialized by a constant expression}} |
| 93 | constexpr auto* ptr = GetEndIter().operator->(); // expected-error {{constexpr variable 'ptr' must be initialized by a constant expression}} |
| 94 | constexpr auto& ref2 = GetEndIter()[0]; // expected-error {{constexpr variable 'ref2' must be initialized by a constant expression}} |
| 95 | } |
Jan Wilken Dörrie | 6c203281 | 2019-11-13 03:32:19 | [diff] [blame] | 96 | |
| 97 | } // namespace base |