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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
// ----------------------------------------------------------------------
#include "CLHEP/Random/Randomize.h"
#include "CLHEP/Random/NonRandomEngine.h"
#include "CLHEP/Random/defs.h"
#include <iostream>
#include <iomanip>
#include <vector>
#define CLEAN_OUTPUT
#ifdef CLEAN_OUTPUT
std::ofstream output("testSaveEngineStatus.cout");
#else
std::ostream & output = std::cout;
#endif
// Normally on for routine validation:
#define TEST_ORIGINAL_SAVE
// Normally off for routine validation:
#ifdef TURNOFF
#define TEST_MISSING_FILES
#define CREATE_OLD_SAVES
#define VERIFY_OLD_SAVES
#endif
#define VERBOSER
#define VERBOSER2
using namespace CLHEP;
double remembered_r2;
double remembered_r1005;
double remembered_r1006;
double remembered_r1007;
// 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));
}
// ------------------- The following should all FAIL ------------
int saveStepX() {
double r = RandGauss::shoot();
output << "r(1) = " << r << std::endl;
HepRandom::saveEngineStatus();
r = RandGauss::shoot();
output << "r(2) = " << r << std::endl;
remembered_r2 = r;
r = RandGauss::shoot();
output << "r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandGauss::shoot();
}
r = RandGauss::shoot();
remembered_r1005 = r;
output << "r1005= " << r << std::endl;
r = RandGauss::shoot();
return 0;
}
int restoreStepX() {
HepRandom::restoreEngineStatus();
double r = RandGauss::shoot();
output << "restored r(2) = " << r << std::endl;
if ( ! equals(r,remembered_r2) ) {
output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
}
r = RandGauss::shoot();
output << "restored r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandGauss::shoot();
}
r = RandGauss::shoot();
output << "restored r1005= " << r << std::endl;
if ( !equals(r,remembered_r1005) ) {
output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
}
return 0;
}
int BsaveStepX() {
int r = RandFlat::shootBit();
output << "r(1) = " << r << std::endl;
HepRandom::saveEngineStatus();
r = RandFlat::shootBit();
output << "r(2) = " << r << std::endl;
remembered_r2 = r;
r = RandFlat::shootBit();
output << "r(3) = " << r << std::endl;
double d;
for (int i=0; i < 1001; i++) {
d = RandFlat::shoot();
if (d > 1) output <<
"This line inserted so clever compilers don't warn about not using d\n";
}
r = RandFlat::shootBit();
remembered_r1005 = r;
output << "r1005= " << r << std::endl;
r = RandFlat::shootBit();
return 0;
}
int BrestoreStepX() {
HepRandom::restoreEngineStatus();
int r = RandFlat::shootBit();
output << "restored r(2) = " << r << std::endl;
if ( r != remembered_r2 ) {
output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
}
r = RandFlat::shootBit();
output << "restored r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandFlat::shootBit();
}
r = RandFlat::shootBit();
output << "restored r1005= " << r << std::endl;
if ( r != remembered_r1005 ) {
output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
}
return 0;
}
// ------------------- The following should all WORK ------------
int saveStep() {
int stat=0;
double r = RandGauss::shoot();
output << "r(1) = " << r << std::endl;
RandGauss::saveEngineStatus();
r = RandGauss::shoot();
output << "r(2) = " << r << std::endl;
remembered_r2 = r;
r = RandGauss::shoot();
output << "r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandGauss::shoot();
}
r = RandGauss::shoot();
remembered_r1005 = r;
output << "r1005= " << r << std::endl;
r = RandGauss::shoot();
return stat;
}
int restoreStep() {
int stat=0;
RandGauss::restoreEngineStatus();
double r = RandGauss::shoot();
output << "restored r(2) = " << r << std::endl;
if ( !equals(r,remembered_r2) ) {
std::cout << "restored r(2) = " << r << std::endl;
std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#ifdef CLEAN_OUTPUT
output << "restored r(2) = " << r << std::endl;
output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#endif
stat += 1;
}
r = RandGauss::shoot();
output << "restored r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandGauss::shoot();
}
r = RandGauss::shoot();
output << "restored r1005= " << r << std::endl;
if ( !equals(r,remembered_r1005) ) {
std::cout << "restored r1005= " << r << std::endl;
std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#ifdef CLEAN_OUTPUT
output << "restored r1005= " << r << std::endl;
output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#endif
stat += 2;
}
return stat;
}
int BsaveStep() {
int stat=0;
int r = RandFlat::shootBit();
output << "r(1) = " << r << std::endl;
RandFlat::saveEngineStatus();
r = RandFlat::shootBit();
output << "r(2) = " << r << std::endl;
remembered_r2 = r;
r = RandFlat::shootBit();
output << "r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandFlat::shootBit();
}
r = RandFlat::shootBit();
remembered_r1005 = r;
output << "r1005 = " << r << std::endl;
r = RandFlat::shootBit();
remembered_r1006 = r;
output << "r1006 = " << r << std::endl;
r = RandFlat::shootBit();
remembered_r1007 = r;
output << "r1007 = " << r << std::endl;
r = RandFlat::shootBit();
return stat;
}
int BrestoreStep() {
int stat=0;
RandFlat::restoreEngineStatus();
int r = RandFlat::shootBit();
output << "restored r(2) = " << r << std::endl;
if ( r != remembered_r2 ) {
stat += 4;
std::cout << "restored r(2) = " << r << std::endl;
std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#ifdef CLEAN_OUTPUT
output << "restored r(2) = " << r << std::endl;
output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#endif
}
r = RandFlat::shootBit();
output << "restored r(3) = " << r << std::endl;
for (int i=0; i < 1001; i++) {
r = RandFlat::shootBit();
}
r = RandFlat::shootBit();
output << "restored r1005= " << r << std::endl;
if ( r != remembered_r1005 ) {
stat += 8;
std::cout << "restored r1005= " << r << std::endl;
std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#ifdef CLEAN_OUTPUT
output << "restored r1005= " << r << std::endl;
output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#endif
}
r = RandFlat::shootBit();
output << "restored r1006= " << r << std::endl;
if ( r != remembered_r1006 ) {
stat += 16;
std::cout << "restored r1006= " << r << std::endl;
std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#ifdef CLEAN_OUTPUT
output << "restored r1006= " << r << std::endl;
output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#endif
}
r = RandFlat::shootBit();
output << "restored r1007= " << r << std::endl;
if ( r != remembered_r1007 ) {
stat += 32;
std::cout << "restored r1007= " << r << std::endl;
std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#ifdef CLEAN_OUTPUT
output << "restored r1007= " << r << std::endl;
output << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
#endif
}
return stat;
}
// --- The following should work, by failing in an expected way -------
template <class E, class D>
int fileNotThere() {
int stat = 0;
HepRandomEngine * old = D::getTheEngine();
E e(123);
output << "File-not-found test restoring "<<D::distributionName()<<":\n";
D::setTheEngine(&e);
D::restoreEngineStatus("noSuchFile");
D::setTheEngine(old); // If we don't do this, then the static engine shared
// by every shoot() method reamins e -- which is about
// to go out of scope and be destructed!
return stat;
}
template <class E>
int fileNotThereEngine() {
int stat = 0;
stat |= fileNotThere <E, RandBinomial>();
stat |= fileNotThere <E, RandBit>();
stat |= fileNotThere <E, RandBreitWigner>();
stat |= fileNotThere <E, RandChiSquare>();
stat |= fileNotThere <E, RandExponential>();
stat |= fileNotThere <E, RandFlat>();
stat |= fileNotThere <E, RandGamma>();
stat |= fileNotThere <E, RandGauss>();
stat |= fileNotThere <E, RandGaussQ>();
stat |= fileNotThere <E, RandGaussT>();
stat |= fileNotThere <E, RandLandau>();
stat |= fileNotThere <E, RandPoisson>();
stat |= fileNotThere <E, RandPoissonQ>();
stat |= fileNotThere <E, RandPoissonT>();
stat |= fileNotThere <E, RandSkewNormal>();
stat |= fileNotThere <E, RandStudentT>();
return stat;
}
int missingFile() {
int stat = 0;
stat |= fileNotThereEngine<DRand48Engine>();
stat |= fileNotThereEngine<DualRand>();
stat |= fileNotThereEngine<Hurd160Engine>();
stat |= fileNotThereEngine<Hurd288Engine>();
stat |= fileNotThereEngine<HepJamesRandom>();
stat |= fileNotThereEngine<MTwistEngine>();
stat |= fileNotThereEngine<RandEngine>();
stat |= fileNotThereEngine<RanecuEngine>();
stat |= fileNotThereEngine<Ranlux64Engine>();
stat |= fileNotThereEngine<RanluxEngine>();
stat |= fileNotThereEngine<RanshiEngine>();
stat |= fileNotThereEngine<TripleRand>();
return stat;
}
// -- The following was used to capture old-form engine states (sans name) --
template <class E, class D>
int saveEngine(const char* filename) {
int stat = 0;
HepRandomEngine * old = D::getTheEngine();
E e(123);
D::setTheEngine(&e);
double r=0;
for (int i=0; i<3; i++) r += D::shoot();
D::saveEngineStatus(filename);
if (r == -99999999.1) stat = 999; // This prevents clever compilers from
// deducing that r is never needed
D::setTheEngine(old); // If we don't do this, then the static engine shared
// by every shoot() method reamins e -- which is about
// to go out of scope and be destructed!
return stat;
}
// -- The following checks on static engine restores, from old and new forms --
template <class E, class D>
int checkSaveEngine(const char* filename) {
int stat = 0;
HepRandomEngine * old = D::getTheEngine();
// Generate save with current format (default file name is fine)
E e(123);
D::setTheEngine(&e);
double r=0;
for (int i=0; i<3; i++) r += D::shoot();
D::saveEngineStatus();
// Figure out what the key variate value should be
double keyValue = D::shoot();
// Restore state based on old file, and check for proper value
D::restoreEngineStatus(filename);
if (!equals(D::shoot(), keyValue)) {
std::cout << "???? Value mismatch from file " << filename << "\n";
#ifdef CLEAN_OUTPUT
output << "???? Value mismatch from file " << filename << "\n";
#endif
stat |= 64;
}
// Restore state based on that save, and check for proper value
D::restoreEngineStatus();
if (!equals(D::shoot(),keyValue)) {
std::cout << "???? Value mismatch from new-format file \n";
#ifdef CLEAN_OUTPUT
output << "???? Value mismatch from new-format file \n";
#endif
stat |= 128;
}
D::setTheEngine(old);
return stat;
}
// ---------------------------------------------
// ---------------------------------------------
// ---------------------------------------------
int main() {
int stat = 0;
#ifdef TEST_ORIGINAL_SAVE
output << "=====================================\n";
output << " Part I \n";
output << "Original tests of static save/restore\n";
output << "=====================================\n\n";
output << "Using old method or HepRandom::saveEngineStatus:\n";
output << "All these tests should have a chance of failure.\n";
output << RandGauss:: getTheEngine()->name();
output << RandGaussQ::getTheEngine()->name();
stat |= saveStepX();
stat |= restoreStepX();
stat |= BsaveStepX();
stat |= BrestoreStepX();
output << "Using the class-specific RandGauss::saveEngineStatus:\n";
output << "All these tests should work properly.\n";
stat |= saveStep();
stat |= restoreStep();
stat |= BsaveStep();
stat |= BrestoreStep();
#endif
#ifdef TEST_MISSING_FILES
output << "\n=======================================\n";
output << " Part Ia \n";
output << "Test of behavior when a file is missing \n";
output << "=======================================\n\n";
output << "Testing restoreEngineStatus with missing file:\n";
output << "Expect a number of <Failure to find or open> messages!\n";
stat |= missingFile();
#endif
#ifdef CREATE_OLD_SAVES
stat |= saveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
stat |= saveEngine<DualRand, RandPoisson>("DualRand.oldsav");
stat |= saveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
stat |= saveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
stat |= saveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
stat |= saveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
stat |= saveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
stat |= saveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
stat |= saveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
stat |= saveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
stat |= saveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
#endif
#ifdef VERIFY_OLD_SAVES
output << "\n==============================================\n";
output << " Part Ib \n";
output << " Verification that changes wont invalidate \n";
output << "invalidate engine saves from previous versions \n";
output << "==============================================\n\n";
stat |= checkSaveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
stat |= checkSaveEngine<DualRand, RandPoisson>("DualRand.oldsav");
stat |= checkSaveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
stat |= checkSaveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
stat |= checkSaveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
stat |= checkSaveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
stat |= checkSaveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
stat |= checkSaveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
stat |= checkSaveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
stat |= checkSaveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
stat |= checkSaveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
#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 << "testSaveEngineStatus passed with no problems detected.\n";
}
if (stat == 0) return 0;
if (stat > 0) return -(stat|1);
return stat|1;
}
|