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