]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/util/regression/rand/assoc/rand_regression_test.hpp
*: Change namespace pb_ds to __gnu_pbds.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / regression / rand / assoc / rand_regression_test.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 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 2, or (at your option) any later
9// version.
10
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.
15
16// You should have received a copy of the GNU General Public License
17// along with this library; see the file COPYING. If not, write to
18// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19// MA 02111-1307, USA.
20
21// As a special exception, you may use this file as part of a free
22// software library without restriction. Specifically, if other files
23// instantiate templates or use macros or inline functions from this
24// file, or you compile this file and link it with other files to
25// produce an executable, this file does not by itself cause the
26// resulting executable to be covered by the GNU General Public
27// License. This exception does not however invalidate any other
28// reasons why the executable file might be covered by the GNU General
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33// Permission to use, copy, modify, sell, and distribute this software
34// is hereby granted without fee, provided that the above copyright
35// notice appears in all copies, and that both that copyright notice
36// and this permission notice appear in supporting documentation. None
37// of the above authors, nor IBM Haifa Research Laboratories, make any
38// representation about the suitability of this software for any
39// purpose. It is provided "as is" without express or implied
40// warranty.
41
42/**
43 * @file rand_regression_test.hpp
44 * Contains a random-operation test.
45 */
46
55a4c999
BK
47#ifndef PB_DS_ASSOC_RAND_REGRESSION_TEST_HPP
48#define PB_DS_ASSOC_RAND_REGRESSION_TEST_HPP
49
4569a895
AT
50#include <iostream>
51#include <vector>
52#include <regression/rand/assoc/container_rand_regression_test.hpp>
53#include <io/verified_cmd_line_input.hpp>
54#include <common_type/assoc/common_type.hpp>
55#include <regression/basic_type.hpp>
56#include <regression/assoc/common_type.hpp>
57
5e11f978 58namespace __gnu_pbds
4569a895
AT
59{
60namespace test
61{
62namespace detail
63{
64#ifndef PB_DS_REGRESSION
65#error "Must define PB_DS_REGRESSION"
66#endif
67
68 struct rand_reg_test
69 {
70 public:
71 rand_reg_test(size_t seed, size_t n, size_t m, double tp, double ip,
72 double ep, double cp, double mp, bool d)
73 : m_sd(seed), m_n(n), m_m(m), m_tp(tp), m_ip(ip), m_ep(ep), m_cp(cp),
74 m_mp(mp), m_disp(d)
75 { }
76
77 template<typename Cntnr>
78 void
3441f106 79 operator()(Cntnr)
4569a895
AT
80 {
81 unsigned long ul = static_cast<unsigned long>(m_sd);
82 container_rand_regression_test<Cntnr> t(ul, m_n, m_n, m_tp, m_ip,
83 m_ep, m_cp, m_mp, m_disp);
84 t();
85 }
86
87 private:
88 const size_t m_sd;
89 const size_t m_n;
90 const size_t m_m;
91 const double m_tp;
92 const double m_ip;
93 const double m_ep;
94 const double m_cp;
95 const double m_mp;
96 const bool m_disp;
97 };
98
99 void
100 usage(const std::string& r_name);
101
102 void
103 verify_params(size_t&, size_t&, size_t&,
104 double&, double&, double&, double&, double&, bool&);
105} // namespace detail
106
107 template<typename TL>
108 int
109 rand_regression_test(size_t iter, size_t keys, const std::string name, TL tl)
110 {
111 // Sane defaults.
112 size_t n = iter;
113 size_t m = keys;
114 size_t sd = 0; // 0 = time-determined arbitrary
115 double tp = 0.2;
116 double ip = 0.6;
117 double ep = 0.2;
118 double cp = 0.001;
578089db 119 double mp = 0.25;
4569a895
AT
120 bool disp = false; // show progress
121
122 try
123 {
124 detail::verify_params(sd, n, m, tp, ip, ep, cp, mp, disp);
125 }
5e11f978 126 catch (__gnu_pbds::test::illegal_input_error&)
4569a895
AT
127 {
128 detail::usage(name);
129 return -1;
130 }
55a4c999 131 catch (...)
4569a895
AT
132 {
133 return -2;
134 };
135
136 xml_test_rand_regression_formatter* p_fmt = NULL;
137 if (sd == 0)
138 sd = twister_rand_gen::get_time_determined_seed();
139 if (disp)
140 p_fmt = new xml_test_rand_regression_formatter(sd, n, m, tp, ip,
141 ep, cp, mp);
142
143 try
144 {
145 detail::rand_reg_test tst(sd, n, m, tp, ip, ep, cp, mp, disp);
d7f245b1 146 __gnu_cxx::typelist::apply(tst, tl);
4569a895 147 }
3441f106 148 catch (...)
4569a895
AT
149 {
150 std::cerr << "Test failed with seed " << sd << std::endl;
151 if (disp)
152 delete p_fmt;
3441f106 153 throw;
4569a895
AT
154 }
155
156 if (disp)
157 delete p_fmt;
158 return 0;
159 }
160
161namespace detail
162{
163 void
164 usage(const std::string& name)
165 {
166 using namespace std;
167 cerr << "usage: " << name << " <sd> <n> <m> <tp> <ip> <ep> <cp> <mp> ['t' | 'f']" <<
168 endl << endl;
169
170 cerr << "This test performs basic regression tests on various associative containers."
171 "For each container, it performs a sequence of operations. At each iteration, it does "
172 "the following: " << endl;
173 cerr << "* Performs an operation on the container " << endl;
174 cerr << "* Performs the same operation on an cntnr object" << endl;
175 cerr << "* Possibly compares the container to the cntnr object" << endl;
176 cerr << "* Checks that exceptions (thrown by an allocator) "
177 "do not violate exception guarantees";
178
179 cerr << endl << endl;
180
181 cerr << "sd = seed for random-number generator; 0 = "
182 "time determined value" << endl;
183 cerr << "n = number of iterations" << endl;
184 cerr << "m = number of distinct values" << endl;
185 cerr << "tp = probability that an exception will be actively thrown" << endl;
186 cerr << "ip = probability that an operation will be insert" << endl;
187 cerr << "ep = probability that an operation will be erase" << endl;
188 cerr << "cp = probability that an operation will be clear" << endl;
189 cerr << "(therefore, 1 - (ip + ep + cp) = probability of any other operation)" << endl;
190 cerr << "mp = probability that the container will be compared to the cntnr object" << endl;
191 cerr << "'t' or 'f' determine whether progress will be displayed" << endl;
192 }
193
194 void
195 verify_params(size_t& r_seed, size_t& r_n,
196 size_t& r_m, double& r_tp, double& r_ip, double& r_ep,
197 double& r_cp, double& r_mp, bool& r_d)
198 {
199 verify_prob(r_tp);
200 verify_prob(r_ip);
201 verify_prob(r_ep);
202 verify_prob(r_cp);
203 verify_prob(r_mp);
204 verify_prob(r_ip + r_ep + r_cp);
205 }
206} // namespace detail
207} // namespace test
5e11f978 208} // namespace __gnu_pbds
4569a895 209
55a4c999 210#endif