]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/extract.cc
1 // Copyright (C) 2016-2022 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_multimap
<int, int>;
28 test_type c
{ {1, 10}, { 1, 11 }, {2, 20}, { 2, 21}, {3, 30}, { 3, 31 } };
29 test_type::node_type node
;
30 test_type::iterator pos
;
34 VERIFY( node
.empty() );
35 VERIFY( c
.size() == 6 );
37 pos
= c
.insert(std::move(node
));
39 VERIFY( node
.empty() );
40 VERIFY( c
.size() == 6 );
41 VERIFY( pos
== c
.end() );
45 VERIFY( !node
.empty() );
46 VERIFY( c
.size() == 5 );
47 VERIFY( node
.get_allocator() == c
.get_allocator() );
48 VERIFY( node
.key() == 1 );
49 int mapped
= node
.mapped();
50 VERIFY( mapped
== 10 || mapped
== 11 );
54 VERIFY( node
.key() == 4 );
55 VERIFY( node
.mapped() == 40 );
57 pos
= c
.insert(std::move(node
));
59 VERIFY( node
.empty() );
60 VERIFY( c
.size() == 6 );
61 VERIFY( pos
!= c
.end() );
62 VERIFY( pos
->first
== 4 );
63 VERIFY( pos
->second
== 40 );
65 pos
= c
.insert(c
.begin(), std::move(node
));
67 VERIFY( node
.empty() );
68 VERIFY( c
.size() == 6 );
69 VERIFY( pos
== c
.end() );
72 mapped
= node
.mapped();
73 pos
= c
.insert(c
.begin(), std::move(node
));
75 VERIFY( c
.size() == 6 );
76 VERIFY( pos
!= c
.end() );
77 VERIFY( pos
->first
== 1 );
78 VERIFY( pos
->second
== mapped
);
82 mapped
= node
.mapped();
83 pos
= c
.insert(std::move(node
));
84 VERIFY( pos
!= c
.end() );
85 VERIFY( node
.empty() );
86 VERIFY( pos
->first
== 1 );
87 VERIFY( pos
->second
== mapped
);
93 test_type c
{ {1, 10}, { 1, 11 }, {2, 20}, { 2, 21}, {3, 30}, { 3, 31 } };
94 test_type::node_type node
;
95 test_type::iterator pos
;
97 const int key
= c
.begin()->first
;
98 const int mapped
= c
.begin()->second
;
99 node
= c
.extract(c
.begin());
100 VERIFY( !node
.empty() );
101 VERIFY( c
.size() == 5 );
102 VERIFY( node
.get_allocator() == c
.get_allocator() );
103 VERIFY( node
.key() == key
);
104 VERIFY( node
.mapped() == mapped
);
106 pos
= c
.insert(std::move(node
));
107 VERIFY( node
.empty() );
108 VERIFY( c
.size() == 6 );
109 VERIFY( pos
!= c
.end() );
110 VERIFY( pos
->first
== key
);
111 VERIFY( pos
->second
== mapped
);
117 struct hash
: std::hash
<int> { };
118 struct equal
: std::equal_to
<int> { };
119 using std::is_same_v
;
120 using compat_type1
= std::unordered_multimap
<int, int, hash
, equal
>;
121 static_assert( is_same_v
<test_type::node_type
, compat_type1::node_type
> );
122 using compat_type2
= std::unordered_map
<int, int>;
123 static_assert( is_same_v
<test_type::node_type
, compat_type2::node_type
> );
124 using compat_type3
= std::unordered_map
<int, int, hash
, equal
>;
125 static_assert( is_same_v
<test_type::node_type
, compat_type3::node_type
> );