| 1 | // Copyright (C) 2020-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 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <unordered_set> |
| 21 | |
| 22 | #include <testsuite_allocator.h> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | int moves = 0; |
| 26 | |
| 27 | struct X |
| 28 | { |
| 29 | X() = default; |
| 30 | X(const X&) = default; |
| 31 | X(int i) : _i(i) {} |
| 32 | |
| 33 | // Move constructor might throw |
| 34 | X(X&& x) noexcept(false) |
| 35 | { |
| 36 | this->_i = x._i; |
| 37 | x._i = -1; |
| 38 | ++moves; |
| 39 | } |
| 40 | |
| 41 | int _i; |
| 42 | }; |
| 43 | |
| 44 | struct XHasher |
| 45 | { |
| 46 | std::size_t |
| 47 | operator()(const X& x) const noexcept |
| 48 | { return x._i; } |
| 49 | }; |
| 50 | |
| 51 | struct XEqualTo |
| 52 | { |
| 53 | bool |
| 54 | operator()(const X& lhs, const X& rhs) const noexcept |
| 55 | { return lhs._i == rhs._i; } |
| 56 | }; |
| 57 | |
| 58 | void |
| 59 | test01() |
| 60 | { |
| 61 | using A = __gnu_test::propagating_allocator<X, false>; |
| 62 | A a1(1), a2(2); |
| 63 | std::unordered_set<X, XHasher, XEqualTo, A> u1(a1), u2(a2); |
| 64 | u1 = { X(0), X(1), X(2) }; |
| 65 | u2 = { X(3), X(4), X(5) }; |
| 66 | |
| 67 | moves = 0; |
| 68 | u1 = std::move(u2); |
| 69 | |
| 70 | VERIFY( moves == 3 ); |
| 71 | VERIFY( u1.count(X(1)) == 0 ); |
| 72 | VERIFY( u1.count(X(3)) == 1 ); |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | main() |
| 77 | { |
| 78 | test01(); |
| 79 | } |