]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/extract.cc
1 // Copyright (C) 2016-2024 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_map>
21 #include <testsuite_hooks.h>
23 using test_type
= std::unordered_map
<int, int>;
28 test_type c
{ {1, 10}, {2, 20}, {3, 30} };
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
.key() == 1 );
52 VERIFY( node
.mapped() == 10 );
56 VERIFY( node
.key() == 4 );
57 VERIFY( node
.mapped() == 40 );
59 ins
= c
.insert(std::move(node
));
61 VERIFY( node
.empty() );
62 VERIFY( c
.size() == 3 );
63 VERIFY( ins
.inserted
);
65 VERIFY( ins
.position
!= c
.end() );
66 VERIFY( ins
.position
->first
== 4 );
67 VERIFY( ins
.position
->second
== 40 );
68 VERIFY( c
.count(1) == 0 );
69 VERIFY( c
.count(4) == 1 );
71 pos
= c
.insert(c
.begin(), std::move(node
));
73 VERIFY( node
.empty() );
74 VERIFY( c
.size() == 3 );
75 VERIFY( pos
== c
.end() );
77 pos
= c
.insert(c
.begin(), c
.extract(2));
78 VERIFY( c
.size() == 3 );
79 VERIFY( pos
!= c
.end() );
80 VERIFY( pos
->first
== 2 );
81 VERIFY( pos
->second
== 20 );
85 ins
= c
.insert(std::move(node
));
86 VERIFY( node
.empty() );
87 VERIFY( ins
.position
!= c
.end() );
88 VERIFY( !ins
.inserted
);
89 VERIFY( !ins
.node
.empty() );
90 VERIFY( ins
.node
.key() == 3 );
91 VERIFY( ins
.node
.mapped() == 30 );
92 auto hasher
= c
.hash_function();
93 VERIFY( hasher(ins
.position
->first
) == hasher(ins
.node
.key()) );
95 VERIFY( eq(ins
.position
->first
, ins
.node
.key()) );
101 test_type c
{ {1, 10}, {2, 20}, {3, 30} };
102 test_type::node_type node
;
103 test_type::insert_return_type ins
;
105 const int key
= c
.begin()->first
;
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() == key
);
112 VERIFY( node
.mapped() == (key
* 10) );
114 ins
= c
.insert(std::move(node
));
115 VERIFY( node
.empty() );
116 VERIFY( c
.size() == 3 );
117 VERIFY( ins
.inserted
);
119 VERIFY( ins
.position
!= c
.end() );
120 VERIFY( ins
.position
->first
== key
);
121 VERIFY( ins
.position
->second
== (key
* 10) );
127 struct hash
: std::hash
<int> { };
128 struct equal
: std::equal_to
<int> { };
129 using std::is_same_v
;
130 using compat_type1
= std::unordered_map
<int, int, hash
, equal
>;
131 static_assert( is_same_v
<test_type::node_type
, compat_type1::node_type
> );
132 using compat_type2
= std::unordered_multimap
<int, int>;
133 static_assert( is_same_v
<test_type::node_type
, compat_type2::node_type
> );
134 using compat_type3
= std::unordered_multimap
<int, int, hash
, equal
>;
135 static_assert( is_same_v
<test_type::node_type
, compat_type3::node_type
> );
141 // Check order of members in insert_return_type
142 auto [pos
, ins
, node
] = test_type::insert_return_type
{};
143 using std::is_same_v
;
144 static_assert( is_same_v
<test_type::iterator
, decltype(pos
)> );
145 static_assert( is_same_v
<bool, decltype(ins
)> );
146 static_assert( is_same_v
<test_type::node_type
, decltype(node
)> );