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