1// Copyright 2019 The Chromium Authors 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 10namespace base { 11 12constexpr int kArray1[] = {1, 2, 3, 4, 5}; 13constexpr int kArray2[] = {1, 2, 3, 4, 5}; 14 15constexpr auto GetBeginIter() { 16 return CheckedContiguousIterator<const int>(kArray1, kArray1 + 5); 17} 18 19constexpr auto GetEndIter() { 20 return CheckedContiguousIterator<const int>(kArray1, kArray1 + 5, 21 kArray1 + 5); 22} 23 24void 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 37void CompareItersFromDifferentContainers() { 38 // Can't compare iterators into different containers. 39 constexpr auto iter1 = GetBeginIter(); 40 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5); 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}} 48} 49 50void 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}} 57} 58 59void 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}} 64} 65 66void 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}} 73} 74 75void 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}} 80} 81 82void DerefBegin() { 83 // Can't use a negative index in operator[]. 84 constexpr auto& ref1 = GetBeginIter()[-1]; // expected-error {{constexpr variable 'ref1' must be initialized by a constant expression}} 85 86 // Can't use a operator[] to deref the end. 87 constexpr auto& ref2 = GetBeginIter()[5]; // expected-error {{constexpr variable 'ref2' must be initialized by a constant expression}} 88} 89 90void 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} 96 97} // namespace base 98