3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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
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.
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/>.
20 #ifndef _GLIBCXX_EXCEPTION_SAFETY_H
21 #define _GLIBCXX_EXCEPTION_SAFETY_H
23 #include <testsuite_container_traits.h>
24 #include <ext/throw_allocator.h>
26 // Container requirement testing.
29 // Base class for exception testing, contains utilities.
32 typedef std::size_t size_type
;
33 typedef std::uniform_int_distribution
<size_type
> distribution_type
;
34 typedef std::mt19937 engine_type
;
36 // Return randomly generated integer on range [0, __max_size].
38 generate(size_type __max_size
)
40 // Make the generator static...
41 const engine_type engine
;
42 const distribution_type distribution
;
43 static auto generator
= std::bind(distribution
, engine
,
44 std::placeholders::_1
);
46 // ... but set the range for this particular invocation here.
47 const typename
distribution_type::param_type
p(0, __max_size
);
48 size_type random
= generator(p
);
49 if (random
< distribution
.min() || random
> distribution
.max())
51 std::string
__s("setup_base::generate");
53 __s
+= "random number generated is: ";
55 __builtin_sprintf(buf
, "%lu", (unsigned long)random
);
58 __builtin_sprintf(buf
, "%lu", (unsigned long)distribution
.min());
61 __builtin_sprintf(buf
, "%lu", (unsigned long)distribution
.max());
64 std::__throw_out_of_range(__s
.c_str());
69 // Given an instantiating type, return a unique value.
70 template<typename _Tp
>
71 struct generate_unique
73 typedef _Tp value_type
;
77 static value_type __ret
;
83 // Partial specialization for pair.
84 template<typename _Tp1
, typename _Tp2
>
85 struct generate_unique
<std::pair
<const _Tp1
, _Tp2
>>
87 typedef _Tp1 first_type
;
88 typedef _Tp2 second_type
;
89 typedef std::pair
<const _Tp1
, _Tp2
> pair_type
;
93 static first_type _S_1
;
94 static second_type _S_2
;
97 return pair_type(_S_1
, _S_2
);
101 // Partial specialization for throw_value
102 template<typename _Cond
>
103 struct generate_unique
<__gnu_cxx::throw_value_base
<_Cond
>>
105 typedef __gnu_cxx::throw_value_base
<_Cond
> value_type
;
107 operator value_type()
109 static size_t _S_i(0);
110 return value_type(_S_i
++);
115 // Construct container of size n directly. _Tp == container type.
116 template<typename _Tp
>
117 struct make_container_base
121 make_container_base() = default;
122 make_container_base(const size_type n
): _M_container(n
) { }
124 operator _Tp
&() { return _M_container
; }
127 // Construct container of size n, via multiple insertions. For
128 // associated and unordered types, unique value_type elements are
130 template<typename _Tp
, bool = traits
<_Tp
>::is_mapped::value
>
131 struct make_insert_container_base
132 : public make_container_base
<_Tp
>
134 using make_container_base
<_Tp
>::_M_container
;
135 typedef typename
_Tp::value_type value_type
;
137 make_insert_container_base(const size_type n
)
139 for (size_type i
= 0; i
< n
; ++i
)
141 value_type v
= generate_unique
<value_type
>();
142 _M_container
.insert(v
);
144 assert(_M_container
.size() == n
);
148 template<typename _Tp
>
149 struct make_insert_container_base
<_Tp
, false>
150 : public make_container_base
<_Tp
>
152 using make_container_base
<_Tp
>::_M_container
;
153 typedef typename
_Tp::value_type value_type
;
155 make_insert_container_base(const size_type n
)
157 for (size_type i
= 0; i
< n
; ++i
)
159 value_type v
= generate_unique
<value_type
>();
160 _M_container
.insert(_M_container
.end(), v
);
162 assert(_M_container
.size() == n
);
166 template<typename _Tp
, bool = traits
<_Tp
>::has_size_type_constructor::value
>
167 struct make_container_n
;
169 // Specialization for non-associative types that have a constructor with
171 template<typename _Tp
>
172 struct make_container_n
<_Tp
, true>
173 : public make_container_base
<_Tp
>
175 make_container_n(const size_type n
) : make_container_base
<_Tp
>(n
) { }
178 template<typename _Tp
>
179 struct make_container_n
<_Tp
, false>
180 : public make_insert_container_base
<_Tp
>
182 make_container_n(const size_type n
)
183 : make_insert_container_base
<_Tp
>(n
) { }
187 // Randomly size and populate a given container reference.
188 // NB: Responsibility for turning off exceptions lies with caller.
189 template<typename _Tp
, bool = traits
<_Tp
>::is_allocator_aware::value
>
192 typedef _Tp container_type
;
193 typedef typename
container_type::allocator_type allocator_type
;
194 typedef typename
container_type::value_type value_type
;
196 populate(_Tp
& __container
)
198 const allocator_type a
= __container
.get_allocator();
200 // Size test container.
201 const size_type max_elements
= 100;
202 size_type n
= generate(max_elements
);
204 // Construct new container.
205 make_container_n
<container_type
> made(n
);
206 container_type
& tmp
= made
;
207 std::swap(tmp
, __container
);
211 // Partial specialization, empty.
212 template<typename _Tp
>
213 struct populate
<_Tp
, false>
218 // Compare two containers for equivalence.
219 // Right now, that means size.
220 // Returns true if equal, throws if not.
221 template<typename _Tp
>
223 compare(const _Tp
& __control
, const _Tp
& __test
)
225 // Make sure test container is in a consistent state, as
226 // compared to the control container.
227 // NB: Should be equivalent to __test != __control, but
228 // computed without equivalence operators
229 const size_type szt
= std::distance(__test
.begin(), __test
.end());
230 const size_type szc
= std::distance(__control
.begin(),
232 bool __equal_size
= szt
== szc
;
234 // Should test iterator validity before and after exception.
235 bool __equal_it
= std::equal(__test
.begin(), __test
.end(),
238 if (!__equal_size
|| !__equal_it
)
239 throw std::logic_error("setup_base::compare containers not equal");
246 // Containing structure holding functors.
247 struct functor_base
: public setup_base
249 // Abstract the erase function.
250 template<typename _Tp
>
253 typedef typename
_Tp::iterator iterator
;
254 typedef typename
_Tp::const_iterator const_iterator
;
256 iterator (_Tp::* _F_erase_point
)(const_iterator
);
257 iterator (_Tp::* _F_erase_range
)(const_iterator
, const_iterator
);
260 : _F_erase_point(&_Tp::erase
), _F_erase_range(&_Tp::erase
) { }
263 // Specializations, old C++03 signatures.
264 template<typename _Tp1
, typename _Tp2
, typename _Tp3
>
265 struct erase_base
<std::basic_string
<_Tp1
, _Tp2
, _Tp3
>>
267 typedef std::basic_string
<_Tp1
, _Tp2
, _Tp3
> container_type
;
268 typedef typename
container_type::iterator iterator
;
270 iterator (container_type::* _F_erase_point
)(iterator
);
271 iterator (container_type::* _F_erase_range
)(iterator
, iterator
);
274 : _F_erase_point(&container_type::erase
),
275 _F_erase_range(&container_type::erase
) { }
278 template<typename _Tp1
, typename _Tp2
, typename _Tp3
,
279 template <typename
, typename
, typename
> class _Tp4
>
280 struct erase_base
<__gnu_cxx::__versa_string
<_Tp1
, _Tp2
, _Tp3
, _Tp4
>>
282 typedef __gnu_cxx::__versa_string
<_Tp1
, _Tp2
, _Tp3
, _Tp4
>
284 typedef typename
container_type::iterator iterator
;
286 iterator (container_type::* _F_erase_point
)(iterator
);
287 iterator (container_type::* _F_erase_range
)(iterator
, iterator
);
290 : _F_erase_point(&container_type::erase
),
291 _F_erase_range(&container_type::erase
) { }
294 template<typename _Tp1
, typename _Tp2
>
295 struct erase_base
<std::deque
<_Tp1
, _Tp2
>>
297 typedef std::deque
<_Tp1
, _Tp2
> container_type
;
298 typedef typename
container_type::iterator iterator
;
300 iterator (container_type::* _F_erase_point
)(iterator
);
301 iterator (container_type::* _F_erase_range
)(iterator
, iterator
);
304 : _F_erase_point(&container_type::erase
),
305 _F_erase_range(&container_type::erase
) { }
308 template<typename _Tp1
, typename _Tp2
>
309 struct erase_base
<std::list
<_Tp1
, _Tp2
>>
311 typedef std::list
<_Tp1
, _Tp2
> container_type
;
312 typedef typename
container_type::iterator iterator
;
314 iterator (container_type::* _F_erase_point
)(iterator
);
315 iterator (container_type::* _F_erase_range
)(iterator
, iterator
);
318 : _F_erase_point(&container_type::erase
),
319 _F_erase_range(&container_type::erase
) { }
322 template<typename _Tp1
, typename _Tp2
>
323 struct erase_base
<std::vector
<_Tp1
, _Tp2
>>
325 typedef std::vector
<_Tp1
, _Tp2
> container_type
;
326 typedef typename
container_type::iterator iterator
;
328 iterator (container_type::* _F_erase_point
)(iterator
);
329 iterator (container_type::* _F_erase_range
)(iterator
, iterator
);
332 : _F_erase_point(&container_type::erase
),
333 _F_erase_range(&container_type::erase
) { }
336 // Specialization, as forward_list has erase_after.
337 template<typename _Tp1
, typename _Tp2
>
338 struct erase_base
<std::forward_list
<_Tp1
, _Tp2
>>
340 typedef std::forward_list
<_Tp1
, _Tp2
> container_type
;
341 typedef typename
container_type::iterator iterator
;
342 typedef typename
container_type::const_iterator const_iterator
;
344 iterator (container_type::* _F_erase_point
)(const_iterator
);
345 iterator (container_type::* _F_erase_range
)(const_iterator
,
349 : _F_erase_point(&container_type::erase_after
),
350 _F_erase_range(&container_type::erase_after
) { }
353 template<typename _Tp
,
354 bool = traits
<_Tp
>::has_erase::value
,
355 bool = traits
<_Tp
>::has_erase_after::value
>
358 // Specialization for most containers.
359 template<typename _Tp
>
360 struct erase_point
<_Tp
, true, false> : public erase_base
<_Tp
>
362 using erase_base
<_Tp
>::_F_erase_point
;
365 operator()(_Tp
& __container
)
369 // NB: Should be equivalent to size() member function, but
370 // computed with begin() and end().
371 const size_type sz
= std::distance(__container
.begin(),
374 // NB: Lowest common denominator: use forward iterator operations.
375 auto i
= __container
.begin();
376 std::advance(i
, generate(sz
));
378 // Makes it easier to think of this as __container.erase(i)
379 (__container
.*_F_erase_point
)(i
);
381 catch(const __gnu_cxx::forced_error
&)
386 // Specialization for forward_list.
387 template<typename _Tp
>
388 struct erase_point
<_Tp
, false, true> : public erase_base
<_Tp
>
390 using erase_base
<_Tp
>::_F_erase_point
;
393 operator()(_Tp
& __container
)
397 // NB: Should be equivalent to size() member function, but
398 // computed with begin() and end().
399 const size_type sz
= std::distance(__container
.begin(),
402 // NB: Lowest common denominator: use forward iterator operations.
403 auto i
= __container
.before_begin();
404 std::advance(i
, generate(sz
));
406 // Makes it easier to think of this as __container.erase(i)
407 (__container
.*_F_erase_point
)(i
);
409 catch(const __gnu_cxx::forced_error
&)
414 // Specialization, empty.
415 template<typename _Tp
>
416 struct erase_point
<_Tp
, false, false>
423 template<typename _Tp
,
424 bool = traits
<_Tp
>::has_erase::value
,
425 bool = traits
<_Tp
>::has_erase_after::value
>
428 // Specialization for most containers.
429 template<typename _Tp
>
430 struct erase_range
<_Tp
, true, false> : public erase_base
<_Tp
>
432 using erase_base
<_Tp
>::_F_erase_range
;
435 operator()(_Tp
& __container
)
439 const size_type sz
= std::distance(__container
.begin(),
441 size_type s1
= generate(sz
);
442 size_type s2
= generate(sz
);
443 auto i1
= __container
.begin();
444 auto i2
= __container
.begin();
445 std::advance(i1
, std::min(s1
, s2
));
446 std::advance(i2
, std::max(s1
, s2
));
448 // Makes it easier to think of this as __container.erase(i1, i2).
449 (__container
.*_F_erase_range
)(i1
, i2
);
451 catch(const __gnu_cxx::forced_error
&)
456 // Specialization for forward_list.
457 template<typename _Tp
>
458 struct erase_range
<_Tp
, false, true> : public erase_base
<_Tp
>
460 using erase_base
<_Tp
>::_F_erase_range
;
463 operator()(_Tp
& __container
)
467 const size_type sz
= std::distance(__container
.begin(),
469 size_type s1
= generate(sz
);
470 size_type s2
= generate(sz
);
471 auto i1
= __container
.before_begin();
472 auto i2
= __container
.before_begin();
473 std::advance(i1
, std::min(s1
, s2
));
474 std::advance(i2
, std::max(s1
, s2
));
476 // Makes it easier to think of this as __container.erase(i1, i2).
477 (__container
.*_F_erase_range
)(i1
, i2
);
479 catch(const __gnu_cxx::forced_error
&)
484 // Specialization, empty.
485 template<typename _Tp
>
486 struct erase_range
<_Tp
, false, false>
493 template<typename _Tp
, bool = traits
<_Tp
>::has_push_pop::value
>
497 operator()(_Tp
& __container
)
501 __container
.pop_front();
503 catch(const __gnu_cxx::forced_error
&)
508 // Specialization, empty.
509 template<typename _Tp
>
510 struct pop_front
<_Tp
, false>
517 template<typename _Tp
, bool = traits
<_Tp
>::has_push_pop::value
518 && traits
<_Tp
>::is_reversible::value
>
522 operator()(_Tp
& __container
)
526 __container
.pop_back();
528 catch(const __gnu_cxx::forced_error
&)
533 // Specialization, empty.
534 template<typename _Tp
>
535 struct pop_back
<_Tp
, false>
542 template<typename _Tp
, bool = traits
<_Tp
>::has_push_pop::value
>
545 typedef _Tp container_type
;
546 typedef typename
container_type::value_type value_type
;
549 operator()(_Tp
& __test
)
553 const value_type cv
= generate_unique
<value_type
>();
554 __test
.push_front(cv
);
556 catch(const __gnu_cxx::forced_error
&)
560 // Assumes containers start out equivalent.
562 operator()(_Tp
& __control
, _Tp
& __test
)
566 const value_type cv
= generate_unique
<value_type
>();
567 __test
.push_front(cv
);
569 catch(const __gnu_cxx::forced_error
&)
574 // Specialization, empty.
575 template<typename _Tp
>
576 struct push_front
<_Tp
, false>
582 operator()(_Tp
&, _Tp
&) { }
586 template<typename _Tp
, bool = traits
<_Tp
>::has_push_pop::value
587 && traits
<_Tp
>::is_reversible::value
>
590 typedef _Tp container_type
;
591 typedef typename
container_type::value_type value_type
;
594 operator()(_Tp
& __test
)
598 const value_type cv
= generate_unique
<value_type
>();
599 __test
.push_back(cv
);
601 catch(const __gnu_cxx::forced_error
&)
605 // Assumes containers start out equivalent.
607 operator()(_Tp
& __control
, _Tp
& __test
)
611 const value_type cv
= generate_unique
<value_type
>();
612 __test
.push_back(cv
);
614 catch(const __gnu_cxx::forced_error
&)
619 // Specialization, empty.
620 template<typename _Tp
>
621 struct push_back
<_Tp
, false>
627 operator()(_Tp
&, _Tp
&) { }
631 // Abstract the insert function into two parts:
632 // 1, insert_base_functions == holds function pointer
633 // 2, insert_base == links function pointer to class insert method
634 template<typename _Tp
>
637 typedef typename
_Tp::iterator iterator
;
638 typedef typename
_Tp::const_iterator const_iterator
;
639 typedef typename
_Tp::value_type value_type
;
641 iterator (_Tp::* _F_insert_point
)(const_iterator
, const value_type
&);
643 insert_base() : _F_insert_point(&_Tp::insert
) { }
646 // Specializations, old C++03 signatures.
647 template<typename _Tp1
, typename _Tp2
>
648 struct insert_base
<std::deque
<_Tp1
, _Tp2
>>
650 typedef std::deque
<_Tp1
, _Tp2
> container_type
;
651 typedef typename
container_type::iterator iterator
;
652 typedef typename
container_type::value_type value_type
;
654 iterator (container_type::* _F_insert_point
)(iterator
,
657 insert_base() : _F_insert_point(&container_type::insert
) { }
660 template<typename _Tp1
, typename _Tp2
>
661 struct insert_base
<std::list
<_Tp1
, _Tp2
>>
663 typedef std::list
<_Tp1
, _Tp2
> container_type
;
664 typedef typename
container_type::iterator iterator
;
665 typedef typename
container_type::value_type value_type
;
667 iterator (container_type::* _F_insert_point
)(iterator
,
670 insert_base() : _F_insert_point(&container_type::insert
) { }
673 template<typename _Tp1
, typename _Tp2
>
674 struct insert_base
<std::vector
<_Tp1
, _Tp2
>>
676 typedef std::vector
<_Tp1
, _Tp2
> container_type
;
677 typedef typename
container_type::iterator iterator
;
678 typedef typename
container_type::value_type value_type
;
680 iterator (container_type::* _F_insert_point
)(iterator
,
683 insert_base() : _F_insert_point(&container_type::insert
) { }
686 // Specialization, as string insertion has a different signature.
687 template<typename _Tp1
, typename _Tp2
, typename _Tp3
>
688 struct insert_base
<std::basic_string
<_Tp1
, _Tp2
, _Tp3
>>
690 typedef std::basic_string
<_Tp1
, _Tp2
, _Tp3
> container_type
;
691 typedef typename
container_type::iterator iterator
;
692 typedef typename
container_type::value_type value_type
;
694 iterator (container_type::* _F_insert_point
)(iterator
, value_type
);
696 insert_base() : _F_insert_point(&container_type::insert
) { }
699 // Likewise for __versa_string.
700 template<typename _Tp1
, typename _Tp2
, typename _Tp3
,
701 template <typename
, typename
, typename
> class _Tp4
>
702 struct insert_base
<__gnu_cxx::__versa_string
<_Tp1
, _Tp2
, _Tp3
, _Tp4
>>
704 typedef __gnu_cxx::__versa_string
<_Tp1
, _Tp2
, _Tp3
, _Tp4
>
706 typedef typename
container_type::iterator iterator
;
707 typedef typename
container_type::value_type value_type
;
709 iterator (container_type::* _F_insert_point
)(iterator
, value_type
);
711 insert_base() : _F_insert_point(&container_type::insert
) { }
714 // Specialization, as forward_list has insert_after.
715 template<typename _Tp1
, typename _Tp2
>
716 struct insert_base
<std::forward_list
<_Tp1
, _Tp2
>>
718 typedef std::forward_list
<_Tp1
, _Tp2
> container_type
;
719 typedef typename
container_type::iterator iterator
;
720 typedef typename
container_type::const_iterator const_iterator
;
721 typedef typename
container_type::value_type value_type
;
723 iterator (container_type::* _F_insert_point
)(const_iterator
,
726 insert_base() : _F_insert_point(&container_type::insert_after
) { }
729 template<typename _Tp
,
730 bool = traits
<_Tp
>::has_insert::value
,
731 bool = traits
<_Tp
>::has_insert_after::value
>
734 // Specialization for most containers.
735 template<typename _Tp
>
736 struct insert_point
<_Tp
, true, false> : public insert_base
<_Tp
>
738 typedef _Tp container_type
;
739 typedef typename
container_type::value_type value_type
;
740 using insert_base
<_Tp
>::_F_insert_point
;
743 operator()(_Tp
& __test
)
747 const value_type cv
= generate_unique
<value_type
>();
748 const size_type sz
= std::distance(__test
.begin(), __test
.end());
749 size_type s
= generate(sz
);
750 auto i
= __test
.begin();
752 (__test
.*_F_insert_point
)(i
, cv
);
754 catch(const __gnu_cxx::forced_error
&)
758 // Assumes containers start out equivalent.
760 operator()(_Tp
& __control
, _Tp
& __test
)
764 const value_type cv
= generate_unique
<value_type
>();
765 const size_type sz
= std::distance(__test
.begin(), __test
.end());
766 size_type s
= generate(sz
);
767 auto i
= __test
.begin();
769 (__test
.*_F_insert_point
)(i
, cv
);
771 catch(const __gnu_cxx::forced_error
&)
776 // Specialization for forward_list.
777 template<typename _Tp
>
778 struct insert_point
<_Tp
, false, true> : public insert_base
<_Tp
>
780 typedef _Tp container_type
;
781 typedef typename
container_type::value_type value_type
;
782 using insert_base
<_Tp
>::_F_insert_point
;
785 operator()(_Tp
& __test
)
789 const value_type cv
= generate_unique
<value_type
>();
790 const size_type sz
= std::distance(__test
.begin(), __test
.end());
791 size_type s
= generate(sz
);
792 auto i
= __test
.before_begin();
794 (__test
.*_F_insert_point
)(i
, cv
);
796 catch(const __gnu_cxx::forced_error
&)
800 // Assumes containers start out equivalent.
802 operator()(_Tp
& __control
, _Tp
& __test
)
806 const value_type cv
= generate_unique
<value_type
>();
807 const size_type sz
= std::distance(__test
.begin(), __test
.end());
808 size_type s
= generate(sz
);
809 auto i
= __test
.before_begin();
811 (__test
.*_F_insert_point
)(i
, cv
);
813 catch(const __gnu_cxx::forced_error
&)
818 // Specialization, empty.
819 template<typename _Tp
>
820 struct insert_point
<_Tp
, false, false>
826 operator()(_Tp
&, _Tp
&) { }
830 template<typename _Tp
, bool = traits
<_Tp
>::is_associative::value
831 || traits
<_Tp
>::is_unordered::value
>
835 operator()(_Tp
& __container
)
841 catch(const __gnu_cxx::forced_error
&)
846 // Specialization, empty.
847 template<typename _Tp
>
848 struct clear
<_Tp
, false>
855 template<typename _Tp
, bool = traits
<_Tp
>::is_unordered::value
>
859 operator()(_Tp
& __test
)
863 size_type s
= generate(__test
.bucket_count());
866 catch(const __gnu_cxx::forced_error
&)
871 operator()(_Tp
& __control
, _Tp
& __test
)
875 size_type s
= generate(__test
.bucket_count());
878 catch(const __gnu_cxx::forced_error
&)
880 // Also check hash status.
882 if (__control
.load_factor() != __test
.load_factor())
884 if (__control
.max_load_factor() != __test
.max_load_factor())
886 if (__control
.bucket_count() != __test
.bucket_count())
888 if (__control
.max_bucket_count() != __test
.max_bucket_count())
894 std::string
__s("setup_base::rehash "
895 "containers not equal");
898 __s
+= "\t\t\tcontrol : test";
900 __s
+= "load_factor\t\t";
901 __builtin_sprintf(buf
, "%lu", __control
.load_factor());
904 __builtin_sprintf(buf
, "%lu", __test
.load_factor());
908 __s
+= "max_load_factor\t\t";
909 __builtin_sprintf(buf
, "%lu", __control
.max_load_factor());
912 __builtin_sprintf(buf
, "%lu", __test
.max_load_factor());
916 __s
+= "bucket_count\t\t";
917 __builtin_sprintf(buf
, "%lu", __control
.bucket_count());
920 __builtin_sprintf(buf
, "%lu", __test
.bucket_count());
924 __s
+= "max_bucket_count\t";
925 __builtin_sprintf(buf
, "%lu", __control
.max_bucket_count());
928 __builtin_sprintf(buf
, "%lu", __test
.max_bucket_count());
932 std::__throw_logic_error(__s
.c_str());
938 // Specialization, empty.
939 template<typename _Tp
>
940 struct rehash
<_Tp
, false>
946 operator()(_Tp
&, _Tp
&) { }
950 template<typename _Tp
>
956 operator()(_Tp
& __container
)
960 __container
.swap(_M_other
);
962 catch(const __gnu_cxx::forced_error
&)
968 template<typename _Tp
>
969 struct iterator_operations
971 typedef _Tp container_type
;
972 typedef typename
container_type::iterator iterator
;
975 operator()(_Tp
& __container
)
980 iterator i
= __container
.begin();
981 iterator
__attribute__((unused
)) icopy(i
);
982 iterator
__attribute__((unused
)) iassign
= i
;
984 catch(const __gnu_cxx::forced_error
&)
990 template<typename _Tp
>
991 struct const_iterator_operations
993 typedef _Tp container_type
;
994 typedef typename
container_type::const_iterator const_iterator
;
997 operator()(_Tp
& __container
)
1002 const_iterator i
= __container
.begin();
1003 const_iterator
__attribute__((unused
)) icopy(i
);
1004 const_iterator
__attribute__((unused
)) iassign
= i
;
1006 catch(const __gnu_cxx::forced_error
&)
1012 // Base class for exception tests.
1013 template<typename _Tp
>
1014 struct test_base
: public functor_base
1016 typedef _Tp container_type
;
1018 typedef functor_base base_type
;
1019 typedef populate
<container_type
> populate
;
1020 typedef make_container_n
<container_type
> make_container_n
;
1022 typedef clear
<container_type
> clear
;
1023 typedef erase_point
<container_type
> erase_point
;
1024 typedef erase_range
<container_type
> erase_range
;
1025 typedef insert_point
<container_type
> insert_point
;
1026 typedef pop_front
<container_type
> pop_front
;
1027 typedef pop_back
<container_type
> pop_back
;
1028 typedef push_front
<container_type
> push_front
;
1029 typedef push_back
<container_type
> push_back
;
1030 typedef rehash
<container_type
> rehash
;
1031 typedef swap
<container_type
> swap
;
1032 typedef iterator_operations
<container_type
> iterator_ops
;
1033 typedef const_iterator_operations
<container_type
> const_iterator_ops
;
1035 using base_type::compare
;
1039 erase_point _M_erasep
;
1040 erase_range _M_eraser
;
1041 insert_point _M_insertp
;
1044 push_front _M_pushf
;
1049 iterator_ops _M_iops
;
1050 const_iterator_ops _M_ciops
;
1054 // Run through all member functions for basic exception safety
1055 // guarantee: no resource leaks when exceptions are thrown.
1057 // Types of resources checked: memory.
1059 // For each member function, use throw_value and throw_allocator as
1060 // value_type and allocator_type to force potential exception safety
1064 // _Tp::value_type is __gnu_cxx::throw_value_*
1065 // _Tp::allocator_type is __gnu_cxx::throw_allocator_*
1066 // And that the _Cond template parameter for them both is
1067 // __gnu_cxx::limit_condition.
1068 template<typename _Tp
>
1069 struct basic_safety
: public test_base
<_Tp
>
1071 typedef _Tp container_type
;
1072 typedef test_base
<container_type
> base_type
;
1073 typedef typename
base_type::populate populate
;
1074 typedef std::function
<void(container_type
&)> function_type
;
1075 typedef __gnu_cxx::limit_condition condition_type
;
1077 using base_type::generate
;
1079 container_type _M_container
;
1080 std::vector
<function_type
> _M_functions
;
1082 basic_safety() { run(); }
1088 condition_type::never_adjustor off
;
1090 // Construct containers.
1091 populate
p1(_M_container
);
1092 populate
p2(base_type::_M_swap
._M_other
);
1094 // Construct list of member functions to exercise.
1095 _M_functions
.push_back(function_type(base_type::_M_iops
));
1096 _M_functions
.push_back(function_type(base_type::_M_ciops
));
1098 _M_functions
.push_back(function_type(base_type::_M_erasep
));
1099 _M_functions
.push_back(function_type(base_type::_M_eraser
));
1100 _M_functions
.push_back(function_type(base_type::_M_insertp
));
1101 _M_functions
.push_back(function_type(base_type::_M_popf
));
1102 _M_functions
.push_back(function_type(base_type::_M_popb
));
1103 _M_functions
.push_back(function_type(base_type::_M_pushf
));
1104 _M_functions
.push_back(function_type(base_type::_M_pushb
));
1105 _M_functions
.push_back(function_type(base_type::_M_rehash
));
1106 _M_functions
.push_back(function_type(base_type::_M_swap
));
1109 _M_functions
.push_back(function_type(base_type::_M_clear
));
1112 for (auto i
= _M_functions
.begin(); i
!= _M_functions
.end(); ++i
)
1114 function_type
& f
= *i
;
1115 run_steps_to_limit(f
);
1119 template<typename _Funct
>
1121 run_steps_to_limit(const _Funct
& __f
)
1125 auto a
= _M_container
.get_allocator();
1129 // Use the current step as an allocator label.
1134 condition_type::limit_adjustor
limit(i
);
1137 // If we get here, done.
1140 catch(const __gnu_cxx::forced_error
&)
1142 // Check this step for allocations.
1143 // NB: Will throw std::logic_error if allocations.
1144 a
.check_allocated(i
);
1146 // Check memory allocated with operator new.
1154 std::cout
<< __f
.target_type().name() << std::endl
;
1155 std::cout
<< "end count " << i
<< std::endl
;
1160 // Run through all member functions with a no throw requirement, sudden death.
1161 // all: member functions erase, pop_back, pop_front, swap
1162 // iterator copy ctor, assignment operator
1163 // unordered and associative: clear
1164 // NB: Assumes _Tp::allocator_type is __gnu_cxx::throw_allocator_random.
1165 template<typename _Tp
>
1166 struct generation_prohibited
: public test_base
<_Tp
>
1168 typedef _Tp container_type
;
1169 typedef test_base
<container_type
> base_type
;
1170 typedef typename
base_type::populate populate
;
1171 typedef __gnu_cxx::random_condition condition_type
;
1173 container_type _M_container
;
1175 generation_prohibited() { run(); }
1180 // Furthermore, assumes that the test functor will throw
1181 // forced_exception via throw_allocator, that all errors are
1182 // propagated and in error. Sudden death!
1186 condition_type::never_adjustor off
;
1187 populate
p1(_M_container
);
1188 populate
p2(base_type::_M_swap
._M_other
);
1193 condition_type::always_adjustor on
;
1195 // NB: Vector and deque are special, erase can throw if the copy
1196 // constructor or assignment operator of value_type throws.
1197 if (!traits
<container_type
>::has_throwing_erase::value
)
1199 this->_M_erasep(_M_container
);
1200 this->_M_eraser(_M_container
);
1203 this->_M_popf(_M_container
);
1204 this->_M_popb(_M_container
);
1206 this->_M_iops(_M_container
);
1207 this->_M_ciops(_M_container
);
1209 this->_M_swap(_M_container
);
1212 this->_M_clear(_M_container
);
1218 // Test strong exception guarantee.
1219 // Run through all member functions with a roll-back, consistent
1220 // coherent requirement.
1221 // all: member functions insert of a single element, push_back, push_front
1222 // unordered: rehash
1223 template<typename _Tp
>
1224 struct propagation_consistent
: public test_base
<_Tp
>
1226 typedef _Tp container_type
;
1227 typedef test_base
<container_type
> base_type
;
1228 typedef typename
base_type::populate populate
;
1229 typedef std::function
<void(container_type
&)> function_type
;
1230 typedef __gnu_cxx::limit_condition condition_type
;
1232 using base_type::compare
;
1234 container_type _M_container_test
;
1235 container_type _M_container_control
;
1236 std::vector
<function_type
> _M_functions
;
1238 propagation_consistent() { run(); }
1242 { _M_container_test
= _M_container_control
; }
1249 condition_type::never_adjustor off
;
1251 // Construct containers.
1252 populate
p(_M_container_control
);
1255 // Construct list of member functions to exercise.
1256 _M_functions
.push_back(function_type(base_type::_M_pushf
));
1257 _M_functions
.push_back(function_type(base_type::_M_pushb
));
1258 _M_functions
.push_back(function_type(base_type::_M_insertp
));
1259 _M_functions
.push_back(function_type(base_type::_M_rehash
));
1262 for (auto i
= _M_functions
.begin(); i
!= _M_functions
.end(); ++i
)
1264 function_type
& f
= *i
;
1265 run_steps_to_limit(f
);
1269 template<typename _Funct
>
1271 run_steps_to_limit(const _Funct
& __f
)
1282 condition_type::limit_adjustor
limit(i
);
1283 __f(_M_container_test
);
1285 // If we get here, done.
1288 catch(const __gnu_cxx::forced_error
&)
1290 compare(_M_container_control
, _M_container_test
);
1297 std::cout
<< __f
.target_type().name() << std::endl
;
1298 std::cout
<< "end count " << i
<< std::endl
;
1302 } // namespace __gnu_test