]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/extract.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_map / modifiers / extract.cc
1 // Copyright (C) 2016-2021 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 <unordered_map>
22 #include <testsuite_hooks.h>
23
24 using test_type = std::unordered_map<int, int>;
25
26 void
27 test01()
28 {
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;
33
34 node = c.extract(0);
35 VERIFY( !node );
36 VERIFY( node.empty() );
37 VERIFY( c.size() == 3 );
38
39 ins = c.insert(std::move(node));
40 VERIFY( !node );
41 VERIFY( node.empty() );
42 VERIFY( c.size() == 3 );
43 VERIFY( !ins.inserted );
44 VERIFY( !ins.node );
45 VERIFY( ins.position == c.end() );
46
47 node = c.extract(1);
48 VERIFY( (bool)node );
49 VERIFY( !node.empty() );
50 VERIFY( c.size() == 2 );
51 VERIFY( node.get_allocator() == c.get_allocator() );
52 VERIFY( node.key() == 1 );
53 VERIFY( node.mapped() == 10 );
54
55 node.key() = 4;
56 node.mapped() = 40;
57 VERIFY( node.key() == 4 );
58 VERIFY( node.mapped() == 40 );
59
60 ins = c.insert(std::move(node));
61 VERIFY( !node );
62 VERIFY( node.empty() );
63 VERIFY( c.size() == 3 );
64 VERIFY( ins.inserted );
65 VERIFY( !ins.node );
66 VERIFY( ins.position != c.end() );
67 VERIFY( ins.position->first == 4 );
68 VERIFY( ins.position->second == 40 );
69 VERIFY( c.count(1) == 0 );
70 VERIFY( c.count(4) == 1 );
71
72 pos = c.insert(c.begin(), std::move(node));
73 VERIFY( !node );
74 VERIFY( node.empty() );
75 VERIFY( c.size() == 3 );
76 VERIFY( pos == c.end() );
77
78 pos = c.insert(c.begin(), c.extract(2));
79 VERIFY( c.size() == 3 );
80 VERIFY( pos != c.end() );
81 VERIFY( pos->first == 2 );
82 VERIFY( pos->second == 20 );
83
84 test_type c2 = c;
85 node = c2.extract(3);
86 ins = c.insert(std::move(node));
87 VERIFY( node.empty() );
88 VERIFY( ins.position != c.end() );
89 VERIFY( !ins.inserted );
90 VERIFY( !ins.node.empty() );
91 VERIFY( ins.node.key() == 3 );
92 VERIFY( ins.node.mapped() == 30 );
93 auto hasher = c.hash_function();
94 VERIFY( hasher(ins.position->first) == hasher(ins.node.key()) );
95 auto eq = c.key_eq();
96 VERIFY( eq(ins.position->first, ins.node.key()) );
97 }
98
99 void
100 test02()
101 {
102 test_type c{ {1, 10}, {2, 20}, {3, 30} };
103 test_type::node_type node;
104 test_type::insert_return_type ins;
105
106 const int key = c.begin()->first;
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() == key );
113 VERIFY( node.mapped() == (key * 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 == key );
122 VERIFY( ins.position->second == (key * 10) );
123 }
124
125 void
126 test03()
127 {
128 struct hash : std::hash<int> { };
129 struct equal : std::equal_to<int> { };
130 using std::is_same_v;
131 using compat_type1 = std::unordered_map<int, int, hash, equal>;
132 static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
133 using compat_type2 = std::unordered_multimap<int, int>;
134 static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
135 using compat_type3 = std::unordered_multimap<int, int, hash, equal>;
136 static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
137 }
138
139 void
140 test04()
141 {
142 // Check order of members in insert_return_type
143 auto [pos, ins, node] = test_type::insert_return_type{};
144 using std::is_same_v;
145 static_assert( is_same_v<test_type::iterator, decltype(pos)> );
146 static_assert( is_same_v<bool, decltype(ins)> );
147 static_assert( is_same_v<test_type::node_type, decltype(node)> );
148 }
149
150 int
151 main()
152 {
153 test01();
154 test02();
155 test03();
156 }