[go: nahoru, domu]

blob: dec0d4e0cb97fcc1af909377b83efe8ec73773d9 [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
Jan Wilken Dörrie6c2032812019-11-13 03:32:1912constexpr int kArray1[] = {1, 2, 3, 4, 5};
13constexpr int kArray2[] = {1, 2, 3, 4, 5};
14
Peter Kasting48cd9b52023-11-07 05:52:4615constexpr 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();
Jan Wilken Dörrie6c2032812019-11-13 03:32:1940 constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
Peter Kasting48cd9b52023-11-07 05:52:4641 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örrie6c2032812019-11-13 03:32:1948}
49
Peter Kasting48cd9b52023-11-07 05:52:4650void 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örrie6c2032812019-11-13 03:32:1957}
58
Peter Kasting48cd9b52023-11-07 05:52:4659void 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örrie6c2032812019-11-13 03:32:1964}
65
Peter Kasting48cd9b52023-11-07 05:52:4666void 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örrie6c2032812019-11-13 03:32:1973}
74
Peter Kasting48cd9b52023-11-07 05:52:4675void 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örrie6c2032812019-11-13 03:32:1980}
81
Peter Kasting48cd9b52023-11-07 05:52:4682void DerefBegin() {
Jan Wilken Dörrie6c2032812019-11-13 03:32:1983 // Can't use a negative index in operator[].
Peter Kasting48cd9b52023-11-07 05:52:4684 constexpr auto& ref1 = GetBeginIter()[-1]; // expected-error {{constexpr variable 'ref1' must be initialized by a constant expression}}
Jan Wilken Dörrie6c2032812019-11-13 03:32:1985
Jan Wilken Dörrie6c2032812019-11-13 03:32:1986 // Can't use a operator[] to deref the end.
Peter Kasting48cd9b52023-11-07 05:52:4687 constexpr auto& ref2 = GetBeginIter()[5]; // expected-error {{constexpr variable 'ref2' must be initialized by a constant expression}}
Jan Wilken Dörrie6c2032812019-11-13 03:32:1988}
89
Peter Kasting48cd9b52023-11-07 05:52:4690void 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örrie6c2032812019-11-13 03:32:1996
97} // namespace base