]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/map/modifiers/extract.cc
1 // Copyright (C) 2016-2020 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" }
22 #include <testsuite_hooks.h>
24 using test_type
= std::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( c
.count(1) == 0 );
52 VERIFY( node
.get_allocator() == c
.get_allocator() );
53 VERIFY( node
.key() == 1 );
54 VERIFY( node
.mapped() == 10 );
58 VERIFY( node
.key() == 4 );
59 VERIFY( node
.mapped() == 40 );
61 ins
= c
.insert(std::move(node
));
63 VERIFY( node
.empty() );
64 VERIFY( c
.size() == 3 );
65 VERIFY( ins
.inserted
);
67 VERIFY( ins
.position
!= c
.end() );
68 VERIFY( ins
.position
->first
== 4 );
69 VERIFY( ins
.position
->second
== 40 );
70 VERIFY( c
.count(1) == 0 );
71 VERIFY( c
.count(4) == 1 );
72 VERIFY( std::is_sorted(c
.begin(), c
.end()) );
74 pos
= c
.insert(c
.begin(), std::move(node
));
76 VERIFY( node
.empty() );
77 VERIFY( c
.size() == 3 );
78 VERIFY( pos
== c
.end() );
81 pos
= c
.insert(c
.begin(), std::move(node
));
82 VERIFY( c
.size() == 3 );
83 VERIFY( pos
!= c
.end() );
84 VERIFY( pos
->first
== 2 );
85 VERIFY( pos
->second
== 20 );
89 ins
= c
.insert(std::move(node
));
90 VERIFY( node
.empty() );
91 VERIFY( ins
.position
!= c
.end() );
92 VERIFY( !ins
.inserted
);
93 VERIFY( !ins
.node
.empty() );
94 VERIFY( ins
.node
.key() == 3 );
95 VERIFY( ins
.node
.mapped() == 30 );
96 VERIFY( 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 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() == 1 );
112 VERIFY( node
.mapped() == 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
== 1 );
121 VERIFY( ins
.position
->second
== 10 );
127 struct less
: std::less
<int> { };
128 using std::is_same_v
;
129 using compat_type1
= std::map
<int, int, less
>;
130 static_assert( is_same_v
<test_type::node_type
, compat_type1::node_type
> );
131 using compat_type2
= std::multimap
<int, int>;
132 static_assert( is_same_v
<test_type::node_type
, compat_type2::node_type
> );
133 using compat_type3
= std::multimap
<int, int, less
>;
134 static_assert( is_same_v
<test_type::node_type
, compat_type3::node_type
> );
140 // Check order of members in insert_return_type
141 auto [pos
, ins
, node
] = test_type::insert_return_type
{};
142 using std::is_same_v
;
143 static_assert( is_same_v
<test_type::iterator
, decltype(pos
)> );
144 static_assert( is_same_v
<bool, decltype(ins
)> );
145 static_assert( is_same_v
<test_type::node_type
, decltype(node
)> );