Debian Codegraph @

libstdc++-v3/testsuite/23_containers/multimap/modifiers/extract.cc

raw
1 // Copyright (C) 2016-2025 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8
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.
13
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/>.
17
18 // { dg-do run { target c++17 } }
19
20 #include <map>
21 #include <algorithm>
22 #include <testsuite_hooks.h>
23
24 using test_type = std::multimap<int, int>;
25
26 void
27 test01()
28 {
29 test_type c{ {1, 10}, { 1, 11 }, {2, 20}, { 2, 21}, {3, 30}, { 3, 31 } };
30 test_type::node_type node;
31 test_type::iterator pos;
32
33 node = c.extract(0);
34 VERIFY( !node );
35 VERIFY( node.empty() );
36 VERIFY( c.size() == 6 );
37
38 pos = c.insert(std::move(node));
39 VERIFY( !node );
40 VERIFY( node.empty() );
41 VERIFY( c.size() == 6 );
42 VERIFY( pos == c.end() );
43
44 node = c.extract(1);
45 VERIFY( (bool)node );
46 VERIFY( !node.empty() );
47 VERIFY( c.size() == 5 );
48 VERIFY( c.count(1) == 1 );
49 VERIFY( node.get_allocator() == c.get_allocator() );
50 VERIFY( node.key() == 1 );
51 int mapped = node.mapped();
52 VERIFY( mapped == 10 || mapped == 11 );
53
54 node.key() = 4;
55 node.mapped() = 40;
56 VERIFY( node.key() == 4 );
57 VERIFY( node.mapped() == 40 );
58
59 pos = c.insert(std::move(node));
60 VERIFY( !node );
61 VERIFY( node.empty() );
62 VERIFY( c.size() == 6 );
63 VERIFY( pos != c.end() );
64 VERIFY( pos->first == 4 );
65 VERIFY( pos->second == 40 );
66 VERIFY( c.count(1) == 1 );
67 VERIFY( c.count(4) == 1 );
68 VERIFY( std::is_sorted(c.begin(), c.end()) );
69
70 pos = c.insert(c.begin(), std::move(node));
71 VERIFY( !node );
72 VERIFY( node.empty() );
73 VERIFY( c.size() == 6 );
74 VERIFY( pos == c.end() );
75
76 node = c.extract(1);
77 mapped = node.mapped();
78 pos = c.insert(c.begin(), std::move(node));
79 VERIFY( !node );
80 VERIFY( c.size() == 6 );
81 VERIFY( pos != c.end() );
82 VERIFY( pos->first == 1 );
83 VERIFY( pos->second == mapped );
84
85 test_type c2 = c;
86 node = c2.extract(1);
87 mapped = node.mapped();
88 pos = c.insert(std::move(node));
89 VERIFY( pos != c.end() );
90 VERIFY( node.empty() );
91 VERIFY( pos->first == 1 );
92 VERIFY( pos->second == mapped );
93 }
94
95 void
96 test02()
97 {
98 test_type c{ {1, 10}, { 1, 11 }, {2, 20}, { 2, 21}, {3, 30}, { 3, 31 } };
99 test_type::node_type node;
100 test_type::iterator pos;
101
102 node = c.extract(c.begin());
103 VERIFY( !node.empty() );
104 VERIFY( c.size() == 5 );
105 VERIFY( node.get_allocator() == c.get_allocator() );
106 VERIFY( node.key() == 1 );
107 VERIFY( node.mapped() == 10 );
108
109 pos = c.insert(std::next(c.begin()), std::move(node));
110 VERIFY( node.empty() );
111 VERIFY( c.size() == 6 );
112 VERIFY( pos != c.end() );
113 VERIFY( pos->first == 1 );
114 VERIFY( pos->second == 10 );
115 VERIFY( pos == std::next(c.begin()) );
116 }
117
118 void
119 test03()
120 {
121 struct less : std::less<int> { };
122 using std::is_same_v;
123 using compat_type1 = std::multimap<int, int, less>;
124 static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
125 using compat_type2 = std::map<int, int>;
126 static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
127 using compat_type3 = std::map<int, int, less>;
128 static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
129 }
130
131 int
132 main()
133 {
134 test01();
135 test02();
136 test03();
137 }