Debian Codegraph @

libstdc++-v3/testsuite/util/testsuite_containers.h

raw
1 // -*- C++ -*-
2
3 // Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
10
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 #ifndef _GLIBCXX_TESTSUITE_CONTAINERS_H
21 #define _GLIBCXX_TESTSUITE_CONTAINERS_H
22
23 #include <bits/boost_concept_check.h>
24 #include <cassert>
25 #include <testsuite_container_traits.h>
26 #include <utility> // for rel_ops.
27
28 // Container requirement testing.
29 namespace __gnu_test
30 {
31 // Compile-time typedef testing.
32 template<typename _Tp, bool _Bt = traits<_Tp>::is_container::value>
33 struct basic_types
34 {
35 // Base container requirements (table 80)
36 typedef _Tp test_type;
37 typedef typename test_type::value_type value_type;
38 typedef typename test_type::pointer pointer;
39 typedef typename test_type::const_pointer const_pointer;
40 typedef typename test_type::reference reference;
41 typedef typename test_type::const_reference const_reference;
42 typedef typename test_type::iterator iterator;
43 typedef typename test_type::const_iterator const_iterator;
44 typedef typename test_type::size_type size_type;
45 typedef typename test_type::difference_type difference_type;
46 };
47
48 // Conditional typedef testing, positive.
49 template<typename _Tp, bool _Bt = traits<_Tp>::is_reversible::value>
50 struct reversible_types
51 {
52 // Reversible container requirements (table 81)
53 typedef _Tp test_type;
54 typedef typename test_type::reverse_iterator reverse_iterator;
55 typedef typename test_type::const_reverse_iterator const_reverse_iterator;
56 };
57
58 template<typename _Tp, bool _Bt = traits<_Tp>::is_allocator_aware::value>
59 struct allocator_aware_types
60 {
61 // _Alloc-aware requirements (table 82)
62 typedef _Tp test_type;
63 typedef typename test_type::allocator_type allocator_type;
64 };
65
66 template<typename _Tp, bool _Bt = traits<_Tp>::is_associative::value>
67 struct associative_types
68 {
69 // Associative container requirements (table 85)
70 typedef _Tp test_type;
71 typedef typename test_type::key_type key_type;
72 typedef typename test_type::key_compare key_compare;
73 typedef typename test_type::value_compare value_compare;
74 };
75
76 template<typename _Tp, bool = traits<_Tp>::is_unordered::value>
77 struct unordered_types
78 {
79 // Unordered associative container requirements (table 87)
80 typedef _Tp test_type;
81 typedef typename test_type::key_type key_type;
82 typedef typename test_type::hasher hasher;
83 typedef typename test_type::key_equal key_equal;
84 typedef typename test_type::local_iterator local_iterator;
85 typedef typename test_type::const_local_iterator const_local_iterator;
86 };
87
88 template<typename _Tp, bool _Bt = traits<_Tp>::is_mapped::value>
89 struct mapped_types
90 {
91 typedef _Tp test_type;
92 typedef typename test_type::mapped_type mapped_type;
93 };
94
95 template<typename _Tp, bool = traits<_Tp>::is_adaptor::value>
96 struct adaptor_types
97 {
98 // Container adaptor requirements.
99 typedef _Tp test_type;
100 typedef typename test_type::value_type value_type;
101 typedef typename test_type::reference reference;
102 typedef typename test_type::const_reference const_reference;
103 typedef typename test_type::size_type size_type;
104 typedef typename test_type::container_type container_type;
105 };
106
107 // Conditional typedef testing, negative.
108 template<typename _Tp>
109 struct basic_types<_Tp, false> { };
110
111 template<typename _Tp>
112 struct adaptor_types<_Tp, false> { };
113
114 template<typename _Tp>
115 struct reversible_types<_Tp, false> { };
116
117 template<typename _Tp>
118 struct allocator_aware_types<_Tp, false> { };
119
120 template<typename _Tp>
121 struct associative_types<_Tp, false> { };
122
123 template<typename _Tp>
124 struct unordered_types<_Tp, false> { };
125
126 template<typename _Tp>
127 struct mapped_types<_Tp, false> { };
128
129 // Primary template.
130 template<typename _Tp>
131 struct types
132 : basic_types<_Tp>, adaptor_types<_Tp>, reversible_types<_Tp>,
133 allocator_aware_types<_Tp>, associative_types<_Tp>,
134 unordered_types<_Tp>, mapped_types<_Tp>
135 { };
136
137
138 // Run-time test for constant_iterator requirements.
139 template<typename _Tp, bool = traits<_Tp>::is_allocator_aware::value>
140 struct populate
141 {
142 populate(_Tp& container)
143 {
144 // Avoid uninitialized warnings, requires DefaultContructible.
145 typedef typename _Tp::value_type value_type;
146 container.insert(container.begin(), value_type());
147 container.insert(container.begin(), value_type());
148 }
149 };
150
151 template<typename _Tp>
152 struct populate<_Tp, false>
153 {
154 populate(_Tp& container) { }
155 };
156
157 template<typename _Tp, bool = traits<_Tp>::is_reversible::value>
158 struct reverse_members
159 {
160 reverse_members(_Tp& container)
161 {
162 assert( container.crbegin() == container.rbegin() );
163 assert( container.crend() == container.rend() );
164 assert( container.crbegin() != container.crend() );
165 }
166 };
167
168 template<typename _Tp>
169 struct reverse_members<_Tp, false>
170 {
171 reverse_members(_Tp&) { }
172 };
173
174 template<typename _Iterator,
175 bool _Mutable,
176 typename = typename std::iterator_traits<_Iterator>::iterator_category>
177 struct iterator_concept_checks;
178
179 #if __cplusplus >= 201103L
180 // DR 691.
181 template<typename _Tp>
182 struct forward_members_unordered
183 {
184 forward_members_unordered(const typename _Tp::value_type& v)
185 {
186 #pragma GCC diagnostic push
187 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
188 // Make sure that even if rel_ops is injected there is no ambiguity
189 // when comparing iterators.
190 using namespace std::rel_ops;
191 #pragma GCC diagnostic pop
192
193 typedef _Tp test_type;
194 test_type container;
195 container.insert(v);
196
197 iterator_concept_checks<typename _Tp::local_iterator, false> cc;
198 iterator_concept_checks<typename _Tp::const_local_iterator,
199 false> ccc;
200
201 assert( container.cbegin(0) == container.begin(0) );
202 assert( container.cend(0) == container.end(0) );
203 const auto bn = container.bucket(1);
204 auto clit = container.cbegin(bn);
205 assert( clit != container.cend(bn) );
206 assert( clit != container.end(bn) );
207 assert( clit++ == container.cbegin(bn) );
208 assert( clit == container.end(bn) );
209
210 clit = container.cbegin(bn);
211 assert( ++clit == container.cend(bn) );
212
213 assert( container.begin(bn) != container.cend(bn) );
214 }
215 };
216 #endif
217
218 template<typename _Iterator>
219 struct iterator_concept_checks<_Iterator, false,
220 std::forward_iterator_tag>
221 {
222 iterator_concept_checks()
223 {
224 using namespace __gnu_cxx;
225 __function_requires<_ForwardIteratorConcept<_Iterator>>();
226 }
227 };
228
229 template<typename _Iterator>
230 struct iterator_concept_checks<_Iterator, true,
231 std::forward_iterator_tag>
232 {
233 iterator_concept_checks()
234 {
235 using namespace __gnu_cxx;
236 __function_requires<_Mutable_ForwardIteratorConcept<_Iterator>>();
237 }
238 };
239
240 template<typename _Iterator>
241 struct iterator_concept_checks<_Iterator, false,
242 std::bidirectional_iterator_tag>
243 {
244 iterator_concept_checks()
245 {
246 using namespace __gnu_cxx;
247 __function_requires<_BidirectionalIteratorConcept<_Iterator>>();
248 }
249 };
250
251 template<typename _Iterator>
252 struct iterator_concept_checks<_Iterator, true,
253 std::bidirectional_iterator_tag>
254 {
255 iterator_concept_checks()
256 {
257 using namespace __gnu_cxx;
258 __function_requires<_Mutable_BidirectionalIteratorConcept<_Iterator>>();
259 }
260 };
261
262 template<typename _Iterator>
263 struct iterator_concept_checks<_Iterator, false,
264 std::random_access_iterator_tag>
265 {
266 iterator_concept_checks()
267 {
268 using namespace __gnu_cxx;
269 __function_requires<_RandomAccessIteratorConcept<_Iterator>>();
270 }
271 };
272
273 template<typename _Iterator>
274 struct iterator_concept_checks<_Iterator, true,
275 std::random_access_iterator_tag>
276 {
277 iterator_concept_checks()
278 {
279 using namespace __gnu_cxx;
280 __function_requires<_Mutable_RandomAccessIteratorConcept<_Iterator>>();
281 }
282 };
283
284 template<typename _Tp>
285 struct forward_members
286 {
287 forward_members(_Tp& container)
288 {
289 #pragma GCC diagnostic push
290 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
291 // Make sure that even if rel_ops is injected there is no ambiguity
292 // when comparing iterators.
293 using namespace std::rel_ops;
294 #pragma GCC diagnostic pop
295
296 typedef traits<_Tp> traits_type;
297 iterator_concept_checks<typename _Tp::iterator,
298 !(traits_type::is_associative::value
299 || traits_type::is_unordered::value)> cc;
300 iterator_concept_checks<typename _Tp::const_iterator, false> ccc;
301
302 assert( container.cbegin() == container.begin() );
303 assert( container.end() == container.cend() );
304 assert( container.cbegin() != container.cend() );
305 assert( container.cbegin() != container.end() );
306 assert( container.begin() != container.cend() );
307 }
308 };
309
310 template<typename _Tp,
311 typename
312 = typename std::iterator_traits<typename _Tp::iterator>::iterator_category>
313 struct category_members : forward_members<_Tp>
314 {
315 category_members(_Tp& container)
316 : forward_members<_Tp>(container)
317 { };
318 };
319
320 template<typename _Tp>
321 struct category_members<_Tp, std::random_access_iterator_tag>
322 : forward_members<_Tp>
323 {
324 category_members(_Tp& container)
325 : forward_members<_Tp>(container)
326 {
327 #pragma GCC diagnostic push
328 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
329 // Make sure that even if rel_ops is injected there is no ambiguity
330 // when comparing iterators.
331 using namespace std::rel_ops;
332 #pragma GCC diagnostic pop
333
334 assert( !(container.begin() < container.begin()) );
335 assert( !(container.cbegin() < container.cbegin()) );
336 assert( !(container.cbegin() < container.begin()) );
337 assert( !(container.begin() < container.cbegin()) );
338 assert( container.begin() <= container.begin() );
339 assert( container.cbegin() <= container.cbegin() );
340 assert( container.cbegin() <= container.begin() );
341 assert( container.begin() <= container.cbegin() );
342
343 assert( !(container.begin() > container.begin()) );
344 assert( !(container.cbegin() > container.cbegin()) );
345 assert( !(container.cbegin() > container.begin()) );
346 assert( !(container.begin() > container.cbegin()) );
347 assert( container.begin() >= container.begin() );
348 assert( container.cbegin() >= container.cbegin() );
349 assert( container.cbegin() >= container.begin() );
350 assert( container.begin() >= container.cbegin() );
351
352 assert( container.begin() - container.begin() == 0 );
353 assert( container.cbegin() - container.cbegin() == 0 );
354 assert( container.cbegin() - container.begin() == 0 );
355 assert( container.begin() - container.cbegin() == 0 );
356
357 assert( container.begin() + 0 == container.begin() );
358 assert( container.cbegin() + 0 == container.cbegin() );
359 assert( 0 + container.begin() == container.begin() );
360 assert( 0 + container.cbegin() == container.cbegin() );
361 assert( container.begin() - 0 == container.begin() );
362 assert( container.cbegin() - 0 == container.cbegin() );
363 }
364 };
365
366 template<typename _Tp>
367 struct citerator
368 {
369 typedef _Tp test_type;
370 typedef traits<test_type> traits_type;
371 typedef typename test_type::value_type value_type;
372
373 static test_type _S_container;
374
375 // Unconditional.
376 struct members : category_members<_Tp>
377 {
378 members() : category_members<_Tp>(_S_container)
379 { }
380 };
381
382 // Run test.
383 citerator()
384 {
385 populate<test_type> p(_S_container);
386 members m1;
387 reverse_members<test_type> m2(_S_container);
388 }
389 };
390
391 template<typename _Tp>
392 _Tp citerator<_Tp>::_S_container;
393
394 // DR 130 vs. C++98 vs. C++11.
395 // Defined in testsuite_shared.cc.
396 void
397 erase_external(std::set<int>& s);
398
399 void
400 erase_external(std::multiset<int>& s);
401
402 void
403 erase_external(std::map<int, int>& s);
404
405 void
406 erase_external(std::multimap<int, int>& s);
407
408 void
409 erase_external_iterators(std::set<int>& s);
410
411 void
412 erase_external_iterators(std::multiset<int>& s);
413
414 void
415 erase_external_iterators(std::map<int, int>& s);
416
417 void
418 erase_external_iterators(std::multimap<int, int>& s);
419
420 #if __cplusplus < 201103L
421 # error "must be compiled with C++11 (or later)"
422 #else
423 template<typename _Tp>
424 void
425 linkage_check_cxx98_cxx11_erase(_Tp& container)
426 {
427 // Crashing when external reference and internal reference symbols are
428 // equivalently mangled but have different size return types in C++98
429 // and C++11 signatures.
430 erase_external(container); // C++98
431 container.erase(container.begin()); // C++11
432 }
433
434 template<typename _Tp>
435 void
436 linkage_check_cxx98_cxx11_erase_iterators(_Tp& container)
437 {
438 // Crashing when external reference and internal reference symbols are
439 // equivalently mangled but have different size return types in C++98
440 // and C++11 signatures.
441 erase_external_iterators(container);// C++98
442
443 auto iter = container.begin();
444 container.erase(iter, ++iter); // C++11
445 }
446 #endif
447
448 } // namespace __gnu_test
449
450 #endif