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