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 | // <unordered_set> |
10 | |
11 | // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>, |
12 | // class Alloc = allocator<Value>> |
13 | // class unordered_set |
14 | |
15 | // unordered_set& operator=(const unordered_set& u); |
16 | |
17 | #include <unordered_set> |
18 | #include <algorithm> |
19 | #include <cassert> |
20 | #include <cfloat> |
21 | #include <cmath> |
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(int, char**) |
31 | { |
32 | { |
33 | typedef test_allocator<int> A; |
34 | typedef std::unordered_set<int, |
35 | test_hash<int>, |
36 | test_equal_to<int>, |
37 | A |
38 | > C; |
39 | typedef int P; |
40 | P a[] = |
41 | { |
42 | P(1), |
43 | P(2), |
44 | P(3), |
45 | P(4), |
46 | P(1), |
47 | P(2) |
48 | }; |
49 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
50 | 7, |
51 | test_hash<int>(8), |
52 | test_equal_to<int>(9), |
53 | A(10) |
54 | ); |
55 | C c(a, a + 2, |
56 | 7, |
57 | test_hash<int>(2), |
58 | test_equal_to<int>(3), |
59 | A(4) |
60 | ); |
61 | c = c0; |
62 | LIBCPP_ASSERT(c.bucket_count() == 7); |
63 | assert(c.size() == 4); |
64 | assert(c.count(1) == 1); |
65 | assert(c.count(2) == 1); |
66 | assert(c.count(3) == 1); |
67 | assert(c.count(4) == 1); |
68 | assert(c.hash_function() == test_hash<int>(8)); |
69 | assert(c.key_eq() == test_equal_to<int>(9)); |
70 | assert(c.get_allocator() == A(4)); |
71 | assert(!c.empty()); |
72 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
73 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
74 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
75 | assert(c.max_load_factor() == 1); |
76 | } |
77 | { |
78 | typedef std::unordered_set<int> C; |
79 | typedef int P; |
80 | P a[] = |
81 | { |
82 | P(1), |
83 | P(2), |
84 | P(3), |
85 | P(4), |
86 | P(1), |
87 | P(2) |
88 | }; |
89 | C c(a, a + sizeof(a)/sizeof(a[0])); |
90 | C *p = &c; |
91 | c = *p; |
92 | assert(c.size() == 4); |
93 | assert(std::is_permutation(c.begin(), c.end(), a)); |
94 | } |
95 | { |
96 | typedef other_allocator<int> A; |
97 | typedef std::unordered_set<int, |
98 | test_hash<int>, |
99 | test_equal_to<int>, |
100 | A |
101 | > C; |
102 | typedef int P; |
103 | P a[] = |
104 | { |
105 | P(1), |
106 | P(2), |
107 | P(3), |
108 | P(4), |
109 | P(1), |
110 | P(2) |
111 | }; |
112 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
113 | 7, |
114 | test_hash<int>(8), |
115 | test_equal_to<int>(9), |
116 | A(10) |
117 | ); |
118 | C c(a, a + 2, |
119 | 7, |
120 | test_hash<int>(2), |
121 | test_equal_to<int>(3), |
122 | A(4) |
123 | ); |
124 | c = c0; |
125 | assert(c.bucket_count() >= 5); |
126 | assert(c.size() == 4); |
127 | assert(c.count(1) == 1); |
128 | assert(c.count(2) == 1); |
129 | assert(c.count(3) == 1); |
130 | assert(c.count(4) == 1); |
131 | assert(c.hash_function() == test_hash<int>(8)); |
132 | assert(c.key_eq() == test_equal_to<int>(9)); |
133 | assert(c.get_allocator() == A(10)); |
134 | assert(!c.empty()); |
135 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
136 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
137 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
138 | assert(c.max_load_factor() == 1); |
139 | } |
140 | #if TEST_STD_VER >= 11 |
141 | { |
142 | typedef min_allocator<int> A; |
143 | typedef std::unordered_set<int, |
144 | test_hash<int>, |
145 | test_equal_to<int>, |
146 | A |
147 | > C; |
148 | typedef int P; |
149 | P a[] = |
150 | { |
151 | P(1), |
152 | P(2), |
153 | P(3), |
154 | P(4), |
155 | P(1), |
156 | P(2) |
157 | }; |
158 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
159 | 7, |
160 | test_hash<int>(8), |
161 | test_equal_to<int>(9), |
162 | A() |
163 | ); |
164 | C c(a, a + 2, |
165 | 7, |
166 | test_hash<int>(2), |
167 | test_equal_to<int>(3), |
168 | A() |
169 | ); |
170 | c = c0; |
171 | LIBCPP_ASSERT(c.bucket_count() == 7); |
172 | assert(c.size() == 4); |
173 | assert(c.count(1) == 1); |
174 | assert(c.count(2) == 1); |
175 | assert(c.count(3) == 1); |
176 | assert(c.count(4) == 1); |
177 | assert(c.hash_function() == test_hash<int>(8)); |
178 | assert(c.key_eq() == test_equal_to<int>(9)); |
179 | assert(c.get_allocator() == A()); |
180 | assert(!c.empty()); |
181 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
182 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
183 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
184 | assert(c.max_load_factor() == 1); |
185 | } |
186 | #endif |
187 | |
188 | return 0; |
189 | } |
190 | |