]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/map/modifiers/extract.cc
libstdc++: Ensure c++NN effective target present in all C++17 tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / map / modifiers / extract.cc
1 // Copyright (C) 2016-2020 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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20
21 #include <map>
22 #include <algorithm>
23 #include <testsuite_hooks.h>
24
25 using test_type = std::map<int, int>;
26
27 void
28 test01()
29 {
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;
34
35 node = c.extract(0);
36 VERIFY( !node );
37 VERIFY( node.empty() );
38 VERIFY( c.size() == 3 );
39
40 ins = c.insert(std::move(node));
41 VERIFY( !node );
42 VERIFY( node.empty() );
43 VERIFY( c.size() == 3 );
44 VERIFY( !ins.inserted );
45 VERIFY( !ins.node );
46 VERIFY( ins.position == c.end() );
47
48 node = c.extract(1);
49 VERIFY( (bool)node );
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 );
56
57 node.key() = 4;
58 node.mapped() = 40;
59 VERIFY( node.key() == 4 );
60 VERIFY( node.mapped() == 40 );
61
62 ins = c.insert(std::move(node));
63 VERIFY( !node );
64 VERIFY( node.empty() );
65 VERIFY( c.size() == 3 );
66 VERIFY( ins.inserted );
67 VERIFY( !ins.node );
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()) );
74
75 pos = c.insert(c.begin(), std::move(node));
76 VERIFY( !node );
77 VERIFY( node.empty() );
78 VERIFY( c.size() == 3 );
79 VERIFY( pos == c.end() );
80
81 node = c.extract(2);
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 );
87
88 test_type c2 = c;
89 node = c2.extract(3);
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() );
98 }
99
100 void
101 test02()
102 {
103 test_type c{ {1, 10}, {2, 20}, {3, 30} };
104 test_type::node_type node;
105 test_type::insert_return_type ins;
106
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 );
114
115 ins = c.insert(std::move(node));
116 VERIFY( node.empty() );
117 VERIFY( c.size() == 3 );
118 VERIFY( ins.inserted );
119 VERIFY( !ins.node );
120 VERIFY( ins.position != c.end() );
121 VERIFY( ins.position->first == 1 );
122 VERIFY( ins.position->second == 10 );
123 }
124
125 void
126 test03()
127 {
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> );
136 }
137
138 void
139 test04()
140 {
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)> );
147 }
148
149 int
150 main()
151 {
152 test01();
153 test02();
154 test03();
155 }