llvm-project
372 строки · 13.3 Кб
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// 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 <set>
17#include <cassert>
18
19#include "test_macros.h"
20#include "min_allocator.h"
21#include "private_constructor.h"
22
23int main(int, char**)
24{
25{
26typedef int V;
27typedef std::set<int> M;
28{
29typedef std::pair<M::iterator, M::iterator> R;
30V ar[] =
31{
325,
337,
349,
3511,
3613,
3715,
3817,
3919
40};
41M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
42R r = m.equal_range(5);
43assert(r.first == std::next(m.begin(), 0));
44assert(r.second == std::next(m.begin(), 1));
45r = m.equal_range(7);
46assert(r.first == std::next(m.begin(), 1));
47assert(r.second == std::next(m.begin(), 2));
48r = m.equal_range(9);
49assert(r.first == std::next(m.begin(), 2));
50assert(r.second == std::next(m.begin(), 3));
51r = m.equal_range(11);
52assert(r.first == std::next(m.begin(), 3));
53assert(r.second == std::next(m.begin(), 4));
54r = m.equal_range(13);
55assert(r.first == std::next(m.begin(), 4));
56assert(r.second == std::next(m.begin(), 5));
57r = m.equal_range(15);
58assert(r.first == std::next(m.begin(), 5));
59assert(r.second == std::next(m.begin(), 6));
60r = m.equal_range(17);
61assert(r.first == std::next(m.begin(), 6));
62assert(r.second == std::next(m.begin(), 7));
63r = m.equal_range(19);
64assert(r.first == std::next(m.begin(), 7));
65assert(r.second == std::next(m.begin(), 8));
66r = m.equal_range(4);
67assert(r.first == std::next(m.begin(), 0));
68assert(r.second == std::next(m.begin(), 0));
69r = m.equal_range(6);
70assert(r.first == std::next(m.begin(), 1));
71assert(r.second == std::next(m.begin(), 1));
72r = m.equal_range(8);
73assert(r.first == std::next(m.begin(), 2));
74assert(r.second == std::next(m.begin(), 2));
75r = m.equal_range(10);
76assert(r.first == std::next(m.begin(), 3));
77assert(r.second == std::next(m.begin(), 3));
78r = m.equal_range(12);
79assert(r.first == std::next(m.begin(), 4));
80assert(r.second == std::next(m.begin(), 4));
81r = m.equal_range(14);
82assert(r.first == std::next(m.begin(), 5));
83assert(r.second == std::next(m.begin(), 5));
84r = m.equal_range(16);
85assert(r.first == std::next(m.begin(), 6));
86assert(r.second == std::next(m.begin(), 6));
87r = m.equal_range(18);
88assert(r.first == std::next(m.begin(), 7));
89assert(r.second == std::next(m.begin(), 7));
90r = m.equal_range(20);
91assert(r.first == std::next(m.begin(), 8));
92assert(r.second == std::next(m.begin(), 8));
93}
94{
95typedef std::pair<M::const_iterator, M::const_iterator> R;
96V ar[] =
97{
985,
997,
1009,
10111,
10213,
10315,
10417,
10519
106};
107const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
108R r = m.equal_range(5);
109assert(r.first == std::next(m.begin(), 0));
110assert(r.second == std::next(m.begin(), 1));
111r = m.equal_range(7);
112assert(r.first == std::next(m.begin(), 1));
113assert(r.second == std::next(m.begin(), 2));
114r = m.equal_range(9);
115assert(r.first == std::next(m.begin(), 2));
116assert(r.second == std::next(m.begin(), 3));
117r = m.equal_range(11);
118assert(r.first == std::next(m.begin(), 3));
119assert(r.second == std::next(m.begin(), 4));
120r = m.equal_range(13);
121assert(r.first == std::next(m.begin(), 4));
122assert(r.second == std::next(m.begin(), 5));
123r = m.equal_range(15);
124assert(r.first == std::next(m.begin(), 5));
125assert(r.second == std::next(m.begin(), 6));
126r = m.equal_range(17);
127assert(r.first == std::next(m.begin(), 6));
128assert(r.second == std::next(m.begin(), 7));
129r = m.equal_range(19);
130assert(r.first == std::next(m.begin(), 7));
131assert(r.second == std::next(m.begin(), 8));
132r = m.equal_range(4);
133assert(r.first == std::next(m.begin(), 0));
134assert(r.second == std::next(m.begin(), 0));
135r = m.equal_range(6);
136assert(r.first == std::next(m.begin(), 1));
137assert(r.second == std::next(m.begin(), 1));
138r = m.equal_range(8);
139assert(r.first == std::next(m.begin(), 2));
140assert(r.second == std::next(m.begin(), 2));
141r = m.equal_range(10);
142assert(r.first == std::next(m.begin(), 3));
143assert(r.second == std::next(m.begin(), 3));
144r = m.equal_range(12);
145assert(r.first == std::next(m.begin(), 4));
146assert(r.second == std::next(m.begin(), 4));
147r = m.equal_range(14);
148assert(r.first == std::next(m.begin(), 5));
149assert(r.second == std::next(m.begin(), 5));
150r = m.equal_range(16);
151assert(r.first == std::next(m.begin(), 6));
152assert(r.second == std::next(m.begin(), 6));
153r = m.equal_range(18);
154assert(r.first == std::next(m.begin(), 7));
155assert(r.second == std::next(m.begin(), 7));
156r = m.equal_range(20);
157assert(r.first == std::next(m.begin(), 8));
158assert(r.second == std::next(m.begin(), 8));
159}
160}
161#if TEST_STD_VER >= 11
162{
163typedef int V;
164typedef std::set<int, std::less<int>, min_allocator<int>> M;
165typedef std::pair<M::iterator, M::iterator> R;
166V ar[] =
167{
1685,
1697,
1709,
17111,
17213,
17315,
17417,
17519
176};
177M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
178R r = m.equal_range(5);
179assert(r.first == std::next(m.begin(), 0));
180assert(r.second == std::next(m.begin(), 1));
181r = m.equal_range(7);
182assert(r.first == std::next(m.begin(), 1));
183assert(r.second == std::next(m.begin(), 2));
184r = m.equal_range(9);
185assert(r.first == std::next(m.begin(), 2));
186assert(r.second == std::next(m.begin(), 3));
187r = m.equal_range(11);
188assert(r.first == std::next(m.begin(), 3));
189assert(r.second == std::next(m.begin(), 4));
190r = m.equal_range(13);
191assert(r.first == std::next(m.begin(), 4));
192assert(r.second == std::next(m.begin(), 5));
193r = m.equal_range(15);
194assert(r.first == std::next(m.begin(), 5));
195assert(r.second == std::next(m.begin(), 6));
196r = m.equal_range(17);
197assert(r.first == std::next(m.begin(), 6));
198assert(r.second == std::next(m.begin(), 7));
199r = m.equal_range(19);
200assert(r.first == std::next(m.begin(), 7));
201assert(r.second == std::next(m.begin(), 8));
202r = m.equal_range(4);
203assert(r.first == std::next(m.begin(), 0));
204assert(r.second == std::next(m.begin(), 0));
205r = m.equal_range(6);
206assert(r.first == std::next(m.begin(), 1));
207assert(r.second == std::next(m.begin(), 1));
208r = m.equal_range(8);
209assert(r.first == std::next(m.begin(), 2));
210assert(r.second == std::next(m.begin(), 2));
211r = m.equal_range(10);
212assert(r.first == std::next(m.begin(), 3));
213assert(r.second == std::next(m.begin(), 3));
214r = m.equal_range(12);
215assert(r.first == std::next(m.begin(), 4));
216assert(r.second == std::next(m.begin(), 4));
217r = m.equal_range(14);
218assert(r.first == std::next(m.begin(), 5));
219assert(r.second == std::next(m.begin(), 5));
220r = m.equal_range(16);
221assert(r.first == std::next(m.begin(), 6));
222assert(r.second == std::next(m.begin(), 6));
223r = m.equal_range(18);
224assert(r.first == std::next(m.begin(), 7));
225assert(r.second == std::next(m.begin(), 7));
226r = m.equal_range(20);
227assert(r.first == std::next(m.begin(), 8));
228assert(r.second == std::next(m.begin(), 8));
229}
230#endif
231#if TEST_STD_VER > 11
232{
233typedef int V;
234typedef std::set<V, std::less<>> M;
235{
236typedef std::pair<M::iterator, M::iterator> R;
237V ar[] =
238{
2395,
2407,
2419,
24211,
24313,
24415,
24517,
24619
247};
248M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
249R r = m.equal_range(5);
250assert(r.first == std::next(m.begin(), 0));
251assert(r.second == std::next(m.begin(), 1));
252r = m.equal_range(7);
253assert(r.first == std::next(m.begin(), 1));
254assert(r.second == std::next(m.begin(), 2));
255r = m.equal_range(9);
256assert(r.first == std::next(m.begin(), 2));
257assert(r.second == std::next(m.begin(), 3));
258r = m.equal_range(11);
259assert(r.first == std::next(m.begin(), 3));
260assert(r.second == std::next(m.begin(), 4));
261r = m.equal_range(13);
262assert(r.first == std::next(m.begin(), 4));
263assert(r.second == std::next(m.begin(), 5));
264r = m.equal_range(15);
265assert(r.first == std::next(m.begin(), 5));
266assert(r.second == std::next(m.begin(), 6));
267r = m.equal_range(17);
268assert(r.first == std::next(m.begin(), 6));
269assert(r.second == std::next(m.begin(), 7));
270r = m.equal_range(19);
271assert(r.first == std::next(m.begin(), 7));
272assert(r.second == std::next(m.begin(), 8));
273r = m.equal_range(4);
274assert(r.first == std::next(m.begin(), 0));
275assert(r.second == std::next(m.begin(), 0));
276r = m.equal_range(6);
277assert(r.first == std::next(m.begin(), 1));
278assert(r.second == std::next(m.begin(), 1));
279r = m.equal_range(8);
280assert(r.first == std::next(m.begin(), 2));
281assert(r.second == std::next(m.begin(), 2));
282r = m.equal_range(10);
283assert(r.first == std::next(m.begin(), 3));
284assert(r.second == std::next(m.begin(), 3));
285r = m.equal_range(12);
286assert(r.first == std::next(m.begin(), 4));
287assert(r.second == std::next(m.begin(), 4));
288r = m.equal_range(14);
289assert(r.first == std::next(m.begin(), 5));
290assert(r.second == std::next(m.begin(), 5));
291r = m.equal_range(16);
292assert(r.first == std::next(m.begin(), 6));
293assert(r.second == std::next(m.begin(), 6));
294r = m.equal_range(18);
295assert(r.first == std::next(m.begin(), 7));
296assert(r.second == std::next(m.begin(), 7));
297r = m.equal_range(20);
298assert(r.first == std::next(m.begin(), 8));
299assert(r.second == std::next(m.begin(), 8));
300}
301}
302{
303typedef PrivateConstructor V;
304typedef std::set<V, std::less<>> M;
305typedef std::pair<M::iterator, M::iterator> R;
306
307M m;
308m.insert ( V::make ( 5 ));
309m.insert ( V::make ( 7 ));
310m.insert ( V::make ( 9 ));
311m.insert ( V::make ( 11 ));
312m.insert ( V::make ( 13 ));
313m.insert ( V::make ( 15 ));
314m.insert ( V::make ( 17 ));
315m.insert ( V::make ( 19 ));
316
317R r = m.equal_range(5);
318assert(r.first == std::next(m.begin(), 0));
319assert(r.second == std::next(m.begin(), 1));
320r = m.equal_range(7);
321assert(r.first == std::next(m.begin(), 1));
322assert(r.second == std::next(m.begin(), 2));
323r = m.equal_range(9);
324assert(r.first == std::next(m.begin(), 2));
325assert(r.second == std::next(m.begin(), 3));
326r = m.equal_range(11);
327assert(r.first == std::next(m.begin(), 3));
328assert(r.second == std::next(m.begin(), 4));
329r = m.equal_range(13);
330assert(r.first == std::next(m.begin(), 4));
331assert(r.second == std::next(m.begin(), 5));
332r = m.equal_range(15);
333assert(r.first == std::next(m.begin(), 5));
334assert(r.second == std::next(m.begin(), 6));
335r = m.equal_range(17);
336assert(r.first == std::next(m.begin(), 6));
337assert(r.second == std::next(m.begin(), 7));
338r = m.equal_range(19);
339assert(r.first == std::next(m.begin(), 7));
340assert(r.second == std::next(m.begin(), 8));
341r = m.equal_range(4);
342assert(r.first == std::next(m.begin(), 0));
343assert(r.second == std::next(m.begin(), 0));
344r = m.equal_range(6);
345assert(r.first == std::next(m.begin(), 1));
346assert(r.second == std::next(m.begin(), 1));
347r = m.equal_range(8);
348assert(r.first == std::next(m.begin(), 2));
349assert(r.second == std::next(m.begin(), 2));
350r = m.equal_range(10);
351assert(r.first == std::next(m.begin(), 3));
352assert(r.second == std::next(m.begin(), 3));
353r = m.equal_range(12);
354assert(r.first == std::next(m.begin(), 4));
355assert(r.second == std::next(m.begin(), 4));
356r = m.equal_range(14);
357assert(r.first == std::next(m.begin(), 5));
358assert(r.second == std::next(m.begin(), 5));
359r = m.equal_range(16);
360assert(r.first == std::next(m.begin(), 6));
361assert(r.second == std::next(m.begin(), 6));
362r = m.equal_range(18);
363assert(r.first == std::next(m.begin(), 7));
364assert(r.second == std::next(m.begin(), 7));
365r = m.equal_range(20);
366assert(r.first == std::next(m.begin(), 8));
367assert(r.second == std::next(m.begin(), 8));
368}
369#endif
370
371return 0;
372}
373