1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <map>
11 
12 // class map
13 
14 //       iterator find(const key_type& k);
15 // const_iterator find(const key_type& k) const;
16 
17 #include <map>
18 #include <cassert>
19 
20 #include "min_allocator.h"
21 #include "private_constructor.hpp"
22 #include "is_transparent.h"
23 
main()24 int main()
25 {
26     {
27     typedef std::pair<const int, double> V;
28     typedef std::map<int, double> M;
29     {
30         typedef M::iterator R;
31         V ar[] =
32         {
33             V(5, 5),
34             V(6, 6),
35             V(7, 7),
36             V(8, 8),
37             V(9, 9),
38             V(10, 10),
39             V(11, 11),
40             V(12, 12)
41         };
42         M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
43         R r = m.find(5);
44         assert(r == m.begin());
45         r = m.find(6);
46         assert(r == next(m.begin()));
47         r = m.find(7);
48         assert(r == next(m.begin(), 2));
49         r = m.find(8);
50         assert(r == next(m.begin(), 3));
51         r = m.find(9);
52         assert(r == next(m.begin(), 4));
53         r = m.find(10);
54         assert(r == next(m.begin(), 5));
55         r = m.find(11);
56         assert(r == next(m.begin(), 6));
57         r = m.find(12);
58         assert(r == next(m.begin(), 7));
59         r = m.find(4);
60         assert(r == next(m.begin(), 8));
61     }
62     {
63         typedef M::const_iterator R;
64         V ar[] =
65         {
66             V(5, 5),
67             V(6, 6),
68             V(7, 7),
69             V(8, 8),
70             V(9, 9),
71             V(10, 10),
72             V(11, 11),
73             V(12, 12)
74         };
75         const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
76         R r = m.find(5);
77         assert(r == m.begin());
78         r = m.find(6);
79         assert(r == next(m.begin()));
80         r = m.find(7);
81         assert(r == next(m.begin(), 2));
82         r = m.find(8);
83         assert(r == next(m.begin(), 3));
84         r = m.find(9);
85         assert(r == next(m.begin(), 4));
86         r = m.find(10);
87         assert(r == next(m.begin(), 5));
88         r = m.find(11);
89         assert(r == next(m.begin(), 6));
90         r = m.find(12);
91         assert(r == next(m.begin(), 7));
92         r = m.find(4);
93         assert(r == next(m.begin(), 8));
94     }
95     }
96 #if __cplusplus >= 201103L
97     {
98     typedef std::pair<const int, double> V;
99     typedef std::map<int, double, std::less<int>, min_allocator<V>> M;
100     {
101         typedef M::iterator R;
102         V ar[] =
103         {
104             V(5, 5),
105             V(6, 6),
106             V(7, 7),
107             V(8, 8),
108             V(9, 9),
109             V(10, 10),
110             V(11, 11),
111             V(12, 12)
112         };
113         M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
114         R r = m.find(5);
115         assert(r == m.begin());
116         r = m.find(6);
117         assert(r == next(m.begin()));
118         r = m.find(7);
119         assert(r == next(m.begin(), 2));
120         r = m.find(8);
121         assert(r == next(m.begin(), 3));
122         r = m.find(9);
123         assert(r == next(m.begin(), 4));
124         r = m.find(10);
125         assert(r == next(m.begin(), 5));
126         r = m.find(11);
127         assert(r == next(m.begin(), 6));
128         r = m.find(12);
129         assert(r == next(m.begin(), 7));
130         r = m.find(4);
131         assert(r == next(m.begin(), 8));
132     }
133     {
134         typedef M::const_iterator R;
135         V ar[] =
136         {
137             V(5, 5),
138             V(6, 6),
139             V(7, 7),
140             V(8, 8),
141             V(9, 9),
142             V(10, 10),
143             V(11, 11),
144             V(12, 12)
145         };
146         const M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
147         R r = m.find(5);
148         assert(r == m.begin());
149         r = m.find(6);
150         assert(r == next(m.begin()));
151         r = m.find(7);
152         assert(r == next(m.begin(), 2));
153         r = m.find(8);
154         assert(r == next(m.begin(), 3));
155         r = m.find(9);
156         assert(r == next(m.begin(), 4));
157         r = m.find(10);
158         assert(r == next(m.begin(), 5));
159         r = m.find(11);
160         assert(r == next(m.begin(), 6));
161         r = m.find(12);
162         assert(r == next(m.begin(), 7));
163         r = m.find(4);
164         assert(r == next(m.begin(), 8));
165     }
166     }
167 #endif
168 #if _LIBCPP_STD_VER > 11
169     {
170     typedef std::pair<const int, double> V;
171     typedef std::map<int, double, std::less<>> M;
172     typedef M::iterator R;
173 
174     V ar[] =
175     {
176         V(5, 5),
177         V(6, 6),
178         V(7, 7),
179         V(8, 8),
180         V(9, 9),
181         V(10, 10),
182         V(11, 11),
183         V(12, 12)
184     };
185     M m(ar, ar+sizeof(ar)/sizeof(ar[0]));
186     R r = m.find(5);
187     assert(r == m.begin());
188     r = m.find(6);
189     assert(r == next(m.begin()));
190     r = m.find(7);
191     assert(r == next(m.begin(), 2));
192     r = m.find(8);
193     assert(r == next(m.begin(), 3));
194     r = m.find(9);
195     assert(r == next(m.begin(), 4));
196     r = m.find(10);
197     assert(r == next(m.begin(), 5));
198     r = m.find(11);
199     assert(r == next(m.begin(), 6));
200     r = m.find(12);
201     assert(r == next(m.begin(), 7));
202     r = m.find(4);
203     assert(r == next(m.begin(), 8));
204 
205     r = m.find(C2Int(5));
206     assert(r == m.begin());
207     r = m.find(C2Int(6));
208     assert(r == next(m.begin()));
209     r = m.find(C2Int(7));
210     assert(r == next(m.begin(), 2));
211     r = m.find(C2Int(8));
212     assert(r == next(m.begin(), 3));
213     r = m.find(C2Int(9));
214     assert(r == next(m.begin(), 4));
215     r = m.find(C2Int(10));
216     assert(r == next(m.begin(), 5));
217     r = m.find(C2Int(11));
218     assert(r == next(m.begin(), 6));
219     r = m.find(C2Int(12));
220     assert(r == next(m.begin(), 7));
221     r = m.find(C2Int(4));
222     assert(r == next(m.begin(), 8));
223     }
224 
225     {
226     typedef PrivateConstructor PC;
227     typedef std::map<PC, double, std::less<>> M;
228     typedef M::iterator R;
229 
230     M m;
231     m [ PC::make(5)  ] = 5;
232     m [ PC::make(6)  ] = 6;
233     m [ PC::make(7)  ] = 7;
234     m [ PC::make(8)  ] = 8;
235     m [ PC::make(9)  ] = 9;
236     m [ PC::make(10) ] = 10;
237     m [ PC::make(11) ] = 11;
238     m [ PC::make(12) ] = 12;
239 
240     R r = m.find(5);
241     assert(r == m.begin());
242     r = m.find(6);
243     assert(r == next(m.begin()));
244     r = m.find(7);
245     assert(r == next(m.begin(), 2));
246     r = m.find(8);
247     assert(r == next(m.begin(), 3));
248     r = m.find(9);
249     assert(r == next(m.begin(), 4));
250     r = m.find(10);
251     assert(r == next(m.begin(), 5));
252     r = m.find(11);
253     assert(r == next(m.begin(), 6));
254     r = m.find(12);
255     assert(r == next(m.begin(), 7));
256     r = m.find(4);
257     assert(r == next(m.begin(), 8));
258     }
259 #endif
260 }
261