Debian Codegraph @

libstdc++-v3/testsuite/23_containers/unordered_set/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_set>
27 #include <vector>
28
29 #include <testsuite_hooks.h>
30 #include <replacement_memory_operators.h>
31
32 static constexpr std::initializer_list<const char*> lst =
33 { "long_str_for_dynamic_allocation" };
34
35 void
36 test01()
37 {
38 __gnu_test::counter::reset();
39 std::unordered_set<std::string> us;
40 us.insert(lst.begin(), lst.end());
41 VERIFY( us.size() == 1 );
42
43 VERIFY( __gnu_test::counter::count() == 3 );
44 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
45
46 us.insert(lst.begin(), lst.end());
47 VERIFY( us.size() == 1 );
48
49 VERIFY( __gnu_test::counter::count() == 3 );
50 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
51 }
52
53 void
54 test02()
55 {
56 __gnu_test::counter::reset();
57 std::unordered_set<std::string,
58 std::hash<std::string_view>,
59 std::equal_to<std::string_view>> us;
60 us.insert(lst.begin(), lst.end());
61 VERIFY( us.size() == 1 );
62
63 VERIFY( __gnu_test::counter::count() == 3 );
64 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
65
66 us.insert(lst.begin(), lst.end());
67 VERIFY( us.size() == 1 );
68
69 VERIFY( __gnu_test::counter::count() == 3 );
70 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
71 }
72
73 std::size_t
74 hash_string_f(const std::string& str) noexcept
75 {
76 std::hash<std::string> h;
77 return h(str);
78 }
79
80 void
81 test11()
82 {
83 typedef std::size_t (*hash_string_t)(const std::string&) noexcept;
84 __gnu_test::counter::reset();
85 hash_string_t hasher = &hash_string_f;
86 std::unordered_set<std::string,
87 hash_string_t,
88 std::equal_to<std::string>> us(0, hasher);
89 us.insert(lst.begin(), lst.end());
90 VERIFY( us.size() == 1 );
91
92 VERIFY( __gnu_test::counter::count() == 3 );
93 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
94
95 us.insert(lst.begin(), lst.end());
96 VERIFY( us.size() == 1 );
97
98 VERIFY( __gnu_test::counter::count() == 3 );
99 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
100 }
101
102 std::size_t
103 hash_string_view_f(const std::string_view& str) noexcept
104 {
105 std::hash<std::string_view> h;
106 return h(str);
107 }
108
109 void
110 test12()
111 {
112 typedef std::size_t (*hash_stringview_t)(const std::string_view&) noexcept;
113 __gnu_test::counter::reset();
114 hash_stringview_t hasher = &hash_string_view_f;
115 std::unordered_set<std::string,
116 hash_stringview_t,
117 std::equal_to<std::string_view>> us(0, hasher);
118 us.insert(lst.begin(), lst.end());
119 VERIFY( us.size() == 1 );
120
121 VERIFY( __gnu_test::counter::count() == 3 );
122 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
123
124 us.insert(lst.begin(), lst.end());
125 VERIFY( us.size() == 1 );
126
127 VERIFY( __gnu_test::counter::count() == 3 );
128 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
129 }
130
131 struct hash_string_functor
132 {
133 std::size_t
134 operator()(const std::string& str) const noexcept
135 {
136 std::hash<std::string> h;
137 return h(str);
138 }
139 };
140
141 void
142 test21()
143 {
144 __gnu_test::counter::reset();
145 std::unordered_set<std::string,
146 hash_string_functor,
147 std::equal_to<std::string>> us;
148 us.insert(lst.begin(), lst.end());
149 VERIFY( us.size() == 1 );
150
151 VERIFY( __gnu_test::counter::count() == 3 );
152 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
153
154 us.insert(lst.begin(), lst.end());
155 VERIFY( us.size() == 1 );
156
157 VERIFY( __gnu_test::counter::count() == 3 );
158 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
159 }
160
161 struct hash_string_view_noexcept_functor
162 {
163 std::size_t
164 operator()(const std::string_view& str) const noexcept
165 {
166 std::hash<std::string_view> h;
167 return h(str);
168 }
169 };
170
171 void
172 test22()
173 {
174 __gnu_test::counter::reset();
175 std::unordered_set<std::string,
176 hash_string_view_noexcept_functor,
177 std::equal_to<std::string_view>> us;
178 us.insert(lst.begin(), lst.end());
179 VERIFY( us.size() == 1 );
180
181 VERIFY( __gnu_test::counter::count() == 3 );
182 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
183
184 us.insert(lst.begin(), lst.end());
185 VERIFY( us.size() == 1 );
186
187 VERIFY( __gnu_test::counter::count() == 3 );
188 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
189 }
190
191 struct hash_string_view_functor
192 {
193 std::size_t
194 operator()(const std::string_view& str) const
195 {
196 std::hash<std::string_view> h;
197 return h(str);
198 }
199 };
200
201 void
202 test23()
203 {
204 __gnu_test::counter::reset();
205 std::unordered_set<std::string,
206 hash_string_view_functor,
207 std::equal_to<std::string_view>> us;
208 us.insert(lst.begin(), lst.end());
209 VERIFY( us.size() == 1 );
210
211 VERIFY( __gnu_test::counter::count() == 3 );
212 VERIFY( __gnu_test::counter::get()._M_increments == 3 );
213
214 us.insert(lst.begin(), lst.end());
215 VERIFY( us.size() == 1 );
216
217 VERIFY( __gnu_test::counter::count() == 3 );
218 VERIFY( __gnu_test::counter::get()._M_increments == 5 );
219 }
220
221 void
222 test03()
223 {
224 std::vector<std::string> v;
225 v.insert(v.end(), lst.begin(), lst.end());
226
227 const auto origin = __gnu_test::counter::count();
228
229 {
230 __gnu_test::counter::reset();
231 std::unordered_set<std::string,
232 std::hash<std::string_view>,
233 std::equal_to<std::string_view>> us;
234 us.insert(v.begin(), v.end());
235 VERIFY( us.size() == 1 );
236
237 // Allocate array of buckets, a node, and the std::string (unless COW).
238 constexpr std::size_t increments = _GLIBCXX_USE_CXX11_ABI ? 3 : 2;
239
240 VERIFY( __gnu_test::counter::count() == origin + increments );
241 VERIFY( __gnu_test::counter::get()._M_increments == increments );
242
243 us.insert(v.begin(), v.end());
244 VERIFY( us.size() == 1 );
245
246 VERIFY( __gnu_test::counter::count() == origin + increments );
247 VERIFY( __gnu_test::counter::get()._M_increments == increments );
248 }
249 VERIFY( __gnu_test::counter::count() == origin );
250
251 {
252 __gnu_test::counter::reset();
253 std::unordered_set<std::string,
254 std::hash<std::string_view>,
255 std::equal_to<std::string_view>> us;
256 us.insert(std::make_move_iterator(v.begin()),
257 std::make_move_iterator(v.end()));
258 VERIFY( us.size() == 1 );
259
260 // Allocate array of buckets and a node. String is moved.
261 constexpr std::size_t increments = 2;
262
263 VERIFY( __gnu_test::counter::count() == origin + increments );
264 VERIFY( __gnu_test::counter::get()._M_increments == increments );
265 }
266 }
267
268 int
269 main()
270 {
271 __gnu_test::counter::scope s;
272 test01();
273 test02();
274 test11();
275 test12();
276 test21();
277 test22();
278 test23();
279 test03();
280 return 0;
281 }