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