Debian Codegraph @

libstdc++-v3/testsuite/23_containers/unordered_map/96088.cc

raw
1 // { dg-do run { target c++17 } }
2 // { dg-require-effective-target std_allocator_new }
3 // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
4
5 // Copyright (C) 2021-2025 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 // libstdc++/96088
23
24 #include <string_view>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28
29 #include <testsuite_hooks.h>
30 #include <replacement_memory_operators.h>
31
32 static constexpr std::initializer_list<std::pair<const char*, const char*>> lst =
33 { { "long_str_for_dynamic_allocation", "long_str_for_dynamic_allocation" } };
34
35 void
36 test01()
37 {
38 __gnu_test::counter::reset();
39 std::unordered_map<std::string, std::string> um;
40 um.insert(lst.begin(), lst.end());
41 VERIFY( um.size() == 1 );
42
43 VERIFY( __gnu_test::counter::count() == 4 );
44 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
45
46 um.insert(lst.begin(), lst.end());
47 VERIFY( um.size() == 1 );
48
49 VERIFY( __gnu_test::counter::count() == 4 );
50 // Allocated another node and a pair<const std::string, std::string>:
51 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
52 }
53
54 void
55 test02()
56 {
57 __gnu_test::counter::reset();
58 std::unordered_map<std::string, std::string,
59 std::hash<std::string_view>,
60 std::equal_to<std::string_view>> um;
61 um.insert(lst.begin(), lst.end());
62 VERIFY( um.size() == 1 );
63
64 VERIFY( __gnu_test::counter::count() == 4 );
65 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
66
67 um.insert(lst.begin(), lst.end());
68 VERIFY( um.size() == 1 );
69
70 VERIFY( __gnu_test::counter::count() == 4 );
71 // Allocated another node and a pair<const std::string, std::string>:
72 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
73 }
74
75 std::size_t
76 hash_string_f(const std::string& str) noexcept
77 {
78 std::hash<std::string> h;
79 return h(str);
80 }
81
82 void
83 test11()
84 {
85 typedef std::size_t (*hash_string_t)(const std::string&) noexcept;
86 __gnu_test::counter::reset();
87 hash_string_t hasher = &hash_string_f;
88 std::unordered_map<std::string, std::string,
89 hash_string_t,
90 std::equal_to<std::string>> um(0, hasher);
91 um.insert(lst.begin(), lst.end());
92 VERIFY( um.size() == 1 );
93
94 VERIFY( __gnu_test::counter::count() == 4 );
95 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
96
97 um.insert(lst.begin(), lst.end());
98 VERIFY( um.size() == 1 );
99
100 VERIFY( __gnu_test::counter::count() == 4 );
101 // Allocated another node and a pair<const std::string, std::string>:
102 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
103 }
104
105 std::size_t
106 hash_string_view_f(const std::string_view& str) noexcept
107 {
108 std::hash<std::string_view> h;
109 return h(str);
110 }
111
112 void
113 test12()
114 {
115 typedef std::size_t (*hash_stringview_t) (const std::string_view&) noexcept;
116 __gnu_test::counter::reset();
117 hash_stringview_t hasher = &hash_string_view_f;
118 std::unordered_map<std::string, std::string, hash_stringview_t,
119 std::equal_to<std::string_view>> um(0, hasher);
120 um.insert(lst.begin(), lst.end());
121 VERIFY( um.size() == 1 );
122
123 VERIFY( __gnu_test::counter::count() == 4 );
124 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
125
126 um.insert(lst.begin(), lst.end());
127 VERIFY( um.size() == 1 );
128
129 VERIFY( __gnu_test::counter::count() == 4 );
130 // Allocated another node and a pair<const std::string, std::string>:
131 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
132 }
133
134 struct hash_string_functor
135 {
136 std::size_t
137 operator()(const std::string& str) const noexcept
138 {
139 std::hash<std::string> h;
140 return h(str);
141 }
142 };
143
144 void
145 test21()
146 {
147 __gnu_test::counter::reset();
148 std::unordered_map<std::string, std::string,
149 hash_string_functor,
150 std::equal_to<std::string>> um;
151 um.insert(lst.begin(), lst.end());
152 VERIFY( um.size() == 1 );
153
154 VERIFY( __gnu_test::counter::count() == 4 );
155 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
156
157 um.insert(lst.begin(), lst.end());
158 VERIFY( um.size() == 1 );
159
160 VERIFY( __gnu_test::counter::count() == 4 );
161 // Allocated another node and a pair<const std::string, std::string>:
162 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
163 }
164
165 struct hash_string_view_noexcept_functor
166 {
167 std::size_t
168 operator()(const std::string_view& str) const noexcept
169 {
170 std::hash<std::string_view> h;
171 return h(str);
172 }
173 };
174
175 void
176 test22()
177 {
178 __gnu_test::counter::reset();
179 std::unordered_map<std::string, std::string,
180 hash_string_view_noexcept_functor,
181 std::equal_to<std::string_view>> um;
182 um.insert(lst.begin(), lst.end());
183 VERIFY( um.size() == 1 );
184
185 VERIFY( __gnu_test::counter::count() == 4 );
186 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
187
188 um.insert(lst.begin(), lst.end());
189 VERIFY( um.size() == 1 );
190
191 VERIFY( __gnu_test::counter::count() == 4 );
192 // Allocated another node and a pair<const std::string, std::string>:
193 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
194 }
195
196 struct hash_string_view_functor
197 {
198 std::size_t
199 operator()(const std::string_view& str) const
200 {
201 std::hash<std::string_view> h;
202 return h(str);
203 }
204 };
205
206 void
207 test23()
208 {
209 __gnu_test::counter::reset();
210 std::unordered_map<std::string, std::string,
211 hash_string_view_functor,
212 std::equal_to<std::string_view>> um;
213 um.insert(lst.begin(), lst.end());
214 VERIFY( um.size() == 1 );
215
216 VERIFY( __gnu_test::counter::count() == 4 );
217 VERIFY( __gnu_test::counter::get()._M_increments == 4 );
218
219 um.insert(lst.begin(), lst.end());
220 VERIFY( um.size() == 1 );
221
222 VERIFY( __gnu_test::counter::count() == 4 );
223 // Allocated another node and a pair<const std::string, std::string>:
224 VERIFY( __gnu_test::counter::get()._M_increments == 7 );
225 }
226
227 void
228 test03()
229 {
230 std::vector<std::pair<std::string, std::string>> v;
231 v.insert(v.end(), lst.begin(), lst.end());
232
233 const auto origin = __gnu_test::counter::count();
234
235 {
236 __gnu_test::counter::reset();
237 std::unordered_map<std::string, std::string,
238 std::hash<std::string_view>,
239 std::equal_to<std::string_view>> um;
240 um.insert(v.begin(), v.end());
241 VERIFY( um.size() == 1 );
242
243 // Allocate array of buckets, a node, and the 2 std::string (unless COW).
244 constexpr std::size_t increments = _GLIBCXX_USE_CXX11_ABI ? 4 : 2;
245
246 VERIFY( __gnu_test::counter::count() == origin + increments );
247 VERIFY( __gnu_test::counter::get()._M_increments == increments );
248
249 um.insert(v.begin(), v.end());
250 VERIFY( um.size() == 1 );
251
252 VERIFY( __gnu_test::counter::count() == origin + increments );
253 VERIFY( __gnu_test::counter::get()._M_increments == increments );
254 }
255 VERIFY( __gnu_test::counter::count() == origin );
256
257 {
258 __gnu_test::counter::reset();
259 std::unordered_map<std::string, std::string,
260 std::hash<std::string_view>,
261 std::equal_to<std::string_view>> um;
262 um.insert(std::make_move_iterator(v.begin()),
263 std::make_move_iterator(v.end()));
264 VERIFY( um.size() == 1 );
265
266 // Allocate array of buckets and a node. String is moved.
267 constexpr std::size_t increments = 2;
268
269 VERIFY( __gnu_test::counter::count() == origin + increments );
270 VERIFY( __gnu_test::counter::get()._M_increments == increments );
271 }
272 }
273
274 int
275 main()
276 {
277 __gnu_test::counter::scope s;
278 test01();
279 test02();
280 test11();
281 test12();
282 test21();
283 test22();
284 test03();
285 return 0;
286 }