3 // Copyright (C) 2005, 2006, 2008, 2009 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
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file container_rand_regression_test.tcc
34 * Contains a random regression test for a specific container type.
37 #ifndef PB_DS_CONTAINER_RAND_REGRESSION_TEST_TCC
38 #define PB_DS_CONTAINER_RAND_REGRESSION_TEST_TCC
40 // Constructor, copy constructor, assignment and destructor.
43 container_rand_regression_test(unsigned long seed, size_t n, size_t m,
44 double tp, double ip, double dp, double ep,
45 double cp, double mp, bool disp)
46 : m_seed(seed == 0 ? twister_rand_gen::get_time_determined_seed(): seed),
47 m_n(n), m_m(m), m_tp(tp), m_ip(ip), m_dp(dp), m_ep(ep), m_cp(cp),
48 m_mp(mp), m_disp(disp), m_p_c(NULL)
53 ~container_rand_regression_test()
61 PB_DS_TRACE("default_constructor");
63 m_alloc.set_probability(m_tp);
69 catch(__gnu_cxx::forced_error&)
75 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
86 m_alloc.set_probability(0);
87 Cntnr* p_c = new Cntnr;
88 m_alloc.set_probability(1);
90 std::swap(p_c, m_p_c);
92 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
100 PB_DS_TRACE("copy_constructor");
103 m_alloc.set_probability(m_tp);
105 typedef typename allocator_type::group_adjustor adjustor;
106 adjustor adjust(m_p_c->size());
110 p_c = new Cntnr(*m_p_c);
111 std::swap(p_c, m_p_c);
113 catch(__gnu_cxx::forced_error&)
119 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
126 assignment_operator()
128 PB_DS_TRACE("assignment operator");
131 m_alloc.set_probability(m_tp);
133 typedef typename allocator_type::group_adjustor adjustor;
134 adjustor adjust(m_p_c->size());
140 std::swap(p_c, m_p_c);
142 catch(__gnu_cxx::forced_error&)
148 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
159 m_alloc.set_probability(m_tp);
160 typedef typename allocator_type::group_adjustor adjustor;
161 adjustor adjust(m_p_c->size());
165 switch(get_next_sub_op(3))
168 p_c = new Cntnr(m_p_c->get_cmp_fn());
172 p_c = new Cntnr(m_p_c->begin(), m_p_c->end());
175 p_c = new Cntnr(m_p_c->begin(), m_p_c->end(), m_p_c->get_cmp_fn());
178 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
181 std::swap(p_c, m_p_c);
183 catch(__gnu_cxx::forced_error&)
189 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
198 cmp(const Cntnr& c, const native_type& native, const std::string& callfn)
200 destructor_printer notify(__FUNCTION__);
204 m_alloc.set_probability(1);
206 const size_t size = c.size();
207 const size_t native_size = native.size();
208 _GLIBCXX_THROW_IF(size != native_size, size << " " << native_size,
211 const bool empty = c.empty();
212 const bool native_empty = native.empty();
213 _GLIBCXX_THROW_IF(empty != native_empty, empty << " " << native_empty,
216 const size_t it_size = std::distance(c.begin(), c.end());
217 _GLIBCXX_THROW_IF(it_size != size, it_size << " " << size, &c, &native);
221 const std::string native_top = native.top();
222 const std::string top = test_traits::native_value(c.top());
223 const bool top_smaller = std::less<std::string>()(top, native_top);
224 const bool top_larger = std::less<std::string>()(native_top, top);
226 if (top_smaller || top_larger)
227 _GLIBCXX_THROW_IF(true, top << " " << native_top, &c, &native);
232 _GLIBCXX_THROW_IF(true, "call-fn: " + callfn, &c, &native);
244 typedef xml_result_set_regression_formatter formatter_type;
245 formatter_type* p_fmt = NULL;
247 p_fmt = new formatter_type(string_form<Cntnr>::name(),
248 string_form<Cntnr>::desc());
251 m_alloc.seed(m_seed);
253 // The __throw_allocator::_S_label defaults to 0, so to make things
254 // easier and more precise when debugging, start at 1.
255 const size_t starting_label(1);
259 prog_bar pb(m_n, std::cout, m_disp);
261 for (m_i = starting_label; m_i <= m_n; ++m_i)
263 PB_DS_TRACE("Op #" << m_i);
265 // Track allocation from this point only.
266 allocator_type::set_label(m_i);
270 PB_DS_RUN_MTHD(default_constructor);
285 switch(get_next_op())
291 PB_DS_RUN_MTHD(modify)
294 switch(get_next_sub_op(3))
300 PB_DS_RUN_MTHD(erase_if)
303 PB_DS_RUN_MTHD(erase_it)
306 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
310 PB_DS_RUN_MTHD(clear)
313 switch(get_next_sub_op(5))
319 PB_DS_RUN_MTHD(copy_constructor)
322 PB_DS_RUN_MTHD(it_constructor)
325 PB_DS_RUN_MTHD(assignment_operator)
328 PB_DS_RUN_MTHD(split_join)
331 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
335 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
343 std::cerr << "Failed at index " << m_i << std::endl;
348 // Clean up, then check for allocation by special label, set above.
349 allocator_type::set_label(0);
354 for (size_t n = starting_label; n <= m_n; ++n)
355 m_alloc.check_allocated(n);
357 catch (std::logic_error& obj)
359 // On fail, check_allocated should throw std::logic_error.
360 std::cerr << obj.what() << std::endl;
361 std::cerr << typeid(Cntnr).name() << std::endl;
365 // Reset throw probability.
366 m_alloc.set_probability(0);
370 std::cout << std::endl;
376 typename PB_DS_CLASS_C_DEC::op
380 const double prob = m_g.get_prob();
385 if (prob < m_ip + m_dp)
388 if (prob < m_ip + m_dp + m_ep)
391 if (prob < m_ip + m_dp + m_ep + m_cp)
394 _GLIBCXX_THROW_IF(prob > 1, prob, m_p_c, &m_native_c);
401 get_next_sub_op(size_t max)
403 const double p = m_g.get_prob();
404 const double delta = 1 / static_cast<double>(max);
407 if (p <= (i + 1) * delta)
409 _GLIBCXX_THROW_IF(i >= max, i << " " << max, m_p_c, &m_native_c);
424 destructor_printer notify(__FUNCTION__);
428 m_alloc.set_probability(0);
429 value_type v = test_traits::generate_value(m_g, m_m);
430 m_alloc.set_probability(m_tp);
431 const typename cntnr::size_type sz = m_p_c->size();
433 _GLIBCXX_THROW_IF(sz != m_p_c->size() - 1, sz, m_p_c, &m_native_c);
434 m_native_c.push(test_traits::native_value(v));
436 catch(__gnu_cxx::forced_error&)
442 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
445 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
457 PB_DS_TRACE("modify");
458 destructor_printer notify(__FUNCTION__);
463 m_alloc.set_probability(0);
464 value_type v = test_traits::generate_value(m_g, m_m);
466 m_alloc.set_probability(m_tp);
467 typename cntnr::iterator it = m_p_c->begin();
468 std::advance(it, m_g.get_unsigned_long(0, m_p_c->size()));
469 if (it != m_p_c->end())
471 typedef typename test_traits::native_value_type native_value_type;
472 native_value_type native_v = test_traits::native_value(*it);
473 native_value_type new_native_v = test_traits::native_value(v);
474 m_p_c->modify(it, v);
475 m_native_c.modify(native_v, new_native_v);
478 catch(__gnu_cxx::forced_error&)
481 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
484 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
495 PB_DS_TRACE("clear");
508 destructor_printer notify(__FUNCTION__);
513 m_alloc.set_probability(1);
520 catch(__gnu_cxx::forced_error&)
523 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
526 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
536 PB_DS_TRACE("erase_if");
537 destructor_printer notify(__FUNCTION__);
543 typename std::iterator_traits<typename cntnr::iterator>::reference
546 m_alloc.set_probability(1);
549 typename test_traits::template erase_if_fn<value_type>
552 const size_t ersd = m_p_c->erase_if(erase_if_fn_t());
555 typename test_traits::template erase_if_fn<std::string>
556 native_erase_if_fn_t;
558 const size_t native_ersd = m_native_c.erase_if(native_erase_if_fn_t());
560 _GLIBCXX_THROW_IF(ersd != native_ersd, ersd << " " << native_ersd,
563 catch(__gnu_cxx::forced_error&)
566 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
569 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
579 PB_DS_TRACE("erase_it");
580 destructor_printer notify(__FUNCTION__);
585 m_alloc.set_probability(1);
586 typename cntnr::iterator it = m_p_c->begin();
587 std::advance(it, m_g.get_unsigned_long(0, m_p_c->size()));
589 if (it != m_p_c->end())
591 m_native_c.erase(*it);
595 catch(__gnu_cxx::forced_error&)
598 _GLIBCXX_THROW_IF(true, "", m_p_c, &m_native_c);
601 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
612 // General container types.
613 typedef typename Cntnr::size_type test_size_type;
614 typedef typename Cntnr::difference_type difference_type;
625 typedef typename Cntnr::value_type test_value_type;
626 typedef typename Cntnr::reference test_reference;
627 typedef typename Cntnr::const_reference test_const_reference;
628 typedef typename Cntnr::pointer test_pointer;
629 typedef typename Cntnr::const_pointer test_const_pointer;
637 typedef typename Cntnr::container_category test_container_category;
645 typedef typename Cntnr::point_iterator test_point_iterator;
646 typedef typename Cntnr::const_point_iterator const_test_point_iterator;
647 typedef typename Cntnr::iterator test_iterator;
648 typedef typename Cntnr::const_iterator const_test_iterator;
656 typedef typename Cntnr::allocator_type test_allocator;
657 typedef typename Cntnr::cmp_fn test_cmp_fn;
667 PB_DS_TRACE("policy_access");
670 typename Cntnr::cmp_fn& r_t = m_p_c->get_cmp_fn();
671 assert(&r_t != NULL);
675 const typename Cntnr::cmp_fn& r_t =((const Cntnr& )*m_p_c).get_cmp_fn();
676 assert(&r_t != NULL);
686 PB_DS_TRACE("split_join");
687 destructor_printer notify(__FUNCTION__);
692 m_alloc.set_probability(0);
695 native_type native_lhs(m_native_c);
696 m_alloc.set_probability(m_tp);
698 typedef typename test_traits::template erase_if_fn<value_type> split_fn_t;
699 lhs.split(split_fn_t(), rhs);
701 typedef typename test_traits::template erase_if_fn<std::string>
704 native_type native_rhs;
705 native_lhs.split(native_split_fn_t(), native_rhs);
706 PB_DS_COND_COMPARE(lhs, native_lhs);
707 PB_DS_COND_COMPARE(rhs, native_rhs);
709 m_alloc.set_probability(m_tp);
711 if (m_g.get_prob() < 0.5)
715 _GLIBCXX_THROW_IF(rhs.size() != 0, rhs.size(), m_p_c, &m_native_c);
716 _GLIBCXX_THROW_IF(!rhs.empty(), rhs.size(), m_p_c, &m_native_c);
718 catch(__gnu_cxx::forced_error&)
721 const bool b = __gnu_pbds::container_traits<cntnr>::split_join_can_throw;
722 _GLIBCXX_THROW_IF(!b, b, m_p_c, &m_native_c);
725 PB_DS_COND_COMPARE(*m_p_c, m_native_c);
730 // Iterator conversions.
736 PB_DS_TRACE("it_copy");
739 typename cntnr::iterator it = m_p_c->end();
740 typename cntnr::const_iterator const_it(it);
741 _GLIBCXX_THROW_IF(const_it != it, "", m_p_c, &m_native_c);
742 _GLIBCXX_THROW_IF(!(const_it == it), "", m_p_c, &m_native_c);
746 typename cntnr::const_iterator const_it = m_p_c->end();
747 typename cntnr::const_point_iterator const_find_it(const_it);
748 _GLIBCXX_THROW_IF(const_find_it != const_it, "", m_p_c, &m_native_c);
749 _GLIBCXX_THROW_IF(!(const_find_it == const_it), "", m_p_c, &m_native_c);
753 typename cntnr::iterator it = m_p_c->end();
754 typename cntnr::const_point_iterator const_find_it1(it);
755 _GLIBCXX_THROW_IF(const_find_it1 != it, "", m_p_c, &m_native_c);
756 _GLIBCXX_THROW_IF(!(const_find_it1 == it), "", m_p_c, &m_native_c);
758 typename cntnr::point_iterator find_it1(it);
759 _GLIBCXX_THROW_IF(find_it1 != it, "", m_p_c, &m_native_c);
760 _GLIBCXX_THROW_IF(!(find_it1 == it), "", m_p_c, &m_native_c);
762 typename cntnr::point_iterator find_it = m_p_c->end();
763 typename cntnr::const_point_iterator const_find_it(find_it);
764 _GLIBCXX_THROW_IF(find_it != const_find_it, "", m_p_c, &m_native_c);
765 _GLIBCXX_THROW_IF(!(find_it == const_find_it), "", m_p_c, &m_native_c);
774 PB_DS_TRACE("it_assign");
777 typename cntnr::iterator it = m_p_c->end();
778 typename cntnr::const_iterator const_it;
780 _GLIBCXX_THROW_IF(const_it != it, "", m_p_c, &m_native_c);
781 _GLIBCXX_THROW_IF(!(const_it == it), "", m_p_c, &m_native_c);
783 typename cntnr::const_point_iterator const_find_it;
785 _GLIBCXX_THROW_IF(const_find_it != it, "", m_p_c, &m_native_c);
786 _GLIBCXX_THROW_IF(!(const_find_it == it), "", m_p_c, &m_native_c);
788 typename cntnr::point_iterator find_it;
790 _GLIBCXX_THROW_IF(find_it != it, "", m_p_c, &m_native_c);
791 _GLIBCXX_THROW_IF(!(find_it == it), "", m_p_c, &m_native_c);
795 typename cntnr::const_iterator const_it = m_p_c->end();
796 typename cntnr::const_point_iterator const_find_it;
797 const_find_it = const_it;
798 _GLIBCXX_THROW_IF(const_find_it != const_it, "", m_p_c, &m_native_c);
799 _GLIBCXX_THROW_IF(!(const_find_it == const_it), "", m_p_c, &m_native_c);
803 typename cntnr::point_iterator find_it = m_p_c->end();
804 typename cntnr::const_point_iterator const_find_it;
805 const_find_it = find_it;
806 _GLIBCXX_THROW_IF(find_it != const_find_it, "", m_p_c, &m_native_c);
807 _GLIBCXX_THROW_IF(!(find_it == const_find_it), "", m_p_c, &m_native_c);
816 print_container(const native_type& cnt, std::ostream& os) const
818 m_alloc.set_probability(0);
819 native_type cpy(cnt);
822 os << cpy.top() << std::endl;
830 print_container(const cntnr& cnt, std::ostream& os) const
832 typedef typename cntnr::const_iterator const_iterator;
833 m_alloc.set_probability(0);
834 for (const_iterator it = cnt.begin(); it != cnt.end(); ++it)
835 os << *it << std::endl;