xref: /llvm-project/libcxx/test/std/containers/unord/unord.multiset/eq.pass.cpp (revision 8bbcc6d548e86c6fbd621ef138aefddeb676e367)
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 Key, class Hash, class Pred, class Alloc>
12 // bool
13 // operator==(const unordered_multiset<Key, Hash, Pred, Alloc>& x,
14 //            const unordered_multiset<Key, Hash, Pred, Alloc>& y);
15 //
16 // template <class Key, class Hash, class Pred, class Alloc>
17 // bool
18 // operator!=(const unordered_multiset<Key, Hash, Pred, Alloc>& x,
19 //            const unordered_multiset<Key, Hash, Pred, Alloc>& y);
20 
21 #include <unordered_set>
22 #include <cassert>
23 #include <cstddef>
24 
25 #include "test_macros.h"
26 #include "min_allocator.h"
27 
28 #include "test_comparisons.h"
29 
main(int,char **)30 int main(int, char**)
31 {
32     {
33         typedef std::unordered_multiset<int> C;
34         typedef int P;
35         P a[] =
36         {
37             P(10),
38             P(20),
39             P(20),
40             P(30),
41             P(40),
42             P(50),
43             P(50),
44             P(50),
45             P(60),
46             P(70),
47             P(80)
48         };
49         const C c1(std::begin(a), std::end(a));
50         const C c2;
51         assert(!(c1 == c2));
52         assert( (c1 != c2));
53     }
54     {
55         typedef std::unordered_multiset<int> C;
56         typedef int P;
57         P a[] =
58         {
59             P(10),
60             P(20),
61             P(20),
62             P(30),
63             P(40),
64             P(50),
65             P(50),
66             P(50),
67             P(60),
68             P(70),
69             P(80)
70         };
71         const C c1(std::begin(a), std::end(a));
72         const C c2 = c1;
73         assert( (c1 == c2));
74         assert(!(c1 != c2));
75     }
76     {
77         typedef std::unordered_multiset<int> C;
78         typedef int P;
79         P a[] =
80         {
81             P(10),
82             P(20),
83             P(20),
84             P(30),
85             P(40),
86             P(50),
87             P(50),
88             P(50),
89             P(60),
90             P(70),
91             P(80)
92         };
93         C c1(std::begin(a), std::end(a));
94         C c2 = c1;
95         c2.rehash(30);
96         assert( (c1 == c2));
97         assert(!(c1 != c2));
98         c2.insert(P(90));
99         assert(!(c1 == c2));
100         assert( (c1 != c2));
101         c1.insert(P(90));
102         assert( (c1 == c2));
103         assert(!(c1 != c2));
104     }
105 #if TEST_STD_VER >= 11
106     {
107         typedef std::unordered_multiset<int, std::hash<int>,
108                                       std::equal_to<int>, min_allocator<int>> C;
109         typedef int P;
110         P a[] =
111         {
112             P(10),
113             P(20),
114             P(20),
115             P(30),
116             P(40),
117             P(50),
118             P(50),
119             P(50),
120             P(60),
121             P(70),
122             P(80)
123         };
124         const C c1(std::begin(a), std::end(a));
125         const C c2;
126         assert(!(c1 == c2));
127         assert( (c1 != c2));
128     }
129     {
130         typedef std::unordered_multiset<int, std::hash<int>,
131                                       std::equal_to<int>, min_allocator<int>> C;
132         typedef int P;
133         P a[] =
134         {
135             P(10),
136             P(20),
137             P(20),
138             P(30),
139             P(40),
140             P(50),
141             P(50),
142             P(50),
143             P(60),
144             P(70),
145             P(80)
146         };
147         const C c1(std::begin(a), std::end(a));
148         const C c2 = c1;
149         assert( (c1 == c2));
150         assert(!(c1 != c2));
151     }
152     {
153         typedef std::unordered_multiset<int, std::hash<int>,
154                                       std::equal_to<int>, min_allocator<int>> C;
155         typedef int P;
156         P a[] =
157         {
158             P(10),
159             P(20),
160             P(20),
161             P(30),
162             P(40),
163             P(50),
164             P(50),
165             P(50),
166             P(60),
167             P(70),
168             P(80)
169         };
170         C c1(std::begin(a), std::end(a));
171         C c2 = c1;
172         c2.rehash(30);
173         assert( (c1 == c2));
174         assert(!(c1 != c2));
175         c2.insert(P(90));
176         assert(!(c1 == c2));
177         assert( (c1 != c2));
178         c1.insert(P(90));
179         assert( (c1 == c2));
180         assert(!(c1 != c2));
181     }
182 #endif
183 
184     // Make sure we take into account the number of times that a key repeats into equality.
185     {
186         int a[] = {1, 1, 1, 2};
187         int b[] = {1, 1, 1, 1, 2};
188 
189         std::unordered_multiset<int> c1(std::begin(a), std::end(a));
190         std::unordered_multiset<int> c2(std::begin(b), std::end(b));
191         assert(testEquality(c1, c2, false));
192     }
193 
194     // Make sure we behave properly when a custom key predicate is provided.
195     {
196         int a[] = {1, 3};
197         int b[] = {1, 1};
198         // A very poor hash
199         struct HashModuloOddness {
200             std::size_t operator()(int x) const { return std::hash<int>()(x % 2); }
201         };
202         // A very poor hash
203         struct CompareModuloOddness {
204             bool operator()(int x, int y) const { return (x % 2) == (y % 2); }
205         };
206 
207         using Set = std::unordered_multiset<int, HashModuloOddness, CompareModuloOddness>;
208         Set c1(std::begin(a), std::end(a));
209         Set c2(std::begin(b), std::end(b));
210 
211         assert(testEquality(c1, c2, false));
212     }
213 
214     return 0;
215 }
216