llvm-project
288 строк · 9.2 Кб
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// <multimap>
10
11// class multimap
12
13// pair<iterator, iterator> equal_range(const key_type& k);
14// pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
15
16#include <map>
17#include <cassert>
18
19#include "test_macros.h"
20#include "min_allocator.h"
21#include "private_constructor.h"
22#include "is_transparent.h"
23
24int main(int, char**)
25{
26typedef std::pair<const int, double> V;
27{
28typedef std::multimap<int, double> M;
29{
30typedef std::pair<M::iterator, M::iterator> R;
31V ar[] =
32{
33V(5, 1),
34V(5, 2),
35V(5, 3),
36V(7, 1),
37V(7, 2),
38V(7, 3),
39V(9, 1),
40V(9, 2),
41V(9, 3)
42};
43M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
44R r = m.equal_range(4);
45assert(r.first == m.begin());
46assert(r.second == m.begin());
47r = m.equal_range(5);
48assert(r.first == m.begin());
49assert(r.second == std::next(m.begin(), 3));
50r = m.equal_range(6);
51assert(r.first == std::next(m.begin(), 3));
52assert(r.second == std::next(m.begin(), 3));
53r = m.equal_range(7);
54assert(r.first == std::next(m.begin(), 3));
55assert(r.second == std::next(m.begin(), 6));
56r = m.equal_range(8);
57assert(r.first == std::next(m.begin(), 6));
58assert(r.second == std::next(m.begin(), 6));
59r = m.equal_range(9);
60assert(r.first == std::next(m.begin(), 6));
61assert(r.second == std::next(m.begin(), 9));
62r = m.equal_range(10);
63assert(r.first == m.end());
64assert(r.second == m.end());
65}
66{
67typedef std::pair<M::const_iterator, M::const_iterator> R;
68V ar[] =
69{
70V(5, 1),
71V(5, 2),
72V(5, 3),
73V(7, 1),
74V(7, 2),
75V(7, 3),
76V(9, 1),
77V(9, 2),
78V(9, 3)
79};
80const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
81R r = m.equal_range(4);
82assert(r.first == m.begin());
83assert(r.second == m.begin());
84r = m.equal_range(5);
85assert(r.first == m.begin());
86assert(r.second == std::next(m.begin(), 3));
87r = m.equal_range(6);
88assert(r.first == std::next(m.begin(), 3));
89assert(r.second == std::next(m.begin(), 3));
90r = m.equal_range(7);
91assert(r.first == std::next(m.begin(), 3));
92assert(r.second == std::next(m.begin(), 6));
93r = m.equal_range(8);
94assert(r.first == std::next(m.begin(), 6));
95assert(r.second == std::next(m.begin(), 6));
96r = m.equal_range(9);
97assert(r.first == std::next(m.begin(), 6));
98assert(r.second == std::next(m.begin(), 9));
99r = m.equal_range(10);
100assert(r.first == m.end());
101assert(r.second == m.end());
102}
103}
104#if TEST_STD_VER >= 11
105{
106typedef std::multimap<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
107{
108typedef std::pair<M::iterator, M::iterator> R;
109V ar[] =
110{
111V(5, 1),
112V(5, 2),
113V(5, 3),
114V(7, 1),
115V(7, 2),
116V(7, 3),
117V(9, 1),
118V(9, 2),
119V(9, 3)
120};
121M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
122R r = m.equal_range(4);
123assert(r.first == m.begin());
124assert(r.second == m.begin());
125r = m.equal_range(5);
126assert(r.first == m.begin());
127assert(r.second == std::next(m.begin(), 3));
128r = m.equal_range(6);
129assert(r.first == std::next(m.begin(), 3));
130assert(r.second == std::next(m.begin(), 3));
131r = m.equal_range(7);
132assert(r.first == std::next(m.begin(), 3));
133assert(r.second == std::next(m.begin(), 6));
134r = m.equal_range(8);
135assert(r.first == std::next(m.begin(), 6));
136assert(r.second == std::next(m.begin(), 6));
137r = m.equal_range(9);
138assert(r.first == std::next(m.begin(), 6));
139assert(r.second == std::next(m.begin(), 9));
140r = m.equal_range(10);
141assert(r.first == m.end());
142assert(r.second == m.end());
143}
144{
145typedef std::pair<M::const_iterator, M::const_iterator> R;
146V ar[] =
147{
148V(5, 1),
149V(5, 2),
150V(5, 3),
151V(7, 1),
152V(7, 2),
153V(7, 3),
154V(9, 1),
155V(9, 2),
156V(9, 3)
157};
158const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
159R r = m.equal_range(4);
160assert(r.first == m.begin());
161assert(r.second == m.begin());
162r = m.equal_range(5);
163assert(r.first == m.begin());
164assert(r.second == std::next(m.begin(), 3));
165r = m.equal_range(6);
166assert(r.first == std::next(m.begin(), 3));
167assert(r.second == std::next(m.begin(), 3));
168r = m.equal_range(7);
169assert(r.first == std::next(m.begin(), 3));
170assert(r.second == std::next(m.begin(), 6));
171r = m.equal_range(8);
172assert(r.first == std::next(m.begin(), 6));
173assert(r.second == std::next(m.begin(), 6));
174r = m.equal_range(9);
175assert(r.first == std::next(m.begin(), 6));
176assert(r.second == std::next(m.begin(), 9));
177r = m.equal_range(10);
178assert(r.first == m.end());
179assert(r.second == m.end());
180}
181}
182#endif
183#if TEST_STD_VER > 11
184{
185typedef std::multimap<int, double, std::less<>> M;
186
187typedef std::pair<M::iterator, M::iterator> R;
188V ar[] =
189{
190V(5, 1),
191V(5, 2),
192V(5, 3),
193V(7, 1),
194V(7, 2),
195V(7, 3),
196V(9, 1),
197V(9, 2),
198V(9, 3)
199};
200M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
201R r = m.equal_range(4);
202assert(r.first == m.begin());
203assert(r.second == m.begin());
204r = m.equal_range(5);
205assert(r.first == m.begin());
206assert(r.second == std::next(m.begin(), 3));
207r = m.equal_range(6);
208assert(r.first == std::next(m.begin(), 3));
209assert(r.second == std::next(m.begin(), 3));
210r = m.equal_range(7);
211assert(r.first == std::next(m.begin(), 3));
212assert(r.second == std::next(m.begin(), 6));
213r = m.equal_range(8);
214assert(r.first == std::next(m.begin(), 6));
215assert(r.second == std::next(m.begin(), 6));
216r = m.equal_range(9);
217assert(r.first == std::next(m.begin(), 6));
218assert(r.second == std::next(m.begin(), 9));
219r = m.equal_range(10);
220assert(r.first == m.end());
221assert(r.second == m.end());
222
223r = m.equal_range(C2Int(4));
224assert(r.first == m.begin());
225assert(r.second == m.begin());
226r = m.equal_range(C2Int(5));
227assert(r.first == m.begin());
228assert(r.second == std::next(m.begin(), 3));
229r = m.equal_range(C2Int(6));
230assert(r.first == std::next(m.begin(), 3));
231assert(r.second == std::next(m.begin(), 3));
232r = m.equal_range(C2Int(7));
233assert(r.first == std::next(m.begin(), 3));
234assert(r.second == std::next(m.begin(), 6));
235r = m.equal_range(C2Int(8));
236assert(r.first == std::next(m.begin(), 6));
237assert(r.second == std::next(m.begin(), 6));
238r = m.equal_range(C2Int(9));
239assert(r.first == std::next(m.begin(), 6));
240assert(r.second == std::next(m.begin(), 9));
241r = m.equal_range(C2Int(10));
242assert(r.first == m.end());
243assert(r.second == m.end());
244}
245
246{
247typedef PrivateConstructor PC;
248typedef std::multimap<PC, double, std::less<>> M;
249typedef std::pair<M::iterator, M::iterator> R;
250
251M m;
252m.insert ( std::make_pair<PC, double> ( PC::make(5), 1 ));
253m.insert ( std::make_pair<PC, double> ( PC::make(5), 2 ));
254m.insert ( std::make_pair<PC, double> ( PC::make(5), 3 ));
255m.insert ( std::make_pair<PC, double> ( PC::make(7), 1 ));
256m.insert ( std::make_pair<PC, double> ( PC::make(7), 2 ));
257m.insert ( std::make_pair<PC, double> ( PC::make(7), 3 ));
258m.insert ( std::make_pair<PC, double> ( PC::make(9), 1 ));
259m.insert ( std::make_pair<PC, double> ( PC::make(9), 2 ));
260m.insert ( std::make_pair<PC, double> ( PC::make(9), 3 ));
261
262// assert(m.size() == 9);
263R r = m.equal_range(4);
264assert(r.first == m.begin());
265assert(r.second == m.begin());
266r = m.equal_range(5);
267assert(r.first == m.begin());
268assert(r.second == std::next(m.begin(), 3));
269r = m.equal_range(6);
270assert(r.first == std::next(m.begin(), 3));
271assert(r.second == std::next(m.begin(), 3));
272r = m.equal_range(7);
273assert(r.first == std::next(m.begin(), 3));
274assert(r.second == std::next(m.begin(), 6));
275r = m.equal_range(8);
276assert(r.first == std::next(m.begin(), 6));
277assert(r.second == std::next(m.begin(), 6));
278r = m.equal_range(9);
279assert(r.first == std::next(m.begin(), 6));
280assert(r.second == std::next(m.begin(), 9));
281r = m.equal_range(10);
282assert(r.first == m.end());
283assert(r.second == m.end());
284}
285#endif
286
287return 0;
288}
289