1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <map>
11 
12 // class map
13 
14 // pair<iterator,iterator>             equal_range(const key_type& k);
15 // pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
16 
17 #include <map>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "min_allocator.h"
22 #include "private_constructor.hpp"
23 #include "is_transparent.h"
24 
main()25 int main()
26 {
27     {
28     typedef std::pair<const int, double> V;
29     typedef std::map<int, double> M;
30     {
31         typedef std::pair<M::iterator, M::iterator> R;
32         V ar[] =
33         {
34             V(5, 5),
35             V(7, 6),
36             V(9, 7),
37             V(11, 8),
38             V(13, 9),
39             V(15, 10),
40             V(17, 11),
41             V(19, 12)
42         };
43         M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
44         R r = m.equal_range(5);
45         assert(r.first == next(m.begin(), 0));
46         assert(r.second == next(m.begin(), 1));
47         r = m.equal_range(7);
48         assert(r.first == next(m.begin(), 1));
49         assert(r.second == next(m.begin(), 2));
50         r = m.equal_range(9);
51         assert(r.first == next(m.begin(), 2));
52         assert(r.second == next(m.begin(), 3));
53         r = m.equal_range(11);
54         assert(r.first == next(m.begin(), 3));
55         assert(r.second == next(m.begin(), 4));
56         r = m.equal_range(13);
57         assert(r.first == next(m.begin(), 4));
58         assert(r.second == next(m.begin(), 5));
59         r = m.equal_range(15);
60         assert(r.first == next(m.begin(), 5));
61         assert(r.second == next(m.begin(), 6));
62         r = m.equal_range(17);
63         assert(r.first == next(m.begin(), 6));
64         assert(r.second == next(m.begin(), 7));
65         r = m.equal_range(19);
66         assert(r.first == next(m.begin(), 7));
67         assert(r.second == next(m.begin(), 8));
68         r = m.equal_range(4);
69         assert(r.first == next(m.begin(), 0));
70         assert(r.second == next(m.begin(), 0));
71         r = m.equal_range(6);
72         assert(r.first == next(m.begin(), 1));
73         assert(r.second == next(m.begin(), 1));
74         r = m.equal_range(8);
75         assert(r.first == next(m.begin(), 2));
76         assert(r.second == next(m.begin(), 2));
77         r = m.equal_range(10);
78         assert(r.first == next(m.begin(), 3));
79         assert(r.second == next(m.begin(), 3));
80         r = m.equal_range(12);
81         assert(r.first == next(m.begin(), 4));
82         assert(r.second == next(m.begin(), 4));
83         r = m.equal_range(14);
84         assert(r.first == next(m.begin(), 5));
85         assert(r.second == next(m.begin(), 5));
86         r = m.equal_range(16);
87         assert(r.first == next(m.begin(), 6));
88         assert(r.second == next(m.begin(), 6));
89         r = m.equal_range(18);
90         assert(r.first == next(m.begin(), 7));
91         assert(r.second == next(m.begin(), 7));
92         r = m.equal_range(20);
93         assert(r.first == next(m.begin(), 8));
94         assert(r.second == next(m.begin(), 8));
95     }
96     {
97         typedef std::pair<M::const_iterator, M::const_iterator> R;
98         V ar[] =
99         {
100             V(5, 5),
101             V(7, 6),
102             V(9, 7),
103             V(11, 8),
104             V(13, 9),
105             V(15, 10),
106             V(17, 11),
107             V(19, 12)
108         };
109         const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
110         R r = m.equal_range(5);
111         assert(r.first == next(m.begin(), 0));
112         assert(r.second == next(m.begin(), 1));
113         r = m.equal_range(7);
114         assert(r.first == next(m.begin(), 1));
115         assert(r.second == next(m.begin(), 2));
116         r = m.equal_range(9);
117         assert(r.first == next(m.begin(), 2));
118         assert(r.second == next(m.begin(), 3));
119         r = m.equal_range(11);
120         assert(r.first == next(m.begin(), 3));
121         assert(r.second == next(m.begin(), 4));
122         r = m.equal_range(13);
123         assert(r.first == next(m.begin(), 4));
124         assert(r.second == next(m.begin(), 5));
125         r = m.equal_range(15);
126         assert(r.first == next(m.begin(), 5));
127         assert(r.second == next(m.begin(), 6));
128         r = m.equal_range(17);
129         assert(r.first == next(m.begin(), 6));
130         assert(r.second == next(m.begin(), 7));
131         r = m.equal_range(19);
132         assert(r.first == next(m.begin(), 7));
133         assert(r.second == next(m.begin(), 8));
134         r = m.equal_range(4);
135         assert(r.first == next(m.begin(), 0));
136         assert(r.second == next(m.begin(), 0));
137         r = m.equal_range(6);
138         assert(r.first == next(m.begin(), 1));
139         assert(r.second == next(m.begin(), 1));
140         r = m.equal_range(8);
141         assert(r.first == next(m.begin(), 2));
142         assert(r.second == next(m.begin(), 2));
143         r = m.equal_range(10);
144         assert(r.first == next(m.begin(), 3));
145         assert(r.second == next(m.begin(), 3));
146         r = m.equal_range(12);
147         assert(r.first == next(m.begin(), 4));
148         assert(r.second == next(m.begin(), 4));
149         r = m.equal_range(14);
150         assert(r.first == next(m.begin(), 5));
151         assert(r.second == next(m.begin(), 5));
152         r = m.equal_range(16);
153         assert(r.first == next(m.begin(), 6));
154         assert(r.second == next(m.begin(), 6));
155         r = m.equal_range(18);
156         assert(r.first == next(m.begin(), 7));
157         assert(r.second == next(m.begin(), 7));
158         r = m.equal_range(20);
159         assert(r.first == next(m.begin(), 8));
160         assert(r.second == next(m.begin(), 8));
161     }
162     }
163 #if TEST_STD_VER >= 11
164     {
165     typedef std::pair<const int, double> V;
166     typedef std::map<int, double, std::less<int>, min_allocator<V>> M;
167     {
168         typedef std::pair<M::iterator, M::iterator> R;
169         V ar[] =
170         {
171             V(5, 5),
172             V(7, 6),
173             V(9, 7),
174             V(11, 8),
175             V(13, 9),
176             V(15, 10),
177             V(17, 11),
178             V(19, 12)
179         };
180         M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
181         R r = m.equal_range(5);
182         assert(r.first == next(m.begin(), 0));
183         assert(r.second == next(m.begin(), 1));
184         r = m.equal_range(7);
185         assert(r.first == next(m.begin(), 1));
186         assert(r.second == next(m.begin(), 2));
187         r = m.equal_range(9);
188         assert(r.first == next(m.begin(), 2));
189         assert(r.second == next(m.begin(), 3));
190         r = m.equal_range(11);
191         assert(r.first == next(m.begin(), 3));
192         assert(r.second == next(m.begin(), 4));
193         r = m.equal_range(13);
194         assert(r.first == next(m.begin(), 4));
195         assert(r.second == next(m.begin(), 5));
196         r = m.equal_range(15);
197         assert(r.first == next(m.begin(), 5));
198         assert(r.second == next(m.begin(), 6));
199         r = m.equal_range(17);
200         assert(r.first == next(m.begin(), 6));
201         assert(r.second == next(m.begin(), 7));
202         r = m.equal_range(19);
203         assert(r.first == next(m.begin(), 7));
204         assert(r.second == next(m.begin(), 8));
205         r = m.equal_range(4);
206         assert(r.first == next(m.begin(), 0));
207         assert(r.second == next(m.begin(), 0));
208         r = m.equal_range(6);
209         assert(r.first == next(m.begin(), 1));
210         assert(r.second == next(m.begin(), 1));
211         r = m.equal_range(8);
212         assert(r.first == next(m.begin(), 2));
213         assert(r.second == next(m.begin(), 2));
214         r = m.equal_range(10);
215         assert(r.first == next(m.begin(), 3));
216         assert(r.second == next(m.begin(), 3));
217         r = m.equal_range(12);
218         assert(r.first == next(m.begin(), 4));
219         assert(r.second == next(m.begin(), 4));
220         r = m.equal_range(14);
221         assert(r.first == next(m.begin(), 5));
222         assert(r.second == next(m.begin(), 5));
223         r = m.equal_range(16);
224         assert(r.first == next(m.begin(), 6));
225         assert(r.second == next(m.begin(), 6));
226         r = m.equal_range(18);
227         assert(r.first == next(m.begin(), 7));
228         assert(r.second == next(m.begin(), 7));
229         r = m.equal_range(20);
230         assert(r.first == next(m.begin(), 8));
231         assert(r.second == next(m.begin(), 8));
232     }
233     {
234         typedef std::pair<M::const_iterator, M::const_iterator> R;
235         V ar[] =
236         {
237             V(5, 5),
238             V(7, 6),
239             V(9, 7),
240             V(11, 8),
241             V(13, 9),
242             V(15, 10),
243             V(17, 11),
244             V(19, 12)
245         };
246         const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
247         R r = m.equal_range(5);
248         assert(r.first == next(m.begin(), 0));
249         assert(r.second == next(m.begin(), 1));
250         r = m.equal_range(7);
251         assert(r.first == next(m.begin(), 1));
252         assert(r.second == next(m.begin(), 2));
253         r = m.equal_range(9);
254         assert(r.first == next(m.begin(), 2));
255         assert(r.second == next(m.begin(), 3));
256         r = m.equal_range(11);
257         assert(r.first == next(m.begin(), 3));
258         assert(r.second == next(m.begin(), 4));
259         r = m.equal_range(13);
260         assert(r.first == next(m.begin(), 4));
261         assert(r.second == next(m.begin(), 5));
262         r = m.equal_range(15);
263         assert(r.first == next(m.begin(), 5));
264         assert(r.second == next(m.begin(), 6));
265         r = m.equal_range(17);
266         assert(r.first == next(m.begin(), 6));
267         assert(r.second == next(m.begin(), 7));
268         r = m.equal_range(19);
269         assert(r.first == next(m.begin(), 7));
270         assert(r.second == next(m.begin(), 8));
271         r = m.equal_range(4);
272         assert(r.first == next(m.begin(), 0));
273         assert(r.second == next(m.begin(), 0));
274         r = m.equal_range(6);
275         assert(r.first == next(m.begin(), 1));
276         assert(r.second == next(m.begin(), 1));
277         r = m.equal_range(8);
278         assert(r.first == next(m.begin(), 2));
279         assert(r.second == next(m.begin(), 2));
280         r = m.equal_range(10);
281         assert(r.first == next(m.begin(), 3));
282         assert(r.second == next(m.begin(), 3));
283         r = m.equal_range(12);
284         assert(r.first == next(m.begin(), 4));
285         assert(r.second == next(m.begin(), 4));
286         r = m.equal_range(14);
287         assert(r.first == next(m.begin(), 5));
288         assert(r.second == next(m.begin(), 5));
289         r = m.equal_range(16);
290         assert(r.first == next(m.begin(), 6));
291         assert(r.second == next(m.begin(), 6));
292         r = m.equal_range(18);
293         assert(r.first == next(m.begin(), 7));
294         assert(r.second == next(m.begin(), 7));
295         r = m.equal_range(20);
296         assert(r.first == next(m.begin(), 8));
297         assert(r.second == next(m.begin(), 8));
298     }
299     }
300 #endif
301 #if TEST_STD_VER > 11
302     {
303     typedef std::pair<const int, double> V;
304     typedef std::map<int, double, std::less<>> M;
305     typedef std::pair<M::iterator, M::iterator> R;
306 
307     V ar[] =
308     {
309         V(5, 5),
310         V(7, 6),
311         V(9, 7),
312         V(11, 8),
313         V(13, 9),
314         V(15, 10),
315         V(17, 11),
316         V(19, 12)
317     };
318     M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
319     R r = m.equal_range(5);
320     assert(r.first == next(m.begin(), 0));
321     assert(r.second == next(m.begin(), 1));
322     r = m.equal_range(7);
323     assert(r.first == next(m.begin(), 1));
324     assert(r.second == next(m.begin(), 2));
325     r = m.equal_range(9);
326     assert(r.first == next(m.begin(), 2));
327     assert(r.second == next(m.begin(), 3));
328     r = m.equal_range(11);
329     assert(r.first == next(m.begin(), 3));
330     assert(r.second == next(m.begin(), 4));
331     r = m.equal_range(13);
332     assert(r.first == next(m.begin(), 4));
333     assert(r.second == next(m.begin(), 5));
334     r = m.equal_range(15);
335     assert(r.first == next(m.begin(), 5));
336     assert(r.second == next(m.begin(), 6));
337     r = m.equal_range(17);
338     assert(r.first == next(m.begin(), 6));
339     assert(r.second == next(m.begin(), 7));
340     r = m.equal_range(19);
341     assert(r.first == next(m.begin(), 7));
342     assert(r.second == next(m.begin(), 8));
343     r = m.equal_range(4);
344     assert(r.first == next(m.begin(), 0));
345     assert(r.second == next(m.begin(), 0));
346     r = m.equal_range(6);
347     assert(r.first == next(m.begin(), 1));
348     assert(r.second == next(m.begin(), 1));
349     r = m.equal_range(8);
350     assert(r.first == next(m.begin(), 2));
351     assert(r.second == next(m.begin(), 2));
352     r = m.equal_range(10);
353     assert(r.first == next(m.begin(), 3));
354     assert(r.second == next(m.begin(), 3));
355     r = m.equal_range(12);
356     assert(r.first == next(m.begin(), 4));
357     assert(r.second == next(m.begin(), 4));
358     r = m.equal_range(14);
359     assert(r.first == next(m.begin(), 5));
360     assert(r.second == next(m.begin(), 5));
361     r = m.equal_range(16);
362     assert(r.first == next(m.begin(), 6));
363     assert(r.second == next(m.begin(), 6));
364     r = m.equal_range(18);
365     assert(r.first == next(m.begin(), 7));
366     assert(r.second == next(m.begin(), 7));
367     r = m.equal_range(20);
368     assert(r.first == next(m.begin(), 8));
369     assert(r.second == next(m.begin(), 8));
370 
371     r = m.equal_range(C2Int(5));
372     assert(r.first == next(m.begin(), 0));
373     assert(r.second == next(m.begin(), 1));
374     r = m.equal_range(C2Int(7));
375     assert(r.first == next(m.begin(), 1));
376     assert(r.second == next(m.begin(), 2));
377     r = m.equal_range(C2Int(9));
378     assert(r.first == next(m.begin(), 2));
379     assert(r.second == next(m.begin(), 3));
380     r = m.equal_range(C2Int(11));
381     assert(r.first == next(m.begin(), 3));
382     assert(r.second == next(m.begin(), 4));
383     r = m.equal_range(C2Int(13));
384     assert(r.first == next(m.begin(), 4));
385     assert(r.second == next(m.begin(), 5));
386     r = m.equal_range(C2Int(15));
387     assert(r.first == next(m.begin(), 5));
388     assert(r.second == next(m.begin(), 6));
389     r = m.equal_range(C2Int(17));
390     assert(r.first == next(m.begin(), 6));
391     assert(r.second == next(m.begin(), 7));
392     r = m.equal_range(C2Int(19));
393     assert(r.first == next(m.begin(), 7));
394     assert(r.second == next(m.begin(), 8));
395     r = m.equal_range(C2Int(4));
396     assert(r.first == next(m.begin(), 0));
397     assert(r.second == next(m.begin(), 0));
398     r = m.equal_range(C2Int(6));
399     assert(r.first == next(m.begin(), 1));
400     assert(r.second == next(m.begin(), 1));
401     r = m.equal_range(C2Int(8));
402     assert(r.first == next(m.begin(), 2));
403     assert(r.second == next(m.begin(), 2));
404     r = m.equal_range(C2Int(10));
405     assert(r.first == next(m.begin(), 3));
406     assert(r.second == next(m.begin(), 3));
407     r = m.equal_range(C2Int(12));
408     assert(r.first == next(m.begin(), 4));
409     assert(r.second == next(m.begin(), 4));
410     r = m.equal_range(C2Int(14));
411     assert(r.first == next(m.begin(), 5));
412     assert(r.second == next(m.begin(), 5));
413     r = m.equal_range(C2Int(16));
414     assert(r.first == next(m.begin(), 6));
415     assert(r.second == next(m.begin(), 6));
416     r = m.equal_range(C2Int(18));
417     assert(r.first == next(m.begin(), 7));
418     assert(r.second == next(m.begin(), 7));
419     r = m.equal_range(C2Int(20));
420     assert(r.first == next(m.begin(), 8));
421     assert(r.second == next(m.begin(), 8));
422     }
423     {
424     typedef PrivateConstructor PC;
425     typedef std::map<PC, double, std::less<>> M;
426     typedef std::pair<M::iterator, M::iterator> R;
427 
428     M m;
429     m [ PC::make(5)  ] = 5;
430     m [ PC::make(7)  ] = 6;
431     m [ PC::make(9)  ] = 7;
432     m [ PC::make(11) ] = 8;
433     m [ PC::make(13) ] = 9;
434     m [ PC::make(15) ] = 10;
435     m [ PC::make(17) ] = 11;
436     m [ PC::make(19) ] = 12;
437 
438     R r = m.equal_range(5);
439     assert(r.first == next(m.begin(), 0));
440     assert(r.second == next(m.begin(), 1));
441     r = m.equal_range(7);
442     assert(r.first == next(m.begin(), 1));
443     assert(r.second == next(m.begin(), 2));
444     r = m.equal_range(9);
445     assert(r.first == next(m.begin(), 2));
446     assert(r.second == next(m.begin(), 3));
447     r = m.equal_range(11);
448     assert(r.first == next(m.begin(), 3));
449     assert(r.second == next(m.begin(), 4));
450     r = m.equal_range(13);
451     assert(r.first == next(m.begin(), 4));
452     assert(r.second == next(m.begin(), 5));
453     r = m.equal_range(15);
454     assert(r.first == next(m.begin(), 5));
455     assert(r.second == next(m.begin(), 6));
456     r = m.equal_range(17);
457     assert(r.first == next(m.begin(), 6));
458     assert(r.second == next(m.begin(), 7));
459     r = m.equal_range(19);
460     assert(r.first == next(m.begin(), 7));
461     assert(r.second == next(m.begin(), 8));
462     r = m.equal_range(4);
463     assert(r.first == next(m.begin(), 0));
464     assert(r.second == next(m.begin(), 0));
465     r = m.equal_range(6);
466     assert(r.first == next(m.begin(), 1));
467     assert(r.second == next(m.begin(), 1));
468     r = m.equal_range(8);
469     assert(r.first == next(m.begin(), 2));
470     assert(r.second == next(m.begin(), 2));
471     r = m.equal_range(10);
472     assert(r.first == next(m.begin(), 3));
473     assert(r.second == next(m.begin(), 3));
474     r = m.equal_range(12);
475     assert(r.first == next(m.begin(), 4));
476     assert(r.second == next(m.begin(), 4));
477     r = m.equal_range(14);
478     assert(r.first == next(m.begin(), 5));
479     assert(r.second == next(m.begin(), 5));
480     r = m.equal_range(16);
481     assert(r.first == next(m.begin(), 6));
482     assert(r.second == next(m.begin(), 6));
483     r = m.equal_range(18);
484     assert(r.first == next(m.begin(), 7));
485     assert(r.second == next(m.begin(), 7));
486     r = m.equal_range(20);
487     assert(r.first == next(m.begin(), 8));
488     assert(r.second == next(m.begin(), 8));
489     }
490 #endif
491 }
492