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 | // <set> |
10 | |
11 | // class set |
12 | |
13 | // iterator begin(); |
14 | // const_iterator begin() const; |
15 | // iterator end(); |
16 | // const_iterator end() const; |
17 | // |
18 | // reverse_iterator rbegin(); |
19 | // const_reverse_iterator rbegin() const; |
20 | // reverse_iterator rend(); |
21 | // const_reverse_iterator rend() const; |
22 | // |
23 | // const_iterator cbegin() const; |
24 | // const_iterator cend() const; |
25 | // const_reverse_iterator crbegin() const; |
26 | // const_reverse_iterator crend() const; |
27 | |
28 | #include <set> |
29 | #include <cassert> |
30 | #include <cstddef> |
31 | |
32 | #include "test_macros.h" |
33 | #include "min_allocator.h" |
34 | |
35 | int main(int, char**) |
36 | { |
37 | { |
38 | typedef int V; |
39 | V ar[] = |
40 | { |
41 | 1, |
42 | 1, |
43 | 1, |
44 | 2, |
45 | 2, |
46 | 2, |
47 | 3, |
48 | 3, |
49 | 3, |
50 | 4, |
51 | 4, |
52 | 4, |
53 | 5, |
54 | 5, |
55 | 5, |
56 | 6, |
57 | 6, |
58 | 6, |
59 | 7, |
60 | 7, |
61 | 7, |
62 | 8, |
63 | 8, |
64 | 8 |
65 | }; |
66 | std::set<int> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
67 | assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size()); |
68 | assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size()); |
69 | std::set<int>::iterator i; |
70 | i = m.begin(); |
71 | std::set<int>::const_iterator k = i; |
72 | assert(i == k); |
73 | for (int j = 1; j <= static_cast<int>(m.size()); ++j, ++i) |
74 | assert(*i == j); |
75 | assert(i == m.end()); |
76 | for (int j = static_cast<int>(m.size()); j >= 1; --j) { |
77 | --i; |
78 | assert(*i == j); |
79 | } |
80 | assert(i == m.begin()); |
81 | } |
82 | { |
83 | typedef int V; |
84 | V ar[] = |
85 | { |
86 | 1, |
87 | 1, |
88 | 1, |
89 | 2, |
90 | 2, |
91 | 2, |
92 | 3, |
93 | 3, |
94 | 3, |
95 | 4, |
96 | 4, |
97 | 4, |
98 | 5, |
99 | 5, |
100 | 5, |
101 | 6, |
102 | 6, |
103 | 6, |
104 | 7, |
105 | 7, |
106 | 7, |
107 | 8, |
108 | 8, |
109 | 8 |
110 | }; |
111 | const std::set<int> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
112 | assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size()); |
113 | assert(static_cast<std::size_t>(std::distance(m.cbegin(), m.cend())) == m.size()); |
114 | assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size()); |
115 | assert(static_cast<std::size_t>(std::distance(m.crbegin(), m.crend())) == m.size()); |
116 | std::set<int>::const_iterator i; |
117 | i = m.begin(); |
118 | for (int j = 1; j <= static_cast<int>(m.size()); ++j, ++i) |
119 | assert(*i == j); |
120 | assert(i == m.end()); |
121 | for (int j = static_cast<int>(m.size()); j >= 1; --j) { |
122 | --i; |
123 | assert(*i == j); |
124 | } |
125 | assert(i == m.begin()); |
126 | } |
127 | #if TEST_STD_VER >= 11 |
128 | { |
129 | typedef int V; |
130 | V ar[] = |
131 | { |
132 | 1, |
133 | 1, |
134 | 1, |
135 | 2, |
136 | 2, |
137 | 2, |
138 | 3, |
139 | 3, |
140 | 3, |
141 | 4, |
142 | 4, |
143 | 4, |
144 | 5, |
145 | 5, |
146 | 5, |
147 | 6, |
148 | 6, |
149 | 6, |
150 | 7, |
151 | 7, |
152 | 7, |
153 | 8, |
154 | 8, |
155 | 8 |
156 | }; |
157 | std::set<int, std::less<int>, min_allocator<int>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
158 | assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size()); |
159 | assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size()); |
160 | std::set<int, std::less<int>, min_allocator<int>>::iterator i; |
161 | i = m.begin(); |
162 | std::set<int, std::less<int>, min_allocator<int>>::const_iterator k = i; |
163 | assert(i == k); |
164 | for (int j = 1; j <= static_cast<int>(m.size()); ++j, ++i) |
165 | assert(*i == j); |
166 | assert(i == m.end()); |
167 | for (int j = static_cast<int>(m.size()); j >= 1; --j) { |
168 | --i; |
169 | assert(*i == j); |
170 | } |
171 | assert(i == m.begin()); |
172 | } |
173 | { |
174 | typedef int V; |
175 | V ar[] = |
176 | { |
177 | 1, |
178 | 1, |
179 | 1, |
180 | 2, |
181 | 2, |
182 | 2, |
183 | 3, |
184 | 3, |
185 | 3, |
186 | 4, |
187 | 4, |
188 | 4, |
189 | 5, |
190 | 5, |
191 | 5, |
192 | 6, |
193 | 6, |
194 | 6, |
195 | 7, |
196 | 7, |
197 | 7, |
198 | 8, |
199 | 8, |
200 | 8 |
201 | }; |
202 | const std::set<int, std::less<int>, min_allocator<int>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
203 | assert(static_cast<std::size_t>(std::distance(m.begin(), m.end())) == m.size()); |
204 | assert(static_cast<std::size_t>(std::distance(m.cbegin(), m.cend())) == m.size()); |
205 | assert(static_cast<std::size_t>(std::distance(m.rbegin(), m.rend())) == m.size()); |
206 | assert(static_cast<std::size_t>(std::distance(m.crbegin(), m.crend())) == m.size()); |
207 | std::set<int, std::less<int>, min_allocator<int>>::const_iterator i; |
208 | i = m.begin(); |
209 | for (int j = 1; j <= static_cast<int>(m.size()); ++j, ++i) |
210 | assert(*i == j); |
211 | assert(i == m.end()); |
212 | for (int j = static_cast<int>(m.size()); j >= 1; --j) { |
213 | --i; |
214 | assert(*i == j); |
215 | } |
216 | assert(i == m.begin()); |
217 | } |
218 | #endif |
219 | #if TEST_STD_VER > 11 |
220 | { // N3644 testing |
221 | typedef std::set<int> C; |
222 | C::iterator ii1{}, ii2{}; |
223 | C::iterator ii4 = ii1; |
224 | C::const_iterator cii{}; |
225 | assert ( ii1 == ii2 ); |
226 | assert ( ii1 == ii4 ); |
227 | |
228 | assert (!(ii1 != ii2 )); |
229 | |
230 | assert ( (ii1 == cii )); |
231 | assert ( (cii == ii1 )); |
232 | assert (!(ii1 != cii )); |
233 | assert (!(cii != ii1 )); |
234 | } |
235 | #endif |
236 | |
237 | return 0; |
238 | } |
239 | |