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 // unordered_multimap(unordered_multimap&& u);
17 
18 #include <unordered_map>
19 #include <string>
20 #include <cassert>
21 #include <cfloat>
22 
23 #include "../../../test_compare.h"
24 #include "../../../test_hash.h"
25 #include "../../../test_allocator.h"
26 #include "../../../min_allocator.h"
27 
28 int main()
29 {
30 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
31     {
32         typedef std::unordered_multimap<int, std::string,
33                                    test_hash<std::hash<int> >,
34                                    test_compare<std::equal_to<int> >,
35                                    test_allocator<std::pair<const int, std::string> >
36                                    > C;
37         typedef std::pair<int, std::string> P;
38         P a[] =
39         {
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(7,
48             test_hash<std::hash<int> >(8),
49             test_compare<std::equal_to<int> >(9),
50             test_allocator<std::pair<const int, std::string> >(10)
51            );
52         C c = std::move(c0);
53         assert(c.bucket_count() == 7);
54         assert(c.size() == 0);
55         assert(c.hash_function() == test_hash<std::hash<int> >(8));
56         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
57         assert(c.get_allocator() ==
58                (test_allocator<std::pair<const int, std::string> >(10)));
59         assert(c.empty());
60         assert(std::distance(c.begin(), c.end()) == c.size());
61         assert(std::distance(c.cbegin(), c.cend()) == c.size());
62         assert(c.load_factor() == 0);
63         assert(c.max_load_factor() == 1);
64 
65         assert(c0.empty());
66     }
67     {
68         typedef std::unordered_multimap<int, std::string,
69                                    test_hash<std::hash<int> >,
70                                    test_compare<std::equal_to<int> >,
71                                    test_allocator<std::pair<const int, std::string> >
72                                    > C;
73         typedef std::pair<int, std::string> P;
74         P a[] =
75         {
76             P(1, "one"),
77             P(2, "two"),
78             P(3, "three"),
79             P(4, "four"),
80             P(1, "four"),
81             P(2, "four"),
82         };
83         C c0(a, a + sizeof(a)/sizeof(a[0]),
84             7,
85             test_hash<std::hash<int> >(8),
86             test_compare<std::equal_to<int> >(9),
87             test_allocator<std::pair<const int, std::string> >(10)
88            );
89         C c = std::move(c0);
90         assert(c.bucket_count() == 7);
91         assert(c.size() == 6);
92         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
93         Eq eq = c.equal_range(1);
94         assert(std::distance(eq.first, eq.second) == 2);
95         C::const_iterator i = eq.first;
96         assert(i->first == 1);
97         assert(i->second == "one");
98         ++i;
99         assert(i->first == 1);
100         assert(i->second == "four");
101         eq = c.equal_range(2);
102         assert(std::distance(eq.first, eq.second) == 2);
103         i = eq.first;
104         assert(i->first == 2);
105         assert(i->second == "two");
106         ++i;
107         assert(i->first == 2);
108         assert(i->second == "four");
109 
110         eq = c.equal_range(3);
111         assert(std::distance(eq.first, eq.second) == 1);
112         i = eq.first;
113         assert(i->first == 3);
114         assert(i->second == "three");
115         eq = c.equal_range(4);
116         assert(std::distance(eq.first, eq.second) == 1);
117         i = eq.first;
118         assert(i->first == 4);
119         assert(i->second == "four");
120         assert(std::distance(c.begin(), c.end()) == c.size());
121         assert(std::distance(c.cbegin(), c.cend()) == c.size());
122         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
123         assert(c.max_load_factor() == 1);
124         assert(c.hash_function() == test_hash<std::hash<int> >(8));
125         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
126         assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10)));
127 
128         assert(c0.empty());
129     }
130 #if __cplusplus >= 201103L
131     {
132         typedef std::unordered_multimap<int, std::string,
133                                    test_hash<std::hash<int> >,
134                                    test_compare<std::equal_to<int> >,
135                                    min_allocator<std::pair<const int, std::string> >
136                                    > C;
137         typedef std::pair<int, std::string> P;
138         P a[] =
139         {
140             P(1, "one"),
141             P(2, "two"),
142             P(3, "three"),
143             P(4, "four"),
144             P(1, "four"),
145             P(2, "four"),
146         };
147         C c0(7,
148             test_hash<std::hash<int> >(8),
149             test_compare<std::equal_to<int> >(9),
150             min_allocator<std::pair<const int, std::string> >()
151            );
152         C c = std::move(c0);
153         assert(c.bucket_count() == 7);
154         assert(c.size() == 0);
155         assert(c.hash_function() == test_hash<std::hash<int> >(8));
156         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
157         assert(c.get_allocator() ==
158                (min_allocator<std::pair<const int, std::string> >()));
159         assert(c.empty());
160         assert(std::distance(c.begin(), c.end()) == c.size());
161         assert(std::distance(c.cbegin(), c.cend()) == c.size());
162         assert(c.load_factor() == 0);
163         assert(c.max_load_factor() == 1);
164 
165         assert(c0.empty());
166     }
167     {
168         typedef std::unordered_multimap<int, std::string,
169                                    test_hash<std::hash<int> >,
170                                    test_compare<std::equal_to<int> >,
171                                    min_allocator<std::pair<const int, std::string> >
172                                    > C;
173         typedef std::pair<int, std::string> P;
174         P a[] =
175         {
176             P(1, "one"),
177             P(2, "two"),
178             P(3, "three"),
179             P(4, "four"),
180             P(1, "four"),
181             P(2, "four"),
182         };
183         C c0(a, a + sizeof(a)/sizeof(a[0]),
184             7,
185             test_hash<std::hash<int> >(8),
186             test_compare<std::equal_to<int> >(9),
187             min_allocator<std::pair<const int, std::string> >()
188            );
189         C c = std::move(c0);
190         assert(c.bucket_count() == 7);
191         assert(c.size() == 6);
192         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
193         Eq eq = c.equal_range(1);
194         assert(std::distance(eq.first, eq.second) == 2);
195         C::const_iterator i = eq.first;
196         assert(i->first == 1);
197         assert(i->second == "one");
198         ++i;
199         assert(i->first == 1);
200         assert(i->second == "four");
201         eq = c.equal_range(2);
202         assert(std::distance(eq.first, eq.second) == 2);
203         i = eq.first;
204         assert(i->first == 2);
205         assert(i->second == "two");
206         ++i;
207         assert(i->first == 2);
208         assert(i->second == "four");
209 
210         eq = c.equal_range(3);
211         assert(std::distance(eq.first, eq.second) == 1);
212         i = eq.first;
213         assert(i->first == 3);
214         assert(i->second == "three");
215         eq = c.equal_range(4);
216         assert(std::distance(eq.first, eq.second) == 1);
217         i = eq.first;
218         assert(i->first == 4);
219         assert(i->second == "four");
220         assert(std::distance(c.begin(), c.end()) == c.size());
221         assert(std::distance(c.cbegin(), c.cend()) == c.size());
222         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
223         assert(c.max_load_factor() == 1);
224         assert(c.hash_function() == test_hash<std::hash<int> >(8));
225         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
226         assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
227 
228         assert(c0.empty());
229     }
230 #endif
231 #if _LIBCPP_DEBUG >= 1
232     {
233         std::unordered_multimap<int, int> s1 = {{1, 1}, {2, 2}, {3, 3}};
234         std::unordered_multimap<int, int>::iterator i = s1.begin();
235         std::pair<const int, int> k = *i;
236         std::unordered_multimap<int, int> s2 = std::move(s1);
237         assert(*i == k);
238         s2.erase(i);
239         assert(s2.size() == 2);
240     }
241 #endif
242 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
243 }
244