1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <unordered_map>
12 
13 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
14 //           class Alloc = allocator<pair<const Key, T>>>
15 // class unordered_map
16 
17 // unordered_map(unordered_map&& u, const allocator_type& a);
18 
19 #include <unordered_map>
20 #include <string>
21 #include <cassert>
22 #include <cfloat>
23 #include <cmath>
24 #include <cstddef>
25 
26 #include "test_macros.h"
27 #include "../../../test_compare.h"
28 #include "../../../test_hash.h"
29 #include "test_allocator.h"
30 #include "min_allocator.h"
31 
main(int,char **)32 int main(int, char**)
33 {
34     {
35         typedef std::pair<int, std::string> P;
36         typedef test_allocator<std::pair<const int, std::string>> A;
37         typedef std::unordered_map<int, std::string,
38                                    test_hash<int>,
39                                    test_equal_to<int>,
40                                    A
41                                    > C;
42         P a[] =
43         {
44             P(1, "one"),
45             P(2, "two"),
46             P(3, "three"),
47             P(4, "four"),
48             P(1, "four"),
49             P(2, "four"),
50         };
51         C c0(a, a + sizeof(a)/sizeof(a[0]),
52             7,
53             test_hash<int>(8),
54             test_equal_to<int>(9),
55             A(10)
56            );
57         C c(std::move(c0), A(12));
58         assert(c.bucket_count() >= 5);
59         assert(c.size() == 4);
60         assert(c.at(1) == "one");
61         assert(c.at(2) == "two");
62         assert(c.at(3) == "three");
63         assert(c.at(4) == "four");
64         assert(c.hash_function() == test_hash<int>(8));
65         assert(c.key_eq() == test_equal_to<int>(9));
66         assert(c.get_allocator() == A(12));
67         assert(!c.empty());
68         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
69         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
70         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
71         assert(c.max_load_factor() == 1);
72 
73         assert(c0.empty());
74     }
75     {
76         typedef std::pair<int, std::string> P;
77         typedef test_allocator<std::pair<const int, std::string>> A;
78         typedef std::unordered_map<int, std::string,
79                                    test_hash<int>,
80                                    test_equal_to<int>,
81                                    A
82                                    > C;
83         P a[] =
84         {
85             P(1, "one"),
86             P(2, "two"),
87             P(3, "three"),
88             P(4, "four"),
89             P(1, "four"),
90             P(2, "four"),
91         };
92         C c0(a, a + sizeof(a)/sizeof(a[0]),
93             7,
94             test_hash<int>(8),
95             test_equal_to<int>(9),
96             A(10)
97            );
98         C c(std::move(c0), A(10));
99         LIBCPP_ASSERT(c.bucket_count() == 7);
100         assert(c.size() == 4);
101         assert(c.at(1) == "one");
102         assert(c.at(2) == "two");
103         assert(c.at(3) == "three");
104         assert(c.at(4) == "four");
105         assert(c.hash_function() == test_hash<int>(8));
106         assert(c.key_eq() == test_equal_to<int>(9));
107         assert(c.get_allocator() == A(10));
108         assert(!c.empty());
109         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
110         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
111         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
112         assert(c.max_load_factor() == 1);
113 
114         assert(c0.empty());
115     }
116     {
117         typedef std::pair<int, std::string> P;
118         typedef min_allocator<std::pair<const int, std::string>> A;
119         typedef std::unordered_map<int, std::string,
120                                    test_hash<int>,
121                                    test_equal_to<int>,
122                                    A
123                                    > C;
124         P a[] =
125         {
126             P(1, "one"),
127             P(2, "two"),
128             P(3, "three"),
129             P(4, "four"),
130             P(1, "four"),
131             P(2, "four"),
132         };
133         C c0(a, a + sizeof(a)/sizeof(a[0]),
134             7,
135             test_hash<int>(8),
136             test_equal_to<int>(9),
137             A()
138            );
139         C c(std::move(c0), A());
140         LIBCPP_ASSERT(c.bucket_count() == 7);
141         assert(c.size() == 4);
142         assert(c.at(1) == "one");
143         assert(c.at(2) == "two");
144         assert(c.at(3) == "three");
145         assert(c.at(4) == "four");
146         assert(c.hash_function() == test_hash<int>(8));
147         assert(c.key_eq() == test_equal_to<int>(9));
148         assert(c.get_allocator() == A());
149         assert(!c.empty());
150         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
151         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
152         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
153         assert(c.max_load_factor() == 1);
154 
155         assert(c0.empty());
156     }
157     {
158         typedef std::pair<int, std::string> P;
159         typedef explicit_allocator<std::pair<const int, std::string>> A;
160         typedef std::unordered_map<int, std::string,
161                                    test_hash<int>,
162                                    test_equal_to<int>,
163                                    A
164                                    > C;
165         P a[] =
166         {
167             P(1, "one"),
168             P(2, "two"),
169             P(3, "three"),
170             P(4, "four"),
171             P(1, "four"),
172             P(2, "four"),
173         };
174         C c0(a, a + sizeof(a)/sizeof(a[0]),
175             7,
176             test_hash<int>(8),
177             test_equal_to<int>(9),
178             A{}
179            );
180         C c(std::move(c0), A{});
181         LIBCPP_ASSERT(c.bucket_count() == 7);
182         assert(c.size() == 4);
183         assert(c.at(1) == "one");
184         assert(c.at(2) == "two");
185         assert(c.at(3) == "three");
186         assert(c.at(4) == "four");
187         assert(c.hash_function() == test_hash<int>(8));
188         assert(c.key_eq() == test_equal_to<int>(9));
189         assert(c.get_allocator() == A{});
190         assert(!c.empty());
191         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
192         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
193         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
194         assert(c.max_load_factor() == 1);
195 
196         assert(c0.empty());
197     }
198 
199   return 0;
200 }
201