| 1 | // Copyright (C) 2016-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++17 } } |
| 19 | |
| 20 | #include <unordered_set> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | using test_type = std::unordered_set<int>; |
| 24 | |
| 25 | void |
| 26 | test01() |
| 27 | { |
| 28 | test_type c{ 1, 2, 3 }; |
| 29 | test_type::node_type node; |
| 30 | test_type::insert_return_type ins; |
| 31 | test_type::iterator pos; |
| 32 | |
| 33 | node = c.extract(0); |
| 34 | VERIFY( !node ); |
| 35 | VERIFY( node.empty() ); |
| 36 | VERIFY( c.size() == 3 ); |
| 37 | |
| 38 | ins = c.insert(std::move(node)); |
| 39 | VERIFY( !node ); |
| 40 | VERIFY( node.empty() ); |
| 41 | VERIFY( c.size() == 3 ); |
| 42 | VERIFY( !ins.inserted ); |
| 43 | VERIFY( !ins.node ); |
| 44 | VERIFY( ins.position == c.end() ); |
| 45 | |
| 46 | node = c.extract(1); |
| 47 | VERIFY( (bool)node ); |
| 48 | VERIFY( !node.empty() ); |
| 49 | VERIFY( c.size() == 2 ); |
| 50 | VERIFY( node.get_allocator() == c.get_allocator() ); |
| 51 | VERIFY( node.value() == 1 ); |
| 52 | |
| 53 | node.value() = 4; |
| 54 | VERIFY( node.value() == 4 ); |
| 55 | |
| 56 | ins = c.insert(std::move(node)); |
| 57 | VERIFY( !node ); |
| 58 | VERIFY( node.empty() ); |
| 59 | VERIFY( c.size() == 3 ); |
| 60 | VERIFY( ins.inserted ); |
| 61 | VERIFY( !ins.node ); |
| 62 | VERIFY( ins.position != c.end() ); |
| 63 | VERIFY( *ins.position == 4 ); |
| 64 | VERIFY( c.count(1) == 0 ); |
| 65 | VERIFY( c.count(4) == 1 ); |
| 66 | |
| 67 | pos = c.insert(c.begin(), std::move(node)); |
| 68 | VERIFY( !node ); |
| 69 | VERIFY( node.empty() ); |
| 70 | VERIFY( c.size() == 3 ); |
| 71 | VERIFY( pos == c.end() ); |
| 72 | |
| 73 | pos = c.insert(c.begin(), c.extract(2)); |
| 74 | VERIFY( c.size() == 3 ); |
| 75 | VERIFY( pos != c.end() ); |
| 76 | VERIFY( *pos == 2 ); |
| 77 | |
| 78 | test_type c2 = c; |
| 79 | node = c2.extract(3); |
| 80 | ins = c.insert(std::move(node)); |
| 81 | VERIFY( node.empty() ); |
| 82 | VERIFY( ins.position != c.end() ); |
| 83 | VERIFY( !ins.inserted ); |
| 84 | VERIFY( !ins.node.empty() ); |
| 85 | VERIFY( ins.node.value() == 3 ); |
| 86 | auto hasher = c.hash_function(); |
| 87 | VERIFY( hasher(*ins.position) == hasher(ins.node.value()) ); |
| 88 | auto eq = c.key_eq(); |
| 89 | VERIFY( eq(*ins.position, ins.node.value()) ); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | test02() |
| 94 | { |
| 95 | test_type c{ 1, 2, 3 }; |
| 96 | test_type::node_type node; |
| 97 | test_type::insert_return_type ins; |
| 98 | |
| 99 | const int val = *c.begin(); |
| 100 | node = c.extract(c.begin()); |
| 101 | VERIFY( (bool)node ); |
| 102 | VERIFY( !node.empty() ); |
| 103 | VERIFY( c.size() == 2 ); |
| 104 | VERIFY( node.get_allocator() == c.get_allocator() ); |
| 105 | VERIFY( node.value() == val ); |
| 106 | |
| 107 | ins = c.insert(std::move(node)); |
| 108 | VERIFY( node.empty() ); |
| 109 | VERIFY( c.size() == 3 ); |
| 110 | VERIFY( ins.inserted ); |
| 111 | VERIFY( !ins.node ); |
| 112 | VERIFY( ins.position != c.end() ); |
| 113 | VERIFY( *ins.position == val ); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | test03() |
| 118 | { |
| 119 | struct hash : std::hash<int> { }; |
| 120 | struct equal : std::equal_to<int> { }; |
| 121 | using std::is_same_v; |
| 122 | using compat_type1 = std::unordered_set<int, hash, equal>; |
| 123 | static_assert( is_same_v<test_type::node_type, compat_type1::node_type> ); |
| 124 | using compat_type2 = std::unordered_multiset<int>; |
| 125 | static_assert( is_same_v<test_type::node_type, compat_type2::node_type> ); |
| 126 | using compat_type3 = std::unordered_multiset<int, hash, equal>; |
| 127 | static_assert( is_same_v<test_type::node_type, compat_type3::node_type> ); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | test04() |
| 132 | { |
| 133 | // Check order of members in insert_return_type |
| 134 | auto [pos, ins, node] = test_type::insert_return_type{}; |
| 135 | using std::is_same_v; |
| 136 | static_assert( is_same_v<test_type::iterator, decltype(pos)> ); |
| 137 | static_assert( is_same_v<bool, decltype(ins)> ); |
| 138 | static_assert( is_same_v<test_type::node_type, decltype(node)> ); |
| 139 | } |
| 140 | |
| 141 | int |
| 142 | main() |
| 143 | { |
| 144 | test01(); |
| 145 | test02(); |
| 146 | test03(); |
| 147 | } |