llvm-project
339 строк · 9.8 Кб
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 lower_bound(const key_type& k);
14// const_iterator lower_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.lower_bound(5);
43assert(r == m.begin());
44r = m.lower_bound(7);
45assert(r == std::next(m.begin()));
46r = m.lower_bound(9);
47assert(r == std::next(m.begin(), 2));
48r = m.lower_bound(11);
49assert(r == std::next(m.begin(), 3));
50r = m.lower_bound(13);
51assert(r == std::next(m.begin(), 4));
52r = m.lower_bound(15);
53assert(r == std::next(m.begin(), 5));
54r = m.lower_bound(17);
55assert(r == std::next(m.begin(), 6));
56r = m.lower_bound(19);
57assert(r == std::next(m.begin(), 7));
58r = m.lower_bound(4);
59assert(r == std::next(m.begin(), 0));
60r = m.lower_bound(6);
61assert(r == std::next(m.begin(), 1));
62r = m.lower_bound(8);
63assert(r == std::next(m.begin(), 2));
64r = m.lower_bound(10);
65assert(r == std::next(m.begin(), 3));
66r = m.lower_bound(12);
67assert(r == std::next(m.begin(), 4));
68r = m.lower_bound(14);
69assert(r == std::next(m.begin(), 5));
70r = m.lower_bound(16);
71assert(r == std::next(m.begin(), 6));
72r = m.lower_bound(18);
73assert(r == std::next(m.begin(), 7));
74r = m.lower_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.lower_bound(5);
92assert(r == m.begin());
93r = m.lower_bound(7);
94assert(r == std::next(m.begin()));
95r = m.lower_bound(9);
96assert(r == std::next(m.begin(), 2));
97r = m.lower_bound(11);
98assert(r == std::next(m.begin(), 3));
99r = m.lower_bound(13);
100assert(r == std::next(m.begin(), 4));
101r = m.lower_bound(15);
102assert(r == std::next(m.begin(), 5));
103r = m.lower_bound(17);
104assert(r == std::next(m.begin(), 6));
105r = m.lower_bound(19);
106assert(r == std::next(m.begin(), 7));
107r = m.lower_bound(4);
108assert(r == std::next(m.begin(), 0));
109r = m.lower_bound(6);
110assert(r == std::next(m.begin(), 1));
111r = m.lower_bound(8);
112assert(r == std::next(m.begin(), 2));
113r = m.lower_bound(10);
114assert(r == std::next(m.begin(), 3));
115r = m.lower_bound(12);
116assert(r == std::next(m.begin(), 4));
117r = m.lower_bound(14);
118assert(r == std::next(m.begin(), 5));
119r = m.lower_bound(16);
120assert(r == std::next(m.begin(), 6));
121r = m.lower_bound(18);
122assert(r == std::next(m.begin(), 7));
123r = m.lower_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.lower_bound(5);
146assert(r == m.begin());
147r = m.lower_bound(7);
148assert(r == std::next(m.begin()));
149r = m.lower_bound(9);
150assert(r == std::next(m.begin(), 2));
151r = m.lower_bound(11);
152assert(r == std::next(m.begin(), 3));
153r = m.lower_bound(13);
154assert(r == std::next(m.begin(), 4));
155r = m.lower_bound(15);
156assert(r == std::next(m.begin(), 5));
157r = m.lower_bound(17);
158assert(r == std::next(m.begin(), 6));
159r = m.lower_bound(19);
160assert(r == std::next(m.begin(), 7));
161r = m.lower_bound(4);
162assert(r == std::next(m.begin(), 0));
163r = m.lower_bound(6);
164assert(r == std::next(m.begin(), 1));
165r = m.lower_bound(8);
166assert(r == std::next(m.begin(), 2));
167r = m.lower_bound(10);
168assert(r == std::next(m.begin(), 3));
169r = m.lower_bound(12);
170assert(r == std::next(m.begin(), 4));
171r = m.lower_bound(14);
172assert(r == std::next(m.begin(), 5));
173r = m.lower_bound(16);
174assert(r == std::next(m.begin(), 6));
175r = m.lower_bound(18);
176assert(r == std::next(m.begin(), 7));
177r = m.lower_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.lower_bound(5);
195assert(r == m.begin());
196r = m.lower_bound(7);
197assert(r == std::next(m.begin()));
198r = m.lower_bound(9);
199assert(r == std::next(m.begin(), 2));
200r = m.lower_bound(11);
201assert(r == std::next(m.begin(), 3));
202r = m.lower_bound(13);
203assert(r == std::next(m.begin(), 4));
204r = m.lower_bound(15);
205assert(r == std::next(m.begin(), 5));
206r = m.lower_bound(17);
207assert(r == std::next(m.begin(), 6));
208r = m.lower_bound(19);
209assert(r == std::next(m.begin(), 7));
210r = m.lower_bound(4);
211assert(r == std::next(m.begin(), 0));
212r = m.lower_bound(6);
213assert(r == std::next(m.begin(), 1));
214r = m.lower_bound(8);
215assert(r == std::next(m.begin(), 2));
216r = m.lower_bound(10);
217assert(r == std::next(m.begin(), 3));
218r = m.lower_bound(12);
219assert(r == std::next(m.begin(), 4));
220r = m.lower_bound(14);
221assert(r == std::next(m.begin(), 5));
222r = m.lower_bound(16);
223assert(r == std::next(m.begin(), 6));
224r = m.lower_bound(18);
225assert(r == std::next(m.begin(), 7));
226r = m.lower_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.lower_bound(5);
250assert(r == m.begin());
251r = m.lower_bound(7);
252assert(r == std::next(m.begin()));
253r = m.lower_bound(9);
254assert(r == std::next(m.begin(), 2));
255r = m.lower_bound(11);
256assert(r == std::next(m.begin(), 3));
257r = m.lower_bound(13);
258assert(r == std::next(m.begin(), 4));
259r = m.lower_bound(15);
260assert(r == std::next(m.begin(), 5));
261r = m.lower_bound(17);
262assert(r == std::next(m.begin(), 6));
263r = m.lower_bound(19);
264assert(r == std::next(m.begin(), 7));
265r = m.lower_bound(4);
266assert(r == std::next(m.begin(), 0));
267r = m.lower_bound(6);
268assert(r == std::next(m.begin(), 1));
269r = m.lower_bound(8);
270assert(r == std::next(m.begin(), 2));
271r = m.lower_bound(10);
272assert(r == std::next(m.begin(), 3));
273r = m.lower_bound(12);
274assert(r == std::next(m.begin(), 4));
275r = m.lower_bound(14);
276assert(r == std::next(m.begin(), 5));
277r = m.lower_bound(16);
278assert(r == std::next(m.begin(), 6));
279r = m.lower_bound(18);
280assert(r == std::next(m.begin(), 7));
281r = m.lower_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.lower_bound(5);
301assert(r == m.begin());
302r = m.lower_bound(7);
303assert(r == std::next(m.begin()));
304r = m.lower_bound(9);
305assert(r == std::next(m.begin(), 2));
306r = m.lower_bound(11);
307assert(r == std::next(m.begin(), 3));
308r = m.lower_bound(13);
309assert(r == std::next(m.begin(), 4));
310r = m.lower_bound(15);
311assert(r == std::next(m.begin(), 5));
312r = m.lower_bound(17);
313assert(r == std::next(m.begin(), 6));
314r = m.lower_bound(19);
315assert(r == std::next(m.begin(), 7));
316r = m.lower_bound(4);
317assert(r == std::next(m.begin(), 0));
318r = m.lower_bound(6);
319assert(r == std::next(m.begin(), 1));
320r = m.lower_bound(8);
321assert(r == std::next(m.begin(), 2));
322r = m.lower_bound(10);
323assert(r == std::next(m.begin(), 3));
324r = m.lower_bound(12);
325assert(r == std::next(m.begin(), 4));
326r = m.lower_bound(14);
327assert(r == std::next(m.begin(), 5));
328r = m.lower_bound(16);
329assert(r == std::next(m.begin(), 6));
330r = m.lower_bound(18);
331assert(r == std::next(m.begin(), 7));
332r = m.lower_bound(20);
333assert(r == std::next(m.begin(), 8));
334}
335#endif
336
337
338return 0;
339}
340