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