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" } }
23 #include <testsuite_hooks.h>
25 using test_type
= std::map
<int, int>;
30 test_type c
{ {1, 10}, {2, 20}, {3, 30} };
31 test_type::node_type node
;
32 test_type::insert_return_type ins
;
33 test_type::iterator pos
;
37 VERIFY( node
.empty() );
38 VERIFY( c
.size() == 3 );
40 ins
= c
.insert(std::move(node
));
42 VERIFY( node
.empty() );
43 VERIFY( c
.size() == 3 );
44 VERIFY( !ins
.inserted
);
46 VERIFY( ins
.position
== c
.end() );
50 VERIFY( !node
.empty() );
51 VERIFY( c
.size() == 2 );
52 VERIFY( c
.count(1) == 0 );
53 VERIFY( node
.get_allocator() == c
.get_allocator() );
54 VERIFY( node
.key() == 1 );
55 VERIFY( node
.mapped() == 10 );
59 VERIFY( node
.key() == 4 );
60 VERIFY( node
.mapped() == 40 );
62 ins
= c
.insert(std::move(node
));
64 VERIFY( node
.empty() );
65 VERIFY( c
.size() == 3 );
66 VERIFY( ins
.inserted
);
68 VERIFY( ins
.position
!= c
.end() );
69 VERIFY( ins
.position
->first
== 4 );
70 VERIFY( ins
.position
->second
== 40 );
71 VERIFY( c
.count(1) == 0 );
72 VERIFY( c
.count(4) == 1 );
73 VERIFY( std::is_sorted(c
.begin(), c
.end()) );
75 pos
= c
.insert(c
.begin(), std::move(node
));
77 VERIFY( node
.empty() );
78 VERIFY( c
.size() == 3 );
79 VERIFY( pos
== c
.end() );
82 pos
= c
.insert(c
.begin(), std::move(node
));
83 VERIFY( c
.size() == 3 );
84 VERIFY( pos
!= c
.end() );
85 VERIFY( pos
->first
== 2 );
86 VERIFY( pos
->second
== 20 );
90 ins
= c
.insert(std::move(node
));
91 VERIFY( node
.empty() );
92 VERIFY( ins
.position
!= c
.end() );
93 VERIFY( !ins
.inserted
);
94 VERIFY( !ins
.node
.empty() );
95 VERIFY( ins
.node
.key() == 3 );
96 VERIFY( ins
.node
.mapped() == 30 );
97 VERIFY( ins
.position
->first
== ins
.node
.key() );
103 test_type c
{ {1, 10}, {2, 20}, {3, 30} };
104 test_type::node_type node
;
105 test_type::insert_return_type ins
;
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() == 1 );
113 VERIFY( node
.mapped() == 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
== 1 );
122 VERIFY( ins
.position
->second
== 10 );
128 struct less
: std::less
<int> { };
129 using std::is_same_v
;
130 using compat_type1
= std::map
<int, int, less
>;
131 static_assert( is_same_v
<test_type::node_type
, compat_type1::node_type
> );
132 using compat_type2
= std::multimap
<int, int>;
133 static_assert( is_same_v
<test_type::node_type
, compat_type2::node_type
> );
134 using compat_type3
= std::multimap
<int, int, less
>;
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
)> );