1 // Copyright (C) 2016-2017 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-options "-std=gnu++17" }
19 // { dg-skip-if "" { *-*-* } { "-D_GLIBCXX_PROFILE" } }
21 #include <unordered_set>
22 #include <testsuite_hooks.h>
24 using test_type
= std::unordered_set
<int>;
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
;
36 VERIFY( node
.empty() );
37 VERIFY( c
.size() == 3 );
39 ins
= c
.insert(std::move(node
));
41 VERIFY( node
.empty() );
42 VERIFY( c
.size() == 3 );
43 VERIFY( !ins
.inserted
);
45 VERIFY( ins
.position
== c
.end() );
49 VERIFY( !node
.empty() );
50 VERIFY( c
.size() == 2 );
51 VERIFY( node
.get_allocator() == c
.get_allocator() );
52 VERIFY( node
.value() == 1 );
55 VERIFY( node
.value() == 4 );
57 ins
= c
.insert(std::move(node
));
59 VERIFY( node
.empty() );
60 VERIFY( c
.size() == 3 );
61 VERIFY( ins
.inserted
);
63 VERIFY( ins
.position
!= c
.end() );
64 VERIFY( *ins
.position
== 4 );
65 VERIFY( c
.count(1) == 0 );
66 VERIFY( c
.count(4) == 1 );
68 pos
= c
.insert(c
.begin(), std::move(node
));
70 VERIFY( node
.empty() );
71 VERIFY( c
.size() == 3 );
72 VERIFY( pos
== c
.end() );
74 pos
= c
.insert(c
.begin(), c
.extract(2));
75 VERIFY( c
.size() == 3 );
76 VERIFY( pos
!= c
.end() );
81 ins
= c
.insert(std::move(node
));
82 VERIFY( node
.empty() );
83 VERIFY( ins
.position
!= c
.end() );
84 VERIFY( !ins
.inserted
);
85 VERIFY( !ins
.node
.empty() );
86 VERIFY( ins
.node
.value() == 3 );
87 auto hasher
= c
.hash_function();
88 VERIFY( hasher(*ins
.position
) == hasher(ins
.node
.value()) );
90 VERIFY( eq(*ins
.position
, ins
.node
.value()) );
96 test_type c
{ 1, 2, 3 };
97 test_type::node_type node
;
98 test_type::insert_return_type ins
;
100 const int val
= *c
.begin();
101 node
= c
.extract(c
.begin());
102 VERIFY( (bool)node
);
103 VERIFY( !node
.empty() );
104 VERIFY( c
.size() == 2 );
105 VERIFY( node
.get_allocator() == c
.get_allocator() );
106 VERIFY( node
.value() == val
);
108 ins
= c
.insert(std::move(node
));
109 VERIFY( node
.empty() );
110 VERIFY( c
.size() == 3 );
111 VERIFY( ins
.inserted
);
113 VERIFY( ins
.position
!= c
.end() );
114 VERIFY( *ins
.position
== val
);
120 struct hash
: std::hash
<int> { };
121 struct equal
: std::equal_to
<int> { };
122 using std::is_same_v
;
123 using compat_type1
= std::unordered_set
<int, hash
, equal
>;
124 static_assert( is_same_v
<test_type::node_type
, compat_type1::node_type
> );
125 using compat_type2
= std::unordered_multiset
<int>;
126 static_assert( is_same_v
<test_type::node_type
, compat_type2::node_type
> );
127 using compat_type3
= std::unordered_multiset
<int, hash
, equal
>;
128 static_assert( is_same_v
<test_type::node_type
, compat_type3::node_type
> );
134 // Check order of members in insert_return_type
135 auto [pos
, ins
, node
] = test_type::insert_return_type
{};
136 using std::is_same_v
;
137 static_assert( is_same_v
<test_type::iterator
, decltype(pos
)> );
138 static_assert( is_same_v
<bool, decltype(ins
)> );
139 static_assert( is_same_v
<test_type::node_type
, decltype(node
)> );