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