]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/extract.cc
1 // Copyright (C) 2016-2021 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-do run { target c++17 } }
21 #include <unordered_map>
22 #include <testsuite_hooks.h>
24 using test_type
= std::unordered_map
<int, int>;
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
;
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
.key() == 1 );
53 VERIFY( node
.mapped() == 10 );
57 VERIFY( node
.key() == 4 );
58 VERIFY( node
.mapped() == 40 );
60 ins
= c
.insert(std::move(node
));
62 VERIFY( node
.empty() );
63 VERIFY( c
.size() == 3 );
64 VERIFY( ins
.inserted
);
66 VERIFY( ins
.position
!= c
.end() );
67 VERIFY( ins
.position
->first
== 4 );
68 VERIFY( ins
.position
->second
== 40 );
69 VERIFY( c
.count(1) == 0 );
70 VERIFY( c
.count(4) == 1 );
72 pos
= c
.insert(c
.begin(), std::move(node
));
74 VERIFY( node
.empty() );
75 VERIFY( c
.size() == 3 );
76 VERIFY( pos
== c
.end() );
78 pos
= c
.insert(c
.begin(), c
.extract(2));
79 VERIFY( c
.size() == 3 );
80 VERIFY( pos
!= c
.end() );
81 VERIFY( pos
->first
== 2 );
82 VERIFY( pos
->second
== 20 );
86 ins
= c
.insert(std::move(node
));
87 VERIFY( node
.empty() );
88 VERIFY( ins
.position
!= c
.end() );
89 VERIFY( !ins
.inserted
);
90 VERIFY( !ins
.node
.empty() );
91 VERIFY( ins
.node
.key() == 3 );
92 VERIFY( ins
.node
.mapped() == 30 );
93 auto hasher
= c
.hash_function();
94 VERIFY( hasher(ins
.position
->first
) == hasher(ins
.node
.key()) );
96 VERIFY( eq(ins
.position
->first
, ins
.node
.key()) );
102 test_type c
{ {1, 10}, {2, 20}, {3, 30} };
103 test_type::node_type node
;
104 test_type::insert_return_type ins
;
106 const int key
= c
.begin()->first
;
107 node
= c
.extract(c
.begin());
108 VERIFY( (bool)node
);
109 VERIFY( !node
.empty() );
110 VERIFY( c
.size() == 2 );
111 VERIFY( node
.get_allocator() == c
.get_allocator() );
112 VERIFY( node
.key() == key
);
113 VERIFY( node
.mapped() == (key
* 10) );
115 ins
= c
.insert(std::move(node
));
116 VERIFY( node
.empty() );
117 VERIFY( c
.size() == 3 );
118 VERIFY( ins
.inserted
);
120 VERIFY( ins
.position
!= c
.end() );
121 VERIFY( ins
.position
->first
== key
);
122 VERIFY( ins
.position
->second
== (key
* 10) );
128 struct hash
: std::hash
<int> { };
129 struct equal
: std::equal_to
<int> { };
130 using std::is_same_v
;
131 using compat_type1
= std::unordered_map
<int, int, hash
, equal
>;
132 static_assert( is_same_v
<test_type::node_type
, compat_type1::node_type
> );
133 using compat_type2
= std::unordered_multimap
<int, int>;
134 static_assert( is_same_v
<test_type::node_type
, compat_type2::node_type
> );
135 using compat_type3
= std::unordered_multimap
<int, int, hash
, equal
>;
136 static_assert( is_same_v
<test_type::node_type
, compat_type3::node_type
> );
142 // Check order of members in insert_return_type
143 auto [pos
, ins
, node
] = test_type::insert_return_type
{};
144 using std::is_same_v
;
145 static_assert( is_same_v
<test_type::iterator
, decltype(pos
)> );
146 static_assert( is_same_v
<bool, decltype(ins
)> );
147 static_assert( is_same_v
<test_type::node_type
, decltype(node
)> );