1 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++17 } }
20 #include <unordered_set>
21 #include <testsuite_hooks.h>
23 using test_type
= std::unordered_set
<int>;
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
;
35 VERIFY( node
.empty() );
36 VERIFY( c
.size() == 3 );
38 ins
= c
.insert(std::move(node
));
40 VERIFY( node
.empty() );
41 VERIFY( c
.size() == 3 );
42 VERIFY( !ins
.inserted
);
44 VERIFY( ins
.position
== c
.end() );
48 VERIFY( !node
.empty() );
49 VERIFY( c
.size() == 2 );
50 VERIFY( node
.get_allocator() == c
.get_allocator() );
51 VERIFY( node
.value() == 1 );
54 VERIFY( node
.value() == 4 );
56 ins
= c
.insert(std::move(node
));
58 VERIFY( node
.empty() );
59 VERIFY( c
.size() == 3 );
60 VERIFY( ins
.inserted
);
62 VERIFY( ins
.position
!= c
.end() );
63 VERIFY( *ins
.position
== 4 );
64 VERIFY( c
.count(1) == 0 );
65 VERIFY( c
.count(4) == 1 );
67 pos
= c
.insert(c
.begin(), std::move(node
));
69 VERIFY( node
.empty() );
70 VERIFY( c
.size() == 3 );
71 VERIFY( pos
== c
.end() );
73 pos
= c
.insert(c
.begin(), c
.extract(2));
74 VERIFY( c
.size() == 3 );
75 VERIFY( pos
!= c
.end() );
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()) );
89 VERIFY( eq(*ins
.position
, ins
.node
.value()) );
95 test_type c
{ 1, 2, 3 };
96 test_type::node_type node
;
97 test_type::insert_return_type ins
;
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
);
107 ins
= c
.insert(std::move(node
));
108 VERIFY( node
.empty() );
109 VERIFY( c
.size() == 3 );
110 VERIFY( ins
.inserted
);
112 VERIFY( ins
.position
!= c
.end() );
113 VERIFY( *ins
.position
== val
);
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
> );
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
)> );