1 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++17 } }
21 #include <unordered_set>
23 #include <testsuite_hooks.h>
25 using test_type
= std::unordered_set
<int>;
28 auto operator()(int i
) const noexcept
{ return ~std::hash
<int>()(i
); }
30 struct equal
: std::equal_to
<> { };
32 template<typename C1
, typename C2
>
33 bool equal_elements(const C1
& c1
, const C2
& c2
)
35 if (c2
.size() != c1
.size())
38 if (c2
.count(i
) != c1
.count(i
))
46 const test_type c0
{ 1, 2, 3, };
47 test_type c1
= c0
, c2
= c0
;
58 c2
.merge(std::move(c1
));
66 const test_type c0
{ 1, 2, 3, };
68 std::unordered_set
<int, hash
, equal
> c2( c0
.begin(), c0
.end() );
72 VERIFY( equal_elements(c2
, c0
) );
81 VERIFY( equal_elements(c2
, c0
) );
83 c1
.merge(std::move(c2
));
91 const test_type c0
{ 1, 2, 3, };
93 std::unordered_multiset
<int, hash
, equal
> c2( c0
.begin(), c0
.end() );
96 VERIFY( equal_elements(c2
, c0
) );
101 VERIFY( c2
.empty() );
104 VERIFY( c1
.empty() );
105 VERIFY( equal_elements(c2
, c0
) );
109 VERIFY( c1
.empty() );
110 VERIFY( c2
.size() == (2 * c0
.size()) );
111 VERIFY( c2
.count(1) == 2 );
112 VERIFY( c2
.count(2) == 2 );
113 VERIFY( c2
.count(3) == 2 );
117 VERIFY( equal_elements(c2
, c0
) );
119 c1
.merge(std::move(c2
));
121 VERIFY( equal_elements(c2
, c0
) );
124 c1
.merge(std::move(c2
));
126 VERIFY( c2
.empty() );
132 const std::unordered_set
<std::string
> c0
{ "abcd", "efgh", "ijkl", };
133 std::unordered_set
<std::string
> c1
= c0
;
134 std::unordered_multiset
<std::string
> c2( c0
.begin(), c0
.end() );
137 VERIFY( equal_elements(c2
, c0
) );
142 VERIFY( c2
.empty() );
145 VERIFY( c1
.empty() );
146 VERIFY( equal_elements(c2
, c0
) );
150 VERIFY( c1
.empty() );
151 VERIFY( c2
.size() == (2 * c0
.size()) );
152 VERIFY( c2
.count("abcd") == 2 );
153 VERIFY( c2
.count("efgh") == 2 );
154 VERIFY( c2
.count("ijkl") == 2 );
158 VERIFY( equal_elements(c2
, c0
) );
160 c1
.merge(std::move(c2
));
162 VERIFY( equal_elements(c2
, c0
) );
165 c1
.merge(std::move(c2
));
167 VERIFY( c2
.empty() );
173 test_type c1
{ 1, 3, 5 };
174 test_type c2
{ 2, 4, 6 };
175 const test_type c3
= c2
;
178 VERIFY( c1
.size() == 6 );
179 VERIFY( c2
.empty() );
183 c1
.merge(std::move(c2
));
185 VERIFY( c2
.empty() );
187 c2
.merge(std::move(c1
));
188 VERIFY( c1
.empty() );
192 VERIFY( c1
.empty() );
205 VERIFY( c2
.size() == c3
.size() + 1 );
212 test_type c1
{ 1, 3, 5 };
213 std::unordered_set
<int, hash
, equal
> c2
{ 2, 4, 6 };
217 VERIFY( c1
.size() == 6 );
218 VERIFY( c2
.empty() );
222 c1
.merge(std::move(c2
));
223 VERIFY( equal_elements(c1
, c3
) );
224 VERIFY( c2
.empty() );
226 c2
.merge(std::move(c1
));
227 VERIFY( c1
.empty() );
231 VERIFY( c1
.empty() );
244 VERIFY( c2
.size() == c3
.size() + 1 );
255 auto operator()(const int& i
) const noexcept
256 { return std::hash
<int>()(i
) + seed
; }
259 using set_type
= std::unordered_set
<int, stateful_hash
>;
260 set_type
c1({ 1, 3, 5 }, 0, stateful_hash
{1});
261 set_type
c2({ 2, 4, 6 }, 0, stateful_hash
{2});
265 VERIFY( c1
.size() == 6 );
266 VERIFY( c2
.empty() );
270 c1
.merge(std::move(c2
));
272 VERIFY( c2
.empty() );
274 c2
.merge(std::move(c1
));
275 VERIFY( c1
.empty() );
279 VERIFY( c1
.empty() );
285 test_type c4
{ -1, -3, -5 };
287 VERIFY( c2
.size() == 6 );
288 VERIFY( c4
.empty() );
291 VERIFY( c6
.size() == 6 );
292 VERIFY( c2
.size() == 3 );