CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

ranRestoreTest.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: ranRestoreTest.cc,v 1.6 2011/05/31 20:57:01 garren Exp $
3 // ----------------------------------------------------------------------
4 #include "CLHEP/Random/Randomize.h"
5 #include "CLHEP/Random/NonRandomEngine.h"
6 #include "CLHEP/Random/defs.h"
7 #include <iostream>
8 #include <iomanip>
9 #include <vector>
10 
11 #define CLEAN_OUTPUT
12 #ifdef CLEAN_OUTPUT
13  std::ofstream output("ranRestoreTest.cout");
14 #else
15  std::ostream & output = std::cout;
16 #endif
17 
18 // Normally on for routine validation:
19 
20 #define TEST_ORIGINAL_SAVE
21 
22 #ifdef TURNOFF
23 #endif
24 
25 #define TEST_ENGINE_NAMES
26 #define TEST_INSTANCE_METHODS
27 #define TEST_SHARED_ENGINES
28 #define TEST_STATIC_SAVE
29 #define TEST_SAVE_STATIC_STATES
30 #define TEST_ANONYMOUS_ENGINE_RESTORE
31 #define TEST_ANONYMOUS_RESTORE_STATICS
32 #define TEST_VECTOR_ENGINE_RESTORE
33 
34 // Normally off for routine validation:
35 
36 #ifdef TURNOFF
37 #define TEST_MISSING_FILES
38 #define CREATE_OLD_SAVES
39 #define VERIFY_OLD_SAVES
40 #endif
41 
42 //#define VERBOSER
43 //#define VERBOSER2
44 
45 using namespace CLHEP;
46 
51 
52 // Absolutely Safe Equals Without Registers Screwing Us Up
53 bool equals01(const std::vector<double> &ab) {
54  return ab[1]==ab[0];
55 }
56 bool equals(double a, double b) {
57  std::vector<double> ab(2);
58  ab[0]=a; ab[1]=b;
59  return (equals01(ab));
60 }
61 
62 // ------------------- The following should all FAIL ------------
63 
64 int saveStepX() {
65  double r = RandGauss::shoot();
66  output << "r(1) = " << r << std::endl;
68  r = RandGauss::shoot();
69  output << "r(2) = " << r << std::endl;
70  remembered_r2 = r;
71  r = RandGauss::shoot();
72  output << "r(3) = " << r << std::endl;
73  for (int i=0; i < 1001; i++) {
74  r = RandGauss::shoot();
75  }
76  r = RandGauss::shoot();
77  remembered_r1005 = r;
78  output << "r1005= " << r << std::endl;
79  r = RandGauss::shoot();
80  return 0;
81 }
82 
83 int restoreStepX() {
85  double r = RandGauss::shoot();
86  output << "restored r(2) = " << r << std::endl;
87  if ( ! equals(r,remembered_r2) ) {
88  output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
89  }
90  r = RandGauss::shoot();
91  output << "restored r(3) = " << r << std::endl;
92  for (int i=0; i < 1001; i++) {
93  r = RandGauss::shoot();
94  }
95  r = RandGauss::shoot();
96  output << "restored r1005= " << r << std::endl;
97  if ( !equals(r,remembered_r1005) ) {
98  output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
99  }
100  return 0;
101 }
102 
103 int BsaveStepX() {
104  int r = RandFlat::shootBit();
105  output << "r(1) = " << r << std::endl;
107  r = RandFlat::shootBit();
108  output << "r(2) = " << r << std::endl;
109  remembered_r2 = r;
110  r = RandFlat::shootBit();
111  output << "r(3) = " << r << std::endl;
112  double d;
113  for (int i=0; i < 1001; i++) {
114  d = RandFlat::shoot();
115  if (d > 1) output <<
116  "This line inserted so clever compilers don't warn about not using d\n";
117  }
118  r = RandFlat::shootBit();
119  remembered_r1005 = r;
120  output << "r1005= " << r << std::endl;
121  r = RandFlat::shootBit();
122  return 0;
123 }
124 
127  int r = RandFlat::shootBit();
128  output << "restored r(2) = " << r << std::endl;
129  if ( r != remembered_r2 ) {
130  output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
131  }
132  r = RandFlat::shootBit();
133  output << "restored r(3) = " << r << std::endl;
134  for (int i=0; i < 1001; i++) {
135  r = RandFlat::shootBit();
136  }
137  r = RandFlat::shootBit();
138  output << "restored r1005= " << r << std::endl;
139  if ( r != remembered_r1005 ) {
140  output << "THIS DOES NOT MATCH REMEMBERED VALUE BUT THAT IS EXPECTED\n";
141  }
142  return 0;
143 }
144 
145 // ------------------- The following should all WORK ------------
146 
147 int saveStep() {
148  int stat=0;
149  double r = RandGauss::shoot();
150  output << "r(1) = " << r << std::endl;
152  r = RandGauss::shoot();
153  output << "r(2) = " << r << std::endl;
154  remembered_r2 = r;
155  r = RandGauss::shoot();
156  output << "r(3) = " << r << std::endl;
157  for (int i=0; i < 1001; i++) {
158  r = RandGauss::shoot();
159  }
160  r = RandGauss::shoot();
161  remembered_r1005 = r;
162  output << "r1005= " << r << std::endl;
163  r = RandGauss::shoot();
164  return stat;
165 }
166 
167 int restoreStep() {
168  int stat=0;
170  double r = RandGauss::shoot();
171  output << "restored r(2) = " << r << std::endl;
172  if ( !equals(r,remembered_r2) ) {
173  std::cout << "restored r(2) = " << r << std::endl;
174  std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
175  stat += 1;
176  }
177  r = RandGauss::shoot();
178  output << "restored r(3) = " << r << std::endl;
179  for (int i=0; i < 1001; i++) {
180  r = RandGauss::shoot();
181  }
182  r = RandGauss::shoot();
183  output << "restored r1005= " << r << std::endl;
184  if ( !equals(r,remembered_r1005) ) {
185  std::cout << "restored r1005= " << r << std::endl;
186  std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
187  stat += 2;
188  }
189  return stat;
190 }
191 
192 int BsaveStep() {
193  int stat=0;
194  int r = RandFlat::shootBit();
195  output << "r(1) = " << r << std::endl;
197  r = RandFlat::shootBit();
198  output << "r(2) = " << r << std::endl;
199  remembered_r2 = r;
200  r = RandFlat::shootBit();
201  output << "r(3) = " << r << std::endl;
202  for (int i=0; i < 1001; i++) {
203  r = RandFlat::shootBit();
204  }
205  r = RandFlat::shootBit();
206  remembered_r1005 = r;
207  output << "r1005 = " << r << std::endl;
208  r = RandFlat::shootBit();
209  remembered_r1006 = r;
210  output << "r1006 = " << r << std::endl;
211  r = RandFlat::shootBit();
212  remembered_r1007 = r;
213  output << "r1007 = " << r << std::endl;
214  r = RandFlat::shootBit();
215  return stat;
216 }
217 
219  int stat=0;
221  int r = RandFlat::shootBit();
222  output << "restored r(2) = " << r << std::endl;
223  if ( r != remembered_r2 ) {
224  stat += 4;
225  std::cout << "restored r(2) = " << r << std::endl;
226  std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
227  }
228  r = RandFlat::shootBit();
229  output << "restored r(3) = " << r << std::endl;
230  for (int i=0; i < 1001; i++) {
231  r = RandFlat::shootBit();
232  }
233  r = RandFlat::shootBit();
234  output << "restored r1005= " << r << std::endl;
235  if ( r != remembered_r1005 ) {
236  stat += 8;
237  std::cout << "restored r1005= " << r << std::endl;
238  std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
239  }
240  r = RandFlat::shootBit();
241  output << "restored r1006= " << r << std::endl;
242  if ( r != remembered_r1006 ) {
243  stat += 16;
244  std::cout << "restored r1006= " << r << std::endl;
245  std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
246  }
247  r = RandFlat::shootBit();
248  output << "restored r1007= " << r << std::endl;
249  if ( r != remembered_r1007 ) {
250  stat += 32;
251  std::cout << "restored r1007= " << r << std::endl;
252  std::cout << "????? THIS DOES NOT MATCH REMEMBERED VALUE!\n";
253  }
254  return stat;
255 }
256 
257 // --- The following should work, by failing in an expected way -------
258 
259 template <class E, class D>
261  int stat = 0;
262  HepRandomEngine * old = D::getTheEngine();
263  E e(123);
264  output << "File-not-found test restoring "<<D::distributionName()<<":\n";
265  D::setTheEngine(&e);
266  D::restoreEngineStatus("noSuchFile");
267  D::setTheEngine(old); // If we don't do this, then the static engine shared
268  // by every shoot() method reamins e -- which is about
269  // to go out of scope and be destructed!
270  return stat;
271 }
272 
273 template <class E>
275  int stat = 0;
276  stat |= fileNotThere <E, RandBinomial>();
277  stat |= fileNotThere <E, RandBit>();
278  stat |= fileNotThere <E, RandBreitWigner>();
279  stat |= fileNotThere <E, RandChiSquare>();
280  stat |= fileNotThere <E, RandExponential>();
281  stat |= fileNotThere <E, RandFlat>();
282  stat |= fileNotThere <E, RandGamma>();
283  stat |= fileNotThere <E, RandGauss>();
284  stat |= fileNotThere <E, RandGaussQ>();
285  stat |= fileNotThere <E, RandGaussT>();
286  stat |= fileNotThere <E, RandLandau>();
287  stat |= fileNotThere <E, RandPoisson>();
288  stat |= fileNotThere <E, RandPoissonQ>();
289  stat |= fileNotThere <E, RandPoissonT>();
290  stat |= fileNotThere <E, RandSkewNormal>();
291  stat |= fileNotThere <E, RandStudentT>();
292  return stat;
293 }
294 
295 int missingFile() {
296  int stat = 0;
297  stat |= fileNotThereEngine<DRand48Engine>();
298  stat |= fileNotThereEngine<DualRand>();
299  stat |= fileNotThereEngine<Hurd160Engine>();
300  stat |= fileNotThereEngine<Hurd288Engine>();
301  stat |= fileNotThereEngine<HepJamesRandom>();
302  stat |= fileNotThereEngine<MTwistEngine>();
303  stat |= fileNotThereEngine<RandEngine>();
304  stat |= fileNotThereEngine<RanecuEngine>();
305  stat |= fileNotThereEngine<Ranlux64Engine>();
306  stat |= fileNotThereEngine<RanluxEngine>();
307  stat |= fileNotThereEngine<RanshiEngine>();
308  stat |= fileNotThereEngine<TripleRand>();
309  return stat;
310 }
311 
312 // -- The following was used to capture old-form engine states (sans name) --
313 
314 template <class E, class D>
315 int saveEngine(const char* filename) {
316  int stat = 0;
317  HepRandomEngine * old = D::getTheEngine();
318  E e(123);
319  D::setTheEngine(&e);
320  double r=0;
321  for (int i=0; i<3; i++) r += D::shoot();
322  D::saveEngineStatus(filename);
323  if (r == -99999999.1) stat = 999; // This prevents clever compilers from
324  // deducing that r is never needed
325  D::setTheEngine(old); // If we don't do this, then the static engine shared
326  // by every shoot() method reamins e -- which is about
327  // to go out of scope and be destructed!
328  return stat;
329 }
330 
331 // -- The following checks on static engine restores, from old and new forms --
332 
333 template <class E, class D>
334 int checkSaveEngine(const char* filename) {
335  int stat = 0;
336  HepRandomEngine * old = D::getTheEngine();
337 
338  // Generate save with current format (default file name is fine)
339  E e(123);
340  D::setTheEngine(&e);
341  double r=0;
342  for (int i=0; i<3; i++) r += D::shoot();
343  D::saveEngineStatus();
344 
345  // Figure out what the key variate value should be
346  double keyValue = D::shoot();
347 
348  // Restore state based on old file, and check for proper value
349  D::restoreEngineStatus(filename);
350  if (!equals(D::shoot(), keyValue)) {
351  std::cout << "???? Value mismatch from file " << filename << "\n";
352  stat |= 64;
353  }
354 
355  // Restore state based on that save, and check for proper value
356  D::restoreEngineStatus();
357  if (!equals(D::shoot(),keyValue)) {
358  std::cout << "???? Value mismatch from new-format file \n";
359  stat |= 128;
360  }
361 
362  D::setTheEngine(old);
363  return stat;
364 }
365 
366 
367 // ----------- Tests for instance methods -----------
368 
369 template <class E>
370 int checkEngineName(const std::string & name) {
371  int stat = 0;
372  output << E::engineName() << "\n";
373  if (E::engineName() != name) {
374  std::cout << "???? engineName mismatch for " << name << " <--> "
375  << E::engineName() << "\n";
376  stat |= 256;
377  }
378  E e(123);
379  if (e.name() != name) {
380  std::cout << "???? name mismatch for " << name << " <--> "
381  << e.name() << "\n";
382  stat |= 256;
383  }
384  return stat;
385 }
386 
387 template <class E, class D>
388 int checkEngine() {
389  int stat = 0;
390  E e(1234);
391  D d(e);
392  if (d.engine().name() != e.name()) {
393  std::cout << "???? Improper d.engine() \n";
394  stat |= 512;
395  }
396  return stat;
397 }
398 
399 template <class E>
401  int stat = 0;
402  output << "checkEngineInstanceSave for " << e.name() << "\n";
403  int pr=output.precision(20);
404  double r=0;
405  for (int i=0; i<100; i++) r += e.flat();
406  {std::ofstream os ("engine.save"); os << e;}
407  for (int i=0; i<100; i++) r += e.flat();
408  double keyValue1 = e.flat();
409  double keyValue2 = e.flat();
410 #ifdef VERBOSER
411  output << keyValue1 << " " << keyValue2 << "\n";
412 #endif
413  E e2;
414  {std::ifstream is ("engine.save"); is >> e2;}
415  for (int i=0; i<100; i++) r += e2.flat();
416  double k1 = e2.flat();
417  double k2 = e2.flat();
418 #ifdef VERBOSER
419  output << k1 << " " << k2 << "\n";
420 #endif
421  if ( !(equals(k1,keyValue1)) || !(equals(k2,keyValue2)) ) {
422  std::cout << "???? checkInstanceSave failed for " << e.name() << "\n";
423  stat |= 1024;
424  }
425  output.precision(pr);
426  return stat;
427 }
428 
429 template <class E, class D>
430 int checkSaveDistribution(D & d, int nth) {
431  dynamic_cast<E &>(d.engine());
432  int stat = 0;
433  output << "checkSaveDistribution with " << d.engine().name()
434  << ", " << d.name() << "\n";
435  double r=0;
436  r = d();
437  double keyValue1, keyValue2, keyValue3, keyValue4;
438  for (int i=0; i<nth; i++) r += d();
439  {std::ofstream os ("distribution.save1"); os << d.engine() << d;}
440  keyValue1 = d();
441  keyValue2 = d();
442  r += d();
443  // A second capture will test non-cached if first tested cached case:
444  {std::ofstream os ("distribution.save2"); os << d.engine() << d;}
445  keyValue3 = d();
446  keyValue4 = d();
447  int pr = output.precision(20);
448 #ifdef VERBOSER
449  output << "keyValue1 = " << keyValue1 <<
450  " keyValue2 = " << keyValue2 << "\n";
451  output << "keyValue3 = " << keyValue3 <<
452  " keyValue3 = " << keyValue4 << "\n";
453 #endif
454  output.precision(pr);
455  E e;
456  D d2(e);
457  { std::ifstream is ("distribution.save1"); is >> e >> d2;}
458  double k1 = d2();
459  double k2 = d2();
460  { std::ifstream is ("distribution.save2"); is >> e >> d2;}
461  double k3 = d2();
462  double k4 = d2();
463 #ifdef VERBOSER
464  pr = output.precision(20);
465  output << "k1 = " << k1 <<
466  " k2 = " << k2 << "\n";
467  output << "k3 = " << k3 <<
468  " k4 = " << k4 << "\n";
469  output.precision(pr);
470 #endif
471  if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
472  !equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
473  std::cout << "???? Incorrect restored value for distribution "
474  << d.name() << "\n";
475  stat |= 2048;
476  }
477 // if (stat) exit(-1);
478  return stat;
479 }
480 
481 template <class E>
483  dynamic_cast<E &>(d.engine());
484  int stat = 0;
485  output << "checkSaveDistribution with " << d.engine().name()
486  << ", " << d.name() << "\n";
487  double r=0;
488  r = d();
489  double keyValue1, keyValue2, keyValue3, keyValue4;
490  for (int i=0; i<nth; i++) r += d();
491  {std::ofstream os ("distribution.save1"); os << d.engine() << d;}
492  keyValue1 = d();
493  keyValue2 = d();
494  r += d();
495  // A second capture will test non-cached if first tested cached case:
496  {std::ofstream os ("distribution.save2"); os << d.engine() << d;}
497  keyValue3 = d();
498  keyValue4 = d();
499  int pr = output.precision(20);
500 #ifdef VERBOSER
501  output << "keyValue1 = " << keyValue1 <<
502  " keyValue2 = " << keyValue2 << "\n";
503  output << "keyValue3 = " << keyValue3 <<
504  " keyValue3 = " << keyValue4 << "\n";
505 #endif
506  output.precision(pr);
507  E e;
508  double temp = 1;
509  RandGeneral d2(e, &temp, 1);
510  { std::ifstream is ("distribution.save1"); is >> e >> d2;}
511  double k1 = d2();
512  double k2 = d2();
513  { std::ifstream is ("distribution.save2"); is >> e >> d2;}
514  double k3 = d2();
515  double k4 = d2();
516 #ifdef VERBOSER
517  pr = output.precision(20);
518  output << "k1 = " << k1 <<
519  " k2 = " << k2 << "\n";
520  output << "k3 = " << k3 <<
521  " k4 = " << k4 << "\n";
522  output.precision(pr);
523 #endif
524  if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
525  !equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
526  std::cout << "???? Incorrect restored value for distribution "
527  << d.name() << "\n";
528  stat |= 2048;
529  }
530 // if (stat) exit(-1);
531  return stat;
532 }
533 
534 template <class E>
536  int stat = 0;
537  {RandGauss d(new E(12561),100.0,3.0);
538  stat |= checkSaveDistribution<E,RandGauss> (d,33); }
539  {RandGauss d(new E(12572),100.0,3.0);
540  stat |= checkSaveDistribution<E,RandGauss> (d,34); }
541  {RandGaussQ d(new E(12563),10.0,4.0);
542  stat |= checkSaveDistribution<E,RandGaussQ> (d,33); }
543  {RandGaussT d(new E(12564),5.0,2.0);
544  stat |= checkSaveDistribution<E,RandGaussT> (d,33); }
545  {RandBinomial d(new E(12565),4,0.6);
546  stat |= checkSaveDistribution<E,RandBinomial> (d,33); }
547  {RandFlat d(new E(12576),12.5,35.0);
548  stat |= checkSaveDistribution<E,RandFlat> (d,33); }
549  {RandBit d(new E(12567));
550  stat |= checkSaveDistribution<E,RandBit> (d,31); }
551  {RandBit d(new E(12578));
552  stat |= checkSaveDistribution<E,RandBit> (d,32); }
553  {RandBit d(new E(12589));
554  stat |= checkSaveDistribution<E,RandBit> (d,33); }
555  {RandBreitWigner d(new E(125611),50.0,15.0);
556  stat |= checkSaveDistribution<E,RandBreitWigner> (d,33); }
557  {RandChiSquare d(new E(125612),5.0);
558  stat |= checkSaveDistribution<E,RandChiSquare> (d,33); }
559  {RandExponential d(new E(125713),8.00);
560  stat |= checkSaveDistribution<E,RandExponential> (d,33); }
561  {RandGamma d(new E(125713),6.0,2.0);
562  stat |= checkSaveDistribution<E,RandGamma> (d,33); }
563  {RandLandau d(new E(125714));
564  stat |= checkSaveDistribution<E,RandLandau> (d,33); }
565  {RandSkewNormal d(new E(125713),8.00);
566  stat |= checkSaveDistribution<E,RandSkewNormal> (d,33); }
567  {RandStudentT d(new E(125715),5);
568  stat |= checkSaveDistribution<E,RandStudentT> (d,33); }
569 
570  // Multiple tests of Poisson distributions for small desired, since
571  // the answer in each test is a small int, and coincidental agreement
572  // is very possible.
573 
574  {RandPoisson d(new E(125616),2.5);
575  stat |= checkSaveDistribution<E,RandPoisson> (d,33); }
576  {RandPoisson d(new E(125617),105.0);
577  stat |= checkSaveDistribution<E,RandPoisson> (d,34); }
578  {RandPoisson d(new E(125618),2.5);
579  stat |= checkSaveDistribution<E,RandPoisson> (d,35); }
580  {RandPoisson d(new E(325618),2.5);
581  stat |= checkSaveDistribution<E,RandPoisson> (d,36); }
582  {RandPoisson d(new E(425618),2.5);
583  stat |= checkSaveDistribution<E,RandPoisson> (d,37); }
584  {RandPoisson d(new E(525618),2.5);
585  stat |= checkSaveDistribution<E,RandPoisson> (d,38); }
586  {RandPoisson d(new E(125619),110.0);
587  stat |= checkSaveDistribution<E,RandPoisson> (d,39); }
588  {RandPoissonQ d(new E(124616),2.5);
589  stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
590  {RandPoissonQ d(new E(126616),2.5);
591  stat |= checkSaveDistribution<E,RandPoissonQ> (d,32); }
592  {RandPoissonQ d(new E(127616),2.5);
593  stat |= checkSaveDistribution<E,RandPoissonQ> (d,31); }
594  {RandPoissonQ d(new E(129616),2.5);
595  stat |= checkSaveDistribution<E,RandPoissonQ> (d,30); }
596  {RandPoissonQ d(new E(125616),110.0);
597  stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
598  {RandPoissonQ d(new E(125616),2.5);
599  stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
600  {RandPoissonQ d(new E(125616),110.0);
601  stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
602  {RandPoissonT d(new E(125616),2.5);
603  stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
604  {RandPoissonT d(new E(125616),110.0);
605  stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
606  {RandPoissonT d(new E(125616),2.5);
607  stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
608  {RandPoissonT d(new E(125616),110.0);
609  stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
610  {RandPoissonT d(new E(125916),2.5);
611  stat |= checkSaveDistribution<E,RandPoissonT> (d,10); }
612  {RandPoissonT d(new E(125816),2.5);
613  stat |= checkSaveDistribution<E,RandPoissonT> (d,11); }
614  {RandPoissonT d(new E(125716),2.5);
615  stat |= checkSaveDistribution<E,RandPoissonT> (d,12); }
616 
617  {std::vector<double> pdf;
618  int nbins = 20;
619  for (int i = 0; i < nbins; ++i)
620  pdf.push_back( 5*i + (10.5-i) * (10.5-i) );
621  RandGeneral d(new E(125917), &pdf[0], 20);
622  stat |= checkRandGeneralDistribution<E> (d,33); }
623 
624  return stat;
625 }
626 
627 template <class E, class D1, class D2>
628 int checkSharingDistributions(D1 & d1, D2 & d2, int n1, int n2) {
629  int stat = 0;
630  output << "checkSharingDistribution with: \n"
631  << d1.name() << " using " << d1.engine().name() << " and\n"
632  << d2.name() << " using " << d2.engine().name() << "\n";
633  double r=0;
634  r = d1();
635  r += d2();
636  double kv11,kv12,kv13,kv14;
637  double kv21,kv22,kv23,kv24;
638  for (int i=0; i<n1; i++) r += d1();
639  for (int j=0; j<n2; j++) r += d2();
640  {std::ofstream os ("shared.save1"); os << d1.engine() << d1 << d2;}
641  kv11 = d1();
642  kv21 = d2();
643  kv12 = d1();
644  kv22 = d2();
645  r += d1() + d2();
646  // A second capture will test non-cached if first tested cached case:
647  {std::ofstream os ("shared.save2"); os << d1.engine() << d1 << d2;}
648  kv13 = d1();
649  kv23 = d2();
650  kv14 = d1();
651  kv24 = d2();
652 #ifdef VERBOSER2
653  int pr = output.precision(20);
654  output << "kv11 = " << kv11 <<
655  " kv21 = " << kv21 << "\n";
656  output << "kv12 = " << kv12 <<
657  " kv22 = " << kv22 << "\n";
658  output << "kv13 = " << kv13 <<
659  " kv23 = " << kv23 << "\n";
660  output << "kv14 = " << kv14 <<
661  " kv24 = " << kv24 << "\n";
662  output.precision(pr);
663 #endif
664  E e;
665  D1 d1r(e);
666  D2 d2r(e);
667  { std::ifstream is ("shared.save1"); is >> e >> d1r >> d2r;}
668  double k11 = d1r();
669  double k21 = d2r();
670  double k12 = d1r();
671  double k22 = d2r();
672  { std::ifstream is ("shared.save2"); is >> e >> d1r >> d2r;}
673  double k13 = d1r();
674  double k23 = d2r();
675  double k14 = d1r();
676  double k24 = d2r();
677 #ifdef VERBOSER2
678  pr = output.precision(20);
679  output << "k11 = " << k11 <<
680  " k21 = " << k21 << "\n";
681  output << "k12 = " << k12 <<
682  " k22 = " << k22 << "\n";
683  output << "k13 = " << k13 <<
684  " k23 = " << k23 << "\n";
685  output << "k14 = " << k14 <<
686  " k24 = " << k24 << "\n";
687  output.precision(pr);
688 #endif
689  if ( !equals(k11,kv11) || !equals(k21,kv21) ||
690  !equals(k12,kv12) || !equals(k22,kv22) ||
691  !equals(k13,kv13) || !equals(k23,kv23) ||
692  !equals(k14,kv14) || !equals(k24,kv24) ) {
693  std::cout << "???? Incorrect restored value for distributions "
694  << d1.name() << " " << d2.name() << "\n";
695  stat |= 4096;
696  }
697 // if (stat) exit(-1);
698  return stat;
699 }
700 
701 
702 
703 template <class E>
705  int stat = 0;
706  E e1(98746);
707  RandGauss g1(e1,50.0,4.0);
708  RandPoissonQ p1(e1, 112.0);
709  RandGauss g2(e1,5.0,44.0);
710  RandPoissonQ p2(e1, 212.0);
711  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,5,4);
712  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p2,6,6);
713  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g2,p1,8,9);
714  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,7,5);
715  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g2,5,4);
716  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,6,6);
717  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g1,8,9);
718  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,7,5);
719  return stat;
720 }
721 
722 std::vector<double> aSequence(int n) {
723  std::vector<double> v;
724  DualRand e(13542);
725  RandFlat f(e);
726  for (int i=0; i<n; i++) {
727  v.push_back(f());
728  }
729  return v;
730 }
731 
732 // ----------- Tests for static methods -----------
733 
734 template <class D>
735 int staticSave(int n) {
736  int stat = 0;
737  int i;
738  output << "staticSave for distribution " << D::distributionName() << "\n";
739  double r = 0;
740  double v1, v2, k1, k2;
741  for (i=0; i < n; i++) r += D::shoot();
742  {
743  std::ofstream file ("distribution.save1");
744  D::saveFullState(file);
745  v1 = D::shoot();
746  D::saveFullState(file);
747  v2 = D::shoot();
748 #ifdef VERBOSER2
749  int pr = output.precision(20);
750  output << "v1 = " << v1 << " v2 = " << v2 << "\n";
751  output.precision(pr);
752 #endif
753  }
754  for (i=0; i < n; i++) r += D::shoot();
755  {
756  std::ifstream file ("distribution.save1");
757  D::restoreFullState(file);
758  k1 = D::shoot();
759  for (i=0; i < n; i++) r += D::shoot();
760  D::restoreFullState(file);
761  k2 = D::shoot();
762 #ifdef VERBOSER2
763  int pr = output.precision(20);
764  output << "k1 = " << k1 << " k2 = " << k2 << "\n";
765  output.precision(pr);
766 #endif
767  }
768  if ( (k1 != v1) || (k2 != v2) ) {
769  std::cout << "???? restoreFullState failed for " << D::distributionName() << "\n";
770  stat |= 8192;
771  }
772 
773  for (i=0; i < n; i++) r += D::shoot();
774  {
775  std::ofstream file ("distribution.save2");
776  D::saveDistState(file) << *D::getTheEngine();
777  v1 = D::shoot();
778  D::saveDistState(file) << *D::getTheEngine();
779  v2 = D::shoot();
780 #ifdef VERBOSER2
781  int pr = output.precision(20);
782  output << "v1 = " << v1 << " v2 = " << v2 << "\n";
783  output.precision(pr);
784 #endif
785  }
786  for (i=0; i < n; i++) r += D::shoot();
787  {
788  std::ifstream file ("distribution.save2");
789  D::restoreDistState(file) >> *D::getTheEngine();
790  k1 = D::shoot();
791  for (i=0; i < n; i++) r += D::shoot();
792  D::restoreDistState(file) >> *D::getTheEngine();
793  k2 = D::shoot();
794 #ifdef VERBOSER2
795  int pr = output.precision(20);
796  output << "k1 = " << k1 << " k2 = " << k2 << "\n";
797  output.precision(pr);
798 #endif
799  }
800  if ( (k1 != v1) || (k2 != v2) ) {
801  std::cout << "???? restoreDistState failed for " << D::distributionName() << "\n";
802  stat |= 16384;
803  }
804 
805  return stat;
806 }
807 
808 template <class D>
810  int stat = 0;
811  int i;
812  output << "staticSaveShootBit for " << D::distributionName() << "\n";
813  double r = 0;
814  int bit = 0;
815  int v1, v2, k1, k2;
816  for (i=0; i < n; i++) r += D::shoot();
817  for (i=0; i < n; i++) bit |= D::shootBit();
818  {
819  std::ofstream file ("distribution.save1");
820  D::saveFullState(file);
821  v1=0;
822  for (i=0; i<25; i++) {
823  v1 <<=1;
824  v1 += D::shootBit();
825  }
826  for (i=0; i < n; i++) bit |= D::shootBit();
827  D::saveFullState(file);
828  v2=0;
829  for (i=0; i<25; i++) {
830  v2 <<=1;
831  v2 += D::shootBit();
832  }
833 #ifdef VERBOSER2
834  int pr = output.precision(20);
835  output << std::hex << "v1 = " << v1 << " v2 = " << v2 << std::dec << "\n";
836  output.precision(pr);
837 #endif
838  }
839  for (i=0; i < n; i++) r += D::shoot();
840  {
841  std::ifstream file ("distribution.save1");
842  D::restoreFullState(file);
843  k1=0;
844  for (i=0; i<25; i++) {
845  k1 <<=1;
846  k1 += D::shootBit();
847  }
848  for (i=0; i < n; i++) r += D::shoot();
849  D::restoreFullState(file);
850  k2=0;
851  for (i=0; i<25; i++) {
852  k2 <<=1;
853  k2 += D::shootBit();
854  }
855 #ifdef VERBOSER2
856  int pr = output.precision(20);
857  output << std::hex << "k1 = " << k1 << " k2 = " << k2 << std::dec << "\n";
858  output.precision(pr);
859 #endif
860  }
861  if ( (k1 != v1) || (k2 != v2) ) {
862  std::cout << "???? restoreFullState failed for D shootBit()\n";
863  stat |= 32768;
864  }
865 
866  for (i=0; i < n; i++) r += D::shoot();
867  for (i=0; i < n; i++) bit |= D::shootBit();
868  {
869  std::ofstream file ("distribution.save2");
870  D::saveDistState(file) << *D::getTheEngine();
871  v1=0;
872  for (i=0; i<25; i++) {
873  v1 <<=1;
874  v1 += D::shootBit();
875  }
876  for (i=0; i < n; i++) bit |= D::shootBit();
877  D::saveDistState(file) << *D::getTheEngine();
878  v2=0;
879  for (i=0; i<25; i++) {
880  v2 <<=1;
881  v2 += D::shootBit();
882  }
883 #ifdef VERBOSER2
884  int pr = output.precision(20);
885  output << std::hex << "v1 = " << v1 << " v2 = " << v2 << std::dec << "\n";
886  output.precision(pr);
887 #endif
888  }
889  for (i=0; i < n; i++) r += D::shoot();
890  {
891  std::ifstream file ("distribution.save2");
892  D::restoreDistState(file) >> *D::getTheEngine();
893  k1=0;
894  for (i=0; i<25; i++) {
895  k1 <<=1;
896  k1 += D::shootBit();
897  }
898  for (i=0; i < n; i++) r += D::shoot();
899  for (i=0; i < n; i++) r += D::shootBit();
900  D::restoreDistState(file) >> *D::getTheEngine();
901  k2=0;
902  for (i=0; i<25; i++) {
903  k2 <<=1;
904  k2 += D::shootBit();
905  }
906 #ifdef VERBOSER2
907  int pr = output.precision(20);
908  output << std::hex << "k1 = " << k1 << " k2 = " << k2 << std::dec << "\n";
909  output.precision(pr);
910 #endif
911  }
912  if ( (k1 != v1) || (k2 != v2) ) {
913  std::cout << "???? restoreDistState failed for RnadFlat::shootBit()\n";
914  stat |= 65536;
915  }
916 
917  return stat;
918 }
919 
920 // ----------- Tests saving all statics together -----------
921 
922 void randomizeStatics(int n) {
923  for (int i=0; i<n; i++) {
927  RandFlat::shoot();
928  RandBit::shoot();
942  }
943 }
944 
945 std::vector<double> captureStatics() {
946  std::vector<double> c;
947  c.push_back( RandGauss::shoot() );
948  c.push_back( RandGaussQ::shoot() );
949  c.push_back( RandGaussT::shoot() );
950  c.push_back( RandFlat::shoot() );
951  c.push_back( RandBit::shoot() );
952  for (int i=0; i<20; i++) {
953  c.push_back( RandFlat::shootBit() );
954  c.push_back( RandBit::shootBit() );
955  }
956  c.push_back( RandPoisson::shoot() );
957  c.push_back( RandPoissonQ::shoot() );
958  c.push_back( RandPoissonT::shoot() );
959  c.push_back( RandBinomial::shoot() );
960  c.push_back( RandBreitWigner::shoot() );
961  c.push_back( RandChiSquare::shoot() );
962  c.push_back( RandExponential::shoot() );
963  c.push_back( RandGamma::shoot() );
964  c.push_back( RandLandau::shoot() );
965  c.push_back( RandSkewNormal::shoot() );
966  c.push_back( RandStudentT::shoot() );
967  return c;
968 }
969 
970 void saveStatics(std::string filename) {
971  std::ofstream os(filename.c_str());
973  // It should be possible to call this from HepRandom, or any distribution.
974  // RandGeneral, which is meaningless as a static distribution, should be the
975  // toughest test, so we use that here.
976 }
977 
978 void restoreStatics(std::string filename) {
979  std::ifstream is(filename.c_str());
981 }
982 
983 // ----------- Anonymous restore of engines -----------
984 
985 template <class E>
986 void anonymousRestore1(int n, std::vector<double> & v) {
987  output << "Anonymous restore for " << E::engineName() << "\n";
988  E e(12349876);
989  double r=0;
990  for (int i=0; i<n; i++) r += e.flat();
991  std::ofstream os("anonymous.save");
992  os << e;
993  for (int j=0; j<25; j++) v.push_back(e.flat());
994 #ifdef VERBOSER2
995  output << "First four of v are: "
996  << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
997 #endif
998  return;
999 }
1000 
1001 template <>
1002 void anonymousRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
1003 #ifdef VERBOSER
1004  output << "Anonymous restore for " << NonRandomEngine::engineName() << "\n";
1005 #endif
1006  std::vector<double> nonRand = aSequence(500);
1007  NonRandomEngine e;
1008  e.setRandomSequence(&nonRand[0], nonRand.size());
1009  double r=0;
1010  for (int i=0; i<n; i++) r += e.flat();
1011  std::ofstream os("anonymous.save");
1012  os << e;
1013  for (int j=0; j<25; j++) v.push_back(e.flat());
1014 #ifdef VERBOSER2
1015  output << "First four of v are: "
1016  << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
1017 #endif
1018  return;
1019 }
1020 
1021 template <class E>
1022 int anonymousRestore2(const std::vector<double> & v) {
1023  int stat = 0;
1024  std::vector<double> k;
1025  std::ifstream is("anonymous.save");
1026  HepRandomEngine * a;
1028  for (int j=0; j<25; j++) k.push_back(a->flat());
1029  delete a;
1030 #ifdef VERBOSER2
1031  output << "First four of k are: "
1032  << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
1033 #endif
1034  for (int m=0; m<25; m++) {
1035  if ( v[m] != k[m] ) {
1036  std::cout << "???? Incorrect restored value for anonymous engine"
1037  << E::engineName() << "\n";
1038  stat |= 262144;
1039  return stat;
1040  }
1041  }
1042  return stat;
1043 }
1044 
1045 
1046 template <class E>
1048  std::vector<double> v;
1049  anonymousRestore1<E>(n,v);
1050  return anonymousRestore2<E>(v);
1051 }
1052 
1053 // ----------- Anonymous restore of all static distributions -----------
1054 
1055 template <class E>
1057  int stat = 0;
1058  HepRandomEngine *e = new E(12456);
1060  randomizeStatics(15);
1061  output << "\nRandomized, with theEngine = " << e->name() << "\n";
1062  saveStatics("distribution.save");
1063  output << "Saved all static distributions\n";
1064  std::vector<double> c = captureStatics();
1065  output << "Captured output of all static distributions\n";
1066  randomizeStatics(11);
1067  output << "Randomized all static distributions\n";
1068  restoreStatics("distribution.save");
1069  output << "Restored all static distributions to saved state\n";
1070  std::vector<double> d = captureStatics();
1071  output << "Captured output of all static distributions\n";
1072  for (unsigned int iv=0; iv<c.size(); iv++) {
1073  if (c[iv] != d[iv]) {
1074  std::cout << "???? restoreStaticRandomStates failed at random "
1075  << iv <<"\n";
1076  stat |= 131072;
1077  }
1078  }
1079  if (stat & 131072 == 0) {
1080  output << "All captured output agrees with earlier values\n";
1081  }
1082  return stat;
1083 }
1084 
1085 
1086 
1087 template <class E1, class E2>
1089  int stat = 0;
1090  if ( E1::engineName() == E2::engineName() ) {
1091  return anonymousRestoreStatics1<E1>();
1092  }
1093  HepRandomEngine *e1 = new E1(12456);
1095  randomizeStatics(15);
1096  output << "\nRandomized, with theEngine = " << e1->name() << "\n";
1097  saveStatics("distribution.save");
1098 #ifdef VERBOSER2
1099  output << "Saved all static distributions\n";
1100 #endif
1101  std::vector<double> c = captureStatics();
1102 #ifdef VERBOSER2
1103  output << "Captured output of all static distributions\n";
1104 #endif
1105  delete e1;
1106  HepRandomEngine *e2 = new E2(24653);
1108  output << "Switched to theEngine = " << e2->name() << "\n";
1109  randomizeStatics(19);
1110  { std::ofstream os("engine.save"); os << *e2; }
1111  double v1 = e2->flat();
1112  double v2 = e2->flat();
1113  { std::ifstream is("engine.save"); is >> *e2; }
1114 #ifdef VERBOSER2
1115  output << "Saved the " << e2->name() << " engine: \n"
1116  << "Next randoms to be " << v1 << " " << v2 << "\n"
1117  << "Restored the " << e2->name() << " engine to that state\n";
1118 #endif
1119  restoreStatics("distribution.save");
1120 #ifdef VERBOSER2
1121  output << "Restored all static distributions to saved state\n"
1122  << "This changes the engine type back to " << E1::engineName() << "\n";
1123 #endif
1124  std::vector<double> d = captureStatics();
1125 #ifdef VERBOSER2
1126  output << "Captured output of all static distributions\n";
1127 #endif
1128  for (unsigned int iv=0; iv<c.size(); iv++) {
1129  if (c[iv] != d[iv]) {
1130  std::cout << "???? restoreStaticRandomStates failed at random "
1131  << iv <<"\n";
1132  stat |= 524288;
1133  }
1134  }
1135  if (stat & 524288 == 0) {
1136  output << "All captured output agrees with earlier values\n";
1137  }
1138  double k1 = e2->flat();
1139  double k2 = e2->flat();
1140 #ifdef VERBOSER2
1141  output << "The " << e2->name() << " engine should not have been affected: \n"
1142  << "Next randoms are " << k1 << " " << k2 << "\n";
1143 #endif
1144  if ( !equals(v1,k1) || !equals(v2,k2) ) {
1145  std::cout << "???? Engine used as theEngine was affected by restoring \n"
1146  << " static distributions to use engine of a different type.\n";
1147  stat |= 1048576;
1148  }
1149  return stat;
1150 }
1151 
1152 // ----------- Vector restore of engines -----------
1153 
1154 template <class E>
1155 std::vector<unsigned long> vectorRestore1(int n, std::vector<double> & v) {
1156  output << "Vector restore for " << E::engineName() << "\n";
1157  E e(97538466);
1158  double r=0;
1159  for (int i=0; i<n; i++) r += e.flat();
1160  std::vector<unsigned long> state = e.put();
1161  for (int j=0; j<25; j++) v.push_back(e.flat());
1162 #ifdef VERBOSER2
1163  output << "First four of v are: "
1164  << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
1165 #endif
1166  return state;
1167 }
1168 
1169 template <>
1170 std::vector<unsigned long>
1171 vectorRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
1172 #ifdef VERBOSER2
1173  output << "Vector restore for " << NonRandomEngine::engineName() << "\n";
1174 #endif
1175  std::vector<double> nonRand = aSequence(500);
1176  NonRandomEngine e;
1177  e.setRandomSequence(&nonRand[0], nonRand.size());
1178  double r=0;
1179  for (int i=0; i<n; i++) r += e.flat();
1180  std::vector<unsigned long> state = e.put();
1181  for (int j=0; j<25; j++) v.push_back(e.flat());
1182 #ifdef VERBOSER2
1183  output << "First four of v are: "
1184  << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
1185 #endif
1186  return state;
1187 }
1188 
1189 template <class E>
1190 int vectorRestore2(const std::vector<unsigned long> state,
1191  const std::vector<double> & v) {
1192  int stat = 0;
1193  std::vector<double> k;
1194  HepRandomEngine * a;
1195  a = HepRandomEngine::newEngine(state);
1196  if (!a) {
1197  std::cout << "???? could not restore engine state from vector for "
1198  << E::engineName() << "\n";
1199  stat |= 1048576;
1200  return stat;
1201  }
1202  if (a->name() != E::engineName()) {
1203  std::cout << "???? restored engine state from vector for "
1204  << E::engineName() << "to different type of engine: "
1205  << a->name() << "\n"
1206  << "There is probably a clash in CRC hashes for these two names!\n";
1207  stat |= 1048576;
1208  return stat;
1209  }
1210  for (int j=0; j<25; j++) k.push_back(a->flat());
1211  delete a;
1212 #ifdef VERBOSER2
1213  output << "First four of k are: "
1214  << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
1215 #endif
1216  for (int m=0; m<25; m++) {
1217  if ( v[m] != k[m] ) {
1218  std::cout << "???? Incorrect vector restored value for anonymous engine: "
1219  << E::engineName() << "\n";
1220  stat |= 1048576;
1221  return stat;
1222  }
1223  }
1224  return stat;
1225 }
1226 
1227 
1228 template <class E>
1229 int vectorRestore(int n) {
1230  std::vector<double> v;
1231  std::vector<unsigned long> state = vectorRestore1<E>(n,v);
1232  return vectorRestore2<E>(state, v);
1233 }
1234 
1235 
1236 
1237 // ---------------------------------------------
1238 // ---------------------------------------------
1239 // ---------------------------------------------
1240 
1241 
1242 int main() {
1243  int stat = 0;
1244 
1245 #ifdef TEST_ORIGINAL_SAVE
1246  output << "=====================================\n";
1247  output << " Part I \n";
1248  output << "Original tests of static save/restore\n";
1249  output << "=====================================\n\n";
1250 
1251  output << "Using old method or HepRandom::saveEngineStatus:\n";
1252  output << "All these tests should have a chance of failure.\n";
1253 
1256 
1257  stat |= saveStepX();
1258  stat |= restoreStepX();
1259  stat |= BsaveStepX();
1260  stat |= BrestoreStepX();
1261 
1262  output << "Using the class-specific RandGauss::saveEngineStatus:\n";
1263  output << "All these tests should work properly.\n";
1264 
1265  stat |= saveStep();
1266  stat |= restoreStep();
1267  stat |= BsaveStep();
1268  stat |= BrestoreStep();
1269 #endif
1270 
1271 #ifdef TEST_MISSING_FILES
1272  output << "\n=======================================\n";
1273  output << " Part Ia \n";
1274  output << "Test of behavior when a file is missing \n";
1275  output << "=======================================\n\n";
1276 
1277  output << "Testing restoreEngineStatus with missing file:\n";
1278  output << "Expect a number of <Failure to find or open> messages!\n";
1279  stat |= missingFile();
1280 #endif
1281 
1282 #ifdef CREATE_OLD_SAVES
1283  stat |= saveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
1284  stat |= saveEngine<DualRand, RandPoisson>("DualRand.oldsav");
1285  stat |= saveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
1286  stat |= saveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
1287  stat |= saveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
1288  stat |= saveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
1289  stat |= saveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
1290  stat |= saveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
1291  stat |= saveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
1292  stat |= saveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
1293  stat |= saveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
1294 #endif
1295 
1296 #ifdef VERIFY_OLD_SAVES
1297  output << "\n==============================================\n";
1298  output << " Part Ib \n";
1299  output << " Verification that changes wont invalidate \n";
1300  output << "invalidate engine saves from previous versions \n";
1301  output << "==============================================\n\n";
1302 
1303  stat |= checkSaveEngine<DRand48Engine, RandPoisson>("DRand48Engine.oldsav");
1304  stat |= checkSaveEngine<DualRand, RandPoisson>("DualRand.oldsav");
1305  stat |= checkSaveEngine<Hurd160Engine, RandPoisson>("Hurd160Engine.oldsav");
1306  stat |= checkSaveEngine<Hurd288Engine, RandPoisson>("Hurd288Engine.oldsav");
1307  stat |= checkSaveEngine<HepJamesRandom,RandPoisson>("HepJamesRandom.oldsav");
1308  stat |= checkSaveEngine<MTwistEngine, RandPoisson>("MTwistEngine.oldsav");
1309  stat |= checkSaveEngine<Ranlux64Engine,RandPoisson>("Ranlux64Engine.oldsav");
1310  stat |= checkSaveEngine<RanluxEngine, RandPoisson>("RanluxEngine.oldsav");
1311  stat |= checkSaveEngine<RanshiEngine, RandPoisson>("RanshiEngine.oldsav");
1312  stat |= checkSaveEngine<TripleRand, RandPoisson>("TripleRand.oldsav");
1313  stat |= checkSaveEngine<RanecuEngine, RandPoisson>("RanecuEngine.oldsav");
1314 #endif
1315 
1316 #ifdef TEST_ENGINE_NAMES
1317  output << "\n=============================================\n";
1318  output << " Part II \n";
1319  output << "Check all engine names were entered correctly \n";
1320  output << "=============================================\n\n";
1321 
1322  stat |= checkEngineName<DRand48Engine >("DRand48Engine");
1323  stat |= checkEngineName<DualRand >("DualRand");
1324  stat |= checkEngineName<Hurd160Engine >("Hurd160Engine");
1325  stat |= checkEngineName<Hurd288Engine >("Hurd288Engine");
1326  stat |= checkEngineName<HepJamesRandom>("HepJamesRandom");
1327  stat |= checkEngineName<MTwistEngine >("MTwistEngine");
1328  stat |= checkEngineName<RandEngine >("RandEngine");
1329  stat |= checkEngineName<RanecuEngine >("RanecuEngine");
1330  stat |= checkEngineName<Ranlux64Engine>("Ranlux64Engine");
1331  stat |= checkEngineName<RanluxEngine >("RanluxEngine");
1332  stat |= checkEngineName<RanshiEngine >("RanshiEngine");
1333  stat |= checkEngineName<TripleRand >("TripleRand");
1334 #endif
1335 
1336 #ifdef TEST_INSTANCE_METHODS
1337  output << "===========================================\n\n";
1338  output << " Part III\n";
1339  output << "Check instance methods for specific engines \n";
1340  output << " specific engines and distributions\n";
1341  output << "===========================================\n\n";
1342 
1343  {DualRand e(234); stat |= checkEngineInstanceSave(e);}
1344  {Hurd160Engine e(234); stat |= checkEngineInstanceSave(e);}
1345  {Hurd288Engine e(234); stat |= checkEngineInstanceSave(e);}
1346  {HepJamesRandom e(234); stat |= checkEngineInstanceSave(e);}
1347  {MTwistEngine e(234); stat |= checkEngineInstanceSave(e);}
1348  {RandEngine e(234); stat |= checkEngineInstanceSave(e);}
1349  {RanecuEngine e(234); stat |= checkEngineInstanceSave(e);}
1350  {Ranlux64Engine e(234); stat |= checkEngineInstanceSave(e);}
1351  {RanluxEngine e(234); stat |= checkEngineInstanceSave(e);}
1352  {RanshiEngine e(234); stat |= checkEngineInstanceSave(e);}
1353  {TripleRand e(234); stat |= checkEngineInstanceSave(e);}
1354 
1355  {std::vector<double> nonRand = aSequence(500);
1356  NonRandomEngine e;
1357  e.setRandomSequence(&nonRand[0], nonRand.size());
1358  stat |= checkEngineInstanceSave(e);}
1359 
1360  stat |= checkDistributions<DualRand>();
1361  stat |= checkDistributions<Hurd160Engine>();
1362  stat |= checkDistributions<Hurd288Engine>();
1363  stat |= checkDistributions<HepJamesRandom>();
1364  stat |= checkDistributions<MTwistEngine>();
1365  stat |= checkDistributions<Ranlux64Engine>();
1366  stat |= checkDistributions<RanluxEngine>();
1367  stat |= checkDistributions<RanshiEngine>();
1368  stat |= checkDistributions<TripleRand>();
1369 
1370  RandGaussQ::shoot(); // Just to verify that the static engine is OK
1371 #endif
1372 
1373 #ifdef TEST_SHARED_ENGINES
1374  output << "\n=============================================\n";
1375  output << " Part IV \n";
1376  output << "Check behavior when engines are shared \n";
1377  output << "=============================================\n\n";
1378 
1379  stat |= checkSharing<DualRand>();
1380  stat |= checkSharing<Hurd160Engine>();
1381  stat |= checkSharing<Hurd288Engine>();
1382  stat |= checkSharing<HepJamesRandom>();
1383  stat |= checkSharing<MTwistEngine>();
1384  stat |= checkSharing<Ranlux64Engine>();
1385  stat |= checkSharing<RanluxEngine>();
1386  stat |= checkSharing<RanshiEngine>();
1387  stat |= checkSharing<TripleRand>();
1388 #endif
1389 
1390 #ifdef TEST_STATIC_SAVE
1391  output << "\n=========================================\n";
1392  output << " Part V \n";
1393  output << "Static Save/restore to/from streams \n";
1394  output << "=========================================\n\n";
1395 
1396  stat |= staticSave <RandGauss>(7);
1397  stat |= staticSave <RandFlat>(7);
1398  stat |= staticSaveShootBit<RandFlat> (19);
1399  stat |= staticSaveShootBit<RandBit> (23);
1400  for (int ibinom=0; ibinom<15; ibinom++) {
1401  stat |= staticSave <RandBinomial>(7+3*ibinom);
1402  }
1403  stat |= staticSave <RandBreitWigner>(7);
1404  stat |= staticSave <RandChiSquare>(7);
1405  stat |= staticSave <RandExponential>(7);
1406  stat |= staticSave <RandGamma>(7);
1407  stat |= staticSave <RandGaussQ>(7);
1408  stat |= staticSave <RandGaussT>(7);
1409  stat |= staticSave <RandLandau>(7);
1410  stat |= staticSave <RandPoisson>(7);
1411  stat |= staticSave <RandPoissonQ>(7);
1412  stat |= staticSave <RandPoissonT>(7);
1413  stat |= staticSave <RandSkewNormal>(7);
1414  stat |= staticSave <RandStudentT>(7);
1415 #endif
1416 
1417 #ifdef TEST_SAVE_STATIC_STATES
1418  output << "\n==============================================\n";
1419  output << " Part VI \n";
1420  output << "Save/restore all static states to/from streams \n";
1421  output << "==============================================\n\n";
1422 
1423  randomizeStatics(15);
1424  saveStatics("distribution.save");
1425  output << "Saved all static distributions\n";
1426  std::vector<double> c = captureStatics();
1427  output << "Captured output of all static distributions\n";
1428  randomizeStatics(11);
1429  output << "Randomized all static distributions\n";
1430  restoreStatics("distribution.save");
1431  output << "Restored all static distributions to saved state\n";
1432  std::vector<double> d = captureStatics();
1433  output << "Captured output of all static distributions\n";
1434  for (unsigned int iv=0; iv<c.size(); iv++) {
1435  if (c[iv] != d[iv]) {
1436  std::cout << "???? restoreStaticRandomStates failed at random "
1437  << iv <<"\n";
1438  stat |= 131072;
1439  }
1440  }
1441  if (stat & 131072 == 0) {
1442  output << "All captured output agrees with earlier values\n";
1443  }
1444 #endif
1445 
1446 #ifdef TEST_ANONYMOUS_ENGINE_RESTORE
1447  output << "\n=================================\n";
1448  output << " Part VII \n";
1449  output << "Anonymous restore of engines \n";
1450  output << "=================================\n\n";
1451 
1452  stat |= anonymousRestore<DualRand>(13);
1453  stat |= anonymousRestore<DRand48Engine>(14);
1454  stat |= anonymousRestore<Hurd160Engine>(15);
1455  stat |= anonymousRestore<Hurd288Engine>(16);
1456  stat |= anonymousRestore<HepJamesRandom>(17);
1457  stat |= anonymousRestore<MTwistEngine>(18);
1458  stat |= anonymousRestore<RandEngine>(29);
1459  stat |= anonymousRestore<RanecuEngine>(39);
1460  stat |= anonymousRestore<Ranlux64Engine>(19);
1461  stat |= anonymousRestore<RanluxEngine>(20);
1462  stat |= anonymousRestore<RanshiEngine>(21);
1463  stat |= anonymousRestore<TripleRand>(22);
1464  stat |= anonymousRestore<NonRandomEngine>(22);
1465 #endif
1466 
1467 #ifdef TEST_ANONYMOUS_RESTORE_STATICS
1468  output << "\n======================================\n";
1469  output << " Part VIII \n";
1470  output << "Anonymous restore static Distributions \n";
1471  output << "======================================\n\n";
1472 
1473  stat |= anonymousRestoreStatics<DualRand, Ranlux64Engine> ( );
1474  stat |= anonymousRestoreStatics<DRand48Engine, TripleRand> ( );
1475  stat |= anonymousRestoreStatics<RandEngine, Ranlux64Engine> ( );
1476  stat |= anonymousRestoreStatics<MTwistEngine, Hurd288Engine> ( );
1477  stat |= anonymousRestoreStatics<RanecuEngine, MTwistEngine> ( );
1478  stat |= anonymousRestoreStatics<HepJamesRandom, RanshiEngine> ( );
1479  stat |= anonymousRestoreStatics<RanecuEngine, RandEngine> ( );
1480  stat |= anonymousRestoreStatics<RanshiEngine, Hurd160Engine> ( );
1481  stat |= anonymousRestoreStatics<TripleRand, DualRand> ( );
1482  stat |= anonymousRestoreStatics<Hurd160Engine, HepJamesRandom> ( );
1483  stat |= anonymousRestoreStatics<Hurd288Engine, RanecuEngine> ( );
1484  stat |= anonymousRestoreStatics<HepJamesRandom, Ranlux64Engine> ( );
1485  stat |= anonymousRestoreStatics<TripleRand, TripleRand> ( );
1486  stat |= anonymousRestoreStatics<HepJamesRandom, HepJamesRandom> ( );
1487 #endif
1488 
1489 #ifdef TEST_VECTOR_ENGINE_RESTORE
1490  output << "\n=================================\n";
1491  output << " Part IX \n";
1492  output << "Save/restore of engines to vectors\n";
1493  output << "=================================\n\n";
1494 
1495  stat |= vectorRestore<DualRand>(113);
1496  stat |= vectorRestore<DRand48Engine>(114);
1497  stat |= vectorRestore<Hurd160Engine>(115);
1498  stat |= vectorRestore<Hurd288Engine>(116);
1499  stat |= vectorRestore<HepJamesRandom>(117);
1500  stat |= vectorRestore<MTwistEngine>(118);
1501  stat |= vectorRestore<RanecuEngine>(139);
1502  stat |= vectorRestore<Ranlux64Engine>(119);
1503  stat |= vectorRestore<RanluxEngine>(120);
1504  stat |= vectorRestore<RanshiEngine>(121);
1505  stat |= vectorRestore<TripleRand>(122);
1506  stat |= vectorRestore<NonRandomEngine>(123);
1507  stat |= vectorRestore<RandEngine>(129);
1508 #endif
1509 
1510 
1511 
1512 
1513  output << "\n=============================================\n\n";
1514 
1515  if (stat != 0) {
1516  std::cout << "One or more problems detected: stat = " << stat << "\n";
1517  } else {
1518  output << "ranRestoreTest passed with no problems detected.\n";
1519  }
1520 
1521  return stat;
1522 }
1523 
int checkEngineInstanceSave(E &e)
std::ofstream output("ranRestoreTest.cout")
double remembered_r1007
bool equals(double a, double b)
bool equals01(const std::vector< double > &ab)
int fileNotThereEngine()
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
double remembered_r1005
static double shoot(double a=1.0, double b=0.2)
int vectorRestore2(const std::vector< unsigned long > state, const std::vector< double > &v)
int checkSharing()
double remembered_r1006
int BrestoreStep()
std::vector< double > aSequence(int n)
std::string name() const
Definition: RandGeneral.cc:57
int checkEngineName(const std::string &name)
int vectorRestore(int n)
int restoreStep()
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:176
static double shoot()
int checkSharingDistributions(D1 &d1, D2 &d2, int n1, int n2)
static double shoot()
static long shoot(double m=1.0)
Definition: RandPoisson.cc:92
std::vector< unsigned long > vectorRestore1< NonRandomEngine >(int n, std::vector< double > &v)
int checkDistributions()
static long shoot(double m=1.0)
Definition: RandPoissonT.cc:56
int saveStep()
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:181
int BsaveStepX()
static HepRandomEngine * newEngine(std::istream &is)
Definition: RandomEngine.cc:90
int staticSaveShootBit(int n)
void anonymousRestore1< NonRandomEngine >(int n, std::vector< double > &v)
virtual double flat()=0
void saveStatics(std::string filename)
static double shoot()
int checkSaveEngine(const char *filename)
int checkSaveDistribution(D &d, int nth)
std::vector< unsigned long > vectorRestore1(int n, std::vector< double > &v)
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:171
static double shoot()
static double shoot()
int saveStepX()
Definition: excDblThrow.cc:17
int checkRandGeneralDistribution(RandGeneral &d, int nth)
int anonymousRestore2(const std::vector< double > &v)
int anonymousRestore(int n)
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandGauss.cc:170
void setRandomSequence(double *s, int n)
int BsaveStep()
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:119
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:104
static double shoot()
void f(void g())
Definition: excDblThrow.cc:38
static double shoot()
Definition: RandGauss.cc:61
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandGauss.cc:190
void randomizeStatics(int n)
double remembered_r2
int saveEngine(const char *filename)
int fileNotThere()
static std::ostream & saveStaticRandomStates(std::ostream &os)
Definition: Random.cc:196
int BrestoreStepX()
static double shoot()
Definition: RandFlat.cc:60
HepRandomEngine & engine()
Definition: RandGeneral.cc:58
static double shoot()
int missingFile()
int anonymousRestoreStatics()
int staticSave(int n)
static long shoot(double m=1.0)
void restoreStatics(std::string filename)
int anonymousRestoreStatics1()
int restoreStepX()
static int shootBit()
static std::istream & restoreStaticRandomStates(std::istream &is)
Definition: Random.cc:200
void anonymousRestore1(int n, std::vector< double > &v)
int main()
Definition: testBug66214.cc:30
int checkEngine()
static int shootBit()
static double shoot()
std::vector< double > captureStatics()
virtual std::string name() const =0
virtual std::ostream & put(std::ostream &os) const