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