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