]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/containers/unord/unord.multimap/unord.multimap.cnstr/range.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / containers / unord / unord.multimap / unord.multimap.cnstr / range.pass.cpp
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 // <unordered_map>
11
12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13 //           class Alloc = allocator<pair<const Key, T>>>
14 // class unordered_multimap
15
16 // template <class InputIterator>
17 //     unordered_multimap(InputIterator first, InputIterator last);
18
19 #include <unordered_map>
20 #include <string>
21 #include <cassert>
22 #include <cfloat>
23 #include <cmath>
24 #include <cstddef>
25
26 #include "test_macros.h"
27 #include "test_iterators.h"
28 #include "../../../NotConstructible.h"
29 #include "../../../test_compare.h"
30 #include "../../../test_hash.h"
31 #include "test_allocator.h"
32 #include "min_allocator.h"
33
34 int main()
35 {
36     {
37         typedef std::unordered_multimap<int, std::string,
38                                    test_hash<std::hash<int> >,
39                                    test_compare<std::equal_to<int> >,
40                                    test_allocator<std::pair<const int, std::string> >
41                                    > C;
42         typedef std::pair<int, std::string> P;
43         P a[] =
44         {
45             P(1, "one"),
46             P(2, "two"),
47             P(3, "three"),
48             P(4, "four"),
49             P(1, "four"),
50             P(2, "four"),
51         };
52         C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])));
53         assert(c.bucket_count() >= 7);
54         assert(c.size() == 6);
55         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
56         Eq eq = c.equal_range(1);
57         assert(std::distance(eq.first, eq.second) == 2);
58         C::const_iterator i = eq.first;
59         assert(i->first == 1);
60         assert(i->second == "one");
61         ++i;
62         assert(i->first == 1);
63         assert(i->second == "four");
64         eq = c.equal_range(2);
65         assert(std::distance(eq.first, eq.second) == 2);
66         i = eq.first;
67         assert(i->first == 2);
68         assert(i->second == "two");
69         ++i;
70         assert(i->first == 2);
71         assert(i->second == "four");
72
73         eq = c.equal_range(3);
74         assert(std::distance(eq.first, eq.second) == 1);
75         i = eq.first;
76         assert(i->first == 3);
77         assert(i->second == "three");
78         eq = c.equal_range(4);
79         assert(std::distance(eq.first, eq.second) == 1);
80         i = eq.first;
81         assert(i->first == 4);
82         assert(i->second == "four");
83         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
84         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
85         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
86         assert(c.max_load_factor() == 1);
87         assert(c.hash_function() == test_hash<std::hash<int> >());
88         assert(c.key_eq() == test_compare<std::equal_to<int> >());
89         assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
90     }
91 #if TEST_STD_VER >= 11
92     {
93         typedef std::unordered_multimap<int, std::string,
94                                    test_hash<std::hash<int> >,
95                                    test_compare<std::equal_to<int> >,
96                                    min_allocator<std::pair<const int, std::string> >
97                                    > C;
98         typedef std::pair<int, std::string> P;
99         P a[] =
100         {
101             P(1, "one"),
102             P(2, "two"),
103             P(3, "three"),
104             P(4, "four"),
105             P(1, "four"),
106             P(2, "four"),
107         };
108         C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])));
109         assert(c.bucket_count() >= 7);
110         assert(c.size() == 6);
111         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
112         Eq eq = c.equal_range(1);
113         assert(std::distance(eq.first, eq.second) == 2);
114         C::const_iterator i = eq.first;
115         assert(i->first == 1);
116         assert(i->second == "one");
117         ++i;
118         assert(i->first == 1);
119         assert(i->second == "four");
120         eq = c.equal_range(2);
121         assert(std::distance(eq.first, eq.second) == 2);
122         i = eq.first;
123         assert(i->first == 2);
124         assert(i->second == "two");
125         ++i;
126         assert(i->first == 2);
127         assert(i->second == "four");
128
129         eq = c.equal_range(3);
130         assert(std::distance(eq.first, eq.second) == 1);
131         i = eq.first;
132         assert(i->first == 3);
133         assert(i->second == "three");
134         eq = c.equal_range(4);
135         assert(std::distance(eq.first, eq.second) == 1);
136         i = eq.first;
137         assert(i->first == 4);
138         assert(i->second == "four");
139         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
140         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
141         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
142         assert(c.max_load_factor() == 1);
143         assert(c.hash_function() == test_hash<std::hash<int> >());
144         assert(c.key_eq() == test_compare<std::equal_to<int> >());
145         assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
146     }
147 #if TEST_STD_VER > 11
148     {
149         typedef std::pair<int, std::string> P;
150         typedef test_allocator<std::pair<const int, std::string>> A;
151         typedef test_hash<std::hash<int>> HF;
152         typedef test_compare<std::equal_to<int>> Comp;
153         typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
154
155         P arr[] =
156         {
157             P(1, "one"),
158             P(2, "two"),
159             P(3, "three"),
160             P(4, "four"),
161             P(1, "four"),
162             P(2, "four"),
163         };
164          A a(42);
165        C c(input_iterator<P*>(arr), input_iterator<P*>(arr + sizeof(arr)/sizeof(arr[0])), 14, a);
166         assert(c.bucket_count() >= 14);
167         assert(c.size() == 6);
168         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
169         Eq eq = c.equal_range(1);
170         assert(std::distance(eq.first, eq.second) == 2);
171         C::const_iterator i = eq.first;
172         assert(i->first == 1);
173         assert(i->second == "one");
174         ++i;
175         assert(i->first == 1);
176         assert(i->second == "four");
177         eq = c.equal_range(2);
178         assert(std::distance(eq.first, eq.second) == 2);
179         i = eq.first;
180         assert(i->first == 2);
181         assert(i->second == "two");
182         ++i;
183         assert(i->first == 2);
184         assert(i->second == "four");
185
186         eq = c.equal_range(3);
187         assert(std::distance(eq.first, eq.second) == 1);
188         i = eq.first;
189         assert(i->first == 3);
190         assert(i->second == "three");
191         eq = c.equal_range(4);
192         assert(std::distance(eq.first, eq.second) == 1);
193         i = eq.first;
194         assert(i->first == 4);
195         assert(i->second == "four");
196         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
197         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
198         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
199         assert(c.max_load_factor() == 1);
200         assert(c.hash_function() == HF());
201         assert(c.key_eq() == Comp());
202         assert(c.get_allocator() == a);
203         assert(!(c.get_allocator() == A()));
204     }
205     {
206         typedef std::pair<int, std::string> P;
207         typedef test_allocator<std::pair<const int, std::string>> A;
208         typedef test_hash<std::hash<int>> HF;
209         typedef test_compare<std::equal_to<int>> Comp;
210         typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
211
212         P arr[] =
213         {
214             P(1, "one"),
215             P(2, "two"),
216             P(3, "three"),
217             P(4, "four"),
218             P(1, "four"),
219             P(2, "four"),
220         };
221         A a(42);
222         HF hf (43);
223         C c(input_iterator<P*>(arr), input_iterator<P*>(arr + sizeof(arr)/sizeof(arr[0])), 12, hf, a );
224         assert(c.bucket_count() >= 12);
225         assert(c.size() == 6);
226         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
227         Eq eq = c.equal_range(1);
228         assert(std::distance(eq.first, eq.second) == 2);
229         C::const_iterator i = eq.first;
230         assert(i->first == 1);
231         assert(i->second == "one");
232         ++i;
233         assert(i->first == 1);
234         assert(i->second == "four");
235         eq = c.equal_range(2);
236         assert(std::distance(eq.first, eq.second) == 2);
237         i = eq.first;
238         assert(i->first == 2);
239         assert(i->second == "two");
240         ++i;
241         assert(i->first == 2);
242         assert(i->second == "four");
243
244         eq = c.equal_range(3);
245         assert(std::distance(eq.first, eq.second) == 1);
246         i = eq.first;
247         assert(i->first == 3);
248         assert(i->second == "three");
249         eq = c.equal_range(4);
250         assert(std::distance(eq.first, eq.second) == 1);
251         i = eq.first;
252         assert(i->first == 4);
253         assert(i->second == "four");
254         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
255         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
256         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
257         assert(c.max_load_factor() == 1);
258         assert(c.hash_function() == hf);
259         assert(!(c.hash_function() == HF()));
260         assert(c.key_eq() == Comp());
261         assert(c.get_allocator() == a);
262         assert(!(c.get_allocator() == A()));
263     }
264 #endif
265 #endif
266 }