| 1 |
// -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2005-2025 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. |
| 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 COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. |
| 22 |
|
| 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. |
| 31 |
|
| 32 |
/** |
| 33 |
* @file container_rand_regression_test.h |
| 34 |
* Contains a random regression test for a specific container type. |
| 35 |
*/ |
| 36 |
|
| 37 |
#ifndef PB_DS_CONTAINER_RAND_REGRESSION_TEST_H |
| 38 |
#define PB_DS_CONTAINER_RAND_REGRESSION_TEST_H |
| 39 |
|
| 40 |
#include <algorithm> |
| 41 |
#include <string> |
| 42 |
#include <sstream> |
| 43 |
#include <utility> |
| 44 |
#include <cassert> |
| 45 |
#include <regression/basic_type.hpp> |
| 46 |
#include <ext/pb_ds/priority_queue.hpp> |
| 47 |
#include <io/prog_bar.hpp> |
| 48 |
#include <testsuite_rng.h> |
| 49 |
#include <common_type/priority_queue/string_form.hpp> |
| 50 |
#include <regression/rand/xml_formatter.hpp> |
| 51 |
#include <regression/trait/priority_queue/trait.hpp> |
| 52 |
|
| 53 |
namespace __gnu_pbds |
| 54 |
{ |
| 55 |
namespace test |
| 56 |
{ |
| 57 |
namespace detail |
| 58 |
{ |
| 59 |
// Rand test specialized for a specific container. |
| 60 |
template<typename Cntnr> |
| 61 |
class container_rand_regression_test |
| 62 |
{ |
| 63 |
private: |
| 64 |
typedef Cntnr cntnr; |
| 65 |
typedef typename cntnr::allocator_type allocator_type; |
| 66 |
typedef typename cntnr::size_type size_type; |
| 67 |
typedef twister_rand_gen gen; |
| 68 |
typedef basic_type value_type; |
| 69 |
typedef native_priority_queue<std::string, true> native_type; |
| 70 |
typedef regression_test_traits<cntnr> test_traits; |
| 71 |
|
| 72 |
enum op |
| 73 |
{ |
| 74 |
insert_op, |
| 75 |
modify_op, |
| 76 |
erase_op, |
| 77 |
clear_op, |
| 78 |
other_op |
| 79 |
}; |
| 80 |
|
| 81 |
op |
| 82 |
get_next_op(); |
| 83 |
|
| 84 |
size_t |
| 85 |
get_next_sub_op(size_t max); |
| 86 |
|
| 87 |
static void |
| 88 |
defs(); |
| 89 |
|
| 90 |
static void |
| 91 |
value_defs(); |
| 92 |
|
| 93 |
static void |
| 94 |
ds_defs(); |
| 95 |
|
| 96 |
static void |
| 97 |
iterator_defs(); |
| 98 |
|
| 99 |
static void |
| 100 |
policy_defs(); |
| 101 |
|
| 102 |
void |
| 103 |
policy_access(); |
| 104 |
|
| 105 |
void |
| 106 |
it_copy(); |
| 107 |
|
| 108 |
void |
| 109 |
it_assign(); |
| 110 |
|
| 111 |
bool |
| 112 |
default_constructor(); |
| 113 |
|
| 114 |
void |
| 115 |
swap(); |
| 116 |
|
| 117 |
bool |
| 118 |
copy_constructor(); |
| 119 |
|
| 120 |
bool |
| 121 |
assignment_operator(); |
| 122 |
|
| 123 |
bool |
| 124 |
it_constructor(); |
| 125 |
|
| 126 |
bool |
| 127 |
push(); |
| 128 |
|
| 129 |
bool |
| 130 |
modify(); |
| 131 |
|
| 132 |
bool |
| 133 |
pop(); |
| 134 |
|
| 135 |
bool |
| 136 |
erase_if(); |
| 137 |
|
| 138 |
bool |
| 139 |
erase_it(); |
| 140 |
|
| 141 |
bool |
| 142 |
clear(); |
| 143 |
|
| 144 |
bool |
| 145 |
split_join(); |
| 146 |
|
| 147 |
void |
| 148 |
cmp(const Cntnr& r_container, const native_type& r_native_c, |
| 149 |
const std::string& r_call_fn); |
| 150 |
|
| 151 |
void |
| 152 |
print_container(const native_type& r_cnt, |
| 153 |
std::ostream& r_os = std::cerr) const; |
| 154 |
|
| 155 |
void |
| 156 |
print_container(const cntnr& r_cnt, |
| 157 |
std::ostream& r_os = std::cerr) const; |
| 158 |
|
| 159 |
struct destructor_printer |
| 160 |
{ |
| 161 |
destructor_printer(const std::string& r_msg) |
| 162 |
: m_msg(r_msg), m_print(true) { } |
| 163 |
|
| 164 |
void |
| 165 |
cancel() |
| 166 |
{ m_print = false; } |
| 167 |
|
| 168 |
~destructor_printer() |
| 169 |
{ |
| 170 |
if (m_print) |
| 171 |
{ |
| 172 |
std::cerr << std::endl << "Uncaught exception: " << std::endl |
| 173 |
<< m_msg << std::endl; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
const std::string m_msg; |
| 178 |
bool m_print; |
| 179 |
}; |
| 180 |
|
| 181 |
const unsigned long m_seed; |
| 182 |
const size_t m_n; |
| 183 |
const size_t m_m; |
| 184 |
const double m_tp; |
| 185 |
const double m_ip; |
| 186 |
const double m_dp; |
| 187 |
const double m_ep; |
| 188 |
const double m_cp; |
| 189 |
const double m_mp; |
| 190 |
const bool m_disp; |
| 191 |
twister_rand_gen m_g; |
| 192 |
Cntnr* m_p_c; |
| 193 |
native_type m_native_c; |
| 194 |
allocator_type m_alloc; |
| 195 |
size_t m_i; |
| 196 |
|
| 197 |
public: |
| 198 |
container_rand_regression_test(unsigned long seed, size_t n, size_t m, |
| 199 |
double tp, double ip, double dp, |
| 200 |
double ep, double cp, double mp, |
| 201 |
bool disp); |
| 202 |
|
| 203 |
virtual |
| 204 |
~container_rand_regression_test(); |
| 205 |
|
| 206 |
void |
| 207 |
operator()(); |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
#ifdef PB_DS_REGRESSION_TRACE |
| 212 |
# define PB_DS_TRACE(X) std::cerr << X << std::endl |
| 213 |
#else |
| 214 |
# define PB_DS_TRACE(X) |
| 215 |
#endif |
| 216 |
|
| 217 |
#define PB_DS_CLASS_T_DEC \ |
| 218 |
template<typename Cntnr> |
| 219 |
|
| 220 |
#define PB_DS_CLASS_C_DEC \ |
| 221 |
container_rand_regression_test<Cntnr> |
| 222 |
|
| 223 |
#define PB_DS_COND_COMPARE(L, R) \ |
| 224 |
if (m_g.get_prob() < m_mp) \ |
| 225 |
cmp(L, R, __FUNCTION__); |
| 226 |
|
| 227 |
#define PB_DS_RUN_MTHD(MTHD) \ |
| 228 |
{ \ |
| 229 |
bool done = false; \ |
| 230 |
while (!done) \ |
| 231 |
done = MTHD(); \ |
| 232 |
} |
| 233 |
|
| 234 |
#define _GLIBCXX_THROW_IF_(PRED, MORE, P_C, P_NC, F, L) \ |
| 235 |
if (PRED) \ |
| 236 |
{ \ |
| 237 |
std::cerr << "Failure at " << F << ": " << L << std::endl; \ |
| 238 |
std::cerr << MORE << std::endl; \ |
| 239 |
std::cerr << "container:" << std::endl; \ |
| 240 |
print_container(*(P_C)); \ |
| 241 |
std::cerr << std::endl; \ |
| 242 |
std::cerr << "native container:" << std::endl; \ |
| 243 |
print_container(*(P_NC)); \ |
| 244 |
std::cerr << std::endl; \ |
| 245 |
throw std::logic_error("pbds throw if failed"); \ |
| 246 |
} |
| 247 |
|
| 248 |
#define _GLIBCXX_THROW_IF(PRED, MORE, P_C, P_NC) \ |
| 249 |
_GLIBCXX_THROW_IF_(PRED, MORE, P_C, P_NC, __FILE__, __LINE__) |
| 250 |
|
| 251 |
#include <regression/rand/priority_queue/container_rand_regression_test.tcc> |
| 252 |
|
| 253 |
#undef PB_DS_COND_COMPARE |
| 254 |
#undef PB_DS_RUN_MTHD |
| 255 |
#undef PB_DS_CLASS_T_DEC |
| 256 |
#undef PB_DS_CLASS_C_DEC |
| 257 |
#undef _GLIBCXX_THROW_IF_ |
| 258 |
#undef _GLIBCXX_THROW_IF |
| 259 |
#undef PB_DS_TRACE |
| 260 |
|
| 261 |
} // namespace detail |
| 262 |
} // namespace test |
| 263 |
} // namespace __gnu_pbds |
| 264 |
|
| 265 |
#endif |