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_multiset
14 
15 // unordered_multiset& operator=(const unordered_multiset& 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 "../../../check_consecutive.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_multiset<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 = c0;
63         LIBCPP_ASSERT(c.bucket_count() == 7);
64         assert(c.size() == 6);
65         CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2);
66         CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2);
67         CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1);
68         CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 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 std::unordered_multiset<int> C;
80         typedef int P;
81         P a[] =
82         {
83             P(1),
84             P(2),
85             P(3),
86             P(4),
87             P(1),
88             P(2)
89         };
90         C c(a, a + sizeof(a)/sizeof(a[0]));
91         C *p = &c;
92         c = *p;
93 
94         assert(c.size() == 6);
95         assert(std::is_permutation(c.begin(), c.end(), a));
96     }
97     {
98         typedef other_allocator<int> A;
99         typedef std::unordered_multiset<int,
100                                    test_hash<int>,
101                                    test_equal_to<int>,
102                                    A
103                                    > C;
104         typedef int P;
105         P a[] =
106         {
107             P(1),
108             P(2),
109             P(3),
110             P(4),
111             P(1),
112             P(2)
113         };
114         C c0(a, a + sizeof(a)/sizeof(a[0]),
115             7,
116             test_hash<int>(8),
117             test_equal_to<int>(9),
118             A(10)
119            );
120         C c(a, a + 2,
121             7,
122             test_hash<int>(2),
123             test_equal_to<int>(3),
124             A(4)
125            );
126         c = c0;
127         assert(c.bucket_count() >= 7);
128         assert(c.size() == 6);
129         CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2);
130         CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2);
131         CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1);
132         CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 4, 1);
133         assert(c.hash_function() == test_hash<int>(8));
134         assert(c.key_eq() == test_equal_to<int>(9));
135         assert(c.get_allocator() == A(10));
136         assert(!c.empty());
137         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
138         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
139         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
140         assert(c.max_load_factor() == 1);
141     }
142 #if TEST_STD_VER >= 11
143     {
144         typedef min_allocator<int> A;
145         typedef std::unordered_multiset<int,
146                                    test_hash<int>,
147                                    test_equal_to<int>,
148                                    A
149                                    > C;
150         typedef int P;
151         P a[] =
152         {
153             P(1),
154             P(2),
155             P(3),
156             P(4),
157             P(1),
158             P(2)
159         };
160         C c0(a, a + sizeof(a)/sizeof(a[0]),
161             7,
162             test_hash<int>(8),
163             test_equal_to<int>(9),
164             A()
165            );
166         C c(a, a + 2,
167             7,
168             test_hash<int>(2),
169             test_equal_to<int>(3),
170             A()
171            );
172         c = c0;
173         LIBCPP_ASSERT(c.bucket_count() == 7);
174         assert(c.size() == 6);
175         CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2);
176         CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2);
177         CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1);
178         CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 4, 1);
179         assert(c.hash_function() == test_hash<int>(8));
180         assert(c.key_eq() == test_equal_to<int>(9));
181         assert(c.get_allocator() == A());
182         assert(!c.empty());
183         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
184         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
185         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
186         assert(c.max_load_factor() == 1);
187     }
188 #endif
189 
190   return 0;
191 }
192