Merge from mainline (160224:163495).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / testsuite / util / exception / safety.h
blob046eab9e51380bfe1be06479b8c4901d45d073a3
1 // -*- C++ -*-
3 // Copyright (C) 2009, 2010 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.
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.
27 namespace __gnu_test
29 // Base class for exception testing, contains utilities.
30 struct setup_base
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].
37 static size_type
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");
52 __s += "\n";
53 __s += "random number generated is: ";
54 char buf[40];
55 __builtin_sprintf(buf, "%lu", random);
56 __s += buf;
57 __s += " on range [";
58 __builtin_sprintf(buf, "%lu", distribution.min());
59 __s += buf;
60 __s += ", ";
61 __builtin_sprintf(buf, "%lu", distribution.max());
62 __s += buf;
63 __s += "]\n";
64 std::__throw_out_of_range(__s.c_str());
66 return random;
69 // Given an instantiating type, return a unique value.
70 template<typename _Tp>
71 struct generate_unique
73 typedef _Tp value_type;
75 operator value_type()
77 static value_type __ret;
78 ++__ret;
79 return __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;
91 operator pair_type()
93 static first_type _S_1;
94 static second_type _S_2;
95 ++_S_1;
96 ++_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
119 _Tp _M_container;
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
129 // necessary.
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
170 // a size argument.
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>
190 struct populate
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>
215 populate(_Tp&) { }
218 // Compare two containers for equivalence.
219 // Right now, that means size.
220 // Returns true if equal, throws if not.
221 template<typename _Tp>
222 static bool
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(),
231 __control.end());
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(),
236 __control.begin());
238 if (!__equal_size || !__equal_it)
239 throw std::logic_error("setup_base::compare containers not equal");
241 return true;
246 // Containing structure holding functors.
247 struct functor_base : public setup_base
249 // Abstract the erase function.
250 template<typename _Tp>
251 struct erase_base
253 typedef typename _Tp::iterator iterator;
255 iterator (_Tp::* _F_erase_point)(iterator);
256 iterator (_Tp::* _F_erase_range)(iterator, iterator);
258 erase_base()
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);
273 erase_base()
274 : _F_erase_point(&container_type::erase_after),
275 _F_erase_range(&container_type::erase_after) { }
278 // Specializations for the unordered containers.
279 template<typename _Tp1, typename _Tp2, typename _Tp3,
280 typename _Tp4, typename _Tp5>
281 struct erase_base<std::unordered_map<_Tp1, _Tp2, _Tp3, _Tp4, _Tp5>>
283 typedef std::unordered_map<_Tp1, _Tp2, _Tp3, _Tp4, _Tp5>
284 container_type;
285 typedef typename container_type::iterator iterator;
286 typedef typename container_type::const_iterator const_iterator;
288 iterator (container_type::* _F_erase_point)(const_iterator);
289 iterator (container_type::* _F_erase_range)(const_iterator,
290 const_iterator);
292 erase_base()
293 : _F_erase_point(&container_type::erase),
294 _F_erase_range(&container_type::erase) { }
297 template<typename _Tp1, typename _Tp2, typename _Tp3,
298 typename _Tp4, typename _Tp5>
299 struct erase_base<std::unordered_multimap<_Tp1, _Tp2, _Tp3,
300 _Tp4, _Tp5>>
302 typedef std::unordered_multimap<_Tp1, _Tp2, _Tp3, _Tp4, _Tp5>
303 container_type;
304 typedef typename container_type::iterator iterator;
305 typedef typename container_type::const_iterator const_iterator;
307 iterator (container_type::* _F_erase_point)(const_iterator);
308 iterator (container_type::* _F_erase_range)(const_iterator,
309 const_iterator);
311 erase_base()
312 : _F_erase_point(&container_type::erase),
313 _F_erase_range(&container_type::erase) { }
316 template<typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4>
317 struct erase_base<std::unordered_set<_Tp1, _Tp2, _Tp3, _Tp4>>
319 typedef std::unordered_set<_Tp1, _Tp2, _Tp3, _Tp4>
320 container_type;
321 typedef typename container_type::iterator iterator;
322 typedef typename container_type::const_iterator const_iterator;
324 iterator (container_type::* _F_erase_point)(const_iterator);
325 iterator (container_type::* _F_erase_range)(const_iterator,
326 const_iterator);
328 erase_base()
329 : _F_erase_point(&container_type::erase),
330 _F_erase_range(&container_type::erase) { }
333 template<typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4>
334 struct erase_base<std::unordered_multiset<_Tp1, _Tp2, _Tp3, _Tp4>>
336 typedef std::unordered_multiset<_Tp1, _Tp2, _Tp3, _Tp4>
337 container_type;
338 typedef typename container_type::iterator iterator;
339 typedef typename container_type::const_iterator const_iterator;
341 iterator (container_type::* _F_erase_point)(const_iterator);
342 iterator (container_type::* _F_erase_range)(const_iterator,
343 const_iterator);
345 erase_base()
346 : _F_erase_point(&container_type::erase),
347 _F_erase_range(&container_type::erase) { }
350 template<typename _Tp, bool = traits<_Tp>::has_erase::value>
351 struct erase_point : public erase_base<_Tp>
353 using erase_base<_Tp>::_F_erase_point;
355 void
356 operator()(_Tp& __container)
360 // NB: Should be equivalent to size() member function, but
361 // computed with begin() and end().
362 const size_type sz = std::distance(__container.begin(),
363 __container.end());
365 // NB: Lowest common denominator: use forward iterator operations.
366 auto i = __container.begin();
367 std::advance(i, generate(sz));
369 // Makes it easier to think of this as __container.erase(i)
370 (__container.*_F_erase_point)(i);
372 catch(const __gnu_cxx::forced_error&)
373 { throw; }
377 // Specialization, empty.
378 template<typename _Tp>
379 struct erase_point<_Tp, false>
381 void
382 operator()(_Tp&) { }
386 template<typename _Tp, bool = traits<_Tp>::has_erase::value>
387 struct erase_range : public erase_base<_Tp>
389 using erase_base<_Tp>::_F_erase_range;
391 void
392 operator()(_Tp& __container)
396 const size_type sz = std::distance(__container.begin(),
397 __container.end());
398 size_type s1 = generate(sz);
399 size_type s2 = generate(sz);
400 auto i1 = __container.begin();
401 auto i2 = __container.begin();
402 std::advance(i1, std::min(s1, s2));
403 std::advance(i2, std::max(s1, s2));
405 // Makes it easier to think of this as __container.erase(i1, i2).
406 (__container.*_F_erase_range)(i1, i2);
408 catch(const __gnu_cxx::forced_error&)
409 { throw; }
413 // Specialization, empty.
414 template<typename _Tp>
415 struct erase_range<_Tp, false>
417 void
418 operator()(_Tp&) { }
422 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value>
423 struct pop_front
425 void
426 operator()(_Tp& __container)
430 __container.pop_front();
432 catch(const __gnu_cxx::forced_error&)
433 { throw; }
437 // Specialization, empty.
438 template<typename _Tp>
439 struct pop_front<_Tp, false>
441 void
442 operator()(_Tp&) { }
446 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value
447 && traits<_Tp>::is_reversible::value>
448 struct pop_back
450 void
451 operator()(_Tp& __container)
455 __container.pop_back();
457 catch(const __gnu_cxx::forced_error&)
458 { throw; }
462 // Specialization, empty.
463 template<typename _Tp>
464 struct pop_back<_Tp, false>
466 void
467 operator()(_Tp&) { }
471 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value>
472 struct push_front
474 typedef _Tp container_type;
475 typedef typename container_type::value_type value_type;
477 void
478 operator()(_Tp& __test)
482 const value_type cv = generate_unique<value_type>();
483 __test.push_front(cv);
485 catch(const __gnu_cxx::forced_error&)
486 { throw; }
489 // Assumes containers start out equivalent.
490 void
491 operator()(_Tp& __control, _Tp& __test)
495 const value_type cv = generate_unique<value_type>();
496 __test.push_front(cv);
498 catch(const __gnu_cxx::forced_error&)
499 { throw; }
503 // Specialization, empty.
504 template<typename _Tp>
505 struct push_front<_Tp, false>
507 void
508 operator()(_Tp&) { }
510 void
511 operator()(_Tp&, _Tp&) { }
515 template<typename _Tp, bool = traits<_Tp>::has_push_pop::value
516 && traits<_Tp>::is_reversible::value>
517 struct push_back
519 typedef _Tp container_type;
520 typedef typename container_type::value_type value_type;
522 void
523 operator()(_Tp& __test)
527 const value_type cv = generate_unique<value_type>();
528 __test.push_back(cv);
530 catch(const __gnu_cxx::forced_error&)
531 { throw; }
534 // Assumes containers start out equivalent.
535 void
536 operator()(_Tp& __control, _Tp& __test)
540 const value_type cv = generate_unique<value_type>();
541 __test.push_back(cv);
543 catch(const __gnu_cxx::forced_error&)
544 { throw; }
548 // Specialization, empty.
549 template<typename _Tp>
550 struct push_back<_Tp, false>
552 void
553 operator()(_Tp&) { }
555 void
556 operator()(_Tp&, _Tp&) { }
560 // Abstract the insert function into two parts:
561 // 1, insert_base_functions == holds function pointer
562 // 2, insert_base == links function pointer to class insert method
563 template<typename _Tp>
564 struct insert_base
566 typedef typename _Tp::iterator iterator;
567 typedef typename _Tp::value_type value_type;
569 iterator (_Tp::* _F_insert_point)(iterator, const value_type&);
571 insert_base() : _F_insert_point(&_Tp::insert) { }
574 // Specialization, as string insertion has a different signature.
575 template<typename _Tp1, typename _Tp2, typename _Tp3>
576 struct insert_base<std::basic_string<_Tp1, _Tp2, _Tp3>>
578 typedef std::basic_string<_Tp1, _Tp2, _Tp3> container_type;
579 typedef typename container_type::iterator iterator;
580 typedef typename container_type::value_type value_type;
582 iterator (container_type::* _F_insert_point)(iterator, value_type);
584 insert_base() : _F_insert_point(&container_type::insert) { }
587 template<typename _Tp1, typename _Tp2, typename _Tp3,
588 template <typename, typename, typename> class _Tp4>
589 struct insert_base<__gnu_cxx::__versa_string<_Tp1, _Tp2, _Tp3, _Tp4>>
591 typedef __gnu_cxx::__versa_string<_Tp1, _Tp2, _Tp3, _Tp4>
592 container_type;
593 typedef typename container_type::iterator iterator;
594 typedef typename container_type::value_type value_type;
596 iterator (container_type::* _F_insert_point)(iterator, value_type);
598 insert_base() : _F_insert_point(&container_type::insert) { }
601 // Specialization, as forward_list insertion has a different signature.
602 template<typename _Tp1, typename _Tp2>
603 struct insert_base<std::forward_list<_Tp1, _Tp2>>
605 typedef std::forward_list<_Tp1, _Tp2> container_type;
606 typedef typename container_type::iterator iterator;
607 typedef typename container_type::const_iterator const_iterator;
608 typedef typename container_type::value_type value_type;
610 iterator (container_type::* _F_insert_point)(const_iterator,
611 const value_type&);
613 insert_base() : _F_insert_point(&container_type::insert_after) { }
616 // Likewise for the unordered containers.
617 template<typename _Tp1, typename _Tp2, typename _Tp3,
618 typename _Tp4, typename _Tp5>
619 struct insert_base<std::unordered_map<_Tp1, _Tp2, _Tp3, _Tp4, _Tp5>>
621 typedef std::unordered_map<_Tp1, _Tp2, _Tp3, _Tp4, _Tp5>
622 container_type;
623 typedef typename container_type::iterator iterator;
624 typedef typename container_type::const_iterator const_iterator;
625 typedef typename container_type::value_type value_type;
627 iterator (container_type::* _F_insert_point)(const_iterator,
628 const value_type&);
630 insert_base() : _F_insert_point(&container_type::insert) { }
633 template<typename _Tp1, typename _Tp2, typename _Tp3,
634 typename _Tp4, typename _Tp5>
635 struct insert_base<std::unordered_multimap<_Tp1, _Tp2, _Tp3,
636 _Tp4, _Tp5>>
638 typedef std::unordered_multimap<_Tp1, _Tp2, _Tp3, _Tp4, _Tp5>
639 container_type;
640 typedef typename container_type::iterator iterator;
641 typedef typename container_type::const_iterator const_iterator;
642 typedef typename container_type::value_type value_type;
644 iterator (container_type::* _F_insert_point)(const_iterator,
645 const value_type&);
647 insert_base() : _F_insert_point(&container_type::insert) { }
650 template<typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4>
651 struct insert_base<std::unordered_set<_Tp1, _Tp2, _Tp3, _Tp4>>
653 typedef std::unordered_set<_Tp1, _Tp2, _Tp3, _Tp4>
654 container_type;
655 typedef typename container_type::iterator iterator;
656 typedef typename container_type::const_iterator const_iterator;
657 typedef typename container_type::value_type value_type;
659 iterator (container_type::* _F_insert_point)(const_iterator,
660 const value_type&);
662 insert_base() : _F_insert_point(&container_type::insert) { }
665 template<typename _Tp1, typename _Tp2, typename _Tp3, typename _Tp4>
666 struct insert_base<std::unordered_multiset<_Tp1, _Tp2, _Tp3, _Tp4>>
668 typedef std::unordered_multiset<_Tp1, _Tp2, _Tp3, _Tp4>
669 container_type;
670 typedef typename container_type::iterator iterator;
671 typedef typename container_type::const_iterator const_iterator;
672 typedef typename container_type::value_type value_type;
674 iterator (container_type::* _F_insert_point)(const_iterator,
675 const value_type&);
677 insert_base() : _F_insert_point(&container_type::insert) { }
680 template<typename _Tp, bool = traits<_Tp>::has_insert::value>
681 struct insert_point : public insert_base<_Tp>
683 typedef _Tp container_type;
684 typedef typename container_type::value_type value_type;
685 using insert_base<_Tp>::_F_insert_point;
687 void
688 operator()(_Tp& __test)
692 const value_type cv = generate_unique<value_type>();
693 const size_type sz = std::distance(__test.begin(), __test.end());
694 size_type s = generate(sz);
695 auto i = __test.begin();
696 std::advance(i, s);
697 (__test.*_F_insert_point)(i, cv);
699 catch(const __gnu_cxx::forced_error&)
700 { throw; }
703 // Assumes containers start out equivalent.
704 void
705 operator()(_Tp& __control, _Tp& __test)
709 const value_type cv = generate_unique<value_type>();
710 const size_type sz = std::distance(__test.begin(), __test.end());
711 size_type s = generate(sz);
712 auto i = __test.begin();
713 std::advance(i, s);
714 (__test.*_F_insert_point)(i, cv);
716 catch(const __gnu_cxx::forced_error&)
717 { throw; }
721 // Specialization, empty.
722 template<typename _Tp>
723 struct insert_point<_Tp, false>
725 void
726 operator()(_Tp&) { }
728 void
729 operator()(_Tp&, _Tp&) { }
733 template<typename _Tp, bool = traits<_Tp>::is_associative::value
734 || traits<_Tp>::is_unordered::value>
735 struct clear
737 void
738 operator()(_Tp& __container)
742 __container.clear();
744 catch(const __gnu_cxx::forced_error&)
745 { throw; }
749 // Specialization, empty.
750 template<typename _Tp>
751 struct clear<_Tp, false>
753 void
754 operator()(_Tp&) { }
758 template<typename _Tp, bool = traits<_Tp>::is_unordered::value>
759 struct rehash
761 void
762 operator()(_Tp& __test)
766 size_type s = generate(__test.bucket_count());
767 __test.rehash(s);
769 catch(const __gnu_cxx::forced_error&)
770 { throw; }
773 void
774 operator()(_Tp& __control, _Tp& __test)
778 size_type s = generate(__test.bucket_count());
779 __test.rehash(s);
781 catch(const __gnu_cxx::forced_error&)
783 // Also check hash status.
784 bool fail(false);
785 if (__control.load_factor() != __test.load_factor())
786 fail = true;
787 if (__control.max_load_factor() != __test.max_load_factor())
788 fail = true;
789 if (__control.bucket_count() != __test.bucket_count())
790 fail = true;
791 if (__control.max_bucket_count() != __test.max_bucket_count())
792 fail = true;
794 if (fail)
796 char buf[40];
797 std::string __s("setup_base::rehash "
798 "containers not equal");
799 __s += "\n";
800 __s += "\n";
801 __s += "\t\t\tcontrol : test";
802 __s += "\n";
803 __s += "load_factor\t\t";
804 __builtin_sprintf(buf, "%lu", __control.load_factor());
805 __s += buf;
806 __s += " : ";
807 __builtin_sprintf(buf, "%lu", __test.load_factor());
808 __s += buf;
809 __s += "\n";
811 __s += "max_load_factor\t\t";
812 __builtin_sprintf(buf, "%lu", __control.max_load_factor());
813 __s += buf;
814 __s += " : ";
815 __builtin_sprintf(buf, "%lu", __test.max_load_factor());
816 __s += buf;
817 __s += "\n";
819 __s += "bucket_count\t\t";
820 __builtin_sprintf(buf, "%lu", __control.bucket_count());
821 __s += buf;
822 __s += " : ";
823 __builtin_sprintf(buf, "%lu", __test.bucket_count());
824 __s += buf;
825 __s += "\n";
827 __s += "max_bucket_count\t";
828 __builtin_sprintf(buf, "%lu", __control.max_bucket_count());
829 __s += buf;
830 __s += " : ";
831 __builtin_sprintf(buf, "%lu", __test.max_bucket_count());
832 __s += buf;
833 __s += "\n";
835 std::__throw_logic_error(__s.c_str());
841 // Specialization, empty.
842 template<typename _Tp>
843 struct rehash<_Tp, false>
845 void
846 operator()(_Tp&) { }
848 void
849 operator()(_Tp&, _Tp&) { }
853 template<typename _Tp>
854 struct swap
856 _Tp _M_other;
858 void
859 operator()(_Tp& __container)
863 __container.swap(_M_other);
865 catch(const __gnu_cxx::forced_error&)
866 { throw; }
871 template<typename _Tp>
872 struct iterator_operations
874 typedef _Tp container_type;
875 typedef typename container_type::iterator iterator;
877 void
878 operator()(_Tp& __container)
882 // Any will do.
883 iterator i = __container.begin();
884 iterator __attribute__((unused)) icopy(i);
885 iterator __attribute__((unused)) iassign = i;
887 catch(const __gnu_cxx::forced_error&)
888 { throw; }
893 template<typename _Tp>
894 struct const_iterator_operations
896 typedef _Tp container_type;
897 typedef typename container_type::const_iterator const_iterator;
899 void
900 operator()(_Tp& __container)
904 // Any will do.
905 const_iterator i = __container.begin();
906 const_iterator __attribute__((unused)) icopy(i);
907 const_iterator __attribute__((unused)) iassign = i;
909 catch(const __gnu_cxx::forced_error&)
910 { throw; }
915 // Base class for exception tests.
916 template<typename _Tp>
917 struct test_base: public functor_base
919 typedef _Tp container_type;
921 typedef functor_base base_type;
922 typedef populate<container_type> populate;
923 typedef make_container_n<container_type> make_container_n;
925 typedef clear<container_type> clear;
926 typedef erase_point<container_type> erase_point;
927 typedef erase_range<container_type> erase_range;
928 typedef insert_point<container_type> insert_point;
929 typedef pop_front<container_type> pop_front;
930 typedef pop_back<container_type> pop_back;
931 typedef push_front<container_type> push_front;
932 typedef push_back<container_type> push_back;
933 typedef rehash<container_type> rehash;
934 typedef swap<container_type> swap;
935 typedef iterator_operations<container_type> iterator_ops;
936 typedef const_iterator_operations<container_type> const_iterator_ops;
938 using base_type::compare;
940 // Functor objects.
941 clear _M_clear;
942 erase_point _M_erasep;
943 erase_range _M_eraser;
944 insert_point _M_insertp;
945 pop_front _M_popf;
946 pop_back _M_popb;
947 push_front _M_pushf;
948 push_back _M_pushb;
949 rehash _M_rehash;
950 swap _M_swap;
952 iterator_ops _M_iops;
953 const_iterator_ops _M_ciops;
957 // Run through all member functions for basic exception safety
958 // guarantee: no resource leaks when exceptions are thrown.
960 // Types of resources checked: memory.
962 // For each member function, use throw_value and throw_allocator as
963 // value_type and allocator_type to force potential exception safety
964 // errors.
966 // NB: Assumes
967 // _Tp::value_type is __gnu_cxx::throw_value_*
968 // _Tp::allocator_type is __gnu_cxx::throw_allocator_*
969 // And that the _Cond template parameter for them both is
970 // __gnu_cxx::limit_condition.
971 template<typename _Tp>
972 struct basic_safety : public test_base<_Tp>
974 typedef _Tp container_type;
975 typedef test_base<container_type> base_type;
976 typedef typename base_type::populate populate;
977 typedef std::function<void(container_type&)> function_type;
978 typedef __gnu_cxx::limit_condition condition_type;
980 using base_type::generate;
982 container_type _M_container;
983 std::vector<function_type> _M_functions;
985 basic_safety() { run(); }
987 void
988 run()
990 // Setup.
991 condition_type::never_adjustor off;
993 // Construct containers.
994 populate p1(_M_container);
995 populate p2(base_type::_M_swap._M_other);
997 // Construct list of member functions to exercise.
998 _M_functions.push_back(function_type(base_type::_M_iops));
999 _M_functions.push_back(function_type(base_type::_M_ciops));
1001 _M_functions.push_back(function_type(base_type::_M_erasep));
1002 _M_functions.push_back(function_type(base_type::_M_eraser));
1003 _M_functions.push_back(function_type(base_type::_M_insertp));
1004 _M_functions.push_back(function_type(base_type::_M_popf));
1005 _M_functions.push_back(function_type(base_type::_M_popb));
1006 _M_functions.push_back(function_type(base_type::_M_pushf));
1007 _M_functions.push_back(function_type(base_type::_M_pushb));
1008 _M_functions.push_back(function_type(base_type::_M_rehash));
1009 _M_functions.push_back(function_type(base_type::_M_swap));
1011 // Last.
1012 _M_functions.push_back(function_type(base_type::_M_clear));
1014 // Run tests.
1015 for (auto i = _M_functions.begin(); i != _M_functions.end(); ++i)
1017 function_type& f = *i;
1018 run_steps_to_limit(f);
1022 template<typename _Funct>
1023 void
1024 run_steps_to_limit(const _Funct& __f)
1026 size_t i(1);
1027 bool exit(false);
1028 auto a = _M_container.get_allocator();
1032 // Use the current step as an allocator label.
1033 a.set_label(i);
1037 condition_type::limit_adjustor limit(i);
1038 __f(_M_container);
1040 // If we get here, done.
1041 exit = true;
1043 catch(const __gnu_cxx::forced_error&)
1045 // Check this step for allocations.
1046 // NB: Will throw std::logic_error if allocations.
1047 a.check_allocated(i);
1049 // Check memory allocated with operator new.
1051 ++i;
1054 while (!exit);
1056 // Log count info.
1057 std::cout << __f.target_type().name() << std::endl;
1058 std::cout << "end count " << i << std::endl;
1063 // Run through all member functions with a no throw requirement, sudden death.
1064 // all: member functions erase, pop_back, pop_front, swap
1065 // iterator copy ctor, assignment operator
1066 // unordered and associative: clear
1067 // NB: Assumes _Tp::allocator_type is __gnu_cxx::throw_allocator_random.
1068 template<typename _Tp>
1069 struct generation_prohibited : 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 __gnu_cxx::random_condition condition_type;
1076 container_type _M_container;
1078 generation_prohibited() { run(); }
1080 void
1081 run()
1083 // Furthermore, assumes that the test functor will throw
1084 // forced_exception via throw_allocator, that all errors are
1085 // propagated and in error. Sudden death!
1087 // Setup.
1089 condition_type::never_adjustor off;
1090 populate p1(_M_container);
1091 populate p2(base_type::_M_swap._M_other);
1094 // Run tests.
1096 condition_type::always_adjustor on;
1098 // NB: Vector and deque are special, erase can throw if the copy
1099 // constructor or assignment operator of value_type throws.
1100 if (!traits<container_type>::has_throwing_erase::value)
1102 _M_erasep(_M_container);
1103 _M_eraser(_M_container);
1106 _M_popf(_M_container);
1107 _M_popb(_M_container);
1109 _M_iops(_M_container);
1110 _M_ciops(_M_container);
1112 _M_swap(_M_container);
1114 // Last.
1115 _M_clear(_M_container);
1121 // Test strong exception guarantee.
1122 // Run through all member functions with a roll-back, consistent
1123 // coherent requirement.
1124 // all: member functions insert of a single element, push_back, push_front
1125 // unordered: rehash
1126 template<typename _Tp>
1127 struct propagation_consistent : public test_base<_Tp>
1129 typedef _Tp container_type;
1130 typedef test_base<container_type> base_type;
1131 typedef typename base_type::populate populate;
1132 typedef std::function<void(container_type&)> function_type;
1133 typedef __gnu_cxx::limit_condition condition_type;
1135 using base_type::compare;
1137 container_type _M_container_test;
1138 container_type _M_container_control;
1139 std::vector<function_type> _M_functions;
1141 propagation_consistent() { run(); }
1143 void
1144 sync()
1145 { _M_container_test = _M_container_control; }
1147 // Run test.
1148 void
1149 run()
1151 // Setup.
1152 condition_type::never_adjustor off;
1154 // Construct containers.
1155 populate p(_M_container_control);
1156 sync();
1158 // Construct list of member functions to exercise.
1159 _M_functions.push_back(function_type(base_type::_M_pushf));
1160 _M_functions.push_back(function_type(base_type::_M_pushb));
1161 _M_functions.push_back(function_type(base_type::_M_insertp));
1162 _M_functions.push_back(function_type(base_type::_M_rehash));
1164 // Run tests.
1165 for (auto i = _M_functions.begin(); i != _M_functions.end(); ++i)
1167 function_type& f = *i;
1168 run_steps_to_limit(f);
1172 template<typename _Funct>
1173 void
1174 run_steps_to_limit(const _Funct& __f)
1176 size_t i(1);
1177 bool exit(false);
1181 sync();
1185 condition_type::limit_adjustor limit(i);
1186 __f(_M_container_test);
1188 // If we get here, done.
1189 exit = true;
1191 catch(const __gnu_cxx::forced_error&)
1193 compare(_M_container_control, _M_container_test);
1194 ++i;
1197 while (!exit);
1199 // Log count info.
1200 std::cout << __f.target_type().name() << std::endl;
1201 std::cout << "end count " << i << std::endl;
1205 } // namespace __gnu_test
1207 #endif