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