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_set>
12 
13 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
14 //           class Alloc = allocator<Value>>
15 // class unordered_set
16 
17 // unordered_set& operator=(unordered_set&& u);
18 
19 #include <unordered_set>
20 #include <cassert>
21 #include <cfloat>
22 #include <cmath>
23 #include <cstddef>
24 
25 #include "test_macros.h"
26 #include "../../../test_compare.h"
27 #include "../../../test_hash.h"
28 #include "test_allocator.h"
29 #include "min_allocator.h"
30 
main(int,char **)31 int main(int, char**)
32 {
33     {
34         typedef test_allocator<int> A;
35         typedef std::unordered_set<int,
36                                    test_hash<int>,
37                                    test_equal_to<int>,
38                                    A
39                                    > C;
40         typedef int P;
41         P a[] =
42         {
43             P(1),
44             P(2),
45             P(3),
46             P(4),
47             P(1),
48             P(2)
49         };
50         C c0(a, a + sizeof(a)/sizeof(a[0]),
51             7,
52             test_hash<int>(8),
53             test_equal_to<int>(9),
54             A(10)
55            );
56         C c(a, a + 2,
57             7,
58             test_hash<int>(2),
59             test_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.count(1) == 1);
66         assert(c.count(2) == 1);
67         assert(c.count(3) == 1);
68         assert(c.count(4) == 1);
69         assert(c.hash_function() == test_hash<int>(8));
70         assert(c.key_eq() == test_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<int> A;
80         typedef std::unordered_set<int,
81                                    test_hash<int>,
82                                    test_equal_to<int>,
83                                    A
84                                    > C;
85         typedef int P;
86         P a[] =
87         {
88             P(1),
89             P(2),
90             P(3),
91             P(4),
92             P(1),
93             P(2)
94         };
95         C c0(a, a + sizeof(a)/sizeof(a[0]),
96             7,
97             test_hash<int>(8),
98             test_equal_to<int>(9),
99             A(10)
100            );
101         C c(a, a + 2,
102             7,
103             test_hash<int>(2),
104             test_equal_to<int>(3),
105             A(10)
106            );
107         C::iterator it0 = c0.begin();
108         c = std::move(c0);
109         LIBCPP_ASSERT(c.bucket_count() == 7);
110         assert(c.size() == 4);
111         assert(c.count(1) == 1);
112         assert(c.count(2) == 1);
113         assert(c.count(3) == 1);
114         assert(c.count(4) == 1);
115         assert(c.hash_function() == test_hash<int>(8));
116         assert(c.key_eq() == test_equal_to<int>(9));
117         assert(c.get_allocator() == A(10));
118         assert(!c.empty());
119         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
120         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
121         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
122         assert(c.max_load_factor() == 1);
123         assert(it0 == c.begin()); // Iterators remain valid
124     }
125     {
126         typedef other_allocator<int> A;
127         typedef std::unordered_set<int,
128                                    test_hash<int>,
129                                    test_equal_to<int>,
130                                    A
131                                    > C;
132         typedef int P;
133         P a[] =
134         {
135             P(1),
136             P(2),
137             P(3),
138             P(4),
139             P(1),
140             P(2)
141         };
142         C c0(a, a + sizeof(a)/sizeof(a[0]),
143             7,
144             test_hash<int>(8),
145             test_equal_to<int>(9),
146             A(10)
147            );
148         C c(a, a + 2,
149             7,
150             test_hash<int>(2),
151             test_equal_to<int>(3),
152             A(4)
153            );
154         C::iterator it0 = c0.begin();
155         c = std::move(c0);
156         LIBCPP_ASSERT(c.bucket_count() == 7);
157         assert(c.size() == 4);
158         assert(c.count(1) == 1);
159         assert(c.count(2) == 1);
160         assert(c.count(3) == 1);
161         assert(c.count(4) == 1);
162         assert(c.hash_function() == test_hash<int>(8));
163         assert(c.key_eq() == test_equal_to<int>(9));
164         assert(c.get_allocator() == A(10));
165         assert(!c.empty());
166         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
167         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
168         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
169         assert(c.max_load_factor() == 1);
170         assert(it0 == c.begin()); // Iterators remain valid
171     }
172     {
173         typedef min_allocator<int> A;
174         typedef std::unordered_set<int,
175                                    test_hash<int>,
176                                    test_equal_to<int>,
177                                    A
178                                    > C;
179         typedef int P;
180         P a[] =
181         {
182             P(1),
183             P(2),
184             P(3),
185             P(4),
186             P(1),
187             P(2)
188         };
189         C c0(a, a + sizeof(a)/sizeof(a[0]),
190             7,
191             test_hash<int>(8),
192             test_equal_to<int>(9),
193             A()
194            );
195         C c(a, a + 2,
196             7,
197             test_hash<int>(2),
198             test_equal_to<int>(3),
199             A()
200            );
201         C::iterator it0 = c0.begin();
202         c = std::move(c0);
203         LIBCPP_ASSERT(c.bucket_count() == 7);
204         assert(c.size() == 4);
205         assert(c.count(1) == 1);
206         assert(c.count(2) == 1);
207         assert(c.count(3) == 1);
208         assert(c.count(4) == 1);
209         assert(c.hash_function() == test_hash<int>(8));
210         assert(c.key_eq() == test_equal_to<int>(9));
211         assert(c.get_allocator() == A());
212         assert(!c.empty());
213         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
214         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
215         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
216         assert(c.max_load_factor() == 1);
217         assert(it0 == c.begin()); // Iterators remain valid
218     }
219 
220   return 0;
221 }
222