CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testEngineCopy.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: testEngineCopy.cc,v 1.2.2.2 2010/06/29 20:12:41 garren Exp $
00003 // ----------------------------------------------------------------------
00004 #include "CLHEP/Random/Randomize.h"
00005 #include "CLHEP/Random/NonRandomEngine.h"
00006 #include "CLHEP/Random/defs.h"
00007 #include <iostream>
00008 #include <iomanip>
00009 #include <vector>
00010 
00011 #define CLEAN_OUTPUT
00012 #ifdef CLEAN_OUTPUT
00013   std::ofstream output("testEngineCopy.cout");  
00014 #else
00015   std::ostream & output = std::cout;
00016 #endif
00017 
00018 // Normally on  for routine validation:
00019 
00020 #ifdef TURNOFF
00021 #endif
00022 
00023 #define TEST_ENGINE_COPY
00024 
00025 #define VERBOSER
00026 #define VERBOSER2
00027 
00028 using namespace CLHEP;
00029 
00030 // Absolutely Safe Equals Without Registers Screwing Us Up
00031 bool equals01(const std::vector<double> &ab) {
00032   return ab[1]==ab[0];
00033 }  
00034 bool equals(double a, double b) {
00035   std::vector<double> ab(2);
00036   ab[0]=a;  ab[1]=b;
00037   return (equals01(ab));
00038 }
00039 
00040 std::vector<double> aSequence(int n) {
00041   std::vector<double> v;
00042   DualRand e(13542);
00043   RandFlat f(e);
00044   for (int i=0; i<n; i++) {
00045     v.push_back(f()); 
00046   }
00047   return v;
00048 }
00049 
00050 
00051 // ----------- Copy of engines -----------
00052 
00053 template <class E>
00054 int vectorTest64(int n) {
00055   output << "Copy 64bit test for " << E::engineName() << "\n";
00056 
00057   E e;                              
00058   double x = 0; 
00059   for (int i=0; i<n; i++) x += e.flat();            
00060   E f( e );
00061   x = e.flat();
00062   output << "x = " << x << std::endl;
00063 
00064   double y = f.flat();
00065   output << "y = " << y << std::endl;
00066   if( x != y ) return n;
00067   
00068   for( int i=0; i<1000; ++i ) {
00069       x = e.flat();
00070       y = f.flat();
00071       if( !equals(x,y) ) {
00072           output << "i = " << i << " x, y " << x << " " << y
00073                  << " vectorTest64 problem: e != f \n";
00074           return n+i;
00075       }
00076   }
00077 
00078   return 0;
00079 }
00080 // special case for NonRandomEngine
00081 template <>
00082 int vectorTest64<NonRandomEngine>(int n) {
00083   output << "Copy 64bit test for " << NonRandomEngine::engineName() << "\n";
00084 
00085   std::vector<double> nonRand = aSequence(500);
00086   NonRandomEngine e; 
00087   e.setRandomSequence(&nonRand[0], nonRand.size());
00088 
00089   double x = 0; 
00090   for (int i=0; i<n; i++) x += e.flat();            
00091   std::vector<unsigned long> v = e.put();
00092   NonRandomEngine f(e);
00093   x = e.flat();
00094   output << "x = " << x << std::endl;
00095 
00096   double y = f.flat();
00097   output << "y = " << y << std::endl;
00098   if( x != y ) return n;
00099   
00100   for( int i=0; i<300; ++i ) {
00101       if( e.flat() != f.flat() ) {
00102           output << "i = " << i << " vectorTest64 for NonRandomEngine problem: e != f \n";
00103           return n+i;
00104       }
00105   }
00106 
00107   return 0;
00108 }
00109 
00110 template <class E>
00111 E vectorRestore1(int n, std::vector<double> & v) {
00112   output << "Copy for " << E::engineName() << "\n";
00113   E e(97538466);                                    
00114   double r=0;                                       
00115   for (int i=0; i<n; i++) r += e.flat();            
00116   E f(e);    
00117   for (int j=0; j<25; j++) v.push_back(e.flat());   
00118 #ifdef VERBOSER2
00119   output << "First four of v are: " 
00120         << v[0] << ",  " << v[1] << ",  " << v[2] << ",  " << v[3] << "\n";
00121 #endif
00122   return f;
00123 }
00124 
00125 template <>
00126 NonRandomEngine
00127 vectorRestore1<NonRandomEngine> (int n, std::vector<double> & v) {
00128 #ifdef VERBOSER2
00129   output << "Copy for " << NonRandomEngine::engineName() << "\n";
00130 #endif
00131   std::vector<double> nonRand = aSequence(500);
00132   NonRandomEngine e; 
00133   e.setRandomSequence(&nonRand[0], nonRand.size());
00134   double r=0;
00135   for (int i=0; i<n; i++) r += e.flat();
00136   NonRandomEngine f(e);
00137   for (int j=0; j<25; j++) v.push_back(e.flat()); 
00138 #ifdef VERBOSER2
00139   output << "First four of v are: " 
00140         << v[0] << ",  " << v[1] << ",  " << v[2] << ",  " << v[3] << "\n";
00141 #endif
00142   return f;
00143 }
00144 
00145 template <class E>
00146 int vectorRestore2(E & f, const std::vector<double> & v) {
00147   int stat = 0;
00148   std::vector<double> k;
00149   for (int j=0; j<25; j++) k.push_back(f.flat());
00150 #ifdef VERBOSER2
00151   output << "First four of k are: " 
00152         << k[0] << ",  " << k[1] << ",  " << k[2] << ",  " << k[3] << "\n";
00153 #endif
00154   for (int m=0; m<25; m++) {
00155     if ( v[m] != k[m] ) {
00156       std::cout << "???? Incorrect copy restored value for engine: " 
00157                 << E::engineName() << "\n"; 
00158       #ifdef CLEAN_OUTPUT
00159       output << "???? Incorrect copy restored value for engine: " 
00160                 << E::engineName() << "\n"; 
00161       #endif
00162       stat |= 1048576;
00163       return stat;
00164     }
00165   }
00166   return stat;       
00167 }
00168 
00169 
00170 template <class E>
00171 int vectorRestore(int n) {
00172   std::vector<double> v;
00173   int status1 = vectorTest64<E>(n);
00174   E f = vectorRestore1<E>(n,v);
00175   int status2 = vectorRestore2<E>(f, v);  
00176   return (status1 | status2);  
00177 }
00178 
00179 
00180 
00181 // ---------------------------------------------
00182 // ---------------------------------------------
00183 // ---------------------------------------------
00184 
00185 
00186 int main() {
00187   int stat = 0;
00188 
00189 #ifdef TEST_ENGINE_COPY
00190   output << "\n=================================\n";
00191   output << "         Part IX \n";
00192   output << "    Copy test of engines\n";
00193   output << "=================================\n\n";
00194 
00195   stat |= vectorRestore<DualRand>(113);
00196   // copies of DRand48Engine are not allowed
00197   //stat |= vectorRestore<DRand48Engine>(114);
00198   stat |= vectorRestore<Hurd160Engine>(115);
00199   stat |= vectorRestore<Hurd288Engine>(116);
00200   stat |= vectorRestore<HepJamesRandom>(117);
00201   stat |= vectorRestore<MTwistEngine>(118);
00202   stat |= vectorRestore<RanecuEngine>(139);
00203   stat |= vectorRestore<Ranlux64Engine>(119);
00204   stat |= vectorRestore<RanluxEngine>(120);
00205   stat |= vectorRestore<RanshiEngine>(121);
00206   stat |= vectorRestore<TripleRand>(122);
00207   stat |= vectorRestore<NonRandomEngine>(123);
00208   // anonymous engines are not copyable
00209   //stat |= vectorRestore<RandEngine>(129);
00210 #endif
00211 
00212   output << "\n=============================================\n\n";
00213 
00214   if (stat != 0) {
00215      std::cout << "One or more problems detected: stat = " << stat << "\n";
00216      output << "One or more problems detected: stat = " << stat << "\n";
00217   }  else {
00218      output << "ranRestoreTest passed with no problems detected.\n";    
00219   }
00220 
00221   if (stat == 0) return 0;
00222   if (stat > 0) return -(stat|1);
00223   return stat|1;
00224 }       
00225 

Generated on Thu Jul 1 22:02:31 2010 for CLHEP by  doxygen 1.4.7