1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// ----------------------------------------------------------------------
#include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
#include "CLHEP/Random/Randomize.h"
#include "CLHEP/Random/NonRandomEngine.h"
#include "CLHEP/Random/defs.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <cassert>
#define CLEAN_OUTPUT
#ifdef CLEAN_OUTPUT
std::ofstream output("testInstanceRestore.cout");
#else
std::ostream & output = std::cout;
#endif
// Normally on for routine validation:
#ifdef TURNOFF
#endif
#define TEST_ENGINE_NAMES
#define TEST_INSTANCE_METHODS
#define VERBOSER
#define VERBOSER2
using namespace CLHEP;
// Absolutely Safe Equals Without Registers Screwing Us Up
bool equals01(const std::vector<double> &ab) {
return ab[1]==ab[0];
}
bool equals(double a, double b) {
std::vector<double> ab(2);
ab[0]=a; ab[1]=b;
return (equals01(ab));
}
std::vector<double> aSequence(int n) {
std::vector<double> v;
DualRand e(13542);
RandFlat f(e);
for (int i=0; i<n; i++) {
v.push_back(f());
}
return v;
}
// ----------- Tests for instance methods -----------
template <class E>
int checkEngineName(const std::string & name) {
int stat = 0;
output << E::engineName() << "\n";
if (E::engineName() != name) {
std::cout << "???? engineName mismatch for " << name << " <--> "
<< E::engineName() << "\n";
#ifdef CLEAN_OUTPUT
output << "???? engineName mismatch for " << name << " <--> "
<< E::engineName() << "\n";
#endif
stat |= 256;
}
E e(123);
if (e.name() != name) {
std::cout << "???? name mismatch for " << name << " <--> "
<< e.name() << "\n";
#ifdef CLEAN_OUTPUT
output << "???? name mismatch for " << name << " <--> "
<< e.name() << "\n";
#endif
stat |= 256;
}
return stat;
}
template <class E, class D>
int checkEngine() {
int stat = 0;
E e(1234);
D d(e);
if (d.engine().name() != e.name()) {
std::cout << "???? Improper d.engine() \n";
#ifdef CLEAN_OUTPUT
output << "???? Improper d.engine() \n";
#endif
stat |= 512;
}
return stat;
}
template <class E>
int checkEngineInstanceSave(E & e) {
int stat = 0;
output << "checkEngineInstanceSave for " << e.name() << "\n";
int pr=output.precision(20);
double r=0;
for (int i=0; i<100; i++) r += e.flat();
{std::ofstream os ("instance_engine.save"); os << e;}
for (int i=0; i<100; i++) r += e.flat();
double keyValue1 = e.flat();
double keyValue2 = e.flat();
#ifdef VERBOSER
output << keyValue1 << " " << keyValue2 << "\n";
#endif
E e2;
{std::ifstream is ("instance_engine.save"); is >> e2;}
for (int i=0; i<100; i++) r += e2.flat();
double k1 = e2.flat();
double k2 = e2.flat();
#ifdef VERBOSER
output << k1 << " " << k2 << "\n";
#endif
if ( !(equals(k1,keyValue1)) || !(equals(k2,keyValue2)) ) {
std::cout << "???? checkInstanceSave failed for " << e.name() << "\n";
#ifdef CLEAN_OUTPUT
output << "???? checkInstanceSave failed for " << e.name() << "\n";
#endif
stat |= 1024;
}
output.precision(pr);
return stat;
}
template <class E, class D>
int checkSaveDistribution(D & d, int nth) {
// verify that engine is the expected type
assert( &dynamic_cast<E &>(d.engine()) );
int stat = 0;
output << "checkSaveDistribution with " << d.engine().name()
<< ", " << d.name() << "\n";
double r=0;
r = d();
double keyValue1, keyValue2, keyValue3, keyValue4;
for (int i=0; i<nth; i++) r += d();
{std::ofstream os ("instance_distribution.save"); os << d.engine() << d;}
keyValue1 = d();
keyValue2 = d();
r += d();
// A second capture will test non-cached if first tested cached case:
{std::ofstream os ("instance2_distribution.save"); os << d.engine() << d;}
keyValue3 = d();
keyValue4 = d();
int pr = output.precision(20);
#ifdef VERBOSER
output << "keyValue1 = " << keyValue1 <<
" keyValue2 = " << keyValue2 << "\n";
output << "keyValue3 = " << keyValue3 <<
" keyValue3 = " << keyValue4 << "\n";
#endif
output.precision(pr);
E e;
D d2(e);
{ std::ifstream is ("instance_distribution.save"); is >> e >> d2;}
double k1 = d2();
double k2 = d2();
{ std::ifstream is ("instance2_distribution.save"); is >> e >> d2;}
double k3 = d2();
double k4 = d2();
#ifdef VERBOSER
pr = output.precision(20);
output << "k1 = " << k1 <<
" k2 = " << k2 << "\n";
output << "k3 = " << k3 <<
" k4 = " << k4 << "\n";
output.precision(pr);
#endif
if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
!equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
std::cout << "???? Incorrect restored value for distribution "
<< d.name() << "\n";
#ifdef CLEAN_OUTPUT
output << "???? Incorrect restored value for distribution "
<< d.name() << "\n";
#endif
stat |= 2048;
}
// if (stat) exit(-1);
return stat;
}
template <class E>
int checkRandGeneralDistribution(RandGeneral & d, int nth) {
assert( &dynamic_cast<E &>(d.engine()) );
int stat = 0;
output << "checkSaveDistribution with " << d.engine().name()
<< ", " << d.name() << "\n";
double r=0;
r = d();
double keyValue1, keyValue2, keyValue3, keyValue4;
for (int i=0; i<nth; i++) r += d();
{std::ofstream os ("instance_distribution.save"); os << d.engine() << d;}
keyValue1 = d();
keyValue2 = d();
r += d();
// A second capture will test non-cached if first tested cached case:
{std::ofstream os ("instance2_distribution.save"); os << d.engine() << d;}
keyValue3 = d();
keyValue4 = d();
int pr = output.precision(20);
#ifdef VERBOSER
output << "keyValue1 = " << keyValue1 <<
" keyValue2 = " << keyValue2 << "\n";
output << "keyValue3 = " << keyValue3 <<
" keyValue3 = " << keyValue4 << "\n";
#endif
output.precision(pr);
E e;
double temp = 1;
RandGeneral d2(e, &temp, 1);
{ std::ifstream is ("instance_distribution.save"); is >> e >> d2;}
double k1 = d2();
double k2 = d2();
{ std::ifstream is ("instance2_distribution.save"); is >> e >> d2;}
double k3 = d2();
double k4 = d2();
#ifdef VERBOSER
pr = output.precision(20);
output << "k1 = " << k1 <<
" k2 = " << k2 << "\n";
output << "k3 = " << k3 <<
" k4 = " << k4 << "\n";
output.precision(pr);
#endif
if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
!equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
std::cout << "???? Incorrect restored value for distribution "
<< d.name() << "\n";
#ifdef CLEAN_OUTPUT
output << "???? Incorrect restored value for distribution "
<< d.name() << "\n";
#endif
stat |= 2048;
}
// if (stat) exit(-1);
return stat;
}
template <class E>
int checkDistributions() {
int stat = 0;
{RandGauss d(new E(12561),100.0,3.0);
stat |= checkSaveDistribution<E,RandGauss> (d,33); }
{RandGauss d(new E(12572),100.0,3.0);
stat |= checkSaveDistribution<E,RandGauss> (d,34); }
{RandGaussQ d(new E(12563),10.0,4.0);
stat |= checkSaveDistribution<E,RandGaussQ> (d,33); }
{RandGaussT d(new E(12564),5.0,2.0);
stat |= checkSaveDistribution<E,RandGaussT> (d,33); }
{RandBinomial d(new E(12565),4,0.6);
stat |= checkSaveDistribution<E,RandBinomial> (d,33); }
{RandFlat d(new E(12576),12.5,35.0);
stat |= checkSaveDistribution<E,RandFlat> (d,33); }
{RandBit d(new E(12567));
stat |= checkSaveDistribution<E,RandBit> (d,31); }
{RandBit d(new E(12578));
stat |= checkSaveDistribution<E,RandBit> (d,32); }
{RandBit d(new E(12589));
stat |= checkSaveDistribution<E,RandBit> (d,33); }
{RandBreitWigner d(new E(125611),50.0,15.0);
stat |= checkSaveDistribution<E,RandBreitWigner> (d,33); }
{RandChiSquare d(new E(125612),5.0);
stat |= checkSaveDistribution<E,RandChiSquare> (d,33); }
{RandExponential d(new E(125713),8.00);
stat |= checkSaveDistribution<E,RandExponential> (d,33); }
{RandGamma d(new E(125713),6.0,2.0);
stat |= checkSaveDistribution<E,RandGamma> (d,33); }
{RandLandau d(new E(125714));
stat |= checkSaveDistribution<E,RandLandau> (d,33); }
{RandStudentT d(new E(125715),5);
stat |= checkSaveDistribution<E,RandStudentT> (d,33); }
// Multiple tests of Poisson distributions for small desired, since
// the answer in each test is a small int, and coincidental agreement
// is very possible.
{RandPoisson d(new E(125616),2.5);
stat |= checkSaveDistribution<E,RandPoisson> (d,33); }
{RandPoisson d(new E(125617),105.0);
stat |= checkSaveDistribution<E,RandPoisson> (d,34); }
{RandPoisson d(new E(125618),2.5);
stat |= checkSaveDistribution<E,RandPoisson> (d,35); }
{RandPoisson d(new E(325618),2.5);
stat |= checkSaveDistribution<E,RandPoisson> (d,36); }
{RandPoisson d(new E(425618),2.5);
stat |= checkSaveDistribution<E,RandPoisson> (d,37); }
{RandPoisson d(new E(525618),2.5);
stat |= checkSaveDistribution<E,RandPoisson> (d,38); }
{RandPoisson d(new E(125619),110.0);
stat |= checkSaveDistribution<E,RandPoisson> (d,39); }
{RandPoissonQ d(new E(124616),2.5);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
{RandPoissonQ d(new E(126616),2.5);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,32); }
{RandPoissonQ d(new E(127616),2.5);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,31); }
{RandPoissonQ d(new E(129616),2.5);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,30); }
{RandPoissonQ d(new E(125616),110.0);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
{RandPoissonQ d(new E(125616),2.5);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
{RandPoissonQ d(new E(125616),110.0);
stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
{RandPoissonT d(new E(125616),2.5);
stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
{RandPoissonT d(new E(125616),110.0);
stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
{RandPoissonT d(new E(125616),2.5);
stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
{RandPoissonT d(new E(125616),110.0);
stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
{RandPoissonT d(new E(125916),2.5);
stat |= checkSaveDistribution<E,RandPoissonT> (d,10); }
{RandPoissonT d(new E(125816),2.5);
stat |= checkSaveDistribution<E,RandPoissonT> (d,11); }
{RandPoissonT d(new E(125716),2.5);
stat |= checkSaveDistribution<E,RandPoissonT> (d,12); }
{std::vector<double> pdf;
int nbins = 20;
for (int i = 0; i < nbins; ++i)
pdf.push_back( 5*i + (10.5-i) * (10.5-i) );
RandGeneral d(new E(125917), &pdf[0], 20);
stat |= checkRandGeneralDistribution<E> (d,33); }
return stat;
}
// ---------------------------------------------
// ---------------------------------------------
// ---------------------------------------------
int main() {
int stat = 0;
#ifdef TEST_ENGINE_NAMES
output << "\n=============================================\n";
output << " Part II \n";
output << "Check all engine names were entered correctly \n";
output << "=============================================\n\n";
stat |= checkEngineName<DRand48Engine >("DRand48Engine");
stat |= checkEngineName<DualRand >("DualRand");
stat |= checkEngineName<Hurd160Engine >("Hurd160Engine");
stat |= checkEngineName<Hurd288Engine >("Hurd288Engine");
stat |= checkEngineName<HepJamesRandom>("HepJamesRandom");
stat |= checkEngineName<MTwistEngine >("MTwistEngine");
stat |= checkEngineName<RandEngine >("RandEngine");
stat |= checkEngineName<RanecuEngine >("RanecuEngine");
stat |= checkEngineName<Ranlux64Engine>("Ranlux64Engine");
stat |= checkEngineName<RanluxEngine >("RanluxEngine");
stat |= checkEngineName<RanshiEngine >("RanshiEngine");
stat |= checkEngineName<TripleRand >("TripleRand");
#endif
#ifdef TEST_INSTANCE_METHODS
output << "===========================================\n\n";
output << " Part III\n";
output << "Check instance methods for specific engines \n";
output << " specific engines and distributions\n";
output << "===========================================\n\n";
{DualRand e(234); stat |= checkEngineInstanceSave(e);}
{Hurd160Engine e(234); stat |= checkEngineInstanceSave(e);}
{Hurd288Engine e(234); stat |= checkEngineInstanceSave(e);}
{HepJamesRandom e(234); stat |= checkEngineInstanceSave(e);}
{MTwistEngine e(234); stat |= checkEngineInstanceSave(e);}
{RandEngine e(234); stat |= checkEngineInstanceSave(e);}
{RanecuEngine e(234); stat |= checkEngineInstanceSave(e);}
{Ranlux64Engine e(234); stat |= checkEngineInstanceSave(e);}
{RanluxEngine e(234); stat |= checkEngineInstanceSave(e);}
{RanshiEngine e(234); stat |= checkEngineInstanceSave(e);}
{TripleRand e(234); stat |= checkEngineInstanceSave(e);}
{std::vector<double> nonRand = aSequence(500);
NonRandomEngine e;
e.setRandomSequence(&nonRand[0], nonRand.size());
stat |= checkEngineInstanceSave(e);}
stat |= checkDistributions<DualRand>();
stat |= checkDistributions<Hurd160Engine>();
stat |= checkDistributions<Hurd288Engine>();
stat |= checkDistributions<HepJamesRandom>();
stat |= checkDistributions<MTwistEngine>();
stat |= checkDistributions<Ranlux64Engine>();
stat |= checkDistributions<RanluxEngine>();
stat |= checkDistributions<RanshiEngine>();
stat |= checkDistributions<TripleRand>();
RandGaussQ::shoot(); // Just to verify that the static engine is OK
#endif
output << "\n=============================================\n\n";
if (stat != 0) {
std::cout << "One or more problems detected: stat = " << stat << "\n";
output << "One or more problems detected: stat = " << stat << "\n";
} else {
output << "testInstanceRestore passed with no problems detected.\n";
}
if (stat == 0) return 0;
if (stat > 0) return -(stat|1);
return stat|1;
}
|