2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / libstdc++-v3 / testsuite / util / regression / rand / priority_queue / rand_regression_test.hpp
blob1cf86020d454fe1d04ef1bfd93f5722170e098aa
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2008, 2009 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
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
30 // warranty.
32 /**
33 * @file rand_regression_test.hpp
34 * Contains a random-operation test.
37 #ifndef PB_DS_PQ_RAND_REGRESSION_TEST_HPP
38 #define PB_DS_PQ_RAND_REGRESSION_TEST_HPP
40 #include <iostream>
41 #include <vector>
42 #include <io/verified_cmd_line_input.hpp>
43 #include <regression/common_type.hpp>
44 #include <regression/rand/priority_queue/container_rand_regression_test.h>
46 namespace __gnu_pbds
48 namespace test
50 namespace detail
52 #ifndef PB_DS_REGRESSION
53 #error "Must define PB_DS_REGRESSION"
54 #endif
56 struct rand_reg_test
58 public:
59 rand_reg_test(size_t seed, size_t n, size_t m, double tp, double ip,
60 double dp, double ep, double cp, double mp, bool d)
61 : m_sd(seed), m_n(n), m_m(m), m_tp(tp), m_ip(ip), m_dp(dp), m_ep(ep),
62 m_cp(cp), m_mp(mp), m_disp(d)
63 { }
65 template<typename Cntnr>
66 void
67 operator()(Cntnr)
69 unsigned long ul = static_cast<unsigned long>(m_sd);
70 container_rand_regression_test<Cntnr> t(ul, m_n, m_n, m_tp, m_ip, m_dp,
71 m_ep, m_cp, m_mp, m_disp);
72 t();
75 private:
76 const size_t m_sd;
77 const size_t m_n;
78 const size_t m_m;
79 const double m_tp;
80 const double m_ip;
81 const double m_dp;
82 const double m_ep;
83 const double m_cp;
84 const double m_mp;
85 const bool m_disp;
88 void
89 usage(const std::string& r_name);
91 void
92 verify_params(size_t&, size_t&, size_t&,
93 double&, double&, double&, double&, double&, double&, bool&);
94 } // namespace detail
96 template<typename TL>
97 int
98 rand_regression_test(size_t iter, size_t keys, const std::string name, TL tl)
100 // Sane defaults.
101 size_t n = iter;
102 size_t m = keys;
103 size_t sd = 0; // 0 = time-determined arbitrary
104 double tp = 0.2;
105 double ip = 0.6;
106 double dp = 0.1;
107 double ep = 0.2;
108 double cp = 0.001;
109 double mp = 1;
110 bool disp = false; // show progress
114 detail::verify_params(sd, n, m, tp, ip, dp, ep, cp, mp, disp);
116 catch(__gnu_pbds::test::illegal_input_error&)
118 detail::usage(name);
119 return -1;
121 catch(...)
123 return -2;
126 xml_test_rand_regression_formatter* p_fmt = NULL;
127 if (sd == 0)
128 sd = twister_rand_gen::get_time_determined_seed();
129 if (disp)
130 p_fmt = new xml_test_rand_regression_formatter(sd, n, m, tp, ip, dp,
131 ep, cp, mp);
135 detail::rand_reg_test tst(sd, n, m, tp, ip, dp, ep, cp, mp, disp);
136 __gnu_cxx::typelist::apply(tst, tl);
138 catch(...)
140 std::cerr << "Test failed with seed " << sd << std::endl;
141 if (disp)
142 delete p_fmt;
143 return -1;
146 if (disp)
147 delete p_fmt;
148 return 0;
151 namespace detail
153 inline void
154 usage(const std::string& name)
156 using namespace std;
157 cerr << "usage: " << name << " <sd> <n> <m> <tp> <ip> <dp> <ep> <cp> <mp> ['t' | 'f']" <<
158 endl << endl;
160 cerr << "This test performs basic regression tests on various priority queues."
161 "For each container, it performs a sequence of operations. At each iteration, it does "
162 "the following: " << endl;
163 cerr << "* Performs an operation on the container " << endl;
164 cerr << "* Performs the same operation on an cntnr object" << endl;
165 cerr << "* Possibly compares the container to the cntnr object" << endl;
166 cerr << "* Checks that exceptions (thrown by an allocator) "
167 "do not violate exception guarantees";
169 cerr << endl << endl;
171 cerr << "sd = seed for random-number generator; 0 = "
172 "time determined value" << endl;
173 cerr << "n = number of iterations" << endl;
174 cerr << "m = number of distinct values" << endl;
175 cerr << "tp = probability that an exception will be actively thrown" << endl;
176 cerr << "ip = probability that an operation will be insert" << endl;
177 cerr << "dp = probability that an operation will be modify" << endl;
178 cerr << "ep = probability that an operation will be erase" << endl;
179 cerr << "cp = probability that an operation will be clear" << endl;
180 cerr << "(therefore, 1 - (ip + dp + ep + cp) = probability of any other operation)" << endl;
181 cerr << "mp = probability that the container will be compared to the cntnr object" << endl;
182 cerr << "'t' or 'f' determine whether progress will be displayed" << endl;
185 inline void
186 verify_params(size_t& r_seed, size_t& r_n,
187 size_t& r_m, double& r_tp, double& r_ip, double& r_dp,
188 double& r_ep, double& r_cp, double& r_mp, bool& r_d)
190 verify_prob(r_tp);
191 verify_prob(r_ip);
192 verify_prob(r_dp);
193 verify_prob(r_ep);
194 verify_prob(r_cp);
195 verify_prob(r_mp);
196 verify_prob(r_ip + r_dp + r_ep + r_cp);
198 } // namespace detail
199 } // namespace test
200 } // namespace __gnu_pbds
202 #endif