llvm-project
338 строк · 9.9 Кб
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// iterator upper_bound(const key_type& k);
14// const_iterator upper_bound(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 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.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{
815,
827,
839,
8411,
8513,
8615,
8717,
8819
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 int V;
130typedef std::set<int, std::less<int>, min_allocator<int>> M;
131{
132typedef M::iterator R;
133V ar[] =
134{
1355,
1367,
1379,
13811,
13913,
14015,
14117,
14219
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{
1845,
1857,
1869,
18711,
18813,
18915,
19017,
19119
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 int V;
234typedef std::set<V, std::less<>> M;
235typedef M::iterator R;
236
237V ar[] =
238{
2395,
2407,
2419,
24211,
24313,
24415,
24517,
24619
247};
248M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
249R r = m.upper_bound(5);
250assert(r == std::next(m.begin(), 1));
251r = m.upper_bound(7);
252assert(r == std::next(m.begin(), 2));
253r = m.upper_bound(9);
254assert(r == std::next(m.begin(), 3));
255r = m.upper_bound(11);
256assert(r == std::next(m.begin(), 4));
257r = m.upper_bound(13);
258assert(r == std::next(m.begin(), 5));
259r = m.upper_bound(15);
260assert(r == std::next(m.begin(), 6));
261r = m.upper_bound(17);
262assert(r == std::next(m.begin(), 7));
263r = m.upper_bound(19);
264assert(r == std::next(m.begin(), 8));
265r = m.upper_bound(4);
266assert(r == std::next(m.begin(), 0));
267r = m.upper_bound(6);
268assert(r == std::next(m.begin(), 1));
269r = m.upper_bound(8);
270assert(r == std::next(m.begin(), 2));
271r = m.upper_bound(10);
272assert(r == std::next(m.begin(), 3));
273r = m.upper_bound(12);
274assert(r == std::next(m.begin(), 4));
275r = m.upper_bound(14);
276assert(r == std::next(m.begin(), 5));
277r = m.upper_bound(16);
278assert(r == std::next(m.begin(), 6));
279r = m.upper_bound(18);
280assert(r == std::next(m.begin(), 7));
281r = m.upper_bound(20);
282assert(r == std::next(m.begin(), 8));
283}
284
285{
286typedef PrivateConstructor V;
287typedef std::set<V, std::less<>> M;
288typedef M::iterator R;
289
290M m;
291m.insert ( V::make ( 5 ));
292m.insert ( V::make ( 7 ));
293m.insert ( V::make ( 9 ));
294m.insert ( V::make ( 11 ));
295m.insert ( V::make ( 13 ));
296m.insert ( V::make ( 15 ));
297m.insert ( V::make ( 17 ));
298m.insert ( V::make ( 19 ));
299
300R r = m.upper_bound(5);
301assert(r == std::next(m.begin(), 1));
302r = m.upper_bound(7);
303assert(r == std::next(m.begin(), 2));
304r = m.upper_bound(9);
305assert(r == std::next(m.begin(), 3));
306r = m.upper_bound(11);
307assert(r == std::next(m.begin(), 4));
308r = m.upper_bound(13);
309assert(r == std::next(m.begin(), 5));
310r = m.upper_bound(15);
311assert(r == std::next(m.begin(), 6));
312r = m.upper_bound(17);
313assert(r == std::next(m.begin(), 7));
314r = m.upper_bound(19);
315assert(r == std::next(m.begin(), 8));
316r = m.upper_bound(4);
317assert(r == std::next(m.begin(), 0));
318r = m.upper_bound(6);
319assert(r == std::next(m.begin(), 1));
320r = m.upper_bound(8);
321assert(r == std::next(m.begin(), 2));
322r = m.upper_bound(10);
323assert(r == std::next(m.begin(), 3));
324r = m.upper_bound(12);
325assert(r == std::next(m.begin(), 4));
326r = m.upper_bound(14);
327assert(r == std::next(m.begin(), 5));
328r = m.upper_bound(16);
329assert(r == std::next(m.begin(), 6));
330r = m.upper_bound(18);
331assert(r == std::next(m.begin(), 7));
332r = m.upper_bound(20);
333assert(r == std::next(m.begin(), 8));
334}
335#endif
336
337return 0;
338}
339