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