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
34int main(int, char**) {
35 {
36 typedef std::pair<int, std::string> P;
37 typedef test_allocator<std::pair<const int, std::string>> A;
38 typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
39 P a[] = {
40 P(1, "one"),
41 P(2, "two"),
42 P(3, "three"),
43 P(4, "four"),
44 P(1, "four"),
45 P(2, "four"),
46 };
47 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10));
48 C c(std::move(c0), A(12));
49 assert(c.bucket_count() >= 7);
50 assert(c.size() == 6);
51 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
52 Eq eq = c.equal_range(x: 1);
53 assert(std::distance(eq.first, eq.second) == 2);
54 std::multiset<std::string> s;
55 s.insert(x: "one");
56 s.insert(x: "four");
57 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
58 eq = c.equal_range(x: 2);
59 assert(std::distance(eq.first, eq.second) == 2);
60 s.insert(x: "two");
61 s.insert(x: "four");
62 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s);
63
64 eq = c.equal_range(x: 3);
65 assert(std::distance(eq.first, eq.second) == 1);
66 C::const_iterator i = eq.first;
67 assert(i->first == 3);
68 assert(i->second == "three");
69 eq = c.equal_range(x: 4);
70 assert(std::distance(eq.first, eq.second) == 1);
71 i = eq.first;
72 assert(i->first == 4);
73 assert(i->second == "four");
74 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
75 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
76 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
77 assert(c.max_load_factor() == 1);
78 assert(c.hash_function() == test_hash<int>(8));
79 assert(c.key_eq() == test_equal_to<int>(9));
80 assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(12)));
81
82 assert(c0.empty());
83 }
84 {
85 typedef std::pair<int, std::string> P;
86 typedef test_allocator<std::pair<const int, std::string>> A;
87 typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
88 P a[] = {
89 P(1, "one"),
90 P(2, "two"),
91 P(3, "three"),
92 P(4, "four"),
93 P(1, "four"),
94 P(2, "four"),
95 };
96 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10));
97 C c(std::move(c0), A(10));
98 LIBCPP_ASSERT(c.bucket_count() == 7);
99 assert(c.size() == 6);
100 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
101 Eq eq = c.equal_range(x: 1);
102 assert(std::distance(eq.first, eq.second) == 2);
103 std::multiset<std::string> s;
104 s.insert(x: "one");
105 s.insert(x: "four");
106 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
107 eq = c.equal_range(x: 2);
108 assert(std::distance(eq.first, eq.second) == 2);
109 s.insert(x: "two");
110 s.insert(x: "four");
111 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s);
112
113 eq = c.equal_range(x: 3);
114 assert(std::distance(eq.first, eq.second) == 1);
115 C::const_iterator i = eq.first;
116 assert(i->first == 3);
117 assert(i->second == "three");
118 eq = c.equal_range(x: 4);
119 assert(std::distance(eq.first, eq.second) == 1);
120 i = eq.first;
121 assert(i->first == 4);
122 assert(i->second == "four");
123 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
124 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
125 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
126 assert(c.max_load_factor() == 1);
127 assert(c.hash_function() == test_hash<int>(8));
128 assert(c.key_eq() == test_equal_to<int>(9));
129 assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10)));
130
131 assert(c0.empty());
132 }
133 {
134 typedef std::pair<int, std::string> P;
135 typedef min_allocator<std::pair<const int, std::string>> A;
136 typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
137 P a[] = {
138 P(1, "one"),
139 P(2, "two"),
140 P(3, "three"),
141 P(4, "four"),
142 P(1, "four"),
143 P(2, "four"),
144 };
145 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A());
146 C c(std::move(c0), A());
147 LIBCPP_ASSERT(c.bucket_count() == 7);
148 assert(c.size() == 6);
149 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
150 Eq eq = c.equal_range(x: 1);
151 assert(std::distance(eq.first, eq.second) == 2);
152 std::multiset<std::string> s;
153 s.insert(x: "one");
154 s.insert(x: "four");
155 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
156 eq = c.equal_range(x: 2);
157 assert(std::distance(eq.first, eq.second) == 2);
158 s.insert(x: "two");
159 s.insert(x: "four");
160 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s);
161
162 eq = c.equal_range(x: 3);
163 assert(std::distance(eq.first, eq.second) == 1);
164 C::const_iterator i = eq.first;
165 assert(i->first == 3);
166 assert(i->second == "three");
167 eq = c.equal_range(x: 4);
168 assert(std::distance(eq.first, eq.second) == 1);
169 i = eq.first;
170 assert(i->first == 4);
171 assert(i->second == "four");
172 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
173 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
174 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
175 assert(c.max_load_factor() == 1);
176 assert(c.hash_function() == test_hash<int>(8));
177 assert(c.key_eq() == test_equal_to<int>(9));
178 assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
179
180 assert(c0.empty());
181 }
182 {
183 typedef std::pair<int, std::string> P;
184 typedef explicit_allocator<std::pair<const int, std::string>> A;
185 typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
186 P a[] = {
187 P(1, "one"),
188 P(2, "two"),
189 P(3, "three"),
190 P(4, "four"),
191 P(1, "four"),
192 P(2, "four"),
193 };
194 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A{});
195 C c(std::move(c0), A());
196 LIBCPP_ASSERT(c.bucket_count() == 7);
197 assert(c.size() == 6);
198 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
199 Eq eq = c.equal_range(x: 1);
200 assert(std::distance(eq.first, eq.second) == 2);
201 std::multiset<std::string> s;
202 s.insert(x: "one");
203 s.insert(x: "four");
204 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
205 eq = c.equal_range(x: 2);
206 assert(std::distance(eq.first, eq.second) == 2);
207 s.insert(x: "two");
208 s.insert(x: "four");
209 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s);
210
211 eq = c.equal_range(x: 3);
212 assert(std::distance(eq.first, eq.second) == 1);
213 C::const_iterator i = eq.first;
214 assert(i->first == 3);
215 assert(i->second == "three");
216 eq = c.equal_range(x: 4);
217 assert(std::distance(eq.first, eq.second) == 1);
218 i = eq.first;
219 assert(i->first == 4);
220 assert(i->second == "four");
221 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
222 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
223 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
224 assert(c.max_load_factor() == 1);
225 assert(c.hash_function() == test_hash<int>(8));
226 assert(c.key_eq() == test_equal_to<int>(9));
227 assert(c.get_allocator() == A{});
228
229 assert(c0.empty());
230 }
231
232 return 0;
233}
234

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.cnstr/move_alloc.pass.cpp