| 1 |
// Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 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) |
| 7 |
// any later version. |
| 8 |
// |
| 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. |
| 13 |
// |
| 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/>. |
| 17 |
// |
| 18 |
|
| 19 |
// { dg-do run { target c++11 } } |
| 20 |
|
| 21 |
#include <set> |
| 22 |
#include <unordered_map> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
struct A |
| 26 |
{ |
| 27 |
int x; |
| 28 |
static std::set<const A*> destroyed; |
| 29 |
|
| 30 |
A() |
| 31 |
{ destroyed.erase(this); } |
| 32 |
|
| 33 |
A(const A& a) |
| 34 |
: x(a.x) |
| 35 |
{ destroyed.erase(this); } |
| 36 |
|
| 37 |
~A() |
| 38 |
{ destroyed.insert(this); } |
| 39 |
|
| 40 |
bool |
| 41 |
operator==(const A& other) const |
| 42 |
{ |
| 43 |
VERIFY( destroyed.find(this) == destroyed.end() ); |
| 44 |
VERIFY( destroyed.find(&other) == destroyed.end() ); |
| 45 |
return x == other.x; |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
std::set<const A*> A::destroyed; |
| 50 |
|
| 51 |
struct hasher |
| 52 |
{ |
| 53 |
std::size_t operator()(const A& a) const |
| 54 |
{ |
| 55 |
VERIFY( A::destroyed.find(&a) == A::destroyed.end() ); |
| 56 |
return a.x / 10; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
void test01() |
| 61 |
{ |
| 62 |
typedef std::unordered_multimap<A, A, hasher> UMMap; |
| 63 |
UMMap map; |
| 64 |
|
| 65 |
A::destroyed.clear(); |
| 66 |
A a; |
| 67 |
a.x = 0; |
| 68 |
map.insert({a, a}); |
| 69 |
map.insert({a, a}); |
| 70 |
VERIFY( map.size() == 2 ); |
| 71 |
std::size_t bkt = map.bucket(a); |
| 72 |
VERIFY( map.bucket_size(bkt) == 2 ); |
| 73 |
|
| 74 |
VERIFY( map.erase( map.begin(bkt)->first ) == 2 ); |
| 75 |
} |
| 76 |
|
| 77 |
void test02() |
| 78 |
{ |
| 79 |
typedef std::unordered_multimap<A, A, hasher> UMMap; |
| 80 |
UMMap map; |
| 81 |
|
| 82 |
A::destroyed.clear(); |
| 83 |
A a; |
| 84 |
a.x = 0; |
| 85 |
map.insert({a, a}); |
| 86 |
map.insert({a, a}); |
| 87 |
VERIFY( map.size() == 2 ); |
| 88 |
std::size_t bkt = map.bucket(a); |
| 89 |
VERIFY( map.bucket_size(bkt) == 2 ); |
| 90 |
|
| 91 |
VERIFY( map.erase( map.begin(bkt)->second ) == 2 ); |
| 92 |
} |
| 93 |
|
| 94 |
int main() |
| 95 |
{ |
| 96 |
test01(); |
| 97 |
test02(); |
| 98 |
return 0; |
| 99 |
} |