1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <vector> |
10 | |
11 | // iterator begin(); |
12 | // iterator end(); |
13 | // const_iterator begin() const; |
14 | // const_iterator end() const; |
15 | // const_iterator cbegin() const; |
16 | // const_iterator cend() const; |
17 | |
18 | #include <vector> |
19 | #include <cassert> |
20 | #include <iterator> |
21 | |
22 | #include "test_macros.h" |
23 | #include "min_allocator.h" |
24 | |
25 | struct A |
26 | { |
27 | int first; |
28 | int second; |
29 | }; |
30 | |
31 | TEST_CONSTEXPR_CXX20 bool tests() |
32 | { |
33 | { |
34 | typedef int T; |
35 | typedef std::vector<T> C; |
36 | C c; |
37 | C::iterator i = c.begin(); |
38 | C::iterator j = c.end(); |
39 | assert(std::distance(i, j) == 0); |
40 | assert(i == j); |
41 | } |
42 | { |
43 | typedef int T; |
44 | typedef std::vector<T> C; |
45 | const C c; |
46 | C::const_iterator i = c.begin(); |
47 | C::const_iterator j = c.end(); |
48 | assert(std::distance(i, j) == 0); |
49 | assert(i == j); |
50 | } |
51 | { |
52 | typedef int T; |
53 | typedef std::vector<T> C; |
54 | C c; |
55 | C::const_iterator i = c.cbegin(); |
56 | C::const_iterator j = c.cend(); |
57 | assert(std::distance(i, j) == 0); |
58 | assert(i == j); |
59 | assert(i == c.end()); |
60 | } |
61 | { |
62 | typedef int T; |
63 | typedef std::vector<T> C; |
64 | const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
65 | C c(std::begin(arr: t), std::end(arr: t)); |
66 | C::iterator i = c.begin(); |
67 | assert(*i == 0); |
68 | ++i; |
69 | assert(*i == 1); |
70 | *i = 10; |
71 | assert(*i == 10); |
72 | assert(std::distance(c.begin(), c.end()) == 10); |
73 | } |
74 | { |
75 | typedef int T; |
76 | typedef std::vector<T> C; |
77 | C::iterator i; |
78 | C::const_iterator j; |
79 | (void) i; |
80 | (void) j; |
81 | } |
82 | #if TEST_STD_VER >= 11 |
83 | { |
84 | typedef int T; |
85 | typedef std::vector<T, min_allocator<T>> C; |
86 | C c; |
87 | C::iterator i = c.begin(); |
88 | C::iterator j = c.end(); |
89 | assert(std::distance(i, j) == 0); |
90 | assert(i == j); |
91 | } |
92 | { |
93 | typedef int T; |
94 | typedef std::vector<T, min_allocator<T>> C; |
95 | const C c; |
96 | C::const_iterator i = c.begin(); |
97 | C::const_iterator j = c.end(); |
98 | assert(std::distance(i, j) == 0); |
99 | assert(i == j); |
100 | } |
101 | { |
102 | typedef int T; |
103 | typedef std::vector<T, min_allocator<T>> C; |
104 | C c; |
105 | C::const_iterator i = c.cbegin(); |
106 | C::const_iterator j = c.cend(); |
107 | assert(std::distance(i, j) == 0); |
108 | assert(i == j); |
109 | assert(i == c.end()); |
110 | } |
111 | { |
112 | typedef int T; |
113 | typedef std::vector<T, min_allocator<T>> C; |
114 | const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
115 | C c(std::begin(t), std::end(t)); |
116 | C::iterator i = c.begin(); |
117 | assert(*i == 0); |
118 | ++i; |
119 | assert(*i == 1); |
120 | *i = 10; |
121 | assert(*i == 10); |
122 | assert(std::distance(c.begin(), c.end()) == 10); |
123 | } |
124 | { |
125 | typedef int T; |
126 | typedef std::vector<T, min_allocator<T>> C; |
127 | C::iterator i; |
128 | C::const_iterator j; |
129 | (void) i; |
130 | (void) j; |
131 | } |
132 | { |
133 | typedef A T; |
134 | typedef std::vector<T, min_allocator<T>> C; |
135 | C c = {A{1, 2}}; |
136 | C::iterator i = c.begin(); |
137 | i->first = 3; |
138 | C::const_iterator j = i; |
139 | assert(j->first == 3); |
140 | } |
141 | #endif |
142 | #if TEST_STD_VER > 11 |
143 | { // N3644 testing |
144 | typedef std::vector<int> C; |
145 | C::iterator ii1{}, ii2{}; |
146 | C::iterator ii4 = ii1; |
147 | C::const_iterator cii{}; |
148 | assert ( ii1 == ii2 ); |
149 | assert ( ii1 == ii4 ); |
150 | |
151 | assert (!(ii1 != ii2 )); |
152 | |
153 | assert ( (ii1 == cii )); |
154 | assert ( (cii == ii1 )); |
155 | assert (!(ii1 != cii )); |
156 | assert (!(cii != ii1 )); |
157 | assert (!(ii1 < cii )); |
158 | assert (!(cii < ii1 )); |
159 | assert ( (ii1 <= cii )); |
160 | assert ( (cii <= ii1 )); |
161 | assert (!(ii1 > cii )); |
162 | assert (!(cii > ii1 )); |
163 | assert ( (ii1 >= cii )); |
164 | assert ( (cii >= ii1 )); |
165 | assert (cii - ii1 == 0); |
166 | assert (ii1 - cii == 0); |
167 | } |
168 | #endif |
169 | |
170 | return true; |
171 | } |
172 | |
173 | int main(int, char**) |
174 | { |
175 | tests(); |
176 | #if TEST_STD_VER > 17 |
177 | static_assert(tests()); |
178 | #endif |
179 | return 0; |
180 | } |
181 | |