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", random);
58 __builtin_sprintf(buf, "%lu", distribution.min());
61 __builtin_sprintf(buf, "%lu", 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;
255 iterator (_Tp::* _F_erase_point)(iterator);
256 iterator (_Tp::* _F_erase_range)(iterator, iterator);
259 : _F_erase_point(&_Tp::erase), _F_erase_range(&_Tp::erase) { }
262 // Specialization, as forward_list has erase_after.
263 template<typename _Tp1, typename _Tp2>
264 struct erase_base<std::forward_list<_Tp1, _Tp2>>
266 typedef std::forward_list<_Tp1, _Tp2> container_type;
267 typedef typename container_type::iterator iterator;
268 typedef typename container_type::const_iterator const_iterator;
270 void (container_type::* _F_erase_point)(const_iterator);
271 void (container_type::* _F_erase_range)(const_iterator, const_iterator);
274 : _F_erase_point(&container_type::erase_after),
275 _F_erase_range(&container_type::erase_after) { }
278 template<typename _Tp, bool = traits<_Tp>::has_erase::value>
279 struct erase_point : public erase_base<_Tp>
281 using erase_base<_Tp>::_F_erase_point;
284 operator()(_Tp& __container)
288 // NB: Should be equivalent to size() member function, but
289 // computed with begin() and end().
290 const size_type sz = std::distance(__container.begin(),
293 // NB: Lowest common denominator: use forward iterator operations.
294 auto i = __container.begin();
295 std::advance(i, generate(sz));
297 // Makes it easier to think of this as __container.erase(i)
298 (__container.*_F_erase_point)(i);
300 catch(const __gnu_cxx::forced_error&)
305 // Specialization, empty.
306 template<typename _Tp>
307 struct erase_point<_Tp, false>
314 template<typename _Tp, bool = traits<_Tp>::has_erase::value>
315 struct erase_range : public erase_base<_Tp>
317 using erase_base<_Tp>::_F_erase_range;
320 operator()(_Tp& __container)
324 const size_type sz = std::distance(__container.begin(),
326 size_type s1 = generate(sz);
327 size_type s2 = generate(sz);
328 auto i1 = __container.begin();
329 auto i2 = __container.begin();
330 std::advance(i1, std::min(s1, s2));
331 std::advance(i2, std::max(s1, s2));
333 // Makes it easier to think of this as __container.erase(i1, i2).
334 (__container.*_F_erase_range)(i1, i2);
336 catch(const __gnu_cxx::forced_error&)
341 // Specialization, empty.
342 template<typename _Tp>
343 struct erase_range<_Tp, false>
350 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value>
354 operator()(_Tp& __container)
358 __container.pop_front();
360 catch(const __gnu_cxx::forced_error&)
365 // Specialization, empty.
366 template<typename _Tp>
367 struct pop_front<_Tp, false>
374 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value
375 && traits<_Tp>::is_reversible::value>
379 operator()(_Tp& __container)
383 __container.pop_back();
385 catch(const __gnu_cxx::forced_error&)
390 // Specialization, empty.
391 template<typename _Tp>
392 struct pop_back<_Tp, false>
399 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value>
402 typedef _Tp container_type;
403 typedef typename container_type::value_type value_type;
406 operator()(_Tp& __test)
410 const value_type cv = generate_unique<value_type>();
411 __test.push_front(cv);
413 catch(const __gnu_cxx::forced_error&)
417 // Assumes containers start out equivalent.
419 operator()(_Tp& __control, _Tp& __test)
423 const value_type cv = generate_unique<value_type>();
424 __test.push_front(cv);
426 catch(const __gnu_cxx::forced_error&)
431 // Specialization, empty.
432 template<typename _Tp>
433 struct push_front<_Tp, false>
439 operator()(_Tp&, _Tp&) { }
443 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value
444 && traits<_Tp>::is_reversible::value>
447 typedef _Tp container_type;
448 typedef typename container_type::value_type value_type;
451 operator()(_Tp& __test)
455 const value_type cv = generate_unique<value_type>();
456 __test.push_back(cv);
458 catch(const __gnu_cxx::forced_error&)
462 // Assumes containers start out equivalent.
464 operator()(_Tp& __control, _Tp& __test)
468 const value_type cv = generate_unique<value_type>();
469 __test.push_back(cv);
471 catch(const __gnu_cxx::forced_error&)
476 // Specialization, empty.
477 template<typename _Tp>
478 struct push_back<_Tp, false>
484 operator()(_Tp&, _Tp&) { }
488 // Abstract the insert function into two parts:
489 // 1, insert_base_functions == holds function pointer
490 // 2, insert_base == links function pointer to class insert method
491 template<typename _Tp>
494 typedef typename _Tp::iterator iterator;
495 typedef typename _Tp::value_type value_type;
497 iterator (_Tp::* _F_insert_point)(iterator, const value_type&);
499 insert_base() : _F_insert_point(&_Tp::insert) { }
502 // Specialization, as string insertion has a different signature.
503 template<typename _Tp1, typename _Tp2, typename _Tp3>
504 struct insert_base<std::basic_string<_Tp1, _Tp2, _Tp3>>
506 typedef std::basic_string<_Tp1, _Tp2, _Tp3> container_type;
507 typedef typename container_type::iterator iterator;
508 typedef typename container_type::value_type value_type;
510 iterator (container_type::* _F_insert_point)(iterator, value_type);
512 insert_base() : _F_insert_point(&container_type::insert) { }
515 template<typename _Tp1, typename _Tp2, typename _Tp3,
516 template <typename, typename, typename> class _Tp4>
517 struct insert_base<__gnu_cxx::__versa_string<_Tp1, _Tp2, _Tp3, _Tp4>>
519 typedef __gnu_cxx::__versa_string<_Tp1, _Tp2, _Tp3, _Tp4>
521 typedef typename container_type::iterator iterator;
522 typedef typename container_type::value_type value_type;
524 iterator (container_type::* _F_insert_point)(iterator, value_type);
526 insert_base() : _F_insert_point(&container_type::insert) { }
529 // Specialization, as forward_list insertion has a different signature.
530 template<typename _Tp1, typename _Tp2>
531 struct insert_base<std::forward_list<_Tp1, _Tp2>>
533 typedef std::forward_list<_Tp1, _Tp2> container_type;
534 typedef typename container_type::iterator iterator;
535 typedef typename container_type::const_iterator const_iterator;
536 typedef typename container_type::value_type value_type;
538 iterator (container_type::* _F_insert_point)(const_iterator,
541 insert_base() : _F_insert_point(&container_type::insert_after) { }
544 template<typename _Tp, bool = traits<_Tp>::has_insert::value>
545 struct insert_point : public insert_base<_Tp>
547 typedef _Tp container_type;
548 typedef typename container_type::value_type value_type;
549 using insert_base<_Tp>::_F_insert_point;
552 operator()(_Tp& __test)
556 const value_type cv = generate_unique<value_type>();
557 const size_type sz = std::distance(__test.begin(), __test.end());
558 size_type s = generate(sz);
559 auto i = __test.begin();
561 (__test.*_F_insert_point)(i, cv);
563 catch(const __gnu_cxx::forced_error&)
567 // Assumes containers start out equivalent.
569 operator()(_Tp& __control, _Tp& __test)
573 const value_type cv = generate_unique<value_type>();
574 const size_type sz = std::distance(__test.begin(), __test.end());
575 size_type s = generate(sz);
576 auto i = __test.begin();
578 (__test.*_F_insert_point)(i, cv);
580 catch(const __gnu_cxx::forced_error&)
585 // Specialization, empty.
586 template<typename _Tp>
587 struct insert_point<_Tp, false>
593 operator()(_Tp&, _Tp&) { }
597 template<typename _Tp, bool = traits<_Tp>::is_associative::value
598 || traits<_Tp>::is_unordered::value>
602 operator()(_Tp& __container)
608 catch(const __gnu_cxx::forced_error&)
613 // Specialization, empty.
614 template<typename _Tp>
615 struct clear<_Tp, false>
622 template<typename _Tp, bool = traits<_Tp>::is_unordered::value>
626 operator()(_Tp& __test)
630 size_type s = generate(__test.bucket_count());
633 catch(const __gnu_cxx::forced_error&)
638 operator()(_Tp& __control, _Tp& __test)
642 size_type s = generate(__test.bucket_count());
645 catch(const __gnu_cxx::forced_error&)
647 // Also check hash status.
649 if (__control.load_factor() != __test.load_factor())
651 if (__control.max_load_factor() != __test.max_load_factor())
653 if (__control.bucket_count() != __test.bucket_count())
655 if (__control.max_bucket_count() != __test.max_bucket_count())
661 std::string __s("setup_base::rehash "
662 "containers not equal");
665 __s += "\t\t\tcontrol : test";
667 __s += "load_factor\t\t";
668 __builtin_sprintf(buf, "%lu", __control.load_factor());
671 __builtin_sprintf(buf, "%lu", __test.load_factor());
675 __s += "max_load_factor\t\t";
676 __builtin_sprintf(buf, "%lu", __control.max_load_factor());
679 __builtin_sprintf(buf, "%lu", __test.max_load_factor());
683 __s += "bucket_count\t\t";
684 __builtin_sprintf(buf, "%lu", __control.bucket_count());
687 __builtin_sprintf(buf, "%lu", __test.bucket_count());
691 __s += "max_bucket_count\t";
692 __builtin_sprintf(buf, "%lu", __control.max_bucket_count());
695 __builtin_sprintf(buf, "%lu", __test.max_bucket_count());
699 std::__throw_logic_error(__s.c_str());
705 // Specialization, empty.
706 template<typename _Tp>
707 struct rehash<_Tp, false>
713 operator()(_Tp&, _Tp&) { }
717 template<typename _Tp>
723 operator()(_Tp& __container)
727 __container.swap(_M_other);
729 catch(const __gnu_cxx::forced_error&)
735 template<typename _Tp>
736 struct iterator_operations
738 typedef _Tp container_type;
739 typedef typename container_type::iterator iterator;
742 operator()(_Tp& __container)
747 iterator i = __container.begin();
748 iterator __attribute__((unused)) icopy(i);
749 iterator __attribute__((unused)) iassign = i;
751 catch(const __gnu_cxx::forced_error&)
757 template<typename _Tp>
758 struct const_iterator_operations
760 typedef _Tp container_type;
761 typedef typename container_type::const_iterator const_iterator;
764 operator()(_Tp& __container)
769 const_iterator i = __container.begin();
770 const_iterator __attribute__((unused)) icopy(i);
771 const_iterator __attribute__((unused)) iassign = i;
773 catch(const __gnu_cxx::forced_error&)
779 // Base class for exception tests.
780 template<typename _Tp>
781 struct test_base: public functor_base
783 typedef _Tp container_type;
785 typedef functor_base base_type;
786 typedef populate<container_type> populate;
787 typedef make_container_n<container_type> make_container_n;
789 typedef clear<container_type> clear;
790 typedef erase_point<container_type> erase_point;
791 typedef erase_range<container_type> erase_range;
792 typedef insert_point<container_type> insert_point;
793 typedef pop_front<container_type> pop_front;
794 typedef pop_back<container_type> pop_back;
795 typedef push_front<container_type> push_front;
796 typedef push_back<container_type> push_back;
797 typedef rehash<container_type> rehash;
798 typedef swap<container_type> swap;
799 typedef iterator_operations<container_type> iterator_ops;
800 typedef const_iterator_operations<container_type> const_iterator_ops;
802 using base_type::compare;
806 erase_point _M_erasep;
807 erase_range _M_eraser;
808 insert_point _M_insertp;
816 iterator_ops _M_iops;
817 const_iterator_ops _M_ciops;
821 // Run through all member functions for basic exception safety
822 // guarantee: no resource leaks when exceptions are thrown.
824 // Types of resources checked: memory.
826 // For each member function, use throw_value and throw_allocator as
827 // value_type and allocator_type to force potential exception safety
831 // _Tp::value_type is __gnu_cxx::throw_value_*
832 // _Tp::allocator_type is __gnu_cxx::throw_allocator_*
833 // And that the _Cond template parameter for them both is
834 // __gnu_cxx::limit_condition.
835 template<typename _Tp>
836 struct basic_safety : public test_base<_Tp>
838 typedef _Tp container_type;
839 typedef test_base<container_type> base_type;
840 typedef typename base_type::populate populate;
841 typedef std::function<void(container_type&)> function_type;
842 typedef __gnu_cxx::limit_condition condition_type;
844 using base_type::generate;
846 container_type _M_container;
847 std::vector<function_type> _M_functions;
849 basic_safety() { run(); }
855 condition_type::never_adjustor off;
857 // Construct containers.
858 populate p1(_M_container);
859 populate p2(base_type::_M_swap._M_other);
861 // Construct list of member functions to exercise.
862 _M_functions.push_back(function_type(base_type::_M_iops));
863 _M_functions.push_back(function_type(base_type::_M_ciops));
865 _M_functions.push_back(function_type(base_type::_M_erasep));
866 _M_functions.push_back(function_type(base_type::_M_eraser));
867 _M_functions.push_back(function_type(base_type::_M_insertp));
868 _M_functions.push_back(function_type(base_type::_M_popf));
869 _M_functions.push_back(function_type(base_type::_M_popb));
870 _M_functions.push_back(function_type(base_type::_M_pushf));
871 _M_functions.push_back(function_type(base_type::_M_pushb));
872 _M_functions.push_back(function_type(base_type::_M_rehash));
873 _M_functions.push_back(function_type(base_type::_M_swap));
876 _M_functions.push_back(function_type(base_type::_M_clear));
879 auto i = _M_functions.begin();
880 for (auto i = _M_functions.begin(); i != _M_functions.end(); ++i)
882 function_type& f = *i;
883 run_steps_to_limit(f);
887 template<typename _Funct>
889 run_steps_to_limit(const _Funct& __f)
893 auto a = _M_container.get_allocator();
897 // Use the current step as an allocator label.
902 condition_type::limit_adjustor limit(i);
905 // If we get here, done.
908 catch(const __gnu_cxx::forced_error&)
910 // Check this step for allocations.
911 // NB: Will throw std::logic_error if allocations.
912 a.check_allocated(i);
914 // Check memory allocated with operator new.
922 std::cout << __f.target_type().name() << std::endl;
923 std::cout << "end count " << i << std::endl;
928 // Run through all member functions with a no throw requirement, sudden death.
929 // all: member functions erase, pop_back, pop_front, swap
930 // iterator copy ctor, assignment operator
931 // unordered and associative: clear
932 // NB: Assumes _Tp::allocator_type is __gnu_cxx::throw_allocator_random.
933 template<typename _Tp>
934 struct generation_prohibited : public test_base<_Tp>
936 typedef _Tp container_type;
937 typedef test_base<container_type> base_type;
938 typedef typename base_type::populate populate;
939 typedef __gnu_cxx::random_condition condition_type;
941 container_type _M_container;
943 generation_prohibited() { run(); }
948 // Furthermore, assumes that the test functor will throw
949 // forced_exception via throw_allocator, that all errors are
950 // propagated and in error. Sudden death!
954 condition_type::never_adjustor off;
955 populate p1(_M_container);
956 populate p2(base_type::_M_swap._M_other);
961 condition_type::always_adjustor on;
963 _M_erasep(_M_container);
964 _M_eraser(_M_container);
966 _M_popf(_M_container);
967 _M_popb(_M_container);
969 _M_iops(_M_container);
970 _M_ciops(_M_container);
972 _M_swap(_M_container);
975 _M_clear(_M_container);
981 // Test strong exception guarantee.
982 // Run through all member functions with a roll-back, consistent
983 // coherent requirement.
984 // all: member functions insert of a single element, push_back, push_front
986 template<typename _Tp>
987 struct propagation_consistent : public test_base<_Tp>
989 typedef _Tp container_type;
990 typedef test_base<container_type> base_type;
991 typedef typename base_type::populate populate;
992 typedef std::function<void(container_type&)> function_type;
993 typedef __gnu_cxx::limit_condition condition_type;
995 using base_type::compare;
997 container_type _M_container_test;
998 container_type _M_container_control;
999 std::vector<function_type> _M_functions;
1001 propagation_consistent() { run(); }
1005 { _M_container_test = _M_container_control; }
1012 condition_type::never_adjustor off;
1014 // Construct containers.
1015 populate p(_M_container_control);
1018 // Construct list of member functions to exercise.
1019 _M_functions.push_back(function_type(base_type::_M_pushf));
1020 _M_functions.push_back(function_type(base_type::_M_pushb));
1021 _M_functions.push_back(function_type(base_type::_M_insertp));
1022 _M_functions.push_back(function_type(base_type::_M_rehash));
1025 auto i = _M_functions.begin();
1026 for (auto i = _M_functions.begin(); i != _M_functions.end(); ++i)
1028 function_type& f = *i;
1029 run_steps_to_limit(f);
1033 template<typename _Funct>
1035 run_steps_to_limit(const _Funct& __f)
1046 condition_type::limit_adjustor limit(i);
1047 __f(_M_container_test);
1049 // If we get here, done.
1052 catch(const __gnu_cxx::forced_error&)
1054 compare(_M_container_control, _M_container_test);
1061 std::cout << __f.target_type().name() << std::endl;
1062 std::cout << "end count " << i << std::endl;
1066 } // namespace __gnu_test