| 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_map> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | using test_type = std::unordered_multimap<int, int>; |
| 24 | |
| 25 | void |
| 26 | test01() |
| 27 | { |
| 28 | test_type c{ {1, 10}, { 1, 11 }, {2, 20}, { 2, 21}, {3, 30}, { 3, 31 } }; |
| 29 | test_type::node_type node; |
| 30 | test_type::iterator pos; |
| 31 | |
| 32 | node = c.extract(0); |
| 33 | VERIFY( !node ); |
| 34 | VERIFY( node.empty() ); |
| 35 | VERIFY( c.size() == 6 ); |
| 36 | |
| 37 | pos = c.insert(std::move(node)); |
| 38 | VERIFY( !node ); |
| 39 | VERIFY( node.empty() ); |
| 40 | VERIFY( c.size() == 6 ); |
| 41 | VERIFY( pos == c.end() ); |
| 42 | |
| 43 | node = c.extract(1); |
| 44 | VERIFY( (bool)node ); |
| 45 | VERIFY( !node.empty() ); |
| 46 | VERIFY( c.size() == 5 ); |
| 47 | VERIFY( node.get_allocator() == c.get_allocator() ); |
| 48 | VERIFY( node.key() == 1 ); |
| 49 | int mapped = node.mapped(); |
| 50 | VERIFY( mapped == 10 || mapped == 11 ); |
| 51 | |
| 52 | node.key() = 4; |
| 53 | node.mapped() = 40; |
| 54 | VERIFY( node.key() == 4 ); |
| 55 | VERIFY( node.mapped() == 40 ); |
| 56 | |
| 57 | pos = c.insert(std::move(node)); |
| 58 | VERIFY( !node ); |
| 59 | VERIFY( node.empty() ); |
| 60 | VERIFY( c.size() == 6 ); |
| 61 | VERIFY( pos != c.end() ); |
| 62 | VERIFY( pos->first == 4 ); |
| 63 | VERIFY( pos->second == 40 ); |
| 64 | |
| 65 | pos = c.insert(c.begin(), std::move(node)); |
| 66 | VERIFY( !node ); |
| 67 | VERIFY( node.empty() ); |
| 68 | VERIFY( c.size() == 6 ); |
| 69 | VERIFY( pos == c.end() ); |
| 70 | |
| 71 | node = c.extract(1); |
| 72 | mapped = node.mapped(); |
| 73 | pos = c.insert(c.begin(), std::move(node)); |
| 74 | VERIFY( !node ); |
| 75 | VERIFY( c.size() == 6 ); |
| 76 | VERIFY( pos != c.end() ); |
| 77 | VERIFY( pos->first == 1 ); |
| 78 | VERIFY( pos->second == mapped ); |
| 79 | |
| 80 | test_type c2 = c; |
| 81 | node = c2.extract(1); |
| 82 | mapped = node.mapped(); |
| 83 | pos = c.insert(std::move(node)); |
| 84 | VERIFY( pos != c.end() ); |
| 85 | VERIFY( node.empty() ); |
| 86 | VERIFY( pos->first == 1 ); |
| 87 | VERIFY( pos->second == mapped ); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | test02() |
| 92 | { |
| 93 | test_type c{ {1, 10}, { 1, 11 }, {2, 20}, { 2, 21}, {3, 30}, { 3, 31 } }; |
| 94 | test_type::node_type node; |
| 95 | test_type::iterator pos; |
| 96 | |
| 97 | const int key = c.begin()->first; |
| 98 | const int mapped = c.begin()->second; |
| 99 | node = c.extract(c.begin()); |
| 100 | VERIFY( !node.empty() ); |
| 101 | VERIFY( c.size() == 5 ); |
| 102 | VERIFY( node.get_allocator() == c.get_allocator() ); |
| 103 | VERIFY( node.key() == key ); |
| 104 | VERIFY( node.mapped() == mapped ); |
| 105 | |
| 106 | pos = c.insert(std::move(node)); |
| 107 | VERIFY( node.empty() ); |
| 108 | VERIFY( c.size() == 6 ); |
| 109 | VERIFY( pos != c.end() ); |
| 110 | VERIFY( pos->first == key ); |
| 111 | VERIFY( pos->second == mapped ); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | test03() |
| 116 | { |
| 117 | struct hash : std::hash<int> { }; |
| 118 | struct equal : std::equal_to<int> { }; |
| 119 | using std::is_same_v; |
| 120 | using compat_type1 = std::unordered_multimap<int, int, hash, equal>; |
| 121 | static_assert( is_same_v<test_type::node_type, compat_type1::node_type> ); |
| 122 | using compat_type2 = std::unordered_map<int, int>; |
| 123 | static_assert( is_same_v<test_type::node_type, compat_type2::node_type> ); |
| 124 | using compat_type3 = std::unordered_map<int, int, hash, equal>; |
| 125 | static_assert( is_same_v<test_type::node_type, compat_type3::node_type> ); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | main() |
| 130 | { |
| 131 | test01(); |
| 132 | test02(); |
| 133 | test03(); |
| 134 | } |