3 // Copyright (C) 2005, 2006, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
24 // Permission to use, copy, modify, sell, and distribute this software
25 // is hereby granted without fee, provided that the above copyright
26 // notice appears in all copies, and that both that copyright notice
27 // and this permission notice appear in supporting documentation. None
28 // of the above authors, nor IBM Haifa Research Laboratories, make any
29 // representation about the suitability of this software for any
30 // purpose. It is provided "as is" without express or implied
34 * @file container_rand_regression_test.tcc
35 * Contains a random regression test for a specific container type.
38 #ifndef PB_DS_CONTAINER_RAND_REGRESSION_TEST_TCC
39 #define PB_DS_CONTAINER_RAND_REGRESSION_TEST_TCC
41 // Constructor, copy constructor, assignment and destructor.
44 container_rand_regression_test(unsigned long seed, size_t n, size_t m,
45 double tp, double ip, double dp, double ep,
46 double cp, double mp, bool disp)
47 : m_seed(seed == 0 ? twister_rand_gen::get_time_determined_seed(): seed),
48 m_n(n), m_m(m), m_tp(tp), m_ip(ip), m_dp(dp), m_ep(ep), m_cp(cp),
49 m_mp(mp), m_disp(disp), m_p_c(0)
54 ~container_rand_regression_test()
62 PB_DS_TRACE("default_constructor");
64 m_alloc.set_probability(m_tp);
70 catch(__gnu_cxx::forced_error&)
76 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
87 m_alloc.set_probability(0);
88 Cntnr* p_c = new Cntnr;
89 m_alloc.set_probability(1);
91 std::swap(p_c, m_p_c);
93 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
101 PB_DS_TRACE("copy_constructor");
104 m_alloc.set_probability(m_tp);
106 typedef typename allocator_type::group_adjustor adjustor;
107 adjustor adjust(m_p_c->size());
111 p_c = new Cntnr(*m_p_c);
112 std::swap(p_c, m_p_c);
114 catch(__gnu_cxx::forced_error&)
120 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
127 assignment_operator()
129 PB_DS_TRACE("assignment operator");
132 m_alloc.set_probability(m_tp);
134 typedef typename allocator_type::group_adjustor adjustor;
135 adjustor adjust(m_p_c->size());
141 std::swap(p_c, m_p_c);
143 catch(__gnu_cxx::forced_error&)
149 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
160 m_alloc.set_probability(m_tp);
161 typedef typename allocator_type::group_adjustor adjustor;
162 adjustor adjust(m_p_c->size());
166 switch(get_next_sub_op(3))
169 p_c = new Cntnr(m_p_c->get_cmp_fn());
173 p_c = new Cntnr(m_p_c->begin(), m_p_c->end());
176 p_c = new Cntnr(m_p_c->begin(), m_p_c->end(), m_p_c->get_cmp_fn());
179 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
182 std::swap(p_c, m_p_c);
184 catch(__gnu_cxx::forced_error&)
190 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
199 cmp(const Cntnr& c, const native_type& native, const std::string& callfn)
201 destructor_printer notify(__FUNCTION__);
205 m_alloc.set_probability(1);
207 const size_t size = c.size();
208 const size_t native_size = native.size();
209 _GLIBCXX_THROW_IF(size != native_size, size << " " << native_size,
212 const bool empty = c.empty();
213 const bool native_empty = native.empty();
214 _GLIBCXX_THROW_IF(empty != native_empty, empty << " " << native_empty,
217 const size_t it_size = std::distance(c.begin(), c.end());
218 _GLIBCXX_THROW_IF(it_size != size, it_size << " " << size, &c, &native);
222 const std::string native_top = native.top();
223 const std::string top = test_traits::native_value(c.top());
224 const bool top_smaller = std::less<std::string>()(top, native_top);
225 const bool top_larger = std::less<std::string>()(native_top, top);
227 if (top_smaller || top_larger)
228 _GLIBCXX_THROW_IF(true, top << " " << native_top, &c, &native);
233 _GLIBCXX_THROW_IF(true, "call-fn: " + callfn, &c, &native);
245 typedef xml_result_set_regression_formatter formatter_type;
246 formatter_type* p_fmt = 0;
248 p_fmt = new formatter_type(string_form<Cntnr>::name(),
249 string_form<Cntnr>::desc());
252 m_alloc.seed(m_seed);
254 // The __throw_allocator::_S_label defaults to 0, so to make things
255 // easier and more precise when debugging, start at 1.
256 const size_t starting_label(1);
260 prog_bar pb(m_n, std::cout, m_disp);
262 for (m_i = starting_label; m_i <= m_n; ++m_i)
264 PB_DS_TRACE("Op #" << m_i);
266 // Track allocation from this point only.
267 allocator_type::set_label(m_i);
271 PB_DS_RUN_MTHD(default_constructor);
286 switch(get_next_op())
292 PB_DS_RUN_MTHD(modify)
295 switch(get_next_sub_op(3))
301 PB_DS_RUN_MTHD(erase_if)
304 PB_DS_RUN_MTHD(erase_it)
307 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
311 PB_DS_RUN_MTHD(clear)
314 switch(get_next_sub_op(5))
320 PB_DS_RUN_MTHD(copy_constructor)
323 PB_DS_RUN_MTHD(it_constructor)
326 PB_DS_RUN_MTHD(assignment_operator)
329 PB_DS_RUN_MTHD(split_join)
332 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
336 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
344 std::cerr << "Failed at index " << m_i << std::endl;
349 // Clean up, then check for allocation by special label, set above.
350 allocator_type::set_label(0);
355 for (size_t n = starting_label; n <= m_n; ++n)
356 m_alloc.check_allocated(n);
358 catch (std::logic_error& obj)
360 // On fail, check_allocated should throw std::logic_error.
361 std::cerr << obj.what() << std::endl;
362 std::cerr << typeid(Cntnr).name() << std::endl;
366 // Reset throw probability.
367 m_alloc.set_probability(0);
371 std::cout << std::endl;
377 typename PB_DS_CLASS_C_DEC::op
381 const double prob = m_g.get_prob();
386 if (prob < m_ip + m_dp)
389 if (prob < m_ip + m_dp + m_ep)
392 if (prob < m_ip + m_dp + m_ep + m_cp)
395 _GLIBCXX_THROW_IF(prob > 1, prob, m_p_c, &m_native_c);
402 get_next_sub_op(size_t max)
404 const double p = m_g.get_prob();
405 const double delta = 1 / static_cast<double>(max);
408 if (p <= (i + 1) * delta)
410 _GLIBCXX_THROW_IF(i >= max, i << " " << max, m_p_c, &m_native_c);
425 destructor_printer notify(__FUNCTION__);
429 m_alloc.set_probability(0);
430 value_type v = test_traits::generate_value(m_g, m_m);
431 m_alloc.set_probability(m_tp);
432 const typename cntnr::size_type sz = m_p_c->size();
434 _GLIBCXX_THROW_IF(sz != m_p_c->size() - 1, sz, m_p_c, &m_native_c);
435 m_native_c.push(test_traits::native_value(v));
437 catch(__gnu_cxx::forced_error&)
443 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
446 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
458 PB_DS_TRACE("modify");
459 destructor_printer notify(__FUNCTION__);
464 m_alloc.set_probability(0);
465 value_type v = test_traits::generate_value(m_g, m_m);
467 m_alloc.set_probability(m_tp);
468 typename cntnr::iterator it = m_p_c->begin();
469 std::advance(it, m_g.get_unsigned_long(0, m_p_c->size()));
470 if (it != m_p_c->end())
472 typedef typename test_traits::native_value_type native_value_type;
473 native_value_type native_v = test_traits::native_value(*it);
474 native_value_type new_native_v = test_traits::native_value(v);
475 m_p_c->modify(it, v);
476 m_native_c.modify(native_v, new_native_v);
479 catch(__gnu_cxx::forced_error&)
482 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
485 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
496 PB_DS_TRACE("clear");
509 destructor_printer notify(__FUNCTION__);
514 m_alloc.set_probability(1);
521 catch(__gnu_cxx::forced_error&)
524 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
527 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
537 PB_DS_TRACE("erase_if");
538 destructor_printer notify(__FUNCTION__);
544 typename std::iterator_traits<typename cntnr::iterator>::reference
547 m_alloc.set_probability(1);
550 typename test_traits::template erase_if_fn<value_type>
553 const size_t ersd = m_p_c->erase_if(erase_if_fn_t());
556 typename test_traits::template erase_if_fn<std::string>
557 native_erase_if_fn_t;
559 const size_t native_ersd = m_native_c.erase_if(native_erase_if_fn_t());
561 _GLIBCXX_THROW_IF(ersd != native_ersd, ersd << " " << native_ersd,
564 catch(__gnu_cxx::forced_error&)
567 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
570 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
580 PB_DS_TRACE("erase_it");
581 destructor_printer notify(__FUNCTION__);
586 m_alloc.set_probability(1);
587 typename cntnr::iterator it = m_p_c->begin();
588 std::advance(it, m_g.get_unsigned_long(0, m_p_c->size()));
590 if (it != m_p_c->end())
592 m_native_c.erase(*it);
596 catch(__gnu_cxx::forced_error&)
599 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
602 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
613 // General container types.
614 typedef typename Cntnr::size_type test_size_type;
615 typedef typename Cntnr::difference_type difference_type;
626 typedef typename Cntnr::value_type test_value_type;
627 typedef typename Cntnr::reference test_reference;
628 typedef typename Cntnr::const_reference test_const_reference;
629 typedef typename Cntnr::pointer test_pointer;
630 typedef typename Cntnr::const_pointer test_const_pointer;
638 typedef typename Cntnr::container_category test_container_category;
646 typedef typename Cntnr::point_iterator test_point_iterator;
647 typedef typename Cntnr::point_const_iterator const_test_point_iterator;
648 typedef typename Cntnr::iterator test_iterator;
649 typedef typename Cntnr::const_iterator const_test_iterator;
657 typedef typename Cntnr::allocator_type test_allocator;
658 typedef typename Cntnr::cmp_fn test_cmp_fn;
668 PB_DS_TRACE("policy_access");
671 typename Cntnr::cmp_fn& r_t = m_p_c->get_cmp_fn();
676 const typename Cntnr::cmp_fn& r_t =((const Cntnr& )*m_p_c).get_cmp_fn();
687 PB_DS_TRACE("split_join");
688 destructor_printer notify(__FUNCTION__);
693 m_alloc.set_probability(0);
696 native_type native_lhs(m_native_c);
697 m_alloc.set_probability(m_tp);
699 typedef typename test_traits::template erase_if_fn<value_type> split_fn_t;
700 lhs.split(split_fn_t(), rhs);
702 typedef typename test_traits::template erase_if_fn<std::string>
705 native_type native_rhs;
706 native_lhs.split(native_split_fn_t(), native_rhs);
707 PB_DS_COND_COMPARE(lhs, native_lhs);
708 PB_DS_COND_COMPARE(rhs, native_rhs);
710 m_alloc.set_probability(m_tp);
712 if (m_g.get_prob() < 0.5)
716 _GLIBCXX_THROW_IF(rhs.size() != 0, rhs.size(), m_p_c, &m_native_c);
717 _GLIBCXX_THROW_IF(!rhs.empty(), rhs.size(), m_p_c, &m_native_c);
719 catch(__gnu_cxx::forced_error&)
722 const bool b = __gnu_pbds::container_traits<cntnr>::split_join_can_throw;
723 _GLIBCXX_THROW_IF(!b, b, m_p_c, &m_native_c);
726 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
731 // Iterator conversions.
737 PB_DS_TRACE("it_copy");
740 typename cntnr::iterator it = m_p_c->end();
741 typename cntnr::const_iterator const_it(it);
742 _GLIBCXX_THROW_IF(const_it != it, "", m_p_c, &m_native_c);
743 _GLIBCXX_THROW_IF(!(const_it == it), "", m_p_c, &m_native_c);
747 typename cntnr::const_iterator const_it = m_p_c->end();
748 typename cntnr::point_const_iterator const_find_it(const_it);
749 _GLIBCXX_THROW_IF(const_find_it != const_it, "", m_p_c, &m_native_c);
750 _GLIBCXX_THROW_IF(!(const_find_it == const_it), "", m_p_c, &m_native_c);
754 typename cntnr::iterator it = m_p_c->end();
755 typename cntnr::point_const_iterator const_find_it1(it);
756 _GLIBCXX_THROW_IF(const_find_it1 != it, "", m_p_c, &m_native_c);
757 _GLIBCXX_THROW_IF(!(const_find_it1 == it), "", m_p_c, &m_native_c);
759 typename cntnr::point_iterator find_it1(it);
760 _GLIBCXX_THROW_IF(find_it1 != it, "", m_p_c, &m_native_c);
761 _GLIBCXX_THROW_IF(!(find_it1 == it), "", m_p_c, &m_native_c);
763 typename cntnr::point_iterator find_it = m_p_c->end();
764 typename cntnr::point_const_iterator const_find_it(find_it);
765 _GLIBCXX_THROW_IF(find_it != const_find_it, "", m_p_c, &m_native_c);
766 _GLIBCXX_THROW_IF(!(find_it == const_find_it), "", m_p_c, &m_native_c);
775 PB_DS_TRACE("it_assign");
778 typename cntnr::iterator it = m_p_c->end();
779 typename cntnr::const_iterator const_it;
781 _GLIBCXX_THROW_IF(const_it != it, "", m_p_c, &m_native_c);
782 _GLIBCXX_THROW_IF(!(const_it == it), "", m_p_c, &m_native_c);
784 typename cntnr::point_const_iterator const_find_it;
786 _GLIBCXX_THROW_IF(const_find_it != it, "", m_p_c, &m_native_c);
787 _GLIBCXX_THROW_IF(!(const_find_it == it), "", m_p_c, &m_native_c);
789 typename cntnr::point_iterator find_it;
791 _GLIBCXX_THROW_IF(find_it != it, "", m_p_c, &m_native_c);
792 _GLIBCXX_THROW_IF(!(find_it == it), "", m_p_c, &m_native_c);
796 typename cntnr::const_iterator const_it = m_p_c->end();
797 typename cntnr::point_const_iterator const_find_it;
798 const_find_it = const_it;
799 _GLIBCXX_THROW_IF(const_find_it != const_it, "", m_p_c, &m_native_c);
800 _GLIBCXX_THROW_IF(!(const_find_it == const_it), "", m_p_c, &m_native_c);
804 typename cntnr::point_iterator find_it = m_p_c->end();
805 typename cntnr::point_const_iterator const_find_it;
806 const_find_it = find_it;
807 _GLIBCXX_THROW_IF(find_it != const_find_it, "", m_p_c, &m_native_c);
808 _GLIBCXX_THROW_IF(!(find_it == const_find_it), "", m_p_c, &m_native_c);
817 print_container(const native_type& cnt, std::ostream& os) const
819 m_alloc.set_probability(0);
820 native_type cpy(cnt);
823 os << cpy.top() << std::endl;
831 print_container(const cntnr& cnt, std::ostream& os) const
833 typedef typename cntnr::const_iterator const_iterator;
834 m_alloc.set_probability(0);
835 for (const_iterator it = cnt.begin(); it != cnt.end(); ++it)
836 os << *it << std::endl;