| 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 <set> |
| 21 | #include <algorithm> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | using test_type = std::set<int>; |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | test_type c{ 1, 2, 3 }; |
| 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( node.get_allocator() == c.get_allocator() ); |
| 52 | VERIFY( node.value() == 1 ); |
| 53 | |
| 54 | node.value() = 4; |
| 55 | VERIFY( node.value() == 4 ); |
| 56 | |
| 57 | ins = c.insert(std::move(node)); |
| 58 | VERIFY( !node ); |
| 59 | VERIFY( node.empty() ); |
| 60 | VERIFY( c.size() == 3 ); |
| 61 | VERIFY( ins.inserted ); |
| 62 | VERIFY( !ins.node ); |
| 63 | VERIFY( ins.position != c.end() ); |
| 64 | VERIFY( *ins.position == 4 ); |
| 65 | VERIFY( c.count(1) == 0 ); |
| 66 | VERIFY( c.count(4) == 1 ); |
| 67 | VERIFY( std::is_sorted(c.begin(), c.end()) ); |
| 68 | |
| 69 | pos = c.insert(c.begin(), std::move(node)); |
| 70 | VERIFY( !node ); |
| 71 | VERIFY( node.empty() ); |
| 72 | VERIFY( c.size() == 3 ); |
| 73 | VERIFY( pos == c.end() ); |
| 74 | |
| 75 | node = c.extract(2); |
| 76 | pos = c.insert(c.begin(), std::move(node)); |
| 77 | VERIFY( c.size() == 3 ); |
| 78 | VERIFY( pos != c.end() ); |
| 79 | VERIFY( *pos == 2 ); |
| 80 | |
| 81 | test_type c2 = c; |
| 82 | node = c2.extract(3); |
| 83 | ins = c.insert(std::move(node)); |
| 84 | VERIFY( node.empty() ); |
| 85 | VERIFY( ins.position != c.end() ); |
| 86 | VERIFY( !ins.inserted ); |
| 87 | VERIFY( !ins.node.empty() ); |
| 88 | VERIFY( ins.node.value() == 3 ); |
| 89 | VERIFY( *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 | node = c.extract(c.begin()); |
| 100 | VERIFY( (bool)node ); |
| 101 | VERIFY( !node.empty() ); |
| 102 | VERIFY( c.size() == 2 ); |
| 103 | VERIFY( node.get_allocator() == c.get_allocator() ); |
| 104 | VERIFY( node.value() == 1 ); |
| 105 | |
| 106 | ins = c.insert(std::move(node)); |
| 107 | VERIFY( node.empty() ); |
| 108 | VERIFY( c.size() == 3 ); |
| 109 | VERIFY( ins.inserted ); |
| 110 | VERIFY( !ins.node ); |
| 111 | VERIFY( ins.position != c.end() ); |
| 112 | VERIFY( *ins.position == 1 ); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | test03() |
| 117 | { |
| 118 | struct less : std::less<int> { }; |
| 119 | using std::is_same_v; |
| 120 | using compat_type1 = std::set<int, less>; |
| 121 | static_assert( is_same_v<test_type::node_type, compat_type1::node_type> ); |
| 122 | using compat_type2 = std::multiset<int>; |
| 123 | static_assert( is_same_v<test_type::node_type, compat_type2::node_type> ); |
| 124 | using compat_type3 = std::multiset<int, less>; |
| 125 | static_assert( is_same_v<test_type::node_type, compat_type3::node_type> ); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | test04() |
| 130 | { |
| 131 | // Check order of members in insert_return_type |
| 132 | auto [pos, ins, node] = test_type::insert_return_type{}; |
| 133 | using std::is_same_v; |
| 134 | static_assert( is_same_v<test_type::iterator, decltype(pos)> ); |
| 135 | static_assert( is_same_v<bool, decltype(ins)> ); |
| 136 | static_assert( is_same_v<test_type::node_type, decltype(node)> ); |
| 137 | } |
| 138 | |
| 139 | int |
| 140 | main() |
| 141 | { |
| 142 | test01(); |
| 143 | test02(); |
| 144 | test03(); |
| 145 | } |