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 } }
22 #include <unordered_map>
24 #include <testsuite_hooks.h>
26 using test_type
= std::unordered_map
<int, int>;
31 auto operator()(const T
& i
) const noexcept
32 { return ~std::hash
<T
>()(i
); }
39 struct __is_fast_hash
<xhash
<T
>> : __is_fast_hash
<std::hash
<T
>>
43 struct equal
: std::equal_to
<> { };
45 template<typename C1
, typename C2
>
46 bool equal_elements(const C1
& c1
, const C2
& c2
)
48 if (c2
.size() != c1
.size())
51 if (c2
.count(i
.first
) != c1
.count(i
.first
))
59 const test_type c0
{ {1, 10}, {2, 20}, {3, 30} };
60 test_type c1
= c0
, c2
= c0
;
71 c2
.merge(std::move(c1
));
79 const test_type c0
{ {1, 10}, {2, 20}, {3, 30} };
81 std::unordered_map
<int, int, xhash
<int>, equal
> c2( c0
.begin(), c0
.end() );
85 VERIFY( equal_elements(c2
, c0
) );
94 VERIFY( equal_elements(c2
, c0
) );
96 c1
.merge(std::move(c2
));
104 const test_type c0
{ {1, 10}, {2, 20}, {3, 30} };
106 std::unordered_multimap
<int, int, xhash
<int>, equal
> c2( c0
.begin(), c0
.end() );
109 VERIFY( equal_elements(c2
, c0
) );
114 VERIFY( c2
.empty() );
117 VERIFY( c1
.empty() );
118 VERIFY( equal_elements(c2
, c0
) );
122 VERIFY( c1
.empty() );
123 VERIFY( c2
.size() == (2 * c0
.size()) );
124 VERIFY( c2
.count(1) == 2 );
125 VERIFY( c2
.count(2) == 2 );
126 VERIFY( c2
.count(3) == 2 );
130 VERIFY( equal_elements(c2
, c0
) );
132 c1
.merge(std::move(c2
));
134 VERIFY( equal_elements(c2
, c0
) );
137 c1
.merge(std::move(c2
));
139 VERIFY( c2
.empty() );
145 const std::unordered_map
<std::string
, int> c0
146 { {"one", 10}, {"two", 20}, {"three", 30} };
148 std::unordered_map
<std::string
, int> c1
= c0
;
149 std::unordered_multimap
<std::string
, int> c2( c0
.begin(), c0
.end() );
152 VERIFY( equal_elements(c2
, c0
) );
157 VERIFY( c2
.empty() );
160 VERIFY( c1
.empty() );
161 VERIFY( equal_elements(c2
, c0
) );
165 VERIFY( c1
.empty() );
166 VERIFY( c2
.size() == (2 * c0
.size()) );
167 VERIFY( c2
.count("one") == 2 );
168 VERIFY( c2
.count("two") == 2 );
169 VERIFY( c2
.count("three") == 2 );
173 VERIFY( equal_elements(c2
, c0
) );
175 c1
.merge(std::move(c2
));
177 VERIFY( equal_elements(c2
, c0
) );
180 c1
.merge(std::move(c2
));
182 VERIFY( c2
.empty() );
188 const std::unordered_map
<std::string
, int> c0
189 { {"one", 10}, {"two", 20}, {"three", 30} };
191 std::unordered_map
<std::string
, int> c1
= c0
;
192 std::unordered_multimap
<std::string
, int, xhash
<std::string
>, equal
> c2( c0
.begin(), c0
.end() );
195 VERIFY( equal_elements(c2
, c0
) );
200 VERIFY( c2
.empty() );
203 VERIFY( c1
.empty() );
204 VERIFY( equal_elements(c2
, c0
) );
208 VERIFY( c1
.empty() );
209 VERIFY( c2
.size() == (2 * c0
.size()) );
210 VERIFY( c2
.count("one") == 2 );
211 VERIFY( c2
.count("two") == 2 );
212 VERIFY( c2
.count("three") == 2 );
216 VERIFY( equal_elements(c2
, c0
) );
218 c1
.merge(std::move(c2
));
220 VERIFY( equal_elements(c2
, c0
) );
223 c1
.merge(std::move(c2
));
225 VERIFY( c2
.empty() );
230 std::function
<std::size_t(const T
&)>;
233 hash_func(const std::string
& str
)
234 { return std::hash
<std::string
>{}(str
); }
237 xhash_func(const std::string
& str
)
238 { return xhash
<std::string
>{}(str
); }
243 struct __is_fast_hash
<hash_f
<T
>> : __is_fast_hash
<std::hash
<T
>>
250 const std::unordered_map
<std::string
, int, hash_f
<std::string
>, equal
>
251 c0({ {"one", 10}, {"two", 20}, {"three", 30} }, 3, &hash_func
);
253 std::unordered_map
<std::string
, int, hash_f
<std::string
>, equal
>
256 std::unordered_multimap
<std::string
, int, hash_f
<std::string
>, equal
>
257 c2(c0
.begin(), c0
.end(), 3, &xhash_func
);
260 VERIFY( equal_elements(c2
, c0
) );
265 VERIFY( c2
.empty() );
268 VERIFY( c1
.empty() );
269 VERIFY( equal_elements(c2
, c0
) );
273 VERIFY( c1
.empty() );
274 VERIFY( c2
.size() == (2 * c0
.size()) );
275 VERIFY( c2
.count("one") == 2 );
276 VERIFY( c2
.count("two") == 2 );
277 VERIFY( c2
.count("three") == 2 );
281 VERIFY( equal_elements(c2
, c0
) );
283 c1
.merge(std::move(c2
));
285 VERIFY( equal_elements(c2
, c0
) );
288 c1
.merge(std::move(c2
));
290 VERIFY( c2
.empty() );
296 test_type c1
{ {1, 1}, {3, 3}, {5, 5} };
297 test_type c2
{ {2, 2}, {4, 4}, {6, 6} };
298 const test_type c3
= c2
;
301 VERIFY( c1
.size() == 6 );
302 VERIFY( c2
.empty() );
303 const test_type c4
= c1
;
307 c1
.merge(std::move(c2
));
309 VERIFY( c2
.empty() );
311 c2
.merge(std::move(c1
));
312 VERIFY( c1
.empty() );
316 VERIFY( c1
.empty() );
329 VERIFY( c2
.size() == c3
.size() + 1 );
336 test_type c1
{ {1, 1}, {3, 3}, {5, 5} };
337 std::unordered_map
<int, int, xhash
<int>, equal
> c2
{ {2, 2}, {4, 4}, {6, 6} };
341 VERIFY( c1
.size() == 6 );
342 VERIFY( c2
.empty() );
343 const test_type c4
= c1
;
347 c1
.merge(std::move(c2
));
348 VERIFY( equal_elements(c1
, c3
) );
349 VERIFY( c2
.empty() );
351 c2
.merge(std::move(c1
));
352 VERIFY( c1
.empty() );
356 VERIFY( c1
.empty() );
369 VERIFY( c2
.size() == c3
.size() + 1 );
380 auto operator()(const int& i
) const noexcept
381 { return std::hash
<int>()(i
) + seed
; }
384 using map_type
= std::unordered_map
<int, int, stateful_hash
>;
385 map_type
c1({ {1, 1}, {3, 3}, {5, 5} }, 0, stateful_hash
{1});
386 map_type
c2({ {2, 2}, {4, 4}, {6, 6} }, 0, stateful_hash
{2});
390 VERIFY( c1
.size() == 6 );
391 VERIFY( c2
.empty() );
395 c1
.merge(std::move(c2
));
397 VERIFY( c2
.empty() );
399 c2
.merge(std::move(c1
));
400 VERIFY( c1
.empty() );
404 VERIFY( c1
.empty() );
410 test_type c5
{ {-1, 1}, {-3, 3}, {-5, 5} };
412 VERIFY( c2
.size() == 6 );
413 VERIFY( c5
.empty() );
416 VERIFY( c6
.size() == 6 );
417 VERIFY( c2
.size() == 3 );