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// <unordered_map>
10
11// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12// class Alloc = allocator<pair<const Key, T>>>
13// class unordered_map
14
15// local_iterator begin (size_type n);
16// local_iterator end (size_type n);
17// const_local_iterator begin (size_type n) const;
18// const_local_iterator end (size_type n) const;
19// const_local_iterator cbegin(size_type n) const;
20// const_local_iterator cend (size_type n) const;
21
22#include <unordered_map>
23#include <string>
24#include <cassert>
25
26#include "test_macros.h"
27#include "min_allocator.h"
28
29int main(int, char**)
30{
31 {
32 typedef std::unordered_map<int, std::string> C;
33 typedef std::pair<int, std::string> P;
34 typedef C::local_iterator I;
35 P a[] =
36 {
37 P(1, "one"),
38 P(2, "two"),
39 P(3, "three"),
40 P(4, "four"),
41 P(1, "four"),
42 P(2, "four"),
43 };
44 C c(a, a + sizeof(a)/sizeof(a[0]));
45 assert(c.bucket_count() >= 5);
46 C::size_type b = c.bucket(key: 0);
47 I i = c.begin(n: b);
48 I j = c.end(n: b);
49 assert(std::distance(i, j) == 0);
50
51 b = c.bucket(key: 1);
52 i = c.begin(n: b);
53 j = c.end(n: b);
54 assert(std::distance(i, j) == 1);
55 assert(i->first == 1);
56 assert(i->second == "one");
57
58 b = c.bucket(key: 2);
59 i = c.begin(n: b);
60 j = c.end(n: b);
61 assert(std::distance(i, j) == 1);
62 assert(i->first == 2);
63 assert(i->second == "two");
64
65 b = c.bucket(key: 3);
66 i = c.begin(n: b);
67 j = c.end(n: b);
68 assert(std::distance(i, j) == 1);
69 assert(i->first == 3);
70 assert(i->second == "three");
71
72 b = c.bucket(key: 4);
73 i = c.begin(n: b);
74 j = c.end(n: b);
75 assert(std::distance(i, j) == 1);
76 assert(i->first == 4);
77 assert(i->second == "four");
78 }
79 {
80 typedef std::unordered_map<int, std::string> C;
81 typedef std::pair<int, std::string> P;
82 typedef C::const_local_iterator I;
83 P a[] =
84 {
85 P(1, "one"),
86 P(2, "two"),
87 P(3, "three"),
88 P(4, "four"),
89 P(1, "four"),
90 P(2, "four"),
91 };
92 const C c(a, a + sizeof(a)/sizeof(a[0]));
93 assert(c.bucket_count() >= 5);
94 C::size_type b = c.bucket(key: 0);
95 I i = c.begin(n: b);
96 I j = c.end(n: b);
97 assert(std::distance(i, j) == 0);
98
99 b = c.bucket(key: 1);
100 i = c.begin(n: b);
101 j = c.end(n: b);
102 assert(std::distance(i, j) == 1);
103 assert(i->first == 1);
104 assert(i->second == "one");
105
106 b = c.bucket(key: 2);
107 i = c.begin(n: b);
108 j = c.end(n: b);
109 assert(std::distance(i, j) == 1);
110 assert(i->first == 2);
111 assert(i->second == "two");
112
113 b = c.bucket(key: 3);
114 i = c.begin(n: b);
115 j = c.end(n: b);
116 assert(std::distance(i, j) == 1);
117 assert(i->first == 3);
118 assert(i->second == "three");
119
120 b = c.bucket(key: 4);
121 i = c.begin(n: b);
122 j = c.end(n: b);
123 assert(std::distance(i, j) == 1);
124 assert(i->first == 4);
125 assert(i->second == "four");
126 }
127 {
128 typedef std::unordered_map<int, std::string> C;
129 typedef std::pair<int, std::string> P;
130 typedef C::const_local_iterator I;
131 P a[] =
132 {
133 P(1, "one"),
134 P(2, "two"),
135 P(3, "three"),
136 P(4, "four"),
137 P(1, "four"),
138 P(2, "four"),
139 };
140 C c(a, a + sizeof(a)/sizeof(a[0]));
141 assert(c.bucket_count() >= 5);
142 C::size_type b = c.bucket(key: 0);
143 I i = c.cbegin(n: b);
144 I j = c.cend(n: b);
145 assert(std::distance(i, j) == 0);
146
147 b = c.bucket(key: 1);
148 i = c.cbegin(n: b);
149 j = c.cend(n: b);
150 assert(std::distance(i, j) == 1);
151 assert(i->first == 1);
152 assert(i->second == "one");
153
154 b = c.bucket(key: 2);
155 i = c.cbegin(n: b);
156 j = c.cend(n: b);
157 assert(std::distance(i, j) == 1);
158 assert(i->first == 2);
159 assert(i->second == "two");
160
161 b = c.bucket(key: 3);
162 i = c.cbegin(n: b);
163 j = c.cend(n: b);
164 assert(std::distance(i, j) == 1);
165 assert(i->first == 3);
166 assert(i->second == "three");
167
168 b = c.bucket(key: 4);
169 i = c.cbegin(n: b);
170 j = c.cend(n: b);
171 assert(std::distance(i, j) == 1);
172 assert(i->first == 4);
173 assert(i->second == "four");
174 }
175 {
176 typedef std::unordered_map<int, std::string> C;
177 typedef std::pair<int, std::string> P;
178 typedef C::const_local_iterator I;
179 P a[] =
180 {
181 P(1, "one"),
182 P(2, "two"),
183 P(3, "three"),
184 P(4, "four"),
185 P(1, "four"),
186 P(2, "four"),
187 };
188 const C c(a, a + sizeof(a)/sizeof(a[0]));
189 assert(c.bucket_count() >= 5);
190 C::size_type b = c.bucket(key: 0);
191 I i = c.cbegin(n: b);
192 I j = c.cend(n: b);
193 assert(std::distance(i, j) == 0);
194
195 b = c.bucket(key: 1);
196 i = c.cbegin(n: b);
197 j = c.cend(n: b);
198 assert(std::distance(i, j) == 1);
199 assert(i->first == 1);
200 assert(i->second == "one");
201
202 b = c.bucket(key: 2);
203 i = c.cbegin(n: b);
204 j = c.cend(n: b);
205 assert(std::distance(i, j) == 1);
206 assert(i->first == 2);
207 assert(i->second == "two");
208
209 b = c.bucket(key: 3);
210 i = c.cbegin(n: b);
211 j = c.cend(n: b);
212 assert(std::distance(i, j) == 1);
213 assert(i->first == 3);
214 assert(i->second == "three");
215
216 b = c.bucket(key: 4);
217 i = c.cbegin(n: b);
218 j = c.cend(n: b);
219 assert(std::distance(i, j) == 1);
220 assert(i->first == 4);
221 assert(i->second == "four");
222 }
223#if TEST_STD_VER >= 11
224 {
225 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
226 min_allocator<std::pair<const int, std::string>>> C;
227 typedef std::pair<int, std::string> P;
228 typedef C::local_iterator I;
229 P a[] =
230 {
231 P(1, "one"),
232 P(2, "two"),
233 P(3, "three"),
234 P(4, "four"),
235 P(1, "four"),
236 P(2, "four"),
237 };
238 C c(a, a + sizeof(a)/sizeof(a[0]));
239 assert(c.bucket_count() >= 5);
240 C::size_type b = c.bucket(0);
241 I i = c.begin(b);
242 I j = c.end(b);
243 assert(std::distance(i, j) == 0);
244
245 b = c.bucket(1);
246 i = c.begin(b);
247 j = c.end(b);
248 assert(std::distance(i, j) == 1);
249 assert(i->first == 1);
250 assert(i->second == "one");
251
252 b = c.bucket(2);
253 i = c.begin(b);
254 j = c.end(b);
255 assert(std::distance(i, j) == 1);
256 assert(i->first == 2);
257 assert(i->second == "two");
258
259 b = c.bucket(3);
260 i = c.begin(b);
261 j = c.end(b);
262 assert(std::distance(i, j) == 1);
263 assert(i->first == 3);
264 assert(i->second == "three");
265
266 b = c.bucket(4);
267 i = c.begin(b);
268 j = c.end(b);
269 assert(std::distance(i, j) == 1);
270 assert(i->first == 4);
271 assert(i->second == "four");
272 }
273 {
274 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
275 min_allocator<std::pair<const int, std::string>>> C;
276 typedef std::pair<int, std::string> P;
277 typedef C::const_local_iterator I;
278 P a[] =
279 {
280 P(1, "one"),
281 P(2, "two"),
282 P(3, "three"),
283 P(4, "four"),
284 P(1, "four"),
285 P(2, "four"),
286 };
287 const C c(a, a + sizeof(a)/sizeof(a[0]));
288 assert(c.bucket_count() >= 5);
289 C::size_type b = c.bucket(0);
290 I i = c.begin(b);
291 I j = c.end(b);
292 assert(std::distance(i, j) == 0);
293
294 b = c.bucket(1);
295 i = c.begin(b);
296 j = c.end(b);
297 assert(std::distance(i, j) == 1);
298 assert(i->first == 1);
299 assert(i->second == "one");
300
301 b = c.bucket(2);
302 i = c.begin(b);
303 j = c.end(b);
304 assert(std::distance(i, j) == 1);
305 assert(i->first == 2);
306 assert(i->second == "two");
307
308 b = c.bucket(3);
309 i = c.begin(b);
310 j = c.end(b);
311 assert(std::distance(i, j) == 1);
312 assert(i->first == 3);
313 assert(i->second == "three");
314
315 b = c.bucket(4);
316 i = c.begin(b);
317 j = c.end(b);
318 assert(std::distance(i, j) == 1);
319 assert(i->first == 4);
320 assert(i->second == "four");
321 }
322 {
323 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
324 min_allocator<std::pair<const int, std::string>>> C;
325 typedef std::pair<int, std::string> P;
326 typedef C::const_local_iterator I;
327 P a[] =
328 {
329 P(1, "one"),
330 P(2, "two"),
331 P(3, "three"),
332 P(4, "four"),
333 P(1, "four"),
334 P(2, "four"),
335 };
336 C c(a, a + sizeof(a)/sizeof(a[0]));
337 assert(c.bucket_count() >= 5);
338 C::size_type b = c.bucket(0);
339 I i = c.cbegin(b);
340 I j = c.cend(b);
341 assert(std::distance(i, j) == 0);
342
343 b = c.bucket(1);
344 i = c.cbegin(b);
345 j = c.cend(b);
346 assert(std::distance(i, j) == 1);
347 assert(i->first == 1);
348 assert(i->second == "one");
349
350 b = c.bucket(2);
351 i = c.cbegin(b);
352 j = c.cend(b);
353 assert(std::distance(i, j) == 1);
354 assert(i->first == 2);
355 assert(i->second == "two");
356
357 b = c.bucket(3);
358 i = c.cbegin(b);
359 j = c.cend(b);
360 assert(std::distance(i, j) == 1);
361 assert(i->first == 3);
362 assert(i->second == "three");
363
364 b = c.bucket(4);
365 i = c.cbegin(b);
366 j = c.cend(b);
367 assert(std::distance(i, j) == 1);
368 assert(i->first == 4);
369 assert(i->second == "four");
370 }
371 {
372 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
373 min_allocator<std::pair<const int, std::string>>> C;
374 typedef std::pair<int, std::string> P;
375 typedef C::const_local_iterator I;
376 P a[] =
377 {
378 P(1, "one"),
379 P(2, "two"),
380 P(3, "three"),
381 P(4, "four"),
382 P(1, "four"),
383 P(2, "four"),
384 };
385 const C c(a, a + sizeof(a)/sizeof(a[0]));
386 assert(c.bucket_count() >= 5);
387 C::size_type b = c.bucket(0);
388 I i = c.cbegin(b);
389 I j = c.cend(b);
390 assert(std::distance(i, j) == 0);
391
392 b = c.bucket(1);
393 i = c.cbegin(b);
394 j = c.cend(b);
395 assert(std::distance(i, j) == 1);
396 assert(i->first == 1);
397 assert(i->second == "one");
398
399 b = c.bucket(2);
400 i = c.cbegin(b);
401 j = c.cend(b);
402 assert(std::distance(i, j) == 1);
403 assert(i->first == 2);
404 assert(i->second == "two");
405
406 b = c.bucket(3);
407 i = c.cbegin(b);
408 j = c.cend(b);
409 assert(std::distance(i, j) == 1);
410 assert(i->first == 3);
411 assert(i->second == "three");
412
413 b = c.bucket(4);
414 i = c.cbegin(b);
415 j = c.cend(b);
416 assert(std::distance(i, j) == 1);
417 assert(i->first == 4);
418 assert(i->second == "four");
419 }
420#endif
421
422 return 0;
423}
424

source code of libcxx/test/std/containers/unord/unord.map/local_iterators.pass.cpp