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